• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.deviceinfo.legal;
16 
17 import android.content.Context;
18 import android.content.Intent;
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.BasePreferenceController;
27 
28 import java.util.List;
29 
30 
31 public abstract class LegalPreferenceController extends BasePreferenceController {
32     private final PackageManager mPackageManager;
33     private Preference mPreference;
34 
LegalPreferenceController(Context context, String key)35     public LegalPreferenceController(Context context, String key) {
36         super(context, key);
37         mPackageManager = mContext.getPackageManager();
38     }
39 
40     @Override
getAvailabilityStatus()41     public int getAvailabilityStatus() {
42         if (findMatchingSpecificActivity() != null) {
43             return AVAILABLE;
44         } else {
45             return UNSUPPORTED_ON_DEVICE;
46         }
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         mPreference = screen.findPreference(getPreferenceKey());
52         super.displayPreference(screen);
53 
54         if (getAvailabilityStatus() == AVAILABLE) {
55             replacePreferenceIntent();
56         }
57     }
58 
getIntent()59     protected abstract Intent getIntent();
60 
findMatchingSpecificActivity()61     private ResolveInfo findMatchingSpecificActivity() {
62         final Intent intent = getIntent();
63         if (intent == null) {
64             return null;
65         }
66 
67         // Find the activity that is in the system image
68         final List<ResolveInfo> list = mPackageManager.queryIntentActivities(intent, 0);
69         if (list == null) {
70             return null;
71         }
72 
73         for (ResolveInfo resolveInfo : list) {
74             if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
75                     != 0) {
76                 return resolveInfo;
77             }
78         }
79 
80         // Did not find a matching activity
81         return null;
82     }
83 
replacePreferenceIntent()84     private void replacePreferenceIntent() {
85         final ResolveInfo resolveInfo = findMatchingSpecificActivity();
86         if (resolveInfo == null) {
87             return;
88         }
89 
90         // Replace the intent with this specific activity
91         mPreference.setIntent(new Intent().setClassName(
92                 resolveInfo.activityInfo.packageName,
93                 resolveInfo.activityInfo.name));
94 
95         mPreference.setTitle(resolveInfo.loadLabel(mPackageManager));
96     }
97 }
98