1 /*
2  * Copyright (C) 2015 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 package com.android.settings.applications;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.AsyncTask;
22 import android.os.Bundle;
23 import android.provider.SearchIndexableResource;
24 import android.support.v7.preference.Preference;
25 import com.android.internal.logging.MetricsProto.MetricsEvent;
26 import com.android.settings.R;
27 import com.android.settings.SettingsPreferenceFragment;
28 import com.android.settings.Utils;
29 import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.search.Indexable;
32 import com.android.settingslib.applications.ApplicationsState;
33 import com.android.settingslib.applications.ApplicationsState.AppEntry;
34 import com.android.settingslib.applications.ApplicationsState.Session;
35 
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 
40 public class AdvancedAppSettings extends SettingsPreferenceFragment implements
41         ApplicationsState.Callbacks, Indexable {
42 
43     static final String TAG = "AdvancedAppSettings";
44 
45     private static final String KEY_APP_PERM = "manage_perms";
46     private static final String KEY_APP_DOMAIN_URLS = "domain_urls";
47     private static final String KEY_HIGH_POWER_APPS = "high_power_apps";
48     private static final String KEY_SYSTEM_ALERT_WINDOW = "system_alert_window";
49     private static final String KEY_WRITE_SETTINGS_APPS = "write_settings_apps";
50 
51     private Session mSession;
52     private Preference mAppPermsPreference;
53     private Preference mAppDomainURLsPreference;
54     private Preference mHighPowerPreference;
55     private Preference mSystemAlertWindowPreference;
56     private Preference mWriteSettingsPreference;
57 
58     private BroadcastReceiver mPermissionReceiver;
59 
60     @Override
onCreate(Bundle icicle)61     public void onCreate(Bundle icicle) {
62         super.onCreate(icicle);
63         addPreferencesFromResource(R.xml.advanced_apps);
64 
65         Preference permissions = getPreferenceScreen().findPreference(KEY_APP_PERM);
66         permissions.setIntent(new Intent(Intent.ACTION_MANAGE_PERMISSIONS));
67 
68         ApplicationsState applicationsState = ApplicationsState.getInstance(
69                 getActivity().getApplication());
70         mSession = applicationsState.newSession(this);
71 
72         mAppPermsPreference = findPreference(KEY_APP_PERM);
73         mAppDomainURLsPreference = findPreference(KEY_APP_DOMAIN_URLS);
74         mHighPowerPreference = findPreference(KEY_HIGH_POWER_APPS);
75         mSystemAlertWindowPreference = findPreference(KEY_SYSTEM_ALERT_WINDOW);
76         mWriteSettingsPreference = findPreference(KEY_WRITE_SETTINGS_APPS);
77     }
78 
79     @Override
getMetricsCategory()80     protected int getMetricsCategory() {
81         return MetricsEvent.APPLICATIONS_ADVANCED;
82     }
83 
84     @Override
onRunningStateChanged(boolean running)85     public void onRunningStateChanged(boolean running) {
86         // No-op.
87     }
88 
89     @Override
onPackageListChanged()90     public void onPackageListChanged() {
91         // No-op.
92     }
93 
94     @Override
onRebuildComplete(ArrayList<AppEntry> apps)95     public void onRebuildComplete(ArrayList<AppEntry> apps) {
96         // No-op.
97     }
98 
99     @Override
onPackageIconChanged()100     public void onPackageIconChanged() {
101         // No-op.
102     }
103 
104     @Override
onPackageSizeChanged(String packageName)105     public void onPackageSizeChanged(String packageName) {
106         // No-op.
107     }
108 
109     @Override
onAllSizesComputed()110     public void onAllSizesComputed() {
111         // No-op.
112     }
113 
114     @Override
onLauncherInfoChanged()115     public void onLauncherInfoChanged() {
116         // No-op.
117     }
118 
119     @Override
onLoadEntriesCompleted()120     public void onLoadEntriesCompleted() {
121         // No-op.
122     }
123 
124     private final PermissionsResultCallback mPermissionCallback = new PermissionsResultCallback() {
125         @Override
126         public void onAppWithPermissionsCountsResult(int standardGrantedPermissionAppCount,
127                 int standardUsedPermissionAppCount) {
128             if (getActivity() == null) {
129                 return;
130             }
131             mPermissionReceiver = null;
132             if (standardUsedPermissionAppCount != 0) {
133                 mAppPermsPreference.setSummary(getContext().getString(
134                         R.string.app_permissions_summary,
135                         standardGrantedPermissionAppCount,
136                         standardUsedPermissionAppCount));
137             } else {
138                 mAppPermsPreference.setSummary(null);
139             }
140         }
141     };
142 
143     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
144             new BaseSearchIndexProvider() {
145                 @Override
146                 public List<SearchIndexableResource> getXmlResourcesToIndex(
147                         Context context, boolean enabled) {
148                     SearchIndexableResource sir = new SearchIndexableResource(context);
149                     sir.xmlResId = R.xml.advanced_apps;
150                     return Arrays.asList(sir);
151                 }
152 
153                 @Override
154                 public List<String> getNonIndexableKeys(Context context) {
155                     return Utils.getNonIndexable(R.xml.advanced_apps, context);
156                 }
157             };
158 }
159