Skip to content

Commit 7e0683b

Browse files
committed
Notify settings URI change without a lock held
bug:22469552 Change-Id: Ie4a42ceef07e3a8e593fe2b1374420239242ce7b
1 parent 405eceb commit 7e0683b

1 file changed

Lines changed: 45 additions & 23 deletions

File tree

packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import android.os.Bundle;
4343
import android.os.DropBoxManager;
4444
import android.os.Environment;
45+
import android.os.Handler;
46+
import android.os.Looper;
47+
import android.os.Message;
4548
import android.os.ParcelFileDescriptor;
4649
import android.os.Process;
4750
import android.os.SystemProperties;
@@ -204,17 +207,13 @@ public class SettingsProvider extends ContentProvider {
204207
// We have to call in the user manager with no lock held,
205208
private volatile UserManager mUserManager;
206209

207-
// We have to call in the app ops manager with no lock held,
208-
private volatile AppOpsManager mAppOpsManager;
209-
210210
// We have to call in the package manager with no lock held,
211211
private volatile PackageManager mPackageManager;
212212

213213
@Override
214214
public boolean onCreate() {
215215
synchronized (mLock) {
216216
mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
217-
mAppOpsManager = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE);
218217
mPackageManager = getContext().getPackageManager();
219218
mSettingsRegistry = new SettingsRegistry();
220219
}
@@ -532,7 +531,7 @@ private void dumpSettings(Cursor cursor, PrintWriter pw) {
532531
} while (cursor.moveToNext());
533532
}
534533

535-
private static final String toDumpString(String s) {
534+
private static String toDumpString(String s) {
536535
if (s != null) {
537536
return s;
538537
}
@@ -1158,18 +1157,6 @@ private boolean updateLocationProvidersAllowedLocked(String value, int owningUse
11581157
getCallingPackage());
11591158
}
11601159

1161-
private void sendNotify(Uri uri, int userId) {
1162-
final long identity = Binder.clearCallingIdentity();
1163-
try {
1164-
getContext().getContentResolver().notifyChange(uri, null, true, userId);
1165-
if (DEBUG) {
1166-
Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
1167-
}
1168-
} finally {
1169-
Binder.restoreCallingIdentity(identity);
1170-
}
1171-
}
1172-
11731160
private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
11741161
int targetSdkVersion, String name) {
11751162
// If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
@@ -1390,8 +1377,11 @@ final class SettingsRegistry {
13901377

13911378
private final BackupManager mBackupManager;
13921379

1380+
private final Handler mHandler;
1381+
13931382
public SettingsRegistry() {
13941383
mBackupManager = new BackupManager(getContext());
1384+
mHandler = new MyHandler(getContext().getMainLooper());
13951385
migrateAllLegacySettingsIfNeeded();
13961386
}
13971387

@@ -1733,15 +1723,17 @@ private void notifyForSettingsChange(int key, String name) {
17331723

17341724
// Inform the backup manager about a data change
17351725
if (backedUpDataChanged) {
1736-
mBackupManager.dataChanged();
1726+
mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
17371727
}
17381728

17391729
// Now send the notification through the content framework.
17401730

17411731
final int userId = getUserIdFromKey(key);
17421732
Uri uri = getNotificationUriFor(key, name);
17431733

1744-
sendNotify(uri, userId);
1734+
mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
1735+
userId, 0, uri).sendToTarget();
1736+
17451737
if (isSecureSettingsKey(key)) {
17461738
maybeNotifyProfiles(userId, uri, name, sSecureCloneToManagedSettings);
17471739
} else if (isSystemSettingsKey(key)) {
@@ -1758,7 +1750,8 @@ private void maybeNotifyProfiles(int userId, Uri uri, String name,
17581750
UserInfo profile = profiles.get(i);
17591751
// the notification for userId has already been sent.
17601752
if (profile.id != userId) {
1761-
sendNotify(uri, profile.id);
1753+
mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
1754+
profile.id, 0, uri).sendToTarget();
17621755
}
17631756
}
17641757
}
@@ -1834,6 +1827,33 @@ private int getMaxBytesPerPackageForType(int type) {
18341827
}
18351828
}
18361829

1830+
private final class MyHandler extends Handler {
1831+
private static final int MSG_NOTIFY_URI_CHANGED = 1;
1832+
private static final int MSG_NOTIFY_DATA_CHANGED = 2;
1833+
1834+
public MyHandler(Looper looper) {
1835+
super(looper);
1836+
}
1837+
1838+
@Override
1839+
public void handleMessage(Message msg) {
1840+
switch (msg.what) {
1841+
case MSG_NOTIFY_URI_CHANGED: {
1842+
final int userId = msg.arg1;
1843+
Uri uri = (Uri) msg.obj;
1844+
getContext().getContentResolver().notifyChange(uri, null, true, userId);
1845+
if (DEBUG) {
1846+
Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
1847+
}
1848+
} break;
1849+
1850+
case MSG_NOTIFY_DATA_CHANGED: {
1851+
mBackupManager.dataChanged();
1852+
} break;
1853+
}
1854+
}
1855+
}
1856+
18371857
private final class UpgradeController {
18381858
private static final int SETTINGS_VERSION = 122;
18391859

@@ -1963,9 +1983,11 @@ private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
19631983
currentVersion = 120;
19641984
}
19651985

1966-
// Before 121, we used a different string encoding logic. We just bump the version
1967-
// here; SettingsState knows how to handle pre-version 120 files.
1968-
currentVersion = 121;
1986+
if (currentVersion == 120) {
1987+
// Before 121, we used a different string encoding logic. We just bump the
1988+
// version here; SettingsState knows how to handle pre-version 120 files.
1989+
currentVersion = 121;
1990+
}
19691991

19701992
if (currentVersion == 121) {
19711993
// Version 122: allow OEMs to set a default payment component in resources.

0 commit comments

Comments
 (0)