diff --git a/app/src/main/java/com/james/status/Status.java b/app/src/main/java/com/james/status/Status.java index c34cb12..fa7feab 100644 --- a/app/src/main/java/com/james/status/Status.java +++ b/app/src/main/java/com/james/status/Status.java @@ -82,7 +82,7 @@ public interface OnPreferenceChangedListener { } public static void showDebug(Context context, String message, int length) { - if (PreferenceData.STATUS_DEBUG.getBooleanValue(context)) + if (PreferenceData.STATUS_DEBUG.getValue(context)) Toast.makeText(context, message, length).show(); else Log.d("Status", message); } diff --git a/app/src/main/java/com/james/status/activities/AppSettingActivity.java b/app/src/main/java/com/james/status/activities/AppSettingActivity.java index 863ee10..d7c66d2 100644 --- a/app/src/main/java/com/james/status/activities/AppSettingActivity.java +++ b/app/src/main/java/com/james/status/activities/AppSettingActivity.java @@ -74,7 +74,7 @@ protected Integer run() throws InterruptedException { protected void done(@Nullable Integer result) { ColorPickerDialog dialog = new ColorPickerDialog(AppSettingActivity.this); dialog.setPreference(app.getIntegerPreference(AppSettingActivity.this, AppData.PreferenceIdentifier.COLOR)); - dialog.setDefaultPreference(result != null ? result : PreferenceData.STATUS_COLOR.getIntValue(AppSettingActivity.this)); + dialog.setDefaultPreference(result != null ? result : (int) PreferenceData.STATUS_COLOR.getValue(AppSettingActivity.this)); dialog.setListener(new PreferenceDialog.OnPreferenceListener() { @Override public void onPreference(PreferenceDialog dialog, Integer preference) { @@ -112,7 +112,7 @@ protected Integer run() throws InterruptedException { @Override protected void done(@Nullable Integer result) { - colorView.setImageDrawable(new ColorDrawable(result != null ? result : PreferenceData.STATUS_COLOR.getIntValue(AppSettingActivity.this))); + colorView.setImageDrawable(new ColorDrawable(result != null ? result : (int) PreferenceData.STATUS_COLOR.getValue(AppSettingActivity.this))); } }.execute(); } @@ -166,7 +166,7 @@ protected void done(@Nullable Integer result) { ColorPickerDialog dialog = new ColorPickerDialog(AppSettingActivity.this); dialog.setPreference(activity.getIntegerPreference(AppSettingActivity.this, AppData.PreferenceIdentifier.COLOR)); - dialog.setDefaultPreference(result != null ? result : PreferenceData.STATUS_COLOR.getIntValue(AppSettingActivity.this)); + dialog.setDefaultPreference(result != null ? result : (int) PreferenceData.STATUS_COLOR.getValue(AppSettingActivity.this)); dialog.setListener(new PreferenceDialog.OnPreferenceListener() { @Override public void onPreference(PreferenceDialog dialog, Integer preference) { diff --git a/app/src/main/java/com/james/status/activities/MainActivity.java b/app/src/main/java/com/james/status/activities/MainActivity.java index 8ce4e5f..c573d19 100644 --- a/app/src/main/java/com/james/status/activities/MainActivity.java +++ b/app/src/main/java/com/james/status/activities/MainActivity.java @@ -143,7 +143,7 @@ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { } }); - service.setChecked(PreferenceData.STATUS_ENABLED.getBooleanValue(this) && StaticUtils.isStatusServiceRunning(this)); + service.setChecked((boolean) PreferenceData.STATUS_ENABLED.getValue(this) && StaticUtils.isStatusServiceRunning(this)); service.setOnCheckedChangeListener(this); adapter = new SimplePagerAdapter(this, getSupportFragmentManager(), viewPager, new GeneralPreferenceFragment(), new IconPreferenceFragment(), new AppPreferenceFragment(), new HelpFragment()); @@ -192,7 +192,7 @@ protected void onResume() { super.onResume(); service.setOnCheckedChangeListener(null); - service.setChecked(PreferenceData.STATUS_ENABLED.getBooleanValue(this) || StaticUtils.isStatusServiceRunning(this)); + service.setChecked((boolean) PreferenceData.STATUS_ENABLED.getValue(this) || StaticUtils.isStatusServiceRunning(this)); service.setOnCheckedChangeListener(this); if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { diff --git a/app/src/main/java/com/james/status/adapters/IconAdapter.java b/app/src/main/java/com/james/status/adapters/IconAdapter.java index 144b6a9..20e6e94 100644 --- a/app/src/main/java/com/james/status/adapters/IconAdapter.java +++ b/app/src/main/java/com/james/status/adapters/IconAdapter.java @@ -11,6 +11,7 @@ import android.widget.CompoundButton; import com.james.status.R; +import com.james.status.data.PreferenceData; import com.james.status.data.icon.IconData; import com.james.status.services.StatusService; import com.james.status.utils.StaticUtils; @@ -42,7 +43,7 @@ public void onBindViewHolder(final ViewHolder holder, int position) { itemView = holder.v; - icon.putPreference(IconData.PreferenceIdentifier.POSITION, position); + PreferenceData.ICON_POSITION.setValue(activity, position, icon.getIdentifierArgs()); holder.checkBox.setText(icon.getTitle()); holder.checkBox.setOnCheckedChangeListener(null); @@ -63,7 +64,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { holder.checkBox.setChecked(false); holder.checkBox.setOnCheckedChangeListener(this); } else { - icon.putPreference(IconData.PreferenceIdentifier.VISIBILITY, isChecked); + PreferenceData.ICON_VISIBILITY.setValue(activity, isChecked, icon.getIdentifierArgs()); StaticUtils.updateStatusService(activity); notifyItemChanged(holder.getAdapterPosition()); diff --git a/app/src/main/java/com/james/status/adapters/IconStyleAdapter.java b/app/src/main/java/com/james/status/adapters/IconStyleAdapter.java index e6b6c64..3b17e28 100644 --- a/app/src/main/java/com/james/status/adapters/IconStyleAdapter.java +++ b/app/src/main/java/com/james/status/adapters/IconStyleAdapter.java @@ -11,6 +11,7 @@ import com.james.status.R; import com.james.status.data.IconStyleData; +import com.james.status.data.PreferenceData; import com.james.status.data.icon.IconData; import com.james.status.dialogs.IconCreatorDialog; import com.james.status.views.CustomImageView; @@ -77,7 +78,7 @@ public void onClick(View view) { if (styles.size() > 0) setIconStyle(styles.get(0)); else notifyDataSetChanged(); - new IconCreatorDialog(context, style, icon.getStringArrayPreference(IconData.PreferenceIdentifier.ICON_STYLE_NAMES), icon.getIconNames()).setListener(new IconCreatorDialog.OnIconStyleListener() { + new IconCreatorDialog(context, style, (String[]) PreferenceData.ICON_ICON_STYLE_NAMES.getSpecificValue(context, icon.getIdentifierArgs()), icon.getIconNames()).setListener(new IconCreatorDialog.OnIconStyleListener() { @Override public void onIconStyle(IconStyleData style) { icon.addIconStyle(style); diff --git a/app/src/main/java/com/james/status/data/AppData.java b/app/src/main/java/com/james/status/data/AppData.java index c232d3e..3f0496a 100644 --- a/app/src/main/java/com/james/status/data/AppData.java +++ b/app/src/main/java/com/james/status/data/AppData.java @@ -87,13 +87,13 @@ public int getColor(Context context) { @ColorInt public int getDefaultColor(Context context) { - if (PreferenceData.STATUS_COLOR_AUTO.getBooleanValue(context)) { + if (PreferenceData.STATUS_COLOR_AUTO.getValue(context)) { Integer color = ColorUtils.getPrimaryColor(context, getComponentName()); if (color != null) return color; } - return PreferenceData.STATUS_COLOR.getIntValue(context); + return PreferenceData.STATUS_COLOR.getValue(context); } public void setTag(String key, Object value) { @@ -262,13 +262,13 @@ public int getColor(Context context) { @ColorInt public int getDefaultColor(Context context) { - if (PreferenceData.STATUS_COLOR_AUTO.getBooleanValue(context)) { + if (PreferenceData.STATUS_COLOR_AUTO.getValue(context)) { Integer color = ColorUtils.getPrimaryColor(context, getComponentName()); if (color != null) return color; } - return PreferenceData.STATUS_COLOR.getIntValue(context); + return PreferenceData.STATUS_COLOR.getValue(context); } @Nullable diff --git a/app/src/main/java/com/james/status/data/NotificationData.java b/app/src/main/java/com/james/status/data/NotificationData.java index e52bb55..b02f6d3 100644 --- a/app/src/main/java/com/james/status/data/NotificationData.java +++ b/app/src/main/java/com/james/status/data/NotificationData.java @@ -194,7 +194,7 @@ public String getName(Context context) { } public boolean shouldShowHeadsUp(Context context) { - return PreferenceData.STATUS_NOTIFICATIONS_HEADS_UP.getBooleanValue(context) && (priority >= NotificationCompat.PRIORITY_HIGH || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) && isAlert; + return (boolean) PreferenceData.STATUS_NOTIFICATIONS_HEADS_UP.getValue(context) && (priority >= NotificationCompat.PRIORITY_HIGH || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) && isAlert; } public boolean shouldHideStatusBar() { diff --git a/app/src/main/java/com/james/status/data/PreferenceData.java b/app/src/main/java/com/james/status/data/PreferenceData.java index 41be915..e957d85 100644 --- a/app/src/main/java/com/james/status/data/PreferenceData.java +++ b/app/src/main/java/com/james/status/data/PreferenceData.java @@ -3,10 +3,11 @@ import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; +import android.graphics.Typeface; import android.os.Build; import android.os.Environment; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.widget.Toast; import com.google.gson.Gson; @@ -21,7 +22,6 @@ import java.util.Set; public enum PreferenceData { - STATUS_ENABLED(false), STATUS_NOTIFICATIONS_COMPAT(false), STATUS_NOTIFICATIONS_HEADS_UP(false), @@ -33,7 +33,7 @@ public enum PreferenceData { STATUS_TINTED_ICONS(false), STATUS_PREVENT_ICON_OVERLAP(false), STATUS_BUMP_MODE(false), - STATUS_HEADS_UP_DURATION(5), + STATUS_HEADS_UP_DURATION(10), STATUS_BACKGROUND_ANIMATIONS(true), STATUS_ICON_ANIMATIONS(true), STATUS_HEADS_UP_LAYOUT(StatusService.HEADSUP_LAYOUT_PLAIN), @@ -44,144 +44,145 @@ public enum PreferenceData { STATUS_BURNIN_PROTECTION(false), STATUS_SIDE_PADDING(0), STATUS_HEIGHT(0), - STATUS_DEBUG(BuildConfig.DEBUG); - - public static final int TYPE_BOOLEAN = 0; - public static final int TYPE_INT = 1; - public static final int TYPE_STRING = 2; - public static final int TYPE_UNKNOWN = -1; + STATUS_DEBUG(BuildConfig.DEBUG), + ICON_VISIBILITY("%1$s/VISIBILITY", true), + ICON_POSITION("%1$s/POSITION", 0), + ICON_GRAVITY("%1$s/GRAVITY", 0), + ICON_TEXT_VISIBILITY("%1$s/TEXT_VISIBILITY", false), + ICON_TEXT_FORMAT("%1$s/TEXT_FORMAT", "h:mm a"), + ICON_TEXT_SIZE("%1$s/TEXT_SIZE", 14), + ICON_TEXT_COLOR("%1$s/TEXT_COLOR", Color.WHITE), + ICON_TEXT_TYPEFACE("%1$s/TEXT_TYPEFACE", ""), + ICON_TEXT_EFFECT("%1$s/TEXT_EFFECT", Typeface.BOLD), + ICON_ICON_VISIBILITY("%1$s/ICON_VISIBILITY", true), + ICON_ICON_STYLE("%1$s/ICON_STYLE", ""), + ICON_ICON_STYLE_NAMES("%1$s/ICON_STYLE_NAMES", new String[]{}), + ICON_ICON_PADDING("%1$s/ICON_PADDING", 2), + ICON_ICON_SCALE("%1$s/ICON_SCALE", 24); private String name; + private Object defaultValue; - private Boolean defaultBooleanValue; - private Integer defaultIntValue; - private String defaultStringValue; - - PreferenceData(boolean value) { - name = name(); - defaultBooleanValue = value; - } - - PreferenceData(int value) { + PreferenceData(Object value) { name = name(); - defaultIntValue = value; + defaultValue = value; } - PreferenceData(String value) { - name = name(); - defaultStringValue = value; - } - - PreferenceData(String name, boolean value) { + PreferenceData(String name, Object value) { this.name = name; - defaultBooleanValue = value; + defaultValue = value; } - PreferenceData(String name, int value) { - this.name = name; - defaultIntValue = value; + public String getName(@Nullable String... args) { + if (args != null && args.length > 0) + return String.format(name, (Object[]) args); + else return name; } - PreferenceData(String name, String value) { - this.name = name; - defaultStringValue = value; - } - - public int getType() { - if (defaultBooleanValue != null) - return TYPE_BOOLEAN; - else if (defaultIntValue != null) - return TYPE_INT; - else if (defaultStringValue != null) - return TYPE_STRING; - else return TYPE_UNKNOWN; - } - - public boolean getDefaultBoolean() { - if (getType() == TYPE_BOOLEAN) - return defaultBooleanValue; - else throw new TypeMismatchException(this, TYPE_BOOLEAN); - } - - public int getDefaultInt() { - if (getType() == TYPE_INT) - return defaultIntValue; - else throw new TypeMismatchException(this, TYPE_INT); - } - - public String getDefaultString() { - if (getType() == TYPE_STRING) - return defaultStringValue; - else throw new TypeMismatchException(this, TYPE_STRING); + public T getDefaultValue() { + try { + return (T) defaultValue; + } catch (ClassCastException e) { + throw new TypeMismatchException(this); + } } - public boolean getBooleanValue(Context context) { - return getBooleanValue(context, getDefaultBoolean()); + public T getValue(Context context) { + return getSpecificOverriddenValue(context, (T) getDefaultValue(), (String[]) null); } - public int getIntValue(Context context) { - return getIntValue(context, getDefaultInt()); + public T getValue(Context context, @Nullable T defaultValue) { + return getSpecificOverriddenValue(context, defaultValue, (String[]) null); } - public String getStringValue(Context context) { - return getStringValue(context, getDefaultString()); + public T getSpecificValue(Context context, @Nullable String... args) { + return getSpecificOverriddenValue(context, (T) getDefaultValue(), args); } - public boolean getBooleanValue(Context context, boolean defaultValue) { + public T getSpecificOverriddenValue(Context context, @Nullable T defaultValue, @Nullable String... args) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String name = getName(args); + T type = defaultValue != null ? defaultValue : (T) getDefaultValue(); - if (prefs.contains(name)) { + if (type instanceof Object[] && prefs.contains(name + "-length")) { try { - return prefs.getBoolean(name, defaultValue); - } catch (ClassCastException ignored) { + int length = prefs.getInt(name + "-length", 0); + + Object[] array; + if (type instanceof Boolean[]) + array = new Boolean[length]; + else if (type instanceof Integer[]) + array = new Integer[length]; + else if (type instanceof String[]) + array = new String[length]; + else throw new TypeMismatchException(this); + + for (int i = 0; i < array.length; i++) { + if (array instanceof Boolean[]) + array[i] = prefs.contains(name + "-" + i) ? prefs.getBoolean(name + "-" + i, false) : null; + else if (array instanceof Integer[]) + array[i] = prefs.contains(name + "-" + i) ? prefs.getInt(name + "-" + i, 0) : null; + else if (array instanceof String[]) + array[i] = prefs.getString(name + "-" + i, ""); + else throw new TypeMismatchException(this); + } + + return (T) array; + } catch (ClassCastException e) { + throw new TypeMismatchException(this, type.getClass()); } - } - - return defaultValue; - } - - public int getIntValue(Context context, int defaultValue) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - if (prefs.contains(name)) { + } else if (prefs.contains(name)) { try { - return prefs.getInt(name, defaultValue); - } catch (ClassCastException ignored) { + if (type instanceof Boolean) + return (T) new Boolean(prefs.getBoolean(name, (Boolean) defaultValue)); + else if (type instanceof Integer) + return (T) new Integer(prefs.getInt(name, (Integer) defaultValue)); + else if (type instanceof String) + return (T) prefs.getString(name, (String) defaultValue); + } catch (ClassCastException e) { + throw new TypeMismatchException(this, type.getClass()); } } return defaultValue; } - public String getStringValue(Context context, @NonNull String defaultValue) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - if (prefs.contains(name)) { - try { - return prefs.getString(name, defaultValue); - } catch (ClassCastException ignored) { - } - } - - return defaultValue; + public void setValue(Context context, @Nullable T value) { + setValue(context, value, (String[]) null); } - public void setValue(Context context, boolean value) { - if (getType() == TYPE_BOOLEAN) - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(name, value).apply(); - else throw new TypeMismatchException(this, TYPE_BOOLEAN); - } + public void setValue(Context context, @Nullable T value, @Nullable String... args) { + SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); + String name = getName(args); + + if (value == null) + editor.remove(name + (defaultValue instanceof Object[] ? "-length" : "")); + else if (value instanceof Object[]) { + Object[] array = (Object[]) value; + + for (int i = 0; i < array.length; i++) { + Object item = array[i]; + if (item instanceof Boolean) + editor.putBoolean(name + "-" + i, (boolean) item); + else if (item instanceof Integer) + editor.putInt(name + "-" + i, (int) item); + else if (item instanceof String) + editor.putString(name + "-" + i, (String) item); + else throw new TypeMismatchException(this); + } - public void setValue(Context context, int value) { - if (getType() == TYPE_INT) - PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(name, value).apply(); - else throw new TypeMismatchException(this, TYPE_INT); - } + editor.putInt(name + "-length", array.length); + } else { + if (value instanceof Boolean) + editor.putBoolean(name, (Boolean) value); + else if (value instanceof Integer) + editor.putInt(name, (Integer) value); + else if (value instanceof String) + editor.putString(name, (String) value); + else throw new TypeMismatchException(this); + } - public void setValue(Context context, String value) { - if (getType() == TYPE_STRING) - PreferenceManager.getDefaultSharedPreferences(context).edit().putString(name, value).apply(); - else throw new TypeMismatchException(this, TYPE_STRING); + editor.apply(); } public static boolean toFile(Context context, File file) { @@ -259,22 +260,16 @@ public static String getBackupsDir() { public static class TypeMismatchException extends RuntimeException { - public TypeMismatchException(PreferenceData data, int expectedType) { - super("Wrong type used for \"" + data.name() + "\": expected " + getTypeString(data.getType()) + ", got " + getTypeString(expectedType)); + public TypeMismatchException(PreferenceData data) { + this(data, null); } - private static String getTypeString(int type) { - switch (type) { - case TYPE_BOOLEAN: - return "boolean"; - case TYPE_INT: - return "int"; - case TYPE_STRING: - return "string"; - default: - return "unknown"; - } + public TypeMismatchException(PreferenceData data, Class expectedType) { + super("Wrong type used for \"" + data.name() + "\"" + + (data.defaultValue != null ? ": expected " + data.defaultValue.getClass().getName() + + (expectedType != null ? ", got " + expectedType.getName() : "") : "")); } } + } diff --git a/app/src/main/java/com/james/status/data/icon/BatteryIconData.java b/app/src/main/java/com/james/status/data/icon/BatteryIconData.java index c4615f7..02c7e60 100644 --- a/app/src/main/java/com/james/status/data/icon/BatteryIconData.java +++ b/app/src/main/java/com/james/status/data/icon/BatteryIconData.java @@ -27,12 +27,6 @@ public boolean canHazText() { return true; } - @Override - public boolean hasText() { - Boolean isBatteryPercent = getBooleanPreference(PreferenceIdentifier.TEXT_VISIBILITY); - return isBatteryPercent != null && isBatteryPercent; - } - @Override public IntentFilter getIntentFilter() { return new IntentFilter(Intent.ACTION_BATTERY_CHANGED); diff --git a/app/src/main/java/com/james/status/data/icon/IconData.java b/app/src/main/java/com/james/status/data/icon/IconData.java index a048dc6..4194d0c 100644 --- a/app/src/main/java/com/james/status/data/icon/IconData.java +++ b/app/src/main/java/com/james/status/data/icon/IconData.java @@ -3,7 +3,6 @@ import android.content.Context; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.content.res.Resources; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; @@ -55,11 +54,11 @@ public abstract class IconData { public IconData(Context context) { this.context = context; - color = PreferenceData.STATUS_COLOR.getIntValue(context); + color = PreferenceData.STATUS_COLOR.getValue(context); - String name = getStringPreference(PreferenceIdentifier.ICON_STYLE); List styles = getIconStyles(); if (styles.size() > 0) { + String name = PreferenceData.ICON_ICON_STYLE.getSpecificOverriddenValue(context, styles.get(0).name, getIdentifierArgs()); if (name != null) { for (IconStyleData style : styles) { if (style.name.equals(name)) { @@ -153,7 +152,7 @@ public final void onTextUpdate(@Nullable String text) { Integer color = getTextColor(); - if (color != null && !(PreferenceData.STATUS_DARK_ICONS.getBooleanValue(getContext()) && (color == Color.WHITE || color == Color.BLACK))) { + if (color != null && !((boolean) PreferenceData.STATUS_DARK_ICONS.getValue(getContext()) && (color == Color.WHITE || color == Color.BLACK))) { textView.setTextColor(color); textView.setTag(color); } else textView.setTag(null); @@ -173,8 +172,7 @@ public final void onTextUpdate(@Nullable String text) { } public final boolean isVisible() { - Boolean isVisible = getBooleanPreference(PreferenceIdentifier.VISIBILITY); - return (isDefaultVisible() ? (isVisible == null || isVisible) : (isVisible != null && isVisible)) && StaticUtils.isPermissionsGranted(getContext(), getPermissions()); + return PreferenceData.ICON_VISIBILITY.getSpecificOverriddenValue(getContext(), isDefaultVisible(), getIdentifierArgs()) && StaticUtils.isPermissionsGranted(getContext(), getPermissions()); } boolean isDefaultVisible() { @@ -187,8 +185,7 @@ public boolean canHazDrawable() { } public boolean hasDrawable() { - Boolean hasDrawable = getBooleanPreference(PreferenceIdentifier.ICON_VISIBILITY); - return canHazDrawable() && (hasDrawable == null || hasDrawable) && style != null; + return canHazDrawable() && PreferenceData.ICON_ICON_VISIBILITY.getSpecificOverriddenValue(getContext(), true, getIdentifierArgs()) && style != null; } public boolean canHazText() { @@ -197,8 +194,7 @@ public boolean canHazText() { } public boolean hasText() { - Boolean hasText = getBooleanPreference(PreferenceIdentifier.TEXT_VISIBILITY); - return canHazText() && (hasText != null && hasText); + return canHazText() && PreferenceData.ICON_TEXT_VISIBILITY.getSpecificOverriddenValue(getContext(), !canHazDrawable(), getIdentifierArgs()); } public String[] getPermissions() { @@ -224,38 +220,29 @@ public void unregister() { } public final int getIconPadding() { - Integer padding = getIntegerPreference(PreferenceIdentifier.ICON_PADDING); - if (padding == null) padding = 2; - return padding; + return PreferenceData.ICON_ICON_PADDING.getSpecificValue(getContext(), getIdentifierArgs()); } public final int getIconScale() { - Integer scale = getIntegerPreference(PreferenceIdentifier.ICON_SCALE); - if (scale == null) scale = 24; - return scale; + return PreferenceData.ICON_ICON_SCALE.getSpecificValue(getContext(), getIdentifierArgs()); } public final float getTextSize() { - Integer size = getIntegerPreference(PreferenceIdentifier.TEXT_SIZE); - if (size == null) size = 14; - return size; + return (float) (int) PreferenceData.ICON_TEXT_SIZE.getSpecificValue(getContext(), getIdentifierArgs()); } - @Nullable @ColorInt public final Integer getTextColor() { - return getIntegerPreference(PreferenceIdentifier.TEXT_COLOR); + return PreferenceData.ICON_TEXT_COLOR.getSpecificValue(getContext(), getIdentifierArgs()); } public final Integer getTextEffect() { - Integer textEffect = getIntegerPreference(PreferenceIdentifier.TEXT_EFFECT); - if (textEffect == null) textEffect = Typeface.BOLD; - return textEffect; + return PreferenceData.ICON_TEXT_EFFECT.getSpecificValue(getContext(), getIdentifierArgs()); } @Nullable public String getTypefaceName() { - return getStringPreference(PreferenceIdentifier.TEXT_TYPEFACE); + return PreferenceData.ICON_TEXT_TYPEFACE.getSpecificOverriddenValue(getContext(), null, getIdentifierArgs()); } @Nullable @@ -274,9 +261,7 @@ public Typeface getTypeface() { } public final int getPosition() { - Integer position = getIntegerPreference(PreferenceIdentifier.POSITION); - if (position == null) position = 0; - return position; + return PreferenceData.ICON_POSITION.getSpecificValue(getContext(), getIdentifierArgs()); } public int getDefaultGravity() { @@ -284,9 +269,7 @@ public int getDefaultGravity() { } public final int getGravity() { - Integer gravity = getIntegerPreference(PreferenceIdentifier.GRAVITY); - if (gravity == null) gravity = getDefaultGravity(); - return gravity; + return PreferenceData.ICON_GRAVITY.getSpecificOverriddenValue(getContext(), getDefaultGravity(), getIdentifierArgs()); } @Nullable @@ -336,14 +319,14 @@ public List getPreferences() { if (canHazDrawable() && (hasText() || !hasDrawable())) { preferences.add(new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_show_drawable) + new BasePreferenceData.Identifier( + PreferenceData.ICON_ICON_VISIBILITY, + getContext().getString(R.string.preference_show_drawable), + getIdentifierArgs() ), - hasDrawable(), new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Boolean preference) { - putPreference(PreferenceIdentifier.ICON_VISIBILITY, preference); StaticUtils.updateStatusService(getContext()); } } @@ -353,14 +336,14 @@ public void onPreferenceChange(Boolean preference) { if (canHazText() && (hasDrawable() || !hasText())) { preferences.add(new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_show_text) + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_VISIBILITY, + getContext().getString(R.string.preference_show_text), + getIdentifierArgs() ), - hasText(), new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Boolean preference) { - putPreference(PreferenceIdentifier.TEXT_VISIBILITY, preference); StaticUtils.updateStatusService(getContext()); } } @@ -370,17 +353,18 @@ public void onPreferenceChange(Boolean preference) { preferences.addAll(Arrays.asList( new ListPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_gravity) + new BasePreferenceData.Identifier<>( + PreferenceData.ICON_GRAVITY, + getContext().getString(R.string.preference_gravity), + getDefaultGravity(), + getIdentifierArgs() ), new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.GRAVITY, preference); StaticUtils.updateStatusService(getContext()); } }, - getGravity(), new ListPreferenceData.ListPreference( getContext().getString(R.string.gravity_left), LEFT_GRAVITY @@ -396,17 +380,17 @@ public void onPreferenceChange(Integer preference) { ), new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_icon_padding) + new BasePreferenceData.Identifier( + PreferenceData.ICON_ICON_PADDING, + getContext().getString(R.string.preference_icon_padding), + getIdentifierArgs() ), - getIconPadding(), getContext().getString(R.string.unit_dp), null, null, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.ICON_PADDING, preference); StaticUtils.updateStatusService(getContext()); } } @@ -416,17 +400,16 @@ public void onPreferenceChange(Integer preference) { if (hasDrawable()) { preferences.add(new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( + PreferenceData.ICON_ICON_SCALE, getContext().getString(R.string.preference_icon_scale) ), - getIconScale(), getContext().getString(R.string.unit_dp), 0, null, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.ICON_SCALE, preference); StaticUtils.updateStatusService(getContext()); } } @@ -436,33 +419,31 @@ public void onPreferenceChange(Integer preference) { if (hasText()) { preferences.add(new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_SIZE, getContext().getString(R.string.preference_text_size) ), - (int) getTextSize(), getContext().getString(R.string.unit_sp), 0, null, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.TEXT_SIZE, preference); StaticUtils.updateStatusService(getContext()); } } )); - Integer color = getTextColor(); preferences.add(new ColorPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_text_color) + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_COLOR, + getContext().getString(R.string.preference_text_color), + getIdentifierArgs() ), - color != null ? color : Color.WHITE, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.TEXT_COLOR, preference); StaticUtils.updateStatusService(getContext()); } } @@ -470,15 +451,17 @@ public void onPreferenceChange(Integer preference) { preferences.add(new FontPreferenceData( getContext(), - new BasePreferenceData.Identifier("Text Font"), + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_TYPEFACE, + getContext().getString(R.string.preference_text_font), + getIdentifierArgs() + ), new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(String preference) { - putPreference(PreferenceIdentifier.TEXT_TYPEFACE, preference); StaticUtils.updateStatusService(getContext()); } }, - getTypefaceName(), "Audiowide.ttf", "BlackOpsOne.ttf", "HennyPenny.ttf", @@ -499,36 +482,37 @@ public void onPreferenceChange(String preference) { preferences.add(new ListPreferenceData( getContext(), - new BasePreferenceData.Identifier(getContext().getString(R.string.preference_text_effect)), + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_EFFECT, + getContext().getString(R.string.preference_text_effect), + getIdentifierArgs() + ), new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.TEXT_EFFECT, preference); StaticUtils.updateStatusService(getContext()); } }, - getTextEffect(), new ListPreferenceData.ListPreference(getContext().getString(R.string.text_effect_none), Typeface.NORMAL), new ListPreferenceData.ListPreference(getContext().getString(R.string.text_effect_bold), Typeface.BOLD), new ListPreferenceData.ListPreference(getContext().getString(R.string.text_effect_italic), Typeface.ITALIC), new ListPreferenceData.ListPreference(getContext().getString(R.string.text_effect_bold_italic), Typeface.BOLD_ITALIC) - )); } if (hasDrawable()) { preferences.add(new IconPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_icon_style) + new BasePreferenceData.Identifier( + PreferenceData.ICON_ICON_STYLE, + getContext().getString(R.string.preference_icon_style), + getIdentifierArgs() ), - style, this, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(IconStyleData preference) { style = preference; - putPreference(PreferenceIdentifier.ICON_STYLE, preference.name); StaticUtils.updateStatusService(getContext()); } } @@ -550,12 +534,10 @@ public List getIconStyles() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); List styles = new ArrayList<>(); - String[] names = getStringArrayPreference(PreferenceIdentifier.ICON_STYLE_NAMES); - if (names != null) { - for (String name : names) { - IconStyleData style = IconStyleData.fromSharedPreferences(prefs, getClass().getName(), name); - if (style != null) styles.add(style); - } + String[] names = PreferenceData.ICON_ICON_STYLE_NAMES.getSpecificValue(getContext(), getIdentifierArgs()); + for (String name : names) { + IconStyleData style = IconStyleData.fromSharedPreferences(prefs, getClass().getName(), name); + if (style != null) styles.add(style); } return styles; @@ -563,132 +545,26 @@ public List getIconStyles() { public final void addIconStyle(IconStyleData style) { if (style.getSize() == getIconStyleSize()) { - String[] names = getStringArrayPreference(PreferenceIdentifier.ICON_STYLE_NAMES); - List list = new ArrayList<>(); - if (names != null) list.addAll(Arrays.asList(names)); + List list = new ArrayList<>(Arrays.asList((String[]) PreferenceData.ICON_ICON_STYLE_NAMES.getSpecificValue(getContext(), getIdentifierArgs()))); SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); style.writeToSharedPreferences(editor, getClass().getName()); editor.apply(); list.add(style.name); - putPreference(PreferenceIdentifier.ICON_STYLE_NAMES, list.toArray(new String[list.size()])); + PreferenceData.ICON_ICON_STYLE_NAMES.setValue(context, list.toArray(new String[list.size()]), getIdentifierArgs()); } } public final void removeIconStyle(IconStyleData style) { - String[] names = getStringArrayPreference(PreferenceIdentifier.ICON_STYLE_NAMES); - List list = new ArrayList<>(); - if (names != null) list.addAll(Arrays.asList(names)); + List list = new ArrayList<>(Arrays.asList((String[]) PreferenceData.ICON_ICON_STYLE_NAMES.getSpecificValue(getContext(), getIdentifierArgs()))); list.remove(style.name); - putPreference(PreferenceIdentifier.ICON_STYLE_NAMES, list.toArray(new String[list.size()])); - } - - @Nullable - public final Boolean getBooleanPreference(PreferenceIdentifier identifier) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.contains(getIdentifierString(identifier))) { - try { - return prefs.getBoolean(getIdentifierString(identifier), false); - } catch (ClassCastException e) { - return null; - } - } else - return null; - } - - @Nullable - public final Integer getIntegerPreference(PreferenceIdentifier identifier) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.contains(getIdentifierString(identifier))) { - try { - return prefs.getInt(getIdentifierString(identifier), 0); - } catch (ClassCastException e) { - return null; - } - } else - return null; - } - - @Nullable - public final String getStringPreference(PreferenceIdentifier identifier) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.contains(getIdentifierString(identifier))) { - try { - return prefs.getString(getIdentifierString(identifier), null); - } catch (ClassCastException e) { - return null; - } - } else - return null; - } - - public final String[] getStringArrayPreference(PreferenceIdentifier identifier) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.contains(getIdentifierString(identifier) + "-length")) { - String[] array = new String[prefs.getInt(getIdentifierString(identifier) + "-length", 0)]; - for (int i = 0; i < array.length; i++) { - array[i] = prefs.getString(getIdentifierString(identifier) + "-" + i, null); - } - - return array; - } else return null; - } - - public final void putPreference(PreferenceIdentifier identifier, boolean object) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(getIdentifierString(identifier), object).apply(); - } - - public final void putPreference(PreferenceIdentifier identifier, int object) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(getIdentifierString(identifier), object).apply(); - } - - public final void putPreference(PreferenceIdentifier identifier, String object) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putString(getIdentifierString(identifier), object).apply(); - } - - public final void putPreference(PreferenceIdentifier identifier, int[] object) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - Resources resources = context.getResources(); - - prefs.edit().putInt(getIdentifierString(identifier) + "-length", object.length).apply(); - - for (int i = 0; i < object.length; i++) { - prefs.edit().putString(getIdentifierString(identifier) + "-" + i, resources.getResourceEntryName(object[i])).apply(); - } - } - - public final void putPreference(PreferenceIdentifier identifier, String[] object) { - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); - - editor.putInt(getIdentifierString(identifier) + "-length", object.length); - for (int i = 0; i < object.length; i++) { - editor.putString(getIdentifierString(identifier) + "-" + i, object[i]); - } - - editor.apply(); - } - - private String getIdentifierString(PreferenceIdentifier identifier) { - return getClass().getName() + "/" + identifier.toString(); + PreferenceData.ICON_ICON_STYLE_NAMES.setValue(context, list.toArray(new String[list.size()]), getIdentifierArgs()); } - public enum PreferenceIdentifier { - VISIBILITY, - POSITION, - GRAVITY, - TEXT_VISIBILITY, - TEXT_FORMAT, - TEXT_SIZE, - TEXT_COLOR, - TEXT_TYPEFACE, - TEXT_EFFECT, - ICON_VISIBILITY, - ICON_STYLE, - ICON_STYLE_NAMES, - ICON_PADDING, - ICON_SCALE + public String[] getIdentifierArgs() { + return new String[]{getClass().getName()}; } public interface DrawableListener { diff --git a/app/src/main/java/com/james/status/data/icon/NotificationsIconData.java b/app/src/main/java/com/james/status/data/icon/NotificationsIconData.java index 8a03a92..6dc4792 100644 --- a/app/src/main/java/com/james/status/data/icon/NotificationsIconData.java +++ b/app/src/main/java/com/james/status/data/icon/NotificationsIconData.java @@ -87,17 +87,17 @@ public List getPreferences() { preferences.add(new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_icon_scale) + new BasePreferenceData.Identifier( + PreferenceData.ICON_ICON_SCALE, + getContext().getString(R.string.preference_icon_scale), + getIdentifierArgs() ), - getIconScale(), getContext().getString(R.string.unit_dp), 0, null, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(Integer preference) { - putPreference(PreferenceIdentifier.ICON_SCALE, preference); StaticUtils.updateStatusService(getContext()); } } @@ -115,7 +115,7 @@ public void register() { notificationLayout.removeAllViewsInLayout(); notifications.clear(); - notificationLayout.setLayoutTransition(PreferenceData.STATUS_ICON_ANIMATIONS.getBooleanValue(getContext()) ? new LayoutTransition() : null); + notificationLayout.setLayoutTransition(PreferenceData.STATUS_ICON_ANIMATIONS.getValue(getContext()) ? new LayoutTransition() : null); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { Intent intent = new Intent(NotificationService.ACTION_GET_NOTIFICATIONS); @@ -205,7 +205,7 @@ static class NotificationReceiver extends IconUpdateReceiver { public TimeIconData(Context context) { super(context); - calendar = Calendar.getInstance(); - - format = getStringPreference(PreferenceIdentifier.TEXT_FORMAT); - if (format == null) format = "h:mm a"; + format = PreferenceData.ICON_TEXT_FORMAT.getValue(context, DateFormat.is24HourFormat(context) ? "HH:mm" : "h:mm a"); } @Override @@ -85,15 +83,15 @@ public List getPreferences() { preferences.add(new FormatPreferenceData( getContext(), - new BasePreferenceData.Identifier( - getContext().getString(R.string.preference_time_format) + new BasePreferenceData.Identifier( + PreferenceData.ICON_TEXT_FORMAT, + getContext().getString(R.string.preference_time_format), + getIdentifierArgs() ), - format, new BasePreferenceData.OnPreferenceChangeListener() { @Override public void onPreferenceChange(String preference) { format = preference; - putPreference(PreferenceIdentifier.TEXT_FORMAT, preference); StaticUtils.updateStatusService(getContext()); } } diff --git a/app/src/main/java/com/james/status/data/preference/BasePreferenceData.java b/app/src/main/java/com/james/status/data/preference/BasePreferenceData.java index 9227ae2..367412f 100644 --- a/app/src/main/java/com/james/status/data/preference/BasePreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/BasePreferenceData.java @@ -1,7 +1,6 @@ package com.james.status.data.preference; import android.content.Context; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v7.widget.RecyclerView; @@ -16,16 +15,16 @@ public class BasePreferenceData implements View.OnClickListener { private final Context context; - private final Identifier identifier; + private final Identifier identifier; private final OnPreferenceChangeListener listener; - public BasePreferenceData(Context context, Identifier identifier) { + public BasePreferenceData(Context context, Identifier identifier) { this.context = context; this.identifier = identifier; listener = null; } - public BasePreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener) { + public BasePreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener) { this.context = context; this.identifier = identifier; this.listener = listener; @@ -35,7 +34,7 @@ public Context getContext() { return context; } - public Identifier getIdentifier() { + public Identifier getIdentifier() { return identifier; } @@ -83,47 +82,70 @@ public ViewHolder(View v) { } } - public static class Identifier { + public static class Identifier { @Nullable private String title, subtitle; - private PreferenceData identifier; + private PreferenceData preference; + private String[] args; private SectionIdentifier sectionIdentifier; + private T defaultValue; - public Identifier(@Nullable String title) { - this.title = title; + public Identifier(PreferenceData preference, @Nullable String title) { + this(preference, title, null, null, null, (String[]) null); + } + + public Identifier(PreferenceData preference, @Nullable String title, String... args) { + this(preference, title, null, null, null, args); } - public Identifier(@Nullable String title, @Nullable String subtitle, SectionIdentifier sectionIdentifier) { - this(null, title, subtitle, sectionIdentifier); + public Identifier(PreferenceData preference, @Nullable String title, T defaultValue, String... args) { + this(preference, title, null, null, defaultValue, args); } - public Identifier(PreferenceData identifier, @Nullable String title, SectionIdentifier sectionIdentifier) { - this(identifier, title, null, sectionIdentifier); + public Identifier(PreferenceData preference, @Nullable String title, SectionIdentifier sectionIdentifier) { + this(preference, title, null, sectionIdentifier, null, (String[]) null); } - public Identifier(PreferenceData identifier, @Nullable String title, @Nullable String subtitle, SectionIdentifier sectionIdentifier) { - this.identifier = identifier; + public Identifier(PreferenceData preference, @Nullable String title, @Nullable String subtitle, SectionIdentifier sectionIdentifier) { + this(preference, title, subtitle, sectionIdentifier, null, (String[]) null); + } + + public Identifier(PreferenceData preference, @Nullable String title, @Nullable String subtitle, SectionIdentifier sectionIdentifier, T defaultValue, @Nullable String... args) { + this.preference = preference; this.title = title; this.subtitle = subtitle; this.sectionIdentifier = sectionIdentifier; + this.defaultValue = defaultValue; + this.args = args; } - @NonNull public String getTitle() { if (title != null) return title; else return ""; } - @NonNull public String getSubtitle() { if (subtitle != null) return subtitle; else return ""; } - @Nullable public PreferenceData getPreference() { - return identifier; + return preference; + } + + public T getPreferenceValue(Context context) { + if (defaultValue != null) { + return getPreferenceValue(context, defaultValue); + } else return preference.getSpecificValue(context, args); + } + + public T getPreferenceValue(Context context, T defaultValue) { + return preference.getSpecificOverriddenValue(context, defaultValue, args); + } + + public void setPreferenceValue(Context context, T value) { + preference.setValue(context, value, args); } @Nullable diff --git a/app/src/main/java/com/james/status/data/preference/BooleanPreferenceData.java b/app/src/main/java/com/james/status/data/preference/BooleanPreferenceData.java index ada97e2..767b78a 100644 --- a/app/src/main/java/com/james/status/data/preference/BooleanPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/BooleanPreferenceData.java @@ -7,17 +7,14 @@ import android.widget.CompoundButton; import com.james.status.R; -import com.james.status.data.PreferenceData; public class BooleanPreferenceData extends BasePreferenceData { public boolean value; - public BooleanPreferenceData(Context context, Identifier identifier, boolean defaultValue, OnPreferenceChangeListener listener) { + public BooleanPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener) { super(context, identifier, listener); - - com.james.status.data.PreferenceData preference = identifier.getPreference(); - value = preference != null ? preference.getBooleanValue(context, defaultValue) : defaultValue; + value = identifier.getPreferenceValue(context); } @Override @@ -38,10 +35,7 @@ public void onBindViewHolder(ViewHolder holder, int position) { public void onCheckedChanged(CompoundButton compoundButton, boolean b) { value = b; - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), b); - + getIdentifier().setPreferenceValue(getContext(), b); onPreferenceChange(b); } }); diff --git a/app/src/main/java/com/james/status/data/preference/ColorPreferenceData.java b/app/src/main/java/com/james/status/data/preference/ColorPreferenceData.java index 8958ee4..b9b0809 100644 --- a/app/src/main/java/com/james/status/data/preference/ColorPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/ColorPreferenceData.java @@ -2,28 +2,23 @@ import android.app.Dialog; import android.content.Context; -import android.support.annotation.ColorInt; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.james.status.R; -import com.james.status.data.PreferenceData; import com.james.status.dialogs.ColorPickerDialog; import com.james.status.dialogs.PreferenceDialog; import com.james.status.views.ColorImageView; public class ColorPreferenceData extends BasePreferenceData { - private int defaultValue, value; + private int value; - public ColorPreferenceData(Context context, Identifier identifier, @ColorInt int defaultValue, OnPreferenceChangeListener listener) { + public ColorPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener) { super(context, identifier, listener); - this.defaultValue = defaultValue; - - com.james.status.data.PreferenceData preference = identifier.getPreference(); - value = preference != null ? preference.getIntValue(context, defaultValue) : defaultValue; + value = identifier.getPreferenceValue(context); } @Override @@ -39,16 +34,13 @@ public void onBindViewHolder(final ViewHolder holder, int position) { @Override public void onClick(final View v) { - Dialog dialog = new ColorPickerDialog(getContext()).setPreference(value).setDefaultPreference(defaultValue).setListener(new PreferenceDialog.OnPreferenceListener() { + Dialog dialog = new ColorPickerDialog(getContext()).setPreference(value).setDefaultPreference((int) getIdentifier().getPreference().getDefaultValue()).setListener(new PreferenceDialog.OnPreferenceListener() { @Override public void onPreference(PreferenceDialog dialog, Integer color) { value = color; ((ColorImageView) v.findViewById(R.id.color)).setColor(color); - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), color); - + getIdentifier().setPreferenceValue(getContext(), color); onPreferenceChange(color); } diff --git a/app/src/main/java/com/james/status/data/preference/FontPreferenceData.java b/app/src/main/java/com/james/status/data/preference/FontPreferenceData.java index b4dc717..962086c 100644 --- a/app/src/main/java/com/james/status/data/preference/FontPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/FontPreferenceData.java @@ -12,7 +12,6 @@ import android.widget.ScrollView; import com.james.status.R; -import com.james.status.data.PreferenceData; import com.james.status.utils.StaticUtils; import java.util.ArrayList; @@ -26,12 +25,11 @@ public class FontPreferenceData extends BasePreferenceData { private String selectedPreference; - public FontPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener, String defaultItem, String... items) { + public FontPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener, String... items) { super(context, identifier, listener); - this.items = new ArrayList<>(); - this.items.addAll(Arrays.asList(items)); - preference = defaultItem; + this.items = new ArrayList<>(Arrays.asList(items)); + preference = getIdentifier().getPreferenceValue(context, ""); } @Override @@ -50,7 +48,7 @@ public void onClick(View v) { AppCompatRadioButton normalButton = (AppCompatRadioButton) LayoutInflater.from(getContext()).inflate(R.layout.item_dialog_radio_button, group, false); normalButton.setId(0); normalButton.setText(R.string.font_default); - normalButton.setChecked(preference == null); + normalButton.setChecked(preference.length() == 0); group.addView(normalButton); for (int i = 0; i < items.size(); i++) { @@ -91,10 +89,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { public void onClick(DialogInterface dialog, int which) { FontPreferenceData.this.preference = selectedPreference; - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), selectedPreference); - + getIdentifier().setPreferenceValue(getContext(), selectedPreference); onPreferenceChange(selectedPreference); selectedPreference = null; } diff --git a/app/src/main/java/com/james/status/data/preference/FormatPreferenceData.java b/app/src/main/java/com/james/status/data/preference/FormatPreferenceData.java index 5ee0421..272e30c 100644 --- a/app/src/main/java/com/james/status/data/preference/FormatPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/FormatPreferenceData.java @@ -4,7 +4,6 @@ import android.content.Context; import android.view.View; -import com.james.status.data.PreferenceData; import com.james.status.dialogs.FormatDialog; import com.james.status.dialogs.PreferenceDialog; @@ -12,11 +11,9 @@ public class FormatPreferenceData extends BasePreferenceData { private String value; - public FormatPreferenceData(Context context, Identifier identifier, String defaultValue, OnPreferenceChangeListener listener) { + public FormatPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener) { super(context, identifier, listener); - - com.james.status.data.PreferenceData preference = identifier.getPreference(); - this.value = preference != null ? preference.getStringValue(context, defaultValue) : defaultValue; + value = identifier.getPreferenceValue(context); } @Override @@ -26,10 +23,7 @@ public void onClick(View v) { public void onPreference(PreferenceDialog dialog, String format) { value = format; - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), format); - + getIdentifier().setPreferenceValue(getContext(), format); onPreferenceChange(format); } diff --git a/app/src/main/java/com/james/status/data/preference/IconPreferenceData.java b/app/src/main/java/com/james/status/data/preference/IconPreferenceData.java index 5026554..7d8bf79 100644 --- a/app/src/main/java/com/james/status/data/preference/IconPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/IconPreferenceData.java @@ -13,17 +13,28 @@ import com.james.status.dialogs.PreferenceDialog; import com.james.status.views.IconStyleImageView; -public class IconPreferenceData extends BasePreferenceData { +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class IconPreferenceData extends BasePreferenceData { private IconStyleData iconStyle; private IconData iconData; + private Map styles; + private OnPreferenceChangeListener listener; - public IconPreferenceData(Context context, Identifier identifier, IconStyleData iconStyle, IconData iconData, OnPreferenceChangeListener listener) { - super(context, identifier, listener); - - if (iconStyle == null) iconStyle = (IconStyleData) iconData.getIconStyles().get(0); - this.iconStyle = iconStyle; + public IconPreferenceData(Context context, Identifier identifier, IconData iconData, OnPreferenceChangeListener listener) { + super(context, identifier); this.iconData = iconData; + styles = new HashMap<>(); + + List styleList = iconData.getIconStyles(); + for (IconStyleData style : styleList) { + styles.put(style.name, style); + } + + iconStyle = styles.get(identifier.getPreferenceValue(context, styleList.get(0).name)); } @Override @@ -46,7 +57,10 @@ public void onPreference(PreferenceDialog dialog, IconStyleData preference) { ((IconStyleImageView) v.findViewById(R.id.icon)).setIconStyle(preference); IconPreferenceData.this.iconStyle = preference; - onPreferenceChange(preference); + getIdentifier().setPreferenceValue(getContext(), preference.name); + onPreferenceChange(preference.name); + if (listener != null) + listener.onPreferenceChange(preference); } } diff --git a/app/src/main/java/com/james/status/data/preference/IntegerPreferenceData.java b/app/src/main/java/com/james/status/data/preference/IntegerPreferenceData.java index 0495d86..74e1ac6 100644 --- a/app/src/main/java/com/james/status/data/preference/IntegerPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/IntegerPreferenceData.java @@ -5,7 +5,6 @@ import android.support.annotation.Nullable; import android.view.View; -import com.james.status.data.PreferenceData; import com.james.status.dialogs.IntegerPickerDialog; import com.james.status.dialogs.PreferenceDialog; @@ -17,11 +16,9 @@ public class IntegerPreferenceData extends BasePreferenceData { @Nullable private Integer min, max; - public IntegerPreferenceData(Context context, Identifier identifier, int defaultValue, String unit, @Nullable Integer min, @Nullable Integer max, OnPreferenceChangeListener listener) { + public IntegerPreferenceData(Context context, Identifier identifier, String unit, @Nullable Integer min, @Nullable Integer max, OnPreferenceChangeListener listener) { super(context, identifier, listener); - - PreferenceData preference = identifier.getPreference(); - this.preference = preference != null ? preference.getIntValue(context, defaultValue) : defaultValue; + preference = identifier.getPreferenceValue(context); this.unit = unit; this.min = min; @@ -35,10 +32,7 @@ public void onClick(View v) { public void onPreference(PreferenceDialog dialog, Integer value) { IntegerPreferenceData.this.preference = preference; - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), value); - + getIdentifier().setPreferenceValue(getContext(), value); onPreferenceChange(value); } diff --git a/app/src/main/java/com/james/status/data/preference/ListPreferenceData.java b/app/src/main/java/com/james/status/data/preference/ListPreferenceData.java index 9e358eb..1264ab6 100644 --- a/app/src/main/java/com/james/status/data/preference/ListPreferenceData.java +++ b/app/src/main/java/com/james/status/data/preference/ListPreferenceData.java @@ -8,8 +8,6 @@ import android.support.v7.app.AlertDialog; import android.view.View; -import com.james.status.data.PreferenceData; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -21,14 +19,13 @@ public class ListPreferenceData extends BasePreferenceData { private ListPreference selectedPreference; - public ListPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener, int defaultItem, ListPreference... items) { + public ListPreferenceData(Context context, Identifier identifier, OnPreferenceChangeListener listener, ListPreference... items) { super(context, identifier, listener); this.items = new ArrayList<>(); this.items.addAll(Arrays.asList(items)); - PreferenceData preference = identifier.getPreference(); - this.preference = preference != null ? preference.getIntValue(context, defaultItem) : defaultItem; + preference = identifier.getPreferenceValue(context); } @Override @@ -60,10 +57,7 @@ public void onClick(DialogInterface dialog, int which) { if (selectedPreference != null) { ListPreferenceData.this.preference = selectedPreference.id; - PreferenceData preference = getIdentifier().getPreference(); - if (preference != null) - preference.setValue(getContext(), selectedPreference.id); - + getIdentifier().setPreferenceValue(getContext(), selectedPreference.id); onPreferenceChange(selectedPreference.id); selectedPreference = null; } diff --git a/app/src/main/java/com/james/status/dialogs/IconDialog.java b/app/src/main/java/com/james/status/dialogs/IconDialog.java index b5dde81..f890f75 100644 --- a/app/src/main/java/com/james/status/dialogs/IconDialog.java +++ b/app/src/main/java/com/james/status/dialogs/IconDialog.java @@ -16,6 +16,7 @@ import com.james.status.R; import com.james.status.adapters.IconStyleAdapter; import com.james.status.data.IconStyleData; +import com.james.status.data.PreferenceData; import com.james.status.data.icon.IconData; import com.james.status.utils.ImageUtils; import com.james.status.utils.StaticUtils; @@ -75,7 +76,7 @@ else if (getContext() instanceof Activity) else Toast.makeText(getContext(), R.string.msg_missing_storage_permission, Toast.LENGTH_SHORT).show(); } else { - new IconCreatorDialog(getContext(), styles.get(0).getSize(), icon.getStringArrayPreference(IconData.PreferenceIdentifier.ICON_STYLE_NAMES), icon.getIconNames()).setListener(new IconCreatorDialog.OnIconStyleListener() { + new IconCreatorDialog(getContext(), styles.get(0).getSize(), (String[]) PreferenceData.ICON_ICON_STYLE_NAMES.getSpecificValue(getContext(), icon.getIdentifierArgs()), icon.getIconNames()).setListener(new IconCreatorDialog.OnIconStyleListener() { @Override public void onIconStyle(IconStyleData style) { icon.addIconStyle(style); diff --git a/app/src/main/java/com/james/status/fragments/GeneralPreferenceFragment.java b/app/src/main/java/com/james/status/fragments/GeneralPreferenceFragment.java index 568d847..485d582 100644 --- a/app/src/main/java/com/james/status/fragments/GeneralPreferenceFragment.java +++ b/app/src/main/java/com/james/status/fragments/GeneralPreferenceFragment.java @@ -3,7 +3,6 @@ import android.Manifest; import android.app.Activity; import android.content.Context; -import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; @@ -46,132 +45,120 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, preferences.addAll(Arrays.asList( new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_COLOR_AUTO, getString(R.string.preference_bar_color_auto), getString(R.string.preference_bar_color_auto_desc), BasePreferenceData.Identifier.SectionIdentifier.COLORS ), - true, this ), new ColorPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_COLOR, getString(R.string.preference_bar_color_chooser), BasePreferenceData.Identifier.SectionIdentifier.COLORS ), - Color.BLACK, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_HOME_TRANSPARENT, getString(R.string.preference_transparent_home), getString(R.string.preference_transparent_home_desc), BasePreferenceData.Identifier.SectionIdentifier.COLORS ), - true, null ), new ColorPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_ICON_COLOR, getString(R.string.preference_default_color_icon), BasePreferenceData.Identifier.SectionIdentifier.ICONS ), - Color.WHITE, null ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_DARK_ICONS, getString(R.string.preference_dark_icons), getString(R.string.preference_dark_icons_desc), BasePreferenceData.Identifier.SectionIdentifier.ICONS ), - true, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_PREVENT_ICON_OVERLAP, getString(R.string.preference_prevent_icon_overlap), getString(R.string.preference_prevent_icon_overlap_desc), BasePreferenceData.Identifier.SectionIdentifier.ICONS ), - false, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_TINTED_ICONS, getString(R.string.preference_tinted_icons), getString(R.string.preference_tinted_icons_desc), BasePreferenceData.Identifier.SectionIdentifier.ICONS ), - false, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_BUMP_MODE, getString(R.string.preference_bump_mode), getString(R.string.preference_bump_mode_desc), BasePreferenceData.Identifier.SectionIdentifier.ICONS ), - false, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_BACKGROUND_ANIMATIONS, getString(R.string.preference_background_animations), getString(R.string.preference_background_animations_desc), BasePreferenceData.Identifier.SectionIdentifier.ANIMATIONS ), - true, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_ICON_ANIMATIONS, getString(R.string.preference_icon_animations), getString(R.string.preference_icon_animations_desc), BasePreferenceData.Identifier.SectionIdentifier.ANIMATIONS ), - true, this ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_NOTIFICATIONS_HEADS_UP, getString(R.string.preference_heads_up), getString(R.string.preference_heads_up_desc), BasePreferenceData.Identifier.SectionIdentifier.NOTIFICATIONS ), - Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP, null ), new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_HEADS_UP_DURATION, getString(R.string.preference_heads_up_duration), getString(R.string.preference_heads_up_duration_desc), BasePreferenceData.Identifier.SectionIdentifier.NOTIFICATIONS ), - 10, getString(R.string.unit_seconds), 0, 20, @@ -179,13 +166,12 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ), new ListPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_HEADS_UP_LAYOUT, getString(R.string.preference_heads_up_layout), BasePreferenceData.Identifier.SectionIdentifier.NOTIFICATIONS ), null, - StatusService.HEADSUP_LAYOUT_PLAIN, new ListPreferenceData.ListPreference( getString(R.string.heads_up_plain), StatusService.HEADSUP_LAYOUT_PLAIN @@ -205,13 +191,12 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_PERSISTENT_NOTIFICATION, getString(R.string.preference_persistent_notification), getString(R.string.preference_persistent_notification_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - true, this ))); @@ -219,13 +204,12 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, preferences.add( new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_IGNORE_PERMISSION_CHECKING, getString(R.string.preference_ignore_permission_checking), getString(R.string.preference_ignore_permission_checking_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - false, this ) ); @@ -234,13 +218,12 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, preferences.addAll(Arrays.asList( new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_HEIGHT, getString(R.string.preference_height), getString(R.string.preference_height_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - 0, getString(R.string.unit_px), 0, Integer.MAX_VALUE, @@ -248,24 +231,22 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_TRANSPARENT_MODE, getString(R.string.preference_transparent_mode), getString(R.string.preference_transparent_mode_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - false, this ), new IntegerPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_SIDE_PADDING, getString(R.string.preference_side_padding), getString(R.string.preference_side_padding_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - 0, getString(R.string.unit_dp), 0, 100, @@ -273,18 +254,18 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_BURNIN_PROTECTION, getString(R.string.preference_burnin_protection), getString(R.string.preference_burnin_protection_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - false, this ), new BasePreferenceData( getContext(), new BasePreferenceData.Identifier( + null, getString(R.string.preference_backups), getString(R.string.preference_backups_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER @@ -300,24 +281,22 @@ public void onClick(View v) { }, new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_HIDE_ON_VOLUME, getString(R.string.preference_hide_on_volume), getString(R.string.preference_hide_on_volume_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - PreferenceData.STATUS_HIDE_ON_VOLUME.getBooleanValue(getContext()), null ), new BooleanPreferenceData( getContext(), - new BasePreferenceData.Identifier( + new BasePreferenceData.Identifier( PreferenceData.STATUS_DEBUG, getString(R.string.preference_debug), getString(R.string.preference_debug_desc), BasePreferenceData.Identifier.SectionIdentifier.OTHER ), - PreferenceData.STATUS_DEBUG.getBooleanValue(getContext()), null ) )); diff --git a/app/src/main/java/com/james/status/services/AccessibilityService.java b/app/src/main/java/com/james/status/services/AccessibilityService.java index 7b9ee88..14f0cdd 100644 --- a/app/src/main/java/com/james/status/services/AccessibilityService.java +++ b/app/src/main/java/com/james/status/services/AccessibilityService.java @@ -98,7 +98,7 @@ protected void onServiceConnected() { @Override public void onAccessibilityEvent(final AccessibilityEvent event) { Log.d("SOMETHING", "Accessibility Event"); - if (PreferenceData.STATUS_ENABLED.getBooleanValue(this)) { + if (PreferenceData.STATUS_ENABLED.getValue(this)) { switch (event.getEventType()) { case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: if (StaticUtils.shouldUseCompatNotifications(this) && !event.getPackageName().toString().matches("com.james.status")) { @@ -159,7 +159,7 @@ public void onAccessibilityEvent(final AccessibilityEvent event) { } Integer color = activityData.getIntegerPreference(this, AppData.PreferenceIdentifier.COLOR); - if (color != null && (!isHome || !PreferenceData.STATUS_HOME_TRANSPARENT.getBooleanValue(this))) { + if (color != null && (!isHome || !(boolean) PreferenceData.STATUS_HOME_TRANSPARENT.getValue(this))) { setStatusBar(color, null, isFullscreen, false, packageName.toString(), activityData); return; } else if (isHome) { @@ -167,14 +167,14 @@ public void onAccessibilityEvent(final AccessibilityEvent event) { return; } - if (!PreferenceData.STATUS_COLOR_AUTO.getBooleanValue(this)) { - setStatusBar(PreferenceData.STATUS_COLOR.getIntValue(this), null, isFullscreen, false, packageName.toString(), activityData); + if (!(boolean) PreferenceData.STATUS_COLOR_AUTO.getValue(this)) { + setStatusBar((int) PreferenceData.STATUS_COLOR.getValue(this), null, isFullscreen, false, packageName.toString(), activityData); return; } if (packageName.toString().equals("com.android.systemui")) { //prevents the creation of some pretty nasty looking color schemes below Lollipop - setStatusBar(PreferenceData.STATUS_COLOR.getIntValue(this), null, false, false, packageName.toString(), activityData); + setStatusBar((int) PreferenceData.STATUS_COLOR.getValue(this), null, false, false, packageName.toString(), activityData); return; } @@ -198,7 +198,7 @@ public void onAccessibilityEvent(final AccessibilityEvent event) { } } - setStatusBar(color != null ? color : PreferenceData.STATUS_COLOR.getIntValue(this), null, isFullscreen, false, packageName.toString(), activityData); + setStatusBar(color != null ? color : (int) PreferenceData.STATUS_COLOR.getValue(this), null, isFullscreen, false, packageName.toString(), activityData); } } } @@ -270,7 +270,7 @@ public void onReceive(Context context, Intent intent) { private void onVolumeChanged() { AccessibilityService service = reference.get(); - if (service != null && PreferenceData.STATUS_HIDE_ON_VOLUME.getBooleanValue(service)) { + if (service != null && (boolean) PreferenceData.STATUS_HIDE_ON_VOLUME.getValue(service)) { Status.showDebug(service, "Volume callback added", Toast.LENGTH_SHORT); service.setStatusBar(null, false, null, true, null, null); handler.removeCallbacks(runnable); diff --git a/app/src/main/java/com/james/status/services/NotificationService.java b/app/src/main/java/com/james/status/services/NotificationService.java index b6c47b8..1811714 100644 --- a/app/src/main/java/com/james/status/services/NotificationService.java +++ b/app/src/main/java/com/james/status/services/NotificationService.java @@ -92,7 +92,7 @@ public void onNotificationPosted(StatusBarNotification sbn) { isEnabled = app.getSpecificBooleanPreference(this, AppData.PreferenceIdentifier.NOTIFICATIONS); if (isEnabled == null) isEnabled = true; - if (PreferenceData.STATUS_ENABLED.getBooleanValue(this) && isEnabled && !StaticUtils.shouldUseCompatNotifications(this) && !sbn.getPackageName().matches("com.james.status")) { + if ((boolean) PreferenceData.STATUS_ENABLED.getValue(this) && isEnabled && !StaticUtils.shouldUseCompatNotifications(this) && !sbn.getPackageName().matches("com.james.status")) { NotificationData notification = new NotificationData(sbn, getKey(sbn)); if (notification.shouldShowHeadsUp(this)) { @@ -117,7 +117,7 @@ public void onNotificationPosted(StatusBarNotification sbn) { @Override public void onNotificationRemoved(StatusBarNotification sbn) { - if (PreferenceData.STATUS_ENABLED.getBooleanValue(this) && !StaticUtils.shouldUseCompatNotifications(this)) { + if ((boolean) PreferenceData.STATUS_ENABLED.getValue(this) && !StaticUtils.shouldUseCompatNotifications(this)) { Intent intent = new Intent(NotificationsIconData.ACTION_NOTIFICATION_REMOVED); intent.putExtra(NotificationsIconData.EXTRA_NOTIFICATION, new NotificationData(sbn, getKey(sbn))); sendBroadcast(intent); @@ -136,7 +136,7 @@ private ArrayList getNotifications() { } private void sendNotifications() { - if (PreferenceData.STATUS_ENABLED.getBooleanValue(this) && !StaticUtils.shouldUseCompatNotifications(this)) { + if ((boolean) PreferenceData.STATUS_ENABLED.getValue(this) && !StaticUtils.shouldUseCompatNotifications(this)) { List notifications = getNotifications(); Collections.reverse(notifications); diff --git a/app/src/main/java/com/james/status/services/StatusService.java b/app/src/main/java/com/james/status/services/StatusService.java index 8569a54..8a806ab 100644 --- a/app/src/main/java/com/james/status/services/StatusService.java +++ b/app/src/main/java/com/james/status/services/StatusService.java @@ -138,10 +138,10 @@ public void run() { } }; - if (PreferenceData.STATUS_ENABLED.getBooleanValue(this)) + if (PreferenceData.STATUS_ENABLED.getValue(this)) setUp(); - headsUpDuration = PreferenceData.STATUS_HEADS_UP_DURATION.getIntValue(this) * 1000; + headsUpDuration = (int) PreferenceData.STATUS_HEADS_UP_DURATION.getValue(this) * 1000; } @Nullable @@ -152,7 +152,7 @@ public IBinder onBind(Intent intent) { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (!PreferenceData.STATUS_ENABLED.getBooleanValue(this)) { + if (!(boolean) PreferenceData.STATUS_ENABLED.getValue(this)) { if (statusView != null) { if (statusView.getParent() != null) windowManager.removeView(statusView); statusView.unregister(); @@ -185,7 +185,7 @@ else if (intent.hasExtra(EXTRA_COLOR) && headsUpView == null) statusView.setFullscreen(intent.getBooleanExtra(EXTRA_IS_FULLSCREEN, isFullscreen())); if (intent.hasExtra(EXTRA_PACKAGE) && intent.hasExtra(EXTRA_ACTIVITY)) { - if (PreferenceData.STATUS_PERSISTENT_NOTIFICATION.getBooleanValue(this)) { + if (PreferenceData.STATUS_PERSISTENT_NOTIFICATION.getValue(this)) { packageName = intent.getStringExtra(EXTRA_PACKAGE); activityData = intent.getParcelableExtra(EXTRA_ACTIVITY); @@ -196,7 +196,7 @@ else if (intent.hasExtra(EXTRA_COLOR) && headsUpView == null) return START_STICKY; } - if (PreferenceData.STATUS_PERSISTENT_NOTIFICATION.getBooleanValue(this)) { + if (PreferenceData.STATUS_PERSISTENT_NOTIFICATION.getValue(this)) { if (packageName != null && activityData != null) startForeground(packageName, activityData); else { @@ -245,7 +245,7 @@ private void startForeground(String packageName, AppData.ActivityData activityDa .setSubText(packageName) .setContentIntent(contentStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT)); - if (PreferenceData.STATUS_COLOR_AUTO.getBooleanValue(this)) { + if (PreferenceData.STATUS_COLOR_AUTO.getValue(this)) { Intent colorIntent = new Intent(this, AppSettingActivity.class); colorIntent.putExtra(AppSettingActivity.EXTRA_APP, appData); colorIntent.putExtra(AppSettingActivity.EXTRA_ACTIVITY, activityData); @@ -389,7 +389,7 @@ public ArrayMap getNotifications() { public void showHeadsUp(NotificationData notification) { int headsUpLayout; - switch (PreferenceData.STATUS_HEADS_UP_LAYOUT.getIntValue(this)) { + switch ((int) PreferenceData.STATUS_HEADS_UP_LAYOUT.getValue(this)) { case HEADSUP_LAYOUT_CARD: headsUpLayout = R.layout.layout_notification_card; break; @@ -457,7 +457,7 @@ else if (headsUpView != null) } }); - headsUpDuration = PreferenceData.STATUS_HEADS_UP_DURATION.getIntValue(this) * 1000; + headsUpDuration = (int) PreferenceData.STATUS_HEADS_UP_DURATION.getValue(this) * 1000; headsUpHandler.postDelayed(headsUpRunnable, headsUpDuration); CustomImageView icon = (CustomImageView) headsUpView.findViewById(R.id.icon); @@ -636,8 +636,9 @@ public static List getIcons(Context context) { icons.add(new OrientationIconData(context)); for (IconData icon : icons) { - if (icon.getIntegerPreference(IconData.PreferenceIdentifier.POSITION) == null) - icon.putPreference(IconData.PreferenceIdentifier.POSITION, icons.indexOf(icon)); + int position = PreferenceData.ICON_POSITION.getSpecificOverriddenValue(context, -1, icon.getIdentifierArgs()); + if (position < 0) + PreferenceData.ICON_POSITION.setValue(context, icons.indexOf(icon), icon.getIdentifierArgs()); } Collections.sort(icons, new Comparator() { @@ -667,7 +668,7 @@ public void onReceive(Context context, Intent intent) { statusView.setSystemShowing(true); headsUpNotification = notification; - headsUpDuration = PreferenceData.STATUS_HEADS_UP_DURATION.getIntValue(StatusService.this) * 1000; + headsUpDuration = (int) PreferenceData.STATUS_HEADS_UP_DURATION.getValue(StatusService.this) * 1000; headsUpHandler.postDelayed(headsUpDisabledRunnable, headsUpDuration); } diff --git a/app/src/main/java/com/james/status/utils/StaticUtils.java b/app/src/main/java/com/james/status/utils/StaticUtils.java index 905ad31..7635266 100644 --- a/app/src/main/java/com/james/status/utils/StaticUtils.java +++ b/app/src/main/java/com/james/status/utils/StaticUtils.java @@ -32,7 +32,7 @@ public class StaticUtils { public static int getStatusBarHeight(Context context) { - int height = PreferenceData.STATUS_HEIGHT.getIntValue(context); + int height = PreferenceData.STATUS_HEIGHT.getValue(context); if (height > 0) return height; @@ -104,7 +104,7 @@ public static boolean isPermissionsGranted(Context context, String[] permissions if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { if (BuildConfig.DEBUG) Log.wtf("Permission", "missing " + permission); - return PreferenceData.STATUS_IGNORE_PERMISSION_CHECKING.getBooleanValue(context); + return PreferenceData.STATUS_IGNORE_PERMISSION_CHECKING.getValue(context); } } @@ -117,7 +117,7 @@ public static boolean isAllPermissionsGranted(Context context) { if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { if (BuildConfig.DEBUG) Log.wtf("Permission", "missing " + permission); - return PreferenceData.STATUS_IGNORE_PERMISSION_CHECKING.getBooleanValue(context); + return PreferenceData.STATUS_IGNORE_PERMISSION_CHECKING.getValue(context); } } } @@ -137,7 +137,7 @@ public static void requestPermissions(Activity activity, String[] permissions) { } public static boolean isStatusServiceRunning(Context context) { - if (PreferenceData.STATUS_ENABLED.getBooleanValue(context) && isReady(context)) { + if ((boolean) PreferenceData.STATUS_ENABLED.getValue(context) && isReady(context)) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (manager != null) { for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { @@ -179,7 +179,7 @@ public static boolean isAccessibilityServiceRunning(Context context) { } public static boolean shouldUseCompatNotifications(Context context) { - return Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 || PreferenceData.STATUS_NOTIFICATIONS_COMPAT.getBooleanValue(context); + return Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 || (boolean) PreferenceData.STATUS_NOTIFICATIONS_COMPAT.getValue(context); } public static boolean isReady(Context context) { diff --git a/app/src/main/java/com/james/status/views/OverflowLinearLayout.java b/app/src/main/java/com/james/status/views/OverflowLinearLayout.java index 064e1b5..6b75902 100644 --- a/app/src/main/java/com/james/status/views/OverflowLinearLayout.java +++ b/app/src/main/java/com/james/status/views/OverflowLinearLayout.java @@ -43,7 +43,7 @@ public void onGlobalLayout() { } private void setUpOverflow(int width) { - if (PreferenceData.STATUS_PREVENT_ICON_OVERLAP.getBooleanValue(OverflowLinearLayout.this.getContext())) + if (PreferenceData.STATUS_PREVENT_ICON_OVERLAP.getValue(OverflowLinearLayout.this.getContext())) onViewsChanged(); else { Intent intent = new Intent(MainActivity.ACTION_TOO_MANY_ICONS); diff --git a/app/src/main/java/com/james/status/views/StatusView.java b/app/src/main/java/com/james/status/views/StatusView.java index 3deab17..62cbd32 100644 --- a/app/src/main/java/com/james/status/views/StatusView.java +++ b/app/src/main/java/com/james/status/views/StatusView.java @@ -248,14 +248,11 @@ private void getOptions() { for (int i = 0; i < OPTIONS.length; i++) { PreferenceData data = OPTIONS[i]; if (data != null) { - switch (data.getType()) { - case PreferenceData.TYPE_BOOLEAN: - booleanOptions[i] = data.getBooleanValue(getContext()); - break; - case PreferenceData.TYPE_INT: - intOptions[i] = data.getIntValue(getContext()); - break; - } + Object defaultValue = data.getDefaultValue(); + if (defaultValue instanceof Boolean) + booleanOptions[i] = data.getValue(getContext()); + else if (defaultValue instanceof Integer) + intOptions[i] = data.getValue(getContext()); } } } @@ -477,12 +474,12 @@ public int getColor() { @ColorInt private int getDefaultColor() { - return PreferenceData.STATUS_COLOR.getIntValue(getContext()); + return PreferenceData.STATUS_COLOR.getValue(getContext()); } @ColorInt private int getDefaultIconColor() { - return PreferenceData.STATUS_ICON_COLOR.getIntValue(getContext()); + return PreferenceData.STATUS_ICON_COLOR.getValue(getContext()); } private void setStatusBackgroundColor(@ColorInt int color) { diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index b0e3b5f..a61f0d3 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -395,6 +395,7 @@ Animations Notifications Other + Text Font diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index ec7b497..225b5ed 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -379,6 +379,7 @@ Animations Notifications Other + Text Font Why can\'t the status bar be transparent? diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index ad39e9f..cfe595a 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -399,6 +399,7 @@ Animations Notifications Other + Text Font Why can\'t the status bar be transparent? diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index f37f4be..04b117f 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -395,6 +395,7 @@ Animations Notifications Other + Text Font Why can\'t the status bar be transparent? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index afc76b8..df4232e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -515,5 +515,6 @@ Finish Cancel Skip + Text Font