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 */
16package com.android.settings.applications;
17
18import android.app.Activity;
19import android.content.BroadcastReceiver;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.os.Bundle;
23import android.os.AsyncTask;
24import android.preference.Preference;
25import android.provider.Settings;
26
27import com.android.internal.logging.MetricsLogger;
28import com.android.settings.R;
29import com.android.settings.SettingsPreferenceFragment;
30import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
31import com.android.settings.fuelgauge.PowerWhitelistBackend;
32import com.android.settingslib.applications.ApplicationsState;
33import com.android.settingslib.applications.ApplicationsState.AppEntry;
34import com.android.settingslib.applications.ApplicationsState.Session;
35
36import java.util.ArrayList;
37
38public class AdvancedAppSettings extends SettingsPreferenceFragment implements
39        ApplicationsState.Callbacks {
40
41    static final String TAG = "AdvancedAppSettings";
42
43    private static final String KEY_APP_PERM = "manage_perms";
44    private static final String KEY_APP_DOMAIN_URLS = "domain_urls";
45    private static final String KEY_HIGH_POWER_APPS = "high_power_apps";
46    private static final String KEY_SYSTEM_ALERT_WINDOW = "system_alert_window";
47    private static final String KEY_WRITE_SETTINGS_APPS = "write_settings_apps";
48
49    private Session mSession;
50    private Preference mAppPermsPreference;
51    private Preference mAppDomainURLsPreference;
52    private Preference mHighPowerPreference;
53    private Preference mSystemAlertWindowPreference;
54    private Preference mWriteSettingsPreference;
55
56    private BroadcastReceiver mPermissionReceiver;
57
58    @Override
59    public void onCreate(Bundle icicle) {
60        super.onCreate(icicle);
61        addPreferencesFromResource(R.xml.advanced_apps);
62
63        Preference permissions = getPreferenceScreen().findPreference(KEY_APP_PERM);
64        permissions.setIntent(new Intent(Intent.ACTION_MANAGE_PERMISSIONS));
65
66        ApplicationsState applicationsState = ApplicationsState.getInstance(
67                getActivity().getApplication());
68        mSession = applicationsState.newSession(this);
69
70        mAppPermsPreference = findPreference(KEY_APP_PERM);
71        mAppDomainURLsPreference = findPreference(KEY_APP_DOMAIN_URLS);
72        mHighPowerPreference = findPreference(KEY_HIGH_POWER_APPS);
73        mSystemAlertWindowPreference = findPreference(KEY_SYSTEM_ALERT_WINDOW);
74        mWriteSettingsPreference = findPreference(KEY_WRITE_SETTINGS_APPS);
75    }
76
77    @Override
78    protected int getMetricsCategory() {
79        return MetricsLogger.APPLICATIONS_ADVANCED;
80    }
81
82    @Override
83    public void onRunningStateChanged(boolean running) {
84        // No-op.
85    }
86
87    @Override
88    public void onPackageListChanged() {
89        // No-op.
90    }
91
92    @Override
93    public void onRebuildComplete(ArrayList<AppEntry> apps) {
94        // No-op.
95    }
96
97    @Override
98    public void onPackageIconChanged() {
99        // No-op.
100    }
101
102    @Override
103    public void onPackageSizeChanged(String packageName) {
104        // No-op.
105    }
106
107    @Override
108    public void onAllSizesComputed() {
109        // No-op.
110    }
111
112    @Override
113    public void onLauncherInfoChanged() {
114        // No-op.
115    }
116
117    @Override
118    public void onLoadEntriesCompleted() {
119        // No-op.
120    }
121
122    private final PermissionsResultCallback mPermissionCallback = new PermissionsResultCallback() {
123        @Override
124        public void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels) {
125            if (getActivity() == null) {
126                return;
127            }
128            mPermissionReceiver = null;
129            if (counts != null) {
130                mAppPermsPreference.setSummary(getContext().getString(
131                        R.string.app_permissions_summary, counts[0], counts[1]));
132            } else {
133                mAppPermsPreference.setSummary(null);
134            }
135        }
136    };
137
138    private class CountAppsWithOverlayPermission extends
139            AsyncTask<AppStateOverlayBridge, Void, Integer> {
140        int numOfPackagesRequestedPermission = 0;
141
142        @Override
143        protected Integer doInBackground(AppStateOverlayBridge... params) {
144            AppStateOverlayBridge overlayBridge = params[0];
145            numOfPackagesRequestedPermission = overlayBridge
146                    .getNumberOfPackagesWithPermission();
147            return overlayBridge.getNumberOfPackagesCanDrawOverlay();
148        }
149
150        @Override
151        protected void onPostExecute(Integer result) {
152            // checks if fragment is still there before updating the preference object
153            if (isAdded()) {
154                mSystemAlertWindowPreference.setSummary(getContext().getString(
155                        R.string.system_alert_window_summary, result,
156                        numOfPackagesRequestedPermission));
157            }
158        }
159    }
160
161    private class CountAppsWithWriteSettingsPermission extends
162        AsyncTask<AppStateWriteSettingsBridge, Void, Integer> {
163        int numOfPackagesRequestedPermission = 0;
164
165        @Override
166        protected Integer doInBackground(AppStateWriteSettingsBridge... params) {
167            AppStateWriteSettingsBridge writeSettingsBridge = params[0];
168            numOfPackagesRequestedPermission = writeSettingsBridge
169                .getNumberOfPackagesWithPermission();
170            return writeSettingsBridge.getNumberOfPackagesCanWriteSettings();
171        }
172
173        @Override
174        protected void onPostExecute(Integer result) {
175            // checks if fragment is still there before updating the preference object
176            if (isAdded()) {
177                mWriteSettingsPreference.setSummary(getContext().getString(
178                        R.string.write_settings_summary, result,
179                        numOfPackagesRequestedPermission));
180            }
181        }
182    }
183}
184