From ab30fc6d6045fb2bb8fd97c12ff2b22971c7c2fb Mon Sep 17 00:00:00 2001 From: DrDisagree Date: Fri, 16 Feb 2024 15:44:31 +0600 Subject: [PATCH] Fix system customizations getting overridden --- .../service/IShizukuConnection.aidl | 1 + .../extension/ThemeOverlayPackage.java | 8 +++-- .../service/ShizukuConnection.java | 29 +++++++++++++++++-- .../colorblendr/utils/MiscUtil.java | 27 +++++++++++++++++ .../colorblendr/utils/OverlayManager.java | 6 +++- 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/app/src/main/aidl/com/drdisagree/colorblendr/service/IShizukuConnection.aidl b/app/src/main/aidl/com/drdisagree/colorblendr/service/IShizukuConnection.aidl index d8c73d68..eed180ff 100644 --- a/app/src/main/aidl/com/drdisagree/colorblendr/service/IShizukuConnection.aidl +++ b/app/src/main/aidl/com/drdisagree/colorblendr/service/IShizukuConnection.aidl @@ -5,4 +5,5 @@ interface IShizukuConnection { void exit() = 1; void applyFabricatedColors(String jsonString) = 2; void removeFabricatedColors() = 3; + String getCurrentSettings() = 4; } \ No newline at end of file diff --git a/app/src/main/java/com/drdisagree/colorblendr/extension/ThemeOverlayPackage.java b/app/src/main/java/com/drdisagree/colorblendr/extension/ThemeOverlayPackage.java index 0ef95b82..dd23cac3 100644 --- a/app/src/main/java/com/drdisagree/colorblendr/extension/ThemeOverlayPackage.java +++ b/app/src/main/java/com/drdisagree/colorblendr/extension/ThemeOverlayPackage.java @@ -14,9 +14,10 @@ public class ThemeOverlayPackage { private static final String TAG = ThemeOverlayPackage.class.getSimpleName(); - private static final String THEME_STYLE = "android.theme.customization.theme_style"; - private static final String COLOR_SOURCE = "android.theme.customization.color_source"; - private static final String SYSTEM_PALETTE = "android.theme.customization.system_palette"; + public static final String THEME_STYLE = "android.theme.customization.theme_style"; + public static final String COLOR_SOURCE = "android.theme.customization.color_source"; + public static final String SYSTEM_PALETTE = "android.theme.customization.system_palette"; + private static final String APPLIED_TIMESTAMP = "_applied_timestamp"; public static JSONObject getThemeCustomizationOverlayPackages() { JSONObject object = new JSONObject(); @@ -31,6 +32,7 @@ public static JSONObject getThemeCustomizationOverlayPackages() { SYSTEM_PALETTE, ColorUtil.intToHexColorNoHash(RPrefs.getInt(MONET_SEED_COLOR, Color.BLUE)) ); + object.putOpt(APPLIED_TIMESTAMP, String.valueOf(System.currentTimeMillis())); } catch (Exception e) { Log.e(TAG, "getThemeCustomizationOverlayPackages:", e); } diff --git a/app/src/main/java/com/drdisagree/colorblendr/service/ShizukuConnection.java b/app/src/main/java/com/drdisagree/colorblendr/service/ShizukuConnection.java index d4a43f46..f76c89b2 100644 --- a/app/src/main/java/com/drdisagree/colorblendr/service/ShizukuConnection.java +++ b/app/src/main/java/com/drdisagree/colorblendr/service/ShizukuConnection.java @@ -7,8 +7,11 @@ import androidx.annotation.Keep; +import com.drdisagree.colorblendr.extension.ThemeOverlayPackage; import com.topjohnwu.superuser.Shell; +import org.json.JSONObject; + public class ShizukuConnection extends IShizukuConnection.Stub { private static final String TAG = ShizukuConnection.class.getSimpleName(); @@ -40,7 +43,29 @@ public void applyFabricatedColors(String jsonString) { @Override public void removeFabricatedColors() { - final String mCommand = "settings delete secure " + THEME_CUSTOMIZATION_OVERLAY_PACKAGES; - Shell.cmd(mCommand).exec(); + try { + String currentSettings = getCurrentSettings(); + JSONObject jsonObject = new JSONObject(currentSettings); + + String[] keysToRemove = new String[]{ + ThemeOverlayPackage.THEME_STYLE, + ThemeOverlayPackage.COLOR_SOURCE, + ThemeOverlayPackage.SYSTEM_PALETTE + }; + + for (String key : keysToRemove) { + jsonObject.remove(key); + } + + applyFabricatedColors(jsonObject.toString()); + } catch (Exception e) { + Log.e(TAG, "removeFabricatedColors: ", e); + } + } + + @Override + public String getCurrentSettings() { + final String mCommand = "settings get secure " + THEME_CUSTOMIZATION_OVERLAY_PACKAGES; + return Shell.cmd(mCommand).exec().getOut().get(0); } } diff --git a/app/src/main/java/com/drdisagree/colorblendr/utils/MiscUtil.java b/app/src/main/java/com/drdisagree/colorblendr/utils/MiscUtil.java index c2489859..63a9eaa3 100644 --- a/app/src/main/java/com/drdisagree/colorblendr/utils/MiscUtil.java +++ b/app/src/main/java/com/drdisagree/colorblendr/utils/MiscUtil.java @@ -8,7 +8,11 @@ import com.google.android.material.appbar.MaterialToolbar; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; +import java.util.Iterator; public class MiscUtil { @@ -42,4 +46,27 @@ public static void setToolbarTitle(Context context, @StringRes int title, boolea ((AppCompatActivity) context).getSupportActionBar().setDisplayShowHomeEnabled(showBackButton); } } + + public static String mergeJsonStrings(String target, String source) throws JSONException { + if (target == null || target.isEmpty()) { + target = new JSONObject().toString(); + } + + if (source == null || source.isEmpty()) { + source = new JSONObject().toString(); + } + + JSONObject targetJson = new JSONObject(target); + JSONObject sourceJson = new JSONObject(source); + return mergeJsonObjects(targetJson, sourceJson).toString(); + } + + public static JSONObject mergeJsonObjects(JSONObject target, JSONObject source) throws JSONException { + Iterator keys = source.keys(); + while (keys.hasNext()) { + String key = keys.next(); + target.put(key, source.get(key)); + } + return target; + } } diff --git a/app/src/main/java/com/drdisagree/colorblendr/utils/OverlayManager.java b/app/src/main/java/com/drdisagree/colorblendr/utils/OverlayManager.java index c785e7fb..ae34b133 100644 --- a/app/src/main/java/com/drdisagree/colorblendr/utils/OverlayManager.java +++ b/app/src/main/java/com/drdisagree/colorblendr/utils/OverlayManager.java @@ -343,9 +343,13 @@ public static boolean applyFabricatedColorsNonRoot(Context context) { } try { + String currentSettings = mShizukuConnection.getCurrentSettings(); String jsonString = ThemeOverlayPackage.getThemeCustomizationOverlayPackages().toString(); + if (!jsonString.isEmpty()) { - mShizukuConnection.applyFabricatedColors(jsonString); + mShizukuConnection.applyFabricatedColors( + MiscUtil.mergeJsonStrings(currentSettings, jsonString) + ); } } catch (Exception e) { Log.d(TAG, "applyFabricatedColorsNonRoot: ", e);