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