1d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk/*
2d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * Copyright (C) 2015 The Android Open Source Project
3d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk *
4d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * Licensed under the Apache License, Version 2.0 (the "License");
5d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * you may not use this file except in compliance with the License.
6d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * You may obtain a copy of the License at
7d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk *
8d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
9d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk *
10d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * Unless required by applicable law or agreed to in writing, software
11d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * distributed under the License is distributed on an "AS IS" BASIS,
12d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * See the License for the specific language governing permissions and
14d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk * limitations under the License.
15d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk */
16d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monkpackage com.android.settings.applications;
17d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
18d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monkimport android.Manifest;
19d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monkimport android.app.AppOpsManager;
20d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monkimport android.content.Context;
21d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monkimport android.util.Log;
22d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
2367cd6ab93a526fe65877743e458590f4e6f187eeTony Mantlerimport com.android.settingslib.applications.ApplicationsState;
2467cd6ab93a526fe65877743e458590f4e6f187eeTony Mantlerimport com.android.settingslib.applications.ApplicationsState.AppEntry;
2567cd6ab93a526fe65877743e458590f4e6f187eeTony Mantlerimport com.android.settingslib.applications.ApplicationsState.AppFilter;
26d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
27d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk/*
287f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau * Connects app usage info to the ApplicationsState. Wraps around the generic AppStateAppOpsBridge
297f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau * class to tailor to the semantics of PACKAGE_USAGE_STATS. Also provides app filters that can use
307f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau * the info.
31d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk */
327f70ba18e6760c2539704c16cf3865bf49953d50Billy Laupublic class AppStateUsageBridge extends AppStateAppOpsBridge {
33d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
34d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    private static final String TAG = "AppStateUsageBridge";
35d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
367f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau    private static final String PM_USAGE_STATS = Manifest.permission.PACKAGE_USAGE_STATS;
377f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau    private static final int APP_OPS_OP_CODE = AppOpsManager.OP_GET_USAGE_STATS;
383d9c7fc9982ddd513bc1bebdce76a12eecb61c88Billy Lau    private static final String[] PM_PERMISSION = {
393d9c7fc9982ddd513bc1bebdce76a12eecb61c88Billy Lau            PM_USAGE_STATS
403d9c7fc9982ddd513bc1bebdce76a12eecb61c88Billy Lau    };
41d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
42d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    public AppStateUsageBridge(Context context, ApplicationsState appState, Callback callback) {
433d9c7fc9982ddd513bc1bebdce76a12eecb61c88Billy Lau        super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSION);
44d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    }
45d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
46d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    @Override
47d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
48d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        app.extraInfo = getUsageInfo(pkg, uid);
49d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    }
50d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
51d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    public UsageState getUsageInfo(String pkg, int uid) {
527f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau        PermissionState permissionState = super.getPermissionInfo(pkg, uid);
537f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau        return new UsageState(permissionState);
54d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    }
55d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
567f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau    public static class UsageState extends AppStateAppOpsBridge.PermissionState {
57d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
587f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau        public UsageState(PermissionState permissionState) {
597f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau            super(permissionState.packageName, permissionState.userHandle);
607f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau            this.packageInfo = permissionState.packageInfo;
617f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau            this.appOpMode = permissionState.appOpMode;
627f70ba18e6760c2539704c16cf3865bf49953d50Billy Lau            this.permissionDeclared = permissionState.permissionDeclared;
63fee785645b57c519a31ed403e60e8f76dcc8abbbBilly Lau            this.staticPermissionGranted = permissionState.staticPermissionGranted;
64d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        }
65d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    }
66d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
67d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    public static final AppFilter FILTER_APP_USAGE = new AppFilter() {
68d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        @Override
69d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        public void init() {
70d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        }
71d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk
72d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        @Override
73d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        public boolean filterApp(AppEntry info) {
74d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk            return info.extraInfo != null;
75d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk        }
76d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk    };
77d8da51ccfecf16f2c06f788e6bcbc232d1f0cb32Jason Monk}
78