Settings were persisted on the system background thread but during
first boot the device is under heavy load and persisting settings
competes with other system components using the shared background
thread. As a result persisting settings can be delayed much longer
than the expected 200ms. This can cause issues with setup wizard
being skipped/went over and its component disaabled being persisted
but the setting whether the device is provisioned not being
persisted - now if the device boots it will have no SUW but also
the home button would be missing. Generally, we need a tansactional
abstraction in the system process to peform all delayed operations
atomically.
bug:
25472484
Change-Id: I8e0cf7ffa32e86e36e777964eb0c3cc7de02d3c3
import android.os.DropBoxManager;
import android.os.Environment;
import android.os.Handler;
+import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
@GuardedBy("mLock")
private SettingsRegistry mSettingsRegistry;
+ @GuardedBy("mLock")
+ private HandlerThread mHandlerThread;
+
+ @GuardedBy("mLock")
+ private Handler mBackgroundHandler;
+
// We have to call in the user manager with no lock held,
private volatile UserManager mUserManager;
synchronized (mLock) {
mUserManager = UserManager.get(getContext());
mPackageManager = AppGlobals.getPackageManager();
+ mHandlerThread = new HandlerThread(LOG_TAG,
+ Process.THREAD_PRIORITY_BACKGROUND);
+ mHandlerThread.start();
+ mBackgroundHandler = new Handler(mHandlerThread.getLooper());
mSettingsRegistry = new SettingsRegistry();
}
registerBroadcastReceivers();
if (mSettingsStates.get(key) == null) {
final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
- maxBytesPerPackage);
+ maxBytesPerPackage, mBackgroundHandler);
mSettingsStates.put(key, settingsState);
}
}
private final Object mLock;
- private final Handler mHandler = new MyHandler();
+ private final Handler mHandler;
@GuardedBy("mLock")
private final ArrayMap<String, Setting> mSettings = new ArrayMap<>();
@GuardedBy("mLock")
private long mNextId;
- public SettingsState(Object lock, File file, int key, int maxBytesPerAppPackage) {
+ public SettingsState(Object lock, File file, int key, int maxBytesPerAppPackage,
+ Handler handler) {
// It is important that we use the same lock as the settings provider
// to ensure multiple mutations on this state are atomicaly persisted
// as the async persistence should be blocked while we make changes.
mLock = lock;
mStatePersistFile = file;
mKey = key;
+ mHandler = handler;
if (maxBytesPerAppPackage == MAX_BYTES_PER_APP_PACKAGE_LIMITED) {
mMaxBytesPerAppPackage = maxBytesPerAppPackage;
mPackageToMemoryUsage = new ArrayMap<>();
*/
package com.android.providers.settings;
+import android.os.Handler;
import android.test.AndroidTestCase;
import android.util.Xml;
final Object lock = new Object();
final SettingsState ssWriter = new SettingsState(lock, file, 1,
- SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED);
+ SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, new Handler());
ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSOIN_NEW_ENCODING);
ssWriter.insertSettingLocked("k1", "\u0000", "package");
}
final SettingsState ssReader = new SettingsState(lock, file, 1,
- SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED);
+ SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, new Handler());
synchronized (lock) {
assertEquals("\u0000", ssReader.getSettingLocked("k1").getValue());
assertEquals("abc", ssReader.getSettingLocked("k2").getValue());
os.close();
final SettingsState ss = new SettingsState(lock, file, 1,
- SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED);
+ SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, new Handler());
synchronized (lock) {
SettingsState.Setting s;
s = ss.getSettingLocked("k0");