Skip to content

Commit

Permalink
Add PreferenceManager.getDefaultSharedPreferences() method
Browse files Browse the repository at this point in the history
  • Loading branch information
neighbWang authored and johnsonlee committed Jun 28, 2020
1 parent 2d0e1c4 commit 8e4b1ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public static SharedPreferences getSharedPreferences(final Context context, Stri
return BoosterSharedPreferences.getSharedPreferences(context, name);
}

public static SharedPreferences getDefaultSharedPreferences(Context context) {
return BoosterSharedPreferences.getSharedPreferences(context, getDefaultSharedPreferencesName(context));
}

private static String getDefaultSharedPreferencesName(Context context) {
return context.getPackageName() + "_preferences";
}

public static SharedPreferences getPreferences(final Activity activity, final int mode) {
return getSharedPreferences(activity.getApplicationContext(), activity.getLocalClassName(), mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ class SharedPreferencesTransformer : ClassTransformer {

klass.methods.forEach { method ->
method.instructions.iterator().asIterable().filter {
it.opcode == INVOKEVIRTUAL
it.opcode == INVOKEVIRTUAL || it.opcode == INVOKESTATIC
}.map {
it as MethodInsnNode
}.forEach {
mapOf(
CONTEXT_GET_SHARED_PREFERENCES to SHADOW_CONTEXT_GET_SHARED_PREFERENCES,
ACTIVITY_GET_PREFERENCES to SHADOW_ACTIVITY_GET_PREFERENCES
ACTIVITY_GET_PREFERENCES to SHADOW_ACTIVITY_GET_PREFERENCES,
PREFERENCE_MANAGER_GET_DEFAULT_SHARED_PREFERENCES to SHADOW_PREFERENCE_MANAGER_GET_DEFAULT_SHARED_PREFERENCES
).forEach { (original, shadow) ->
if (it.opcode == original.opcode && it.name == original.name && it.desc == original.desc && context.klassPool.get(original.owner).isAssignableFrom(it.owner)) {
logger.println(" * ${shadow.owner}${shadow.name}${shadow.desc} => ${it.owner}.${it.name}${it.desc}: ${klass.name}.${method.name}${method.desc}")
Expand All @@ -72,12 +73,17 @@ class SharedPreferencesTransformer : ClassTransformer {

private const val ACTIVITY = "android/app/Activity"
private const val CONTEXT = "android/content/Context"
private const val PREFERENCE_MANAGER = "android.preference/PreferenceManager"
private const val SHARED_PREFERENCES = "android/content/SharedPreferences"
private const val SUPPORT_MULTIDEX_PACKAGE_PREFIX = "android/support/multidex/"
private const val BOOSTER_DIRECTORY_PREFIX = "com/didiglobal/booster/instrument"
private const val SHADOW_SHARED_PREFERENCES = "$BOOSTER_DIRECTORY_PREFIX/ShadowSharedPreferences"

private val CONTEXT_GET_SHARED_PREFERENCES = MethodInsnNode(INVOKEVIRTUAL, CONTEXT, "getSharedPreferences", "(Ljava/lang/String;I)L$SHARED_PREFERENCES;")
private val SHADOW_CONTEXT_GET_SHARED_PREFERENCES = MethodInsnNode(INVOKESTATIC, SHADOW_SHARED_PREFERENCES, "getSharedPreferences", "(L$CONTEXT;Ljava/lang/String;I)L$SHARED_PREFERENCES;")

private val PREFERENCE_MANAGER_GET_DEFAULT_SHARED_PREFERENCES = MethodInsnNode(INVOKESTATIC, PREFERENCE_MANAGER, "getDefaultSharedPreferences", "(L$CONTEXT;)L$SHARED_PREFERENCES;")
private val SHADOW_PREFERENCE_MANAGER_GET_DEFAULT_SHARED_PREFERENCES = MethodInsnNode(INVOKESTATIC, SHADOW_SHARED_PREFERENCES, "getDefaultSharedPreferences", "(L$CONTEXT;)L$SHARED_PREFERENCES;")

private val ACTIVITY_GET_PREFERENCES = MethodInsnNode(INVOKEVIRTUAL, ACTIVITY, "getPreferences", "(I)L$SHARED_PREFERENCES;")
private val SHADOW_ACTIVITY_GET_PREFERENCES = MethodInsnNode(INVOKESTATIC, SHADOW_SHARED_PREFERENCES, "getPreferences", "(L$ACTIVITY;I)L$SHARED_PREFERENCES;")

0 comments on commit 8e4b1ec

Please sign in to comment.