1 /* 2 * Copyright (C) 2018 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.server.content; 17 18 import android.app.ActivityManagerInternal; 19 import android.app.usage.UsageStatsManagerInternal; 20 import android.content.pm.UserPackage; 21 import android.os.SystemClock; 22 23 import com.android.server.LocalServices; 24 25 import java.util.HashMap; 26 27 class SyncAdapterStateFetcher { 28 29 private final HashMap<UserPackage, Integer> mBucketCache = new HashMap<>(); 30 SyncAdapterStateFetcher()31 public SyncAdapterStateFetcher() { 32 } 33 34 /** 35 * Return sync adapter state with a cache. 36 */ getStandbyBucket(int userId, String packageName)37 public int getStandbyBucket(int userId, String packageName) { 38 final UserPackage key = UserPackage.of(userId, packageName); 39 final Integer cached = mBucketCache.get(key); 40 if (cached != null) { 41 return cached; 42 } 43 final UsageStatsManagerInternal usmi = 44 LocalServices.getService(UsageStatsManagerInternal.class); 45 if (usmi == null) { 46 return -1; // Unknown. 47 } 48 49 final int value = usmi.getAppStandbyBucket(packageName, userId, 50 SystemClock.elapsedRealtime()); 51 mBucketCache.put(key, value); 52 return value; 53 } 54 55 /** 56 * Return UID active state. 57 */ isAppActive(int uid)58 public boolean isAppActive(int uid) { 59 final ActivityManagerInternal ami = LocalServices.getService(ActivityManagerInternal.class); 60 return (ami != null) ? ami.isUidActive(uid) : false; 61 } 62 } 63