OSDN Git Service

Handle rotation (or refresh) before callback is received
[android-x86/packages-apps-Settings.git] / src / com / android / settings / applications / AdvancedAppSettings.java
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.Intent;
20 import android.content.pm.ApplicationInfo;
21 import android.os.Bundle;
22 import android.preference.Preference;
23
24 import com.android.internal.logging.MetricsLogger;
25 import com.android.settings.R;
26 import com.android.settings.SettingsPreferenceFragment;
27 import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
28 import com.android.settings.fuelgauge.PowerWhitelistBackend;
29 import com.android.settingslib.applications.ApplicationsState;
30 import com.android.settingslib.applications.ApplicationsState.AppEntry;
31 import com.android.settingslib.applications.ApplicationsState.Session;
32
33 import java.util.ArrayList;
34
35 public class AdvancedAppSettings extends SettingsPreferenceFragment implements
36         ApplicationsState.Callbacks {
37
38     static final String TAG = "AdvancedAppSettings";
39
40     private static final String KEY_APP_PERM = "manage_perms";
41     private static final String KEY_APP_DOMAIN_URLS = "domain_urls";
42     private static final String KEY_HIGH_POWER_APPS = "high_power_apps";
43
44     private Session mSession;
45     private Preference mAppPermsPreference;
46     private Preference mAppDomainURLsPreference;
47     private Preference mHighPowerPreference;
48
49     private BroadcastReceiver mPermissionReceiver;
50
51     @Override
52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54         addPreferencesFromResource(R.xml.advanced_apps);
55
56         Preference permissions = getPreferenceScreen().findPreference(KEY_APP_PERM);
57         permissions.setIntent(new Intent(Intent.ACTION_MANAGE_PERMISSIONS));
58
59         ApplicationsState applicationsState = ApplicationsState.getInstance(
60                 getActivity().getApplication());
61         mSession = applicationsState.newSession(this);
62
63         mAppPermsPreference = findPreference(KEY_APP_PERM);
64         mAppDomainURLsPreference = findPreference(KEY_APP_DOMAIN_URLS);
65         mHighPowerPreference = findPreference(KEY_HIGH_POWER_APPS);
66         updateUI();
67     }
68
69     @Override
70     public void onDestroy() {
71         super.onDestroy();
72         if (mPermissionReceiver != null) {
73             getContext().unregisterReceiver(mPermissionReceiver);
74             mPermissionReceiver = null;
75         }
76     }
77
78     private void updateUI() {
79         ArrayList<AppEntry> allApps = mSession.getAllApps();
80
81         int countAppWithDomainURLs = 0;
82         for (AppEntry entry : allApps) {
83             boolean hasDomainURLs =
84                     (entry.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS) != 0;
85             if (hasDomainURLs) countAppWithDomainURLs++;
86         }
87         String summary = getResources().getQuantityString(
88                 R.plurals.domain_urls_apps_summary, countAppWithDomainURLs, countAppWithDomainURLs);
89         mAppDomainURLsPreference.setSummary(summary);
90
91         int highPowerCount = PowerWhitelistBackend.getInstance().getWhitelistSize();
92         mHighPowerPreference.setSummary(getResources().getQuantityString(R.plurals.high_power_count,
93                 highPowerCount, highPowerCount));
94
95         if (mPermissionReceiver != null) {
96             getContext().unregisterReceiver(mPermissionReceiver);
97         }
98         mPermissionReceiver = PermissionsSummaryHelper.getAppWithPermissionsCounts(getContext(),
99                 mPermissionCallback);
100     }
101
102     @Override
103     protected int getMetricsCategory() {
104         return MetricsLogger.APPLICATIONS_ADVANCED;
105     }
106
107     @Override
108     public void onRunningStateChanged(boolean running) {
109         // No-op.
110     }
111
112     @Override
113     public void onPackageListChanged() {
114         updateUI();
115     }
116
117     @Override
118     public void onRebuildComplete(ArrayList<AppEntry> apps) {
119         // No-op.
120     }
121
122     @Override
123     public void onPackageIconChanged() {
124         // No-op.
125     }
126
127     @Override
128     public void onPackageSizeChanged(String packageName) {
129         // No-op.
130     }
131
132     @Override
133     public void onAllSizesComputed() {
134         // No-op.
135     }
136
137     @Override
138     public void onLauncherInfoChanged() {
139         // No-op.
140     }
141
142     @Override
143     public void onLoadEntriesCompleted() {
144         // No-op.
145     }
146
147     private final PermissionsResultCallback mPermissionCallback = new PermissionsResultCallback() {
148         @Override
149         public void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels) {
150             if (getActivity() == null) {
151                 return;
152             }
153             mPermissionReceiver = null;
154             if (counts != null) {
155                 mAppPermsPreference.setSummary(getContext().getString(
156                         R.string.app_permissions_summary, counts[0], counts[1]));
157             } else {
158                 mAppPermsPreference.setSummary(null);
159             }
160         }
161     };
162 }