blob: 62fb75107755720fae72ef359b1556f00e127b80 [file] [log] [blame]
Makoto Onuki75ad2492018-03-28 14:42:42 -07001/*
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 */
16package com.android.server.content;
17
18import android.app.usage.UsageStatsManagerInternal;
19import android.os.SystemClock;
20import android.util.Pair;
21
22import com.android.server.AppStateTracker;
23import com.android.server.LocalServices;
24
25import java.util.HashMap;
26
27class SyncAdapterStateFetcher {
28
29 private final HashMap<Pair<Integer, String>, Integer> mBucketCache =
30 new HashMap<>();
31
32 public SyncAdapterStateFetcher() {
33 }
34
35 /**
36 * Return sync adapter state with a cache.
37 */
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 */
59 public boolean isAppActive(int uid) {
60 final AppStateTracker ast =
61 LocalServices.getService(AppStateTracker.class);
62 if (ast == null) {
63 return false;
64 }
65
66 return ast.isUidActive(uid);
67 }
68}