Merge "Disable LockPatternUtilsCache" into lmp-mr1-dev

automerge: a0ecc71

* commit 'a0ecc714c3aeaaa05649e0ab1f054cc8efbb126f':
  Disable LockPatternUtilsCache
This commit is contained in:
Adrian Roos
2014-11-10 19:06:54 +00:00
committed by android-build-merger
4 changed files with 50 additions and 7 deletions

View File

@@ -18,5 +18,14 @@ package com.android.internal.widget;
/** {@hide} */
oneway interface ILockSettingsObserver {
/**
* Called when a lock setting has changed.
*
* Note: Impementations of this should do as little work as possible, because this may be
* called synchronously while writing a setting.
*
* @param key the key of the setting that has changed or {@code null} if any may have changed.
* @param userId the user whose setting has changed.
*/
void onLockSettingChanged(in String key, in int userId);
}

View File

@@ -64,6 +64,13 @@ public class LockPatternUtils {
private static final String TAG = "LockPatternUtils";
private static final boolean DEBUG = false;
/**
* If true, LockPatternUtils will cache its values in-process. While this leads to faster reads,
* it can cause problems because writes to to the settings are no longer synchronous
* across all processes.
*/
private static final boolean ENABLE_CLIENT_CACHE = false;
/**
* The maximum number of incorrect attempts before the user is prevented
* from trying again for {@link #FAILED_ATTEMPT_TIMEOUT_MS}.
@@ -207,8 +214,13 @@ public class LockPatternUtils {
private ILockSettings getLockSettings() {
if (mLockSettingsService == null) {
mLockSettingsService = LockPatternUtilsCache.getInstance(
ILockSettings.Stub.asInterface(ServiceManager.getService("lock_settings")));
ILockSettings service = ILockSettings.Stub.asInterface(
ServiceManager.getService("lock_settings"));
if (ENABLE_CLIENT_CACHE) {
mLockSettingsService = LockPatternUtilsCache.getInstance(service);
} else {
mLockSettingsService = service;
}
}
return mLockSettingsService;
}

View File

@@ -18,7 +18,9 @@ package com.android.internal.widget;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
/**
* A decorator for {@link ILockSettings} that caches the key-value responses in memory.
@@ -28,9 +30,11 @@ import android.util.ArrayMap;
*/
public class LockPatternUtilsCache implements ILockSettings {
private static final String HAS_LOCK_PATTERN_CACHE_KEY
private static final String TAG = "LockPatternUtilsCache";
public static final String HAS_LOCK_PATTERN_CACHE_KEY
= "LockPatternUtils.Cache.HasLockPatternCacheKey";
private static final String HAS_LOCK_PASSWORD_CACHE_KEY
public static final String HAS_LOCK_PASSWORD_CACHE_KEY
= "LockPatternUtils.Cache.HasLockPasswordCacheKey";
private static LockPatternUtilsCache sInstance;
@@ -53,7 +57,7 @@ public class LockPatternUtilsCache implements ILockSettings {
// ILockSettings
private LockPatternUtilsCache(ILockSettings service) {
public LockPatternUtilsCache(ILockSettings service) {
mService = service;
try {
service.registerObserver(mObserver);
@@ -186,6 +190,7 @@ public class LockPatternUtilsCache implements ILockSettings {
// Caching
private Object peekCache(String key, int userId) {
if (!validateUserId(userId)) return null;
synchronized (mCache) {
// Safe to reuse mCacheKey, because it is not stored in the map.
return mCache.get(mCacheKey.set(key, userId));
@@ -193,6 +198,7 @@ public class LockPatternUtilsCache implements ILockSettings {
}
private void putCache(String key, int userId, Object value) {
if (!validateUserId(userId)) return;
synchronized (mCache) {
// Create a new key, because this will be stored in the map.
mCache.put(new CacheKey().set(key, userId), value);
@@ -200,9 +206,14 @@ public class LockPatternUtilsCache implements ILockSettings {
}
private void invalidateCache(String key, int userId) {
if (!validateUserId(userId)) return;
synchronized (mCache) {
// Safe to reuse mCacheKey, because it is not stored in the map.
mCache.remove(mCacheKey.set(key, userId));
if (key != null) {
// Safe to reuse mCacheKey, because it is not stored in the map.
mCache.remove(mCacheKey.set(key, userId));
} else {
mCache.clear();
}
}
}
@@ -213,6 +224,14 @@ public class LockPatternUtilsCache implements ILockSettings {
}
};
private final boolean validateUserId(int userId) {
if (userId < UserHandle.USER_OWNER) {
Log.e(TAG, "User " + userId + " not supported: Must be a concrete user.");
return false;
}
return true;
}
private static final class CacheKey {
String key;
int userId;

View File

@@ -49,6 +49,7 @@ import android.util.Slog;
import com.android.internal.widget.ILockSettings;
import com.android.internal.widget.ILockSettingsObserver;
import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.LockPatternUtilsCache;
import java.util.ArrayList;
import java.util.Arrays;
@@ -348,6 +349,7 @@ public class LockSettingsService extends ILockSettings.Stub {
final byte[] hash = LockPatternUtils.patternToHash(
LockPatternUtils.stringToPattern(pattern));
mStorage.writePatternHash(hash, userId);
notifyObservers(LockPatternUtilsCache.HAS_LOCK_PATTERN_CACHE_KEY, userId);
}
@Override
@@ -357,6 +359,7 @@ public class LockSettingsService extends ILockSettings.Stub {
maybeUpdateKeystore(password, userId);
mStorage.writePasswordHash(mLockPatternUtils.passwordToHash(password, userId), userId);
notifyObservers(LockPatternUtilsCache.HAS_LOCK_PASSWORD_CACHE_KEY, userId);
}
@Override