1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.developeroptions.applications.appinfo;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ResolveInfo;
23 import android.text.TextUtils;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.car.developeroptions.overlay.FeatureFactory;
28 
29 public class AppSettingPreferenceController extends AppInfoPreferenceControllerBase {
30 
31     private String mPackageName;
32 
AppSettingPreferenceController(Context context, String preferenceKey)33     public AppSettingPreferenceController(Context context, String preferenceKey) {
34         super(context, preferenceKey);
35     }
36 
setPackageName(String packageName)37     public AppSettingPreferenceController setPackageName(String packageName) {
38         mPackageName = packageName;
39         return this;
40     }
41 
42     @Override
getAvailabilityStatus()43     public int getAvailabilityStatus() {
44         if (TextUtils.isEmpty(mPackageName) || mParent == null) {
45             return CONDITIONALLY_UNAVAILABLE;
46         }
47         final Intent intent = resolveIntent(
48                 new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName));
49         return intent != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
50     }
51 
52     @Override
handlePreferenceTreeClick(Preference preference)53     public boolean handlePreferenceTreeClick(Preference preference) {
54         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
55             return false;
56         }
57         final Intent intent = resolveIntent(
58                 new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName));
59         if (intent == null) {
60             return false;
61         }
62         FeatureFactory.getFactory(mContext).getMetricsFeatureProvider()
63                 .action(SettingsEnums.PAGE_UNKNOWN,
64                         SettingsEnums.ACTION_OPEN_APP_SETTING,
65                         mParent.getMetricsCategory(),
66                         null, 0);
67         mContext.startActivity(intent);
68         return true;
69     }
70 
resolveIntent(Intent i)71     private Intent resolveIntent(Intent i) {
72         ResolveInfo result = mContext.getPackageManager().resolveActivity(i, 0);
73         if (result != null) {
74             return new Intent(i.getAction())
75                     .setClassName(result.activityInfo.packageName, result.activityInfo.name);
76         }
77         return null;
78     }
79 }
80