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.Manifest;
19import android.app.AppOpsManager;
20import android.content.Context;
21import android.util.Log;
22
23import com.android.settingslib.applications.ApplicationsState;
24import com.android.settingslib.applications.ApplicationsState.AppEntry;
25import com.android.settingslib.applications.ApplicationsState.AppFilter;
26
27/*
28 * Connects info of apps that draw overlay to the ApplicationsState. Wraps around the generic
29 * AppStateAppOpsBridge class to tailor to the semantics of SYSTEM_ALERT_WINDOW. Also provides app
30 * filters that can use the info.
31 */
32public class AppStateWriteSettingsBridge extends AppStateAppOpsBridge {
33
34    private static final String TAG = "AppStateWriteSettingsBridge";
35    private static final int APP_OPS_OP_CODE = AppOpsManager.OP_WRITE_SETTINGS;
36    private static final String PM_WRITE_SETTINGS = Manifest.permission.WRITE_SETTINGS;
37    private static final String PM_CHANGE_NETWORK_STATE = Manifest.permission.CHANGE_NETWORK_STATE;
38    // CHANGE_NETWORK_STATE is now merged with WRITE_SETTINGS
39    private static final String[] PM_PERMISSIONS = {
40            PM_WRITE_SETTINGS,
41            PM_CHANGE_NETWORK_STATE
42    };
43
44    public AppStateWriteSettingsBridge(Context context, ApplicationsState appState, Callback
45            callback) {
46        super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSIONS);
47    }
48
49    @Override
50    protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
51        app.extraInfo = getWriteSettingsInfo(pkg, uid);
52    }
53
54    public WriteSettingsState getWriteSettingsInfo(String pkg, int uid) {
55        PermissionState permissionState = super.getPermissionInfo(pkg, uid);
56        return new WriteSettingsState(permissionState);
57    }
58
59    // TODO: figure out how to filter out system apps for this method
60    public int getNumberOfPackagesWithPermission() {
61        return super.getNumPackagesDeclaredPermission();
62    }
63
64    // TODO: figure out how to filter out system apps for this method
65    public int getNumberOfPackagesCanWriteSettings() {
66        return super.getNumPackagesAllowedByAppOps();
67    }
68
69    public static class WriteSettingsState extends AppStateAppOpsBridge.PermissionState {
70        public WriteSettingsState(PermissionState permissionState) {
71            super(permissionState.packageName, permissionState.userHandle);
72            this.packageInfo = permissionState.packageInfo;
73            this.appOpMode = permissionState.appOpMode;
74            this.permissionDeclared = permissionState.permissionDeclared;
75            this.staticPermissionGranted = permissionState.staticPermissionGranted;
76        }
77    }
78
79    public static final AppFilter FILTER_WRITE_SETTINGS = new AppFilter() {
80        @Override
81        public void init() {
82        }
83
84        @Override
85        public boolean filterApp(AppEntry info) {
86            return info.extraInfo != null;
87        }
88    };
89}
90