1 /* 2 * Copyright (C) 2011 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 com.android.server.pm; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.pm.ApplicationInfo; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 import com.android.server.pm.permission.LegacyPermissionState; 25 import com.android.server.pm.pkg.mutate.PackageStateMutator; 26 import com.android.server.utils.Snappable; 27 import com.android.server.utils.Watchable; 28 import com.android.server.utils.WatchableImpl; 29 import com.android.server.utils.Watcher; 30 31 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) 32 public abstract class SettingBase implements Watchable, Snappable { 33 34 // TODO: Remove in favor of individual boolean APIs. It's not clear what flag values are saved 35 // and bugs exist where callers query for an unsaved flag. 36 private int mPkgFlags; 37 private int mPkgPrivateFlags; 38 39 /** 40 * Watchable machinery 41 */ 42 private final Watchable mWatchable = new WatchableImpl(); 43 44 /** 45 * Ensures an observer is in the list, exactly once. The observer cannot be null. The 46 * function quietly returns if the observer is already in the list. 47 * 48 * @param observer The {@link Watcher} to be notified when the {@link Watchable} changes. 49 */ 50 @Override registerObserver(@onNull Watcher observer)51 public void registerObserver(@NonNull Watcher observer) { 52 mWatchable.registerObserver(observer); 53 } 54 55 /** 56 * Ensures an observer is not in the list. The observer must not be null. The function 57 * quietly returns if the objserver is not in the list. 58 * 59 * @param observer The {@link Watcher} that should not be in the notification list. 60 */ 61 @Override unregisterObserver(@onNull Watcher observer)62 public void unregisterObserver(@NonNull Watcher observer) { 63 mWatchable.unregisterObserver(observer); 64 } 65 66 /** 67 * Return true if the {@link Watcher) is a registered observer. 68 * @param observer A {@link Watcher} that might be registered 69 * @return true if the observer is registered with this {@link Watchable}. 70 */ 71 @Override isRegisteredObserver(@onNull Watcher observer)72 public boolean isRegisteredObserver(@NonNull Watcher observer) { 73 return mWatchable.isRegisteredObserver(observer); 74 } 75 76 /** 77 * Invokes {@link Watcher#onChange} on each registered observer. The method can be called 78 * with the {@link Watchable} that generated the event. In a tree of {@link Watchable}s, this 79 * is generally the first (deepest) {@link Watchable} to detect a change. 80 * 81 * @param what The {@link Watchable} that generated the event. 82 */ 83 @Override dispatchChange(@ullable Watchable what)84 public void dispatchChange(@Nullable Watchable what) { 85 mWatchable.dispatchChange(what); 86 } 87 88 /** 89 * Notify listeners that this object has changed. 90 */ onChanged()91 public void onChanged() { 92 PackageStateMutator.onPackageStateChanged(); 93 dispatchChange(this); 94 } 95 96 /** 97 * The legacy permission state that is read from package settings persistence for migration. 98 * This state here can not reflect the current permission state and should not be used for 99 * purposes other than migration. 100 */ 101 @Deprecated 102 protected final LegacyPermissionState mLegacyPermissionsState = new LegacyPermissionState(); 103 SettingBase(int pkgFlags, int pkgPrivateFlags)104 SettingBase(int pkgFlags, int pkgPrivateFlags) { 105 setFlags(pkgFlags); 106 setPrivateFlags(pkgPrivateFlags); 107 } 108 SettingBase(@ullable SettingBase orig)109 SettingBase(@Nullable SettingBase orig) { 110 if (orig != null) { 111 copySettingBase(orig); 112 } 113 } 114 copySettingBase(SettingBase orig)115 public final void copySettingBase(SettingBase orig) { 116 mPkgFlags = orig.mPkgFlags; 117 mPkgPrivateFlags = orig.mPkgPrivateFlags; 118 mLegacyPermissionsState.copyFrom(orig.mLegacyPermissionsState); 119 onChanged(); 120 } 121 122 @Deprecated getLegacyPermissionState()123 public LegacyPermissionState getLegacyPermissionState() { 124 return mLegacyPermissionsState; 125 } 126 setFlags(int pkgFlags)127 public SettingBase setFlags(int pkgFlags) { 128 this.mPkgFlags = pkgFlags 129 & (ApplicationInfo.FLAG_SYSTEM 130 | ApplicationInfo.FLAG_EXTERNAL_STORAGE 131 | ApplicationInfo.FLAG_TEST_ONLY); 132 onChanged(); 133 return this; 134 } 135 setPrivateFlags(int pkgPrivateFlags)136 public SettingBase setPrivateFlags(int pkgPrivateFlags) { 137 this.mPkgPrivateFlags = pkgPrivateFlags 138 & (ApplicationInfo.PRIVATE_FLAG_PRIVILEGED 139 | ApplicationInfo.PRIVATE_FLAG_OEM 140 | ApplicationInfo.PRIVATE_FLAG_VENDOR 141 | ApplicationInfo.PRIVATE_FLAG_PRODUCT 142 | ApplicationInfo.PRIVATE_FLAG_SYSTEM_EXT 143 | ApplicationInfo.PRIVATE_FLAG_REQUIRED_FOR_SYSTEM_USER 144 | ApplicationInfo.PRIVATE_FLAG_ODM); 145 onChanged(); 146 return this; 147 } 148 149 /** 150 * Unconditionally set both mPkgFlags and mPkgPrivateFlags. 151 * Should not be used outside pkgSetting initialization or update. 152 */ setPkgFlags(int flags, int privateFlags)153 SettingBase setPkgFlags(int flags, int privateFlags) { 154 this.mPkgFlags = flags; 155 this.mPkgPrivateFlags = privateFlags; 156 onChanged(); 157 return this; 158 } 159 getFlags()160 public int getFlags() { 161 return mPkgFlags; 162 } 163 getPrivateFlags()164 public int getPrivateFlags() { 165 return mPkgPrivateFlags; 166 } 167 } 168