diff --git a/api/lineage_current.txt b/api/lineage_current.txt index 094a2e7b..9f363065 100644 --- a/api/lineage_current.txt +++ b/api/lineage_current.txt @@ -803,7 +803,9 @@ package lineageos.providers { field public static final java.lang.String BATTERY_LIGHT_PULSE = "battery_light_pulse"; field public static final java.lang.String BERRY_CURRENT_ACCENT = "berry_current_accent"; field public static final java.lang.String BERRY_GLOBAL_STYLE = "berry_global_style"; + field public static final java.lang.String BERRY_MANAGED_BY_APP = "berry_managed_by_app"; field public static final java.lang.String BLUETOOTH_ACCEPT_ALL_FILES = "bluetooth_accept_all_files"; + field public static final java.lang.String BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED = "button_backlight_only_when_pressed"; field public static final java.lang.String CALL_RECORDING_FORMAT = "call_recording_format"; field public static final java.lang.String CAMERA_LAUNCH = "camera_launch"; field public static final java.lang.String CAMERA_SLEEP_ON_RELEASE = "camera_sleep_on_release"; @@ -989,10 +991,13 @@ package lineageos.providers { package lineageos.style { public class StyleInterface { + method public java.lang.String getAccent(); + method public int getGlobalStyle(); method public static lineageos.style.StyleInterface getInstance(android.content.Context); method public lineageos.style.Suggestion getSuggestion(android.graphics.Bitmap, int[]); + method public java.util.List getTrustedAccents(); method public boolean setAccent(java.lang.String); - method public boolean setGlobalStyle(int); + method public boolean setGlobalStyle(int, java.lang.String); field public static final java.lang.String ACCENT_DEFAULT = "lineageos"; field public static final java.lang.String CHANGE_STYLE_SETTINGS_PERMISSION = "lineageos.permission.CHANGE_STYLE"; field public static final int STYLE_GLOBAL_AUTO_DAYTIME = 1; // 0x1 diff --git a/lineage/lib/main/java/org/lineageos/platform/internal/StyleInterfaceService.java b/lineage/lib/main/java/org/lineageos/platform/internal/StyleInterfaceService.java index d7b5e4a1..2700a0cf 100644 --- a/lineage/lib/main/java/org/lineageos/platform/internal/StyleInterfaceService.java +++ b/lineage/lib/main/java/org/lineageos/platform/internal/StyleInterfaceService.java @@ -39,6 +39,9 @@ import lineageos.style.StyleInterface; import lineageos.style.Suggestion; import lineageos.util.palette.Palette; +import java.util.ArrayList; +import java.util.List; + /** @hide */ public class StyleInterfaceService extends LineageSystemService { private static final String TAG = "LineageStyleInterfaceService"; @@ -76,9 +79,23 @@ public class StyleInterfaceService extends LineageSystemService { "You do not have permissions to change system style"); } - private boolean setGlobalStyleInternal(int mode) { - return LineageSettings.System.putInt(mContext.getContentResolver(), + private boolean setGlobalStyleInternal(int mode, String packageName) { + // Check whether the packageName is valid + if (isAValidPackage(packageName)) { + throw new IllegalArgumentException(packageName + " is not a valid package name!"); + } + + boolean statusValue = LineageSettings.System.putInt(mContext.getContentResolver(), LineageSettings.System.BERRY_GLOBAL_STYLE, mode); + boolean packageNameValue = LineageSettings.System.putString(mContext.getContentResolver(), + LineageSettings.System.BERRY_MANAGED_BY_APP, packageName); + return statusValue && packageNameValue; + } + + private int getGlobalStyleInternal() { + return LineageSettings.System.getInt(mContext.getContentResolver(), + LineageSettings.System.BERRY_GLOBAL_STYLE, + StyleInterface.STYLE_GLOBAL_AUTO_WALLPAPER); } private boolean setAccentInternal(String pkgName) { @@ -90,8 +107,8 @@ public class StyleInterfaceService extends LineageSystemService { int userId = UserHandle.myUserId(); // Disable current accent - String currentAccent = LineageSettings.System.getString(mContext.getContentResolver(), - LineageSettings.System.BERRY_CURRENT_ACCENT); + String currentAccent = getAccentInternal(); + try { mOverlayService.setEnabled(currentAccent, false, userId); } catch (RemoteException e) { @@ -114,6 +131,11 @@ public class StyleInterfaceService extends LineageSystemService { return false; } + private String getAccentInternal() { + return LineageSettings.System.getString(mContext.getContentResolver(), + LineageSettings.System.BERRY_CURRENT_ACCENT); + } + private Suggestion getSuggestionInternal(Bitmap source, int[] colors) { Palette palette = Palette.from(source).generate(); @@ -131,6 +153,21 @@ public class StyleInterfaceService extends LineageSystemService { return new Suggestion(suggestedGlobalStyle, bestColorPosition); } + private List getTrustedAccentsInternal() { + List results = new ArrayList<>(); + String[] packages = mContext.getResources() + .getStringArray(R.array.trusted_accent_packages); + + results.add(StyleInterface.ACCENT_DEFAULT); + for (String item : packages) { + if (isChangeableOverlay(item)) { + results.add(item); + } + } + + return results; + } + private int getBestColor(int sourceColor, int[] colors) { int best = 0; double minDiff = Double.MAX_VALUE; @@ -180,9 +217,17 @@ public class StyleInterfaceService extends LineageSystemService { } } + private boolean isAValidPackage(String packageName) { + try { + return packageName != null && mPackageManager.getPackageInfo(packageName, 0) == null; + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } + private final IBinder mService = new IStyleInterface.Stub() { @Override - public boolean setGlobalStyle(int style) { + public boolean setGlobalStyle(int style, String packageName) { enforceChangeStylePermission(); /* * We need to clear the caller's identity in order to @@ -190,11 +235,25 @@ public class StyleInterfaceService extends LineageSystemService { * not allowed by the caller's permissions. */ long token = clearCallingIdentity(); - boolean success = setGlobalStyleInternal(style); + boolean success = setGlobalStyleInternal(style, packageName); restoreCallingIdentity(token); return success; } + @Override + public int getGlobalStyle() { + enforceChangeStylePermission(); + /* + * We need to clear the caller's identity in order to + * allow this method call to modify settings + * not allowed by the caller's permissions. + */ + long token = clearCallingIdentity(); + int result = getGlobalStyleInternal(); + restoreCallingIdentity(token); + return result; + } + @Override public boolean setAccent(String pkgName) { enforceChangeStylePermission(); @@ -209,6 +268,20 @@ public class StyleInterfaceService extends LineageSystemService { return success; } + @Override + public String getAccent() { + enforceChangeStylePermission(); + /* + * We need to clear the caller's identity in order to + * allow this method call to modify settings + * not allowed by the caller's permissions. + */ + long token = clearCallingIdentity(); + String result = getAccentInternal(); + restoreCallingIdentity(token); + return result; + } + @Override public Suggestion getSuggestion(Bitmap source, int[] colors) { enforceChangeStylePermission(); @@ -222,5 +295,19 @@ public class StyleInterfaceService extends LineageSystemService { restoreCallingIdentity(token); return result; } + + @Override + public List getTrustedAccents() { + enforceChangeStylePermission(); + /* + * We need to clear the caller's identity in order to + * allow this method call to modify settings + * not allowed by the caller's permissions. + */ + long token = clearCallingIdentity(); + List result = getTrustedAccentsInternal(); + restoreCallingIdentity(token); + return result; + } }; } \ No newline at end of file diff --git a/lineage/res/res/values/arrays.xml b/lineage/res/res/values/arrays.xml index 2251e4d4..9a5e58ca 100644 --- a/lineage/res/res/values/arrays.xml +++ b/lineage/res/res/values/arrays.xml @@ -116,4 +116,17 @@ com.google.android.gsf|com.google.android.talk + + + org.lineageos.overlay.accent.red + org.lineageos.overlay.accent.pink + org.lineageos.overlay.accent.purple + org.lineageos.overlay.accent.blue + org.lineageos.overlay.accent.cyan + org.lineageos.overlay.accent.green + org.lineageos.overlay.accent.orange + org.lineageos.overlay.accent.yellow + org.lineageos.overlay.accent.brown + org.lineageos.overlay.accent.black + diff --git a/lineage/res/res/values/symbols.xml b/lineage/res/res/values/symbols.xml index ed44a149..1bf265ce 100644 --- a/lineage/res/res/values/symbols.xml +++ b/lineage/res/res/values/symbols.xml @@ -149,6 +149,7 @@ + diff --git a/sdk/src/java/lineageos/providers/LineageSettings.java b/sdk/src/java/lineageos/providers/LineageSettings.java index 6ed7fdaf..590983b7 100644 --- a/sdk/src/java/lineageos/providers/LineageSettings.java +++ b/sdk/src/java/lineageos/providers/LineageSettings.java @@ -1331,6 +1331,15 @@ public final class LineageSettings { public static final Validator BERRY_CURRENT_ACCENT_VALIDATOR = sNonNullStringValidator; + /** + * Current application managing the style + */ + public static final String BERRY_MANAGED_BY_APP = "berry_managed_by_app"; + + /** @hide */ + public static final Validator BERRY_MANAGED_BY_APP_VALIDATOR = + sNonNullStringValidator; + /** * Enable looking up of phone numbers of nearby places * 0 = 0ff, 1 = on @@ -2192,6 +2201,7 @@ public final class LineageSettings { VALIDATORS.put(PROXIMITY_ON_WAKE, PROXIMITY_ON_WAKE_VALIDATOR); VALIDATORS.put(BERRY_GLOBAL_STYLE, BERRY_GLOBAL_STYLE_VALIDATOR); VALIDATORS.put(BERRY_CURRENT_ACCENT, BERRY_CURRENT_ACCENT_VALIDATOR); + VALIDATORS.put(BERRY_MANAGED_BY_APP, BERRY_MANAGED_BY_APP_VALIDATOR); VALIDATORS.put(ENABLE_FORWARD_LOOKUP, ENABLE_FORWARD_LOOKUP_VALIDATOR); VALIDATORS.put(ENABLE_PEOPLE_LOOKUP, ENABLE_PEOPLE_LOOKUP_VALIDATOR); VALIDATORS.put(ENABLE_REVERSE_LOOKUP, ENABLE_REVERSE_LOOKUP_VALIDATOR); diff --git a/sdk/src/java/lineageos/style/IStyleInterface.aidl b/sdk/src/java/lineageos/style/IStyleInterface.aidl index a69c07c2..d7147de1 100644 --- a/sdk/src/java/lineageos/style/IStyleInterface.aidl +++ b/sdk/src/java/lineageos/style/IStyleInterface.aidl @@ -20,9 +20,14 @@ package lineageos.style; import android.graphics.Bitmap; import lineageos.style.Suggestion; +import java.util.List; + /** {@hide} */ interface IStyleInterface { - boolean setGlobalStyle(int style); + boolean setGlobalStyle(int style, String pkgName); + int getGlobalStyle(); boolean setAccent(String pkgName); + String getAccent(); Suggestion getSuggestion(in Bitmap source, in int[] colors); + List getTrustedAccents(); } \ No newline at end of file diff --git a/sdk/src/java/lineageos/style/StyleInterface.java b/sdk/src/java/lineageos/style/StyleInterface.java index cf6f7a91..b1e70112 100644 --- a/sdk/src/java/lineageos/style/StyleInterface.java +++ b/sdk/src/java/lineageos/style/StyleInterface.java @@ -25,6 +25,14 @@ import android.util.Log; import lineageos.app.LineageContextConstants; +import java.util.ArrayList; +import java.util.List; + +/** + * Interface used to customize the System colors. It's capable of setting a global + * light and dark mode and custom color accents. + */ + public class StyleInterface { /** @@ -56,8 +64,8 @@ public class StyleInterface { public static final int STYLE_GLOBAL_DARK = 3; /** - * Default accent - * Used to remove any active accent and use default one + * Default accent name. + * It can also be used to remove any active accent * * @see #setAccent */ @@ -95,8 +103,9 @@ public class StyleInterface { } /** - * Get or create an instance of the {@link lineageos.app.StyleInterface} - * @param context + * Get or create an instance of the {@link lineageos.style.StyleInterface} + * + * @param context Used to get the service * @return {@link StyleInterface} */ public static StyleInterface getInstance(Context context) { @@ -138,19 +147,42 @@ public class StyleInterface { * {@link #STYLE_GLOBAL_AUTO_DAYTIME}, * {@link #STYLE_GLOBAL_LIGHT} or * {@link #STYLE_GLOBAL_DARK} + * @param pkgName The package name of the calling application + * + * @return Whether the process failed */ - public boolean setGlobalStyle(int style) { + public boolean setGlobalStyle(int style, String pkgName) { if (sService == null) { return false; } try { - return sService.setGlobalStyle(style); + return sService.setGlobalStyle(style, pkgName); } catch (RemoteException e) { Log.e(TAG, e.getLocalizedMessage(), e); } return false; } + /** + * Get the current global style. + * + * @return One of {@link #STYLE_GLOBAL_AUTO_WALLPAPER}, + * {@link #STYLE_GLOBAL_AUTO_DAYTIME}, + * {@link #STYLE_GLOBAL_LIGHT} or + * {@link #STYLE_GLOBAL_DARK} + */ + public int getGlobalStyle() { + if (sService == null) { + return STYLE_GLOBAL_AUTO_WALLPAPER; + } + try { + return sService.getGlobalStyle(); + } catch (RemoteException e) { + Log.e(TAG, e.getLocalizedMessage(), e); + } + return STYLE_GLOBAL_AUTO_WALLPAPER; + } + /** * Set color accent package. * @@ -158,6 +190,8 @@ public class StyleInterface { * to utilize this functionality. * * @param pkgName The package name of the accent + * + * @return Whether the process failed */ public boolean setAccent(String pkgName) { if (sService == null) { @@ -171,13 +205,51 @@ public class StyleInterface { return false; } + /** + * Get the current accent package. + * + * @return The current accent package name. Defaults to {#ACCENT_DEFAULT} + */ + public String getAccent() { + if (sService == null) { + return ACCENT_DEFAULT; + } + try { + return sService.getAccent(); + } catch (RemoteException e) { + Log.e(TAG, e.getLocalizedMessage(), e); + } + return ACCENT_DEFAULT; + } + + /** + * Get a list of trusted accents that are included in the build. + * + * The first element (if any) is the default accent. + * + * @see #setAccent + * + * @return A list of accents package names that can be used with {#setAccent}. + */ + public List getTrustedAccents() { + if (sService == null) { + return new ArrayList<>(); + } + try { + return sService.getTrustedAccents(); + } catch (RemoteException e) { + Log.e(TAG, e.getLocalizedMessage(), e); + } + return new ArrayList<>(); + } + /** * Get the best color that suites a bitmap object and the appropriate global style * * @param source The object you want the suggested color to be matched with * @param colors A list of colors that the selection will be made from * - * @return suggestion for global style + accent combination + * @return A {@link lineageos.style.Suggestion} which holds the best style + accent combination */ public Suggestion getSuggestion(Bitmap source, int[] colors) { if (colors.length == 0) { @@ -196,4 +268,4 @@ public class StyleInterface { } return fallback; } -} \ No newline at end of file +} diff --git a/sdk/src/java/lineageos/style/Suggestion.java b/sdk/src/java/lineageos/style/Suggestion.java index cbb379cc..314797e4 100644 --- a/sdk/src/java/lineageos/style/Suggestion.java +++ b/sdk/src/java/lineageos/style/Suggestion.java @@ -24,6 +24,10 @@ import lineageos.os.Build; import lineageos.os.Concierge; import lineageos.os.Concierge.ParcelInfo; +/** + * Style suggestion holder class. + * This is returned when calling {@link #lineageos.style.StyleInterface#getSuggestion} + */ public class Suggestion implements Parcelable { public final int globalStyle; public final int selectedAccent; @@ -31,10 +35,11 @@ public class Suggestion implements Parcelable { /** * Default constructor * - * @see lineageos.style.StyleInterface#getSuggestion + * @see {@link lineageos.style.StyleInterface#getSuggestion} * - * @param globalStyle one of {@link #STYLE_GLOBAL_LIGHT} or {@link #STYLE_GLOBAL_DARK} - * @param colorPosition position of selected color in the input array + * @param globalStyle One of {@link #lineageos.style.StyleInterface#STYLE_GLOBAL_LIGHT} or + * {@link #lineageos.style.StyleInterface#STYLE_GLOBAL_DARK} + * @param selectedAccent The position of the selected color in the input array */ public Suggestion(int globalStyle, int selectedAccent) { this.globalStyle = globalStyle;