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 app usage info to the ApplicationsState. Wraps around the generic AppStateAppOpsBridge
29 * class to tailor to the semantics of PACKAGE_USAGE_STATS. Also provides app filters that can use
30 * the info.
31 */
32public class AppStateUsageBridge extends AppStateAppOpsBridge {
33
34    private static final String TAG = "AppStateUsageBridge";
35
36    private static final String PM_USAGE_STATS = Manifest.permission.PACKAGE_USAGE_STATS;
37    private static final int APP_OPS_OP_CODE = AppOpsManager.OP_GET_USAGE_STATS;
38    private static final String[] PM_PERMISSION = {
39            PM_USAGE_STATS
40    };
41
42    public AppStateUsageBridge(Context context, ApplicationsState appState, Callback callback) {
43        super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSION);
44    }
45
46    @Override
47    protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
48        app.extraInfo = getUsageInfo(pkg, uid);
49    }
50
51    public UsageState getUsageInfo(String pkg, int uid) {
52        PermissionState permissionState = super.getPermissionInfo(pkg, uid);
53        return new UsageState(permissionState);
54    }
55
56    public static class UsageState extends AppStateAppOpsBridge.PermissionState {
57
58        public UsageState(PermissionState permissionState) {
59            super(permissionState.packageName, permissionState.userHandle);
60            this.packageInfo = permissionState.packageInfo;
61            this.appOpMode = permissionState.appOpMode;
62            this.permissionDeclared = permissionState.permissionDeclared;
63            this.staticPermissionGranted = permissionState.staticPermissionGranted;
64        }
65    }
66
67    public static final AppFilter FILTER_APP_USAGE = new AppFilter() {
68        @Override
69        public void init() {
70        }
71
72        @Override
73        public boolean filterApp(AppEntry info) {
74            return info.extraInfo != null;
75        }
76    };
77}
78