Merge "Properly compute default and system set flag on an upgrade" into oc-mr1-dev

This commit is contained in:
Svetoslav Ganov
2017-08-22 23:12:25 +00:00
committed by Android (Google) Code Review
2 changed files with 98 additions and 35 deletions

View File

@@ -2896,7 +2896,7 @@ public class SettingsProvider extends ContentProvider {
} }
private final class UpgradeController { private final class UpgradeController {
private static final int SETTINGS_VERSION = 147; private static final int SETTINGS_VERSION = 148;
private final int mUserId; private final int mUserId;
@@ -3345,22 +3345,11 @@ public class SettingsProvider extends ContentProvider {
} }
if (currentVersion == 141) { if (currentVersion == 141) {
// Version 142: We added the notion of a default and whether the system set // This implementation was incorrectly setting the current value of
// the setting. This is used for resetting the internal state and we need // settings changed by non-system packages as the default which default
// to make sure this value is updated for the existing settings, otherwise // is set by the system. We add a new upgrade step at the end to properly
// we would delete system set settings while they should stay unmodified. // handle this case which would also fix incorrect changes made by the
SettingsState globalSettings = getGlobalSettingsLocked(); // old implementation of this step.
ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings);
globalSettings.persistSyncLocked();
SettingsState secureSettings = getSecureSettingsLocked(mUserId);
ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings);
secureSettings.persistSyncLocked();
SettingsState systemSettings = getSystemSettingsLocked(mUserId);
ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings);
systemSettings.persistSyncLocked();
currentVersion = 142; currentVersion = 142;
} }
@@ -3407,24 +3396,51 @@ public class SettingsProvider extends ContentProvider {
} }
if (currentVersion == 145) { if (currentVersion == 145) {
// Version 146: Set the default value for WIFI_WAKEUP_AVAILABLE. // Version 146: In step 142 we had a bug where incorrectly
// some settings were considered system set and as a result
// made the default and marked as the default being set by
// the system. Here reevaluate the default and default system
// set flags. This would both fix corruption by the old impl
// of step 142 and also properly handle devices which never
// run 142.
if (userId == UserHandle.USER_SYSTEM) { if (userId == UserHandle.USER_SYSTEM) {
final SettingsState globalSettings = getGlobalSettingsLocked(); SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked( ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
Settings.Global.WIFI_WAKEUP_AVAILABLE); globalSettings.persistSyncLocked();
final int defaultValue = getContext().getResources().getInteger(
com.android.internal.R.integer.config_wifi_wakeup_available);
globalSettings.insertSettingLocked(
Settings.Global.WIFI_WAKEUP_AVAILABLE,
String.valueOf(defaultValue),
null, true, SettingsState.SYSTEM_PACKAGE_NAME);
} }
SettingsState secureSettings = getSecureSettingsLocked(mUserId);
ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
secureSettings.persistSyncLocked();
SettingsState systemSettings = getSystemSettingsLocked(mUserId);
ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
systemSettings.persistSyncLocked();
currentVersion = 146; currentVersion = 146;
} }
if (currentVersion == 146) { if (currentVersion == 146) {
// Version 146: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA. // Version 147: Set the default value for WIFI_WAKEUP_AVAILABLE.
if (userId == UserHandle.USER_SYSTEM) {
final SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked(
Settings.Global.WIFI_WAKEUP_AVAILABLE);
if (currentSetting.getValue() == null) {
final int defaultValue = getContext().getResources().getInteger(
com.android.internal.R.integer.config_wifi_wakeup_available);
globalSettings.insertSettingLocked(
Settings.Global.WIFI_WAKEUP_AVAILABLE,
String.valueOf(defaultValue),
null, true, SettingsState.SYSTEM_PACKAGE_NAME);
}
}
currentVersion = 147;
}
if (currentVersion == 147) {
// Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
if (userId == UserHandle.USER_SYSTEM) { if (userId == UserHandle.USER_SYSTEM) {
final SettingsState globalSettings = getGlobalSettingsLocked(); final SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked( final Setting currentSetting = globalSettings.getSettingLocked(
@@ -3437,7 +3453,7 @@ public class SettingsProvider extends ContentProvider {
null, true, SettingsState.SYSTEM_PACKAGE_NAME); null, true, SettingsState.SYSTEM_PACKAGE_NAME);
} }
} }
currentVersion = 147; currentVersion = 148;
} }
// vXXX: Add new settings above this point. // vXXX: Add new settings above this point.
@@ -3458,19 +3474,46 @@ public class SettingsProvider extends ContentProvider {
} }
} }
private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings) { private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
int userId) {
List<String> names = settings.getSettingNamesLocked(); List<String> names = settings.getSettingNamesLocked();
final int nameCount = names.size(); final int nameCount = names.size();
for (int i = 0; i < nameCount; i++) { for (int i = 0; i < nameCount; i++) {
String name = names.get(i); String name = names.get(i);
Setting setting = settings.getSettingLocked(name); Setting setting = settings.getSettingLocked(name);
if (setting.getDefaultValue() == null) {
boolean systemSet = SettingsState.isSystemPackage(getContext(), // In the upgrade case we pretend the call is made from the app
setting.getPackageName()); // that made the last change to the setting to properly determine
// whether the call has been made by a system component.
int callingUid = -1;
try {
callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
} catch (RemoteException e) {
/* ignore - handled below */
}
if (callingUid < 0) {
Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
continue;
}
try {
final boolean systemSet = SettingsState.isSystemPackage(getContext(),
setting.getPackageName(), callingUid);
if (systemSet) { if (systemSet) {
settings.insertSettingLocked(name, setting.getValue(), settings.insertSettingLocked(name, setting.getValue(),
setting.getTag(), true, setting.getPackageName()); setting.getTag(), true, setting.getPackageName());
} else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
// We had a bug where changes by non-system packages were marked
// as system made and as a result set as the default. Therefore, if
// the package changed the setting last is not a system one but the
// setting is marked as its default coming from the system we clear
// the default and clear the system set flag.
settings.resetSettingDefaultValueLocked(name);
} }
} catch (IllegalStateException e) {
// If the package goes over its quota during the upgrade, don't
// crash but just log the error as the system does the upgrade.
Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
} }
} }
} }

View File

@@ -297,6 +297,22 @@ final class SettingsState {
return insertSettingLocked(name, value, tag, makeValue, packageName); return insertSettingLocked(name, value, tag, makeValue, packageName);
} }
// The settings provider must hold its lock when calling here.
public void resetSettingDefaultValueLocked(String name) {
Setting oldSetting = getSettingLocked(name);
if (oldSetting != null && !oldSetting.isNull() && oldSetting.getDefaultValue() != null) {
String oldValue = oldSetting.getValue();
String oldDefaultValue = oldSetting.getDefaultValue();
Setting newSetting = new Setting(name, oldSetting.getValue(), null,
oldSetting.getPackageName(), oldSetting.getTag(), false,
oldSetting.getId());
mSettings.put(name, newSetting);
updateMemoryUsagePerPackageLocked(newSetting.getPackageName(), oldValue,
newSetting.getValue(), oldDefaultValue, newSetting.getDefaultValue());
scheduleWriteIfNeededLocked();
}
}
// The settings provider must hold its lock when calling here. // The settings provider must hold its lock when calling here.
public boolean insertSettingLocked(String name, String value, String tag, public boolean insertSettingLocked(String name, String value, String tag,
boolean makeDefault, String packageName) { boolean makeDefault, String packageName) {
@@ -1007,6 +1023,10 @@ final class SettingsState {
} }
public static boolean isSystemPackage(Context context, String packageName) { public static boolean isSystemPackage(Context context, String packageName) {
return isSystemPackage(context, packageName, Binder.getCallingUid());
}
public static boolean isSystemPackage(Context context, String packageName, int callingUid) {
synchronized (sLock) { synchronized (sLock) {
if (SYSTEM_PACKAGE_NAME.equals(packageName)) { if (SYSTEM_PACKAGE_NAME.equals(packageName)) {
return true; return true;
@@ -1019,7 +1039,7 @@ final class SettingsState {
} }
// Native services running as a special UID get a pass // Native services running as a special UID get a pass
final int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); final int callingAppId = UserHandle.getAppId(callingUid);
if (callingAppId < FIRST_APPLICATION_UID) { if (callingAppId < FIRST_APPLICATION_UID) {
sSystemUids.put(callingAppId, callingAppId); sSystemUids.put(callingAppId, callingAppId);
return true; return true;
@@ -1030,7 +1050,7 @@ final class SettingsState {
// profile for the purpose of determining whether the other end is a // profile for the purpose of determining whether the other end is a
// system component we need to use the user id of the caller for // system component we need to use the user id of the caller for
// pulling information about the caller from the package manager. // pulling information about the caller from the package manager.
final int callingUserId = UserHandle.getCallingUserId(); final int callingUserId = UserHandle.getUserId(callingUid);
final long identity = Binder.clearCallingIdentity(); final long identity = Binder.clearCallingIdentity();
try { try {