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.annotation.NonNull; 20 import android.app.PropertyInvalidatedCache; 21 import android.content.Context; 22 import android.os.Binder; 23 import android.os.RemoteException; 24 import android.os.ServiceManager; 25 26 import com.android.internal.compat.IPlatformCompat; 27 28 /** 29 * Handles caching of calls to {@link com.android.internal.compat.IPlatformCompat} 30 * @hide 31 */ 32 public final class ChangeIdStateCache 33 extends PropertyInvalidatedCache<ChangeIdStateQuery, Boolean> { 34 private static final String CACHE_KEY = "cache_key.is_compat_change_enabled"; 35 private static final int MAX_ENTRIES = 64; 36 private static boolean sDisabled = false; 37 private volatile IPlatformCompat mPlatformCompat; 38 39 /** @hide */ ChangeIdStateCache()40 public ChangeIdStateCache() { 41 super(MAX_ENTRIES, CACHE_KEY); 42 } 43 44 /** 45 * Disable cache. 46 * 47 * <p>Should only be used in unit tests. 48 * @hide 49 */ disable()50 public static void disable() { 51 sDisabled = true; 52 } 53 54 /** 55 * Invalidate the cache. 56 * 57 * <p>Can only be called by the system server process. 58 * @hide 59 */ invalidate()60 public static void invalidate() { 61 if (!sDisabled) { 62 PropertyInvalidatedCache.invalidateCache(CACHE_KEY); 63 } 64 } 65 66 @NonNull getPlatformCompatService()67 IPlatformCompat getPlatformCompatService() { 68 IPlatformCompat platformCompat = mPlatformCompat; 69 if (platformCompat == null) { 70 synchronized (this) { 71 platformCompat = mPlatformCompat; 72 if (platformCompat == null) { 73 platformCompat = IPlatformCompat.Stub.asInterface( 74 ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE)); 75 if (platformCompat == null) { 76 throw new RuntimeException( 77 "Could not get PlatformCompatService instance!"); 78 } 79 mPlatformCompat = platformCompat; 80 } 81 } 82 } 83 return platformCompat; 84 } 85 86 @Override recompute(ChangeIdStateQuery query)87 public Boolean recompute(ChangeIdStateQuery query) { 88 final long token = Binder.clearCallingIdentity(); 89 try { 90 if (query.type == ChangeIdStateQuery.QUERY_BY_PACKAGE_NAME) { 91 return getPlatformCompatService().isChangeEnabledByPackageName(query.changeId, 92 query.packageName, 93 query.userId); 94 } else if (query.type == ChangeIdStateQuery.QUERY_BY_UID) { 95 return getPlatformCompatService().isChangeEnabledByUid(query.changeId, query.uid); 96 } else { 97 throw new IllegalArgumentException("Invalid query type: " + query.type); 98 } 99 } catch (RemoteException e) { 100 e.rethrowFromSystemServer(); 101 } finally { 102 Binder.restoreCallingIdentity(token); 103 } 104 throw new IllegalStateException("Could not recompute value!"); 105 } 106 } 107