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 */ 16 package com.android.settings.applications; 17 18 import android.Manifest; 19 import android.app.AppOpsManager; 20 import android.content.Context; 21 22 import com.android.settingslib.applications.ApplicationsState; 23 import com.android.settingslib.applications.ApplicationsState.AppEntry; 24 import 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 */ 31 public 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 String PM_LOADER_STATS = Manifest.permission.LOADER_USAGE_STATS; 37 private static final int APP_OPS_USAGE_STATS = AppOpsManager.OP_GET_USAGE_STATS; 38 private static final int APP_OPS_LOADER_STATS = AppOpsManager.OP_LOADER_USAGE_STATS; 39 private static final int[] APP_OPS_OP_CODES = { 40 APP_OPS_USAGE_STATS, 41 APP_OPS_LOADER_STATS, 42 }; 43 private static final String[] PM_PERMISSIONS = { 44 PM_USAGE_STATS, 45 PM_LOADER_STATS, 46 }; 47 AppStateUsageBridge(Context context, ApplicationsState appState, Callback callback)48 public AppStateUsageBridge(Context context, ApplicationsState appState, Callback callback) { 49 super(context, appState, callback, APP_OPS_OP_CODES, PM_PERMISSIONS); 50 } 51 52 @Override updateExtraInfo(AppEntry app, String pkg, int uid)53 protected void updateExtraInfo(AppEntry app, String pkg, int uid) { 54 app.extraInfo = getUsageInfo(pkg, uid); 55 } 56 getUsageInfo(String pkg, int uid)57 public UsageState getUsageInfo(String pkg, int uid) { 58 PermissionState permissionState = super.getPermissionInfo(pkg, uid); 59 return new UsageState(permissionState); 60 } 61 62 public static class UsageState extends AppStateAppOpsBridge.PermissionState { 63 UsageState(PermissionState permissionState)64 public UsageState(PermissionState permissionState) { 65 super(permissionState.packageName, permissionState.userHandle); 66 this.packageInfo = permissionState.packageInfo; 67 this.appOpMode = permissionState.appOpMode; 68 this.permissionDeclared = permissionState.permissionDeclared; 69 this.staticPermissionGranted = permissionState.staticPermissionGranted; 70 } 71 } 72 73 public static final AppFilter FILTER_APP_USAGE = new AppFilter() { 74 @Override 75 public void init() { 76 } 77 78 @Override 79 public boolean filterApp(AppEntry info) { 80 return info.extraInfo != null; 81 } 82 }; 83 } 84