Skip to content

Commit

Permalink
Fix system customizations getting overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud0808 committed Feb 16, 2024
1 parent cccf6d7 commit ab30fc6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ interface IShizukuConnection {
void exit() = 1;
void applyFabricatedColors(String jsonString) = 2;
void removeFabricatedColors() = 3;
String getCurrentSettings() = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/drdisagree/colorblendr/utils/MiscUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<String> keys = source.keys();
while (keys.hasNext()) {
String key = keys.next();
target.put(key, source.get(key));
}
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ab30fc6

Please sign in to comment.