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