sdk: Styles API finalization for API9

Changes:

- Added getter for global style and current accent
- Added getter for a list of (available) trusted overlays
- Apps that want to change the global style now MUST specify their
  package name when calling setGlobalStyle().
  LineageParts will expose the name of the app that's currently managing
  the global style
- Improved documentation

Change-Id: Iaa1b106f43684b4120aa0f39023ebfddcb379806
Signed-off-by: Joey <joey@lineageos.org>
This commit is contained in:
Joey
2018-03-06 17:46:14 +01:00
parent d70eb3905a
commit 5a72b57502
8 changed files with 217 additions and 19 deletions

View File

@@ -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<java.lang.String> 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

View File

@@ -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<String> getTrustedAccentsInternal() {
List<String> 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<String> 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<String> result = getTrustedAccentsInternal();
restoreCallingIdentity(token);
return result;
}
};
}

View File

@@ -116,4 +116,17 @@
<item>com.google.android.gsf|com.google.android.talk</item>
</string-array>
<!-- Trusted accents package names -->
<string-array name="trusted_accent_packages" translatable="false">
<item>org.lineageos.overlay.accent.red</item>
<item>org.lineageos.overlay.accent.pink</item>
<item>org.lineageos.overlay.accent.purple</item>
<item>org.lineageos.overlay.accent.blue</item>
<item>org.lineageos.overlay.accent.cyan</item>
<item>org.lineageos.overlay.accent.green</item>
<item>org.lineageos.overlay.accent.orange</item>
<item>org.lineageos.overlay.accent.yellow</item>
<item>org.lineageos.overlay.accent.brown</item>
<item>org.lineageos.overlay.accent.black</item>
</string-array>
</resources>

View File

@@ -149,6 +149,7 @@
<java-symbol type="string" name="megabytespersecond_short" />
<!-- Accent colors -->
<java-symbol type="array" name="trusted_accent_packages" />
<java-symbol type="string" name="accent_black" />
<java-symbol type="string" name="accent_blue" />
<java-symbol type="string" name="accent_brown" />

View File

@@ -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);

View File

@@ -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<String> getTrustedAccents();
}

View File

@@ -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<String> 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;
}
}
}

View File

@@ -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;