Andrei Onea | 3ef678b | 2020-02-10 18:06:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 17 | package android.app.compat; |
| 18 | |
| 19 | import android.app.PropertyInvalidatedCache; |
| 20 | import android.content.Context; |
| 21 | import android.os.Binder; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | |
| 25 | import com.android.internal.compat.IPlatformCompat; |
| 26 | |
| 27 | /** |
| 28 | * Handles caching of calls to {@link com.android.internal.compat.IPlatformCompat} |
| 29 | * @hide |
| 30 | */ |
| 31 | public final class ChangeIdStateCache |
| 32 | extends PropertyInvalidatedCache<ChangeIdStateQuery, Boolean> { |
| 33 | private static final String CACHE_KEY = "cache_key.is_compat_change_enabled"; |
| 34 | private static final int MAX_ENTRIES = 20; |
| 35 | private static boolean sDisabled = false; |
| 36 | |
| 37 | /** @hide */ |
| 38 | public ChangeIdStateCache() { |
| 39 | super(MAX_ENTRIES, CACHE_KEY); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Disable cache. |
| 44 | * |
| 45 | * <p>Should only be used in unit tests. |
| 46 | * @hide |
| 47 | */ |
| 48 | public static void disable() { |
| 49 | sDisabled = true; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Invalidate the cache. |
| 54 | * |
| 55 | * <p>Can only be called by the system server process. |
| 56 | * @hide |
| 57 | */ |
| 58 | public static void invalidate() { |
| 59 | if (!sDisabled) { |
| 60 | PropertyInvalidatedCache.invalidateCache(CACHE_KEY); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected Boolean recompute(ChangeIdStateQuery query) { |
| 66 | IPlatformCompat platformCompat = IPlatformCompat.Stub.asInterface( |
| 67 | ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE)); |
| 68 | final long token = Binder.clearCallingIdentity(); |
| 69 | try { |
| 70 | if (query.type == ChangeIdStateQuery.QUERY_BY_PACKAGE_NAME) { |
| 71 | return platformCompat.isChangeEnabledByPackageName(query.changeId, |
| 72 | query.packageName, |
| 73 | query.userId); |
| 74 | } else if (query.type == ChangeIdStateQuery.QUERY_BY_UID) { |
| 75 | return platformCompat.isChangeEnabledByUid(query.changeId, query.uid); |
| 76 | } else { |
| 77 | throw new IllegalArgumentException("Invalid query type: " + query.type); |
| 78 | } |
| 79 | } catch (RemoteException e) { |
| 80 | e.rethrowFromSystemServer(); |
| 81 | } finally { |
| 82 | Binder.restoreCallingIdentity(token); |
| 83 | } |
| 84 | throw new IllegalStateException("Could not recompute value!"); |
| 85 | } |
| 86 | } |