DrawOverlayDetailPreferenceController.java revision a0006d93bdeb24137cb29f44cfb2de020bd64f6e
1/*
2 * Copyright (C) 2017 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
17package com.android.settings.applications.appinfo;
18
19import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
20
21import android.content.Context;
22import android.content.pm.PackageInfo;
23import android.os.UserManager;
24import android.support.annotation.VisibleForTesting;
25import android.support.v7.preference.Preference;
26
27import com.android.settings.SettingsPreferenceFragment;
28
29public class DrawOverlayDetailPreferenceController extends AppInfoPreferenceControllerBase {
30
31    private static final String KEY = "system_alert_window";
32
33    public DrawOverlayDetailPreferenceController(Context context, AppInfoDashboardFragment parent) {
34        super(context, parent, KEY);
35    }
36
37    @Override
38    public int getAvailabilityStatus() {
39        if (UserManager.get(mContext).isManagedProfile()) {
40            return DISABLED_FOR_USER;
41        }
42        final PackageInfo packageInfo = mParent.getPackageInfo();
43        if (packageInfo == null || packageInfo.requestedPermissions == null) {
44            return DISABLED_FOR_USER;
45        }
46        for (int i = 0; i < packageInfo.requestedPermissions.length; i++) {
47            if (packageInfo.requestedPermissions[i].equals(SYSTEM_ALERT_WINDOW)) {
48                return AVAILABLE;
49            }
50        }
51        return DISABLED_FOR_USER;
52    }
53
54    @Override
55    public void updateState(Preference preference) {
56        preference.setSummary(getSummary());
57    }
58
59    @Override
60    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
61        return DrawOverlayDetails.class;
62    }
63
64    @VisibleForTesting
65    CharSequence getSummary() {
66        return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry());
67    }
68
69}
70