Skip to content

Commit

Permalink
Dynamic theme 4.4.0
Browse files Browse the repository at this point in the history
Refactor theme fragments.
Add support for system corner radius.
  • Loading branch information
pranavpandey committed Nov 28, 2022
1 parent 31d9aaa commit 11bce49
Show file tree
Hide file tree
Showing 14 changed files with 410 additions and 244 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ buildscript {
'material' : '1.8.0-alpha03',
'preferences' : '2.2.2',
'swiperefresh': '1.1.0',
'theme' : '4.3.1',
'theme' : '4.4.0',
'toasts' : '4.1.2',
'work' : '2.8.0-beta02'
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,8 @@ public boolean isFontScale() {

@Override
public int getCornerRadius(boolean resolve) {
if (resolve && getCornerRadius(false) == Theme.Corner.AUTO) {
if (resolve && (getCornerRadius(false) == Theme.Corner.AUTO
|| getCornerRadius(false) == Theme.Corner.SYSTEM)) {
return getThemeFallback(false).getCornerRadius();
}

Expand All @@ -1391,7 +1392,8 @@ public int getCornerRadius() {

@Override
public int getCornerSize(boolean resolve) {
if (!resolve && getCornerRadius(false) == Theme.Corner.AUTO) {
if (!resolve && (getCornerRadius(false) == Theme.Corner.AUTO
|| getCornerRadius(false) == Theme.Corner.SYSTEM)) {
return Theme.Corner.AUTO;
}

Expand All @@ -1405,8 +1407,8 @@ public int getCornerSize() {

@Override
public @NonNull DynamicAppTheme setCornerSize(float cornerSize) {
return setCornerRadius(cornerSize == Theme.Corner.AUTO ? (int) cornerSize
: DynamicUnitUtils.convertDpToPixels(cornerSize));
return setCornerRadius(cornerSize == Theme.Corner.AUTO || cornerSize == Theme.Corner.SYSTEM
? (int) cornerSize : DynamicUnitUtils.convertDpToPixels(cornerSize));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.pranavpandey.android.dynamic.theme.base.WidgetTheme;
import com.pranavpandey.android.dynamic.theme.strategy.ExcludeStrategy;
import com.pranavpandey.android.dynamic.theme.util.DynamicThemeUtils;
import com.pranavpandey.android.dynamic.util.DynamicSdkUtils;

/**
* An app widget theme to store various colors and attributes for app widget which can be
Expand Down Expand Up @@ -68,7 +69,8 @@ public DynamicWidgetTheme() {
* @param widgetId The widget id to be used.
*/
public DynamicWidgetTheme(int widgetId) {
this(widgetId, new DynamicAppTheme());
this(widgetId, new DynamicAppTheme().setCornerSize(DynamicSdkUtils.is31()
? Theme.Corner.SYSTEM : Theme.Corner.AUTO));
}

/**
Expand Down Expand Up @@ -318,6 +320,25 @@ public void writeToParcel(Parcel dest, int flags) {
return super.getTextSecondaryColorInverse(resolve, inverse);
}

@Override
public int getCornerRadius(boolean resolve) {
if (resolve && super.getCornerRadius(false) == Theme.Corner.SYSTEM) {
return DynamicTheme.getInstance().getWidgetCornerRadius(
super.getCornerRadius(true));
}

return super.getCornerRadius(resolve);
}

@Override
public int getCornerSize(boolean resolve) {
if (!resolve && super.getCornerRadius(false) == Theme.Corner.SYSTEM) {
return Theme.Corner.SYSTEM;
}

return super.getCornerSize(resolve);
}

@Override
public @ColorInt int getStrokeColor() {
if (getPrimaryColorDark(false) == Theme.AUTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -229,7 +230,8 @@ public class DynamicTheme implements DynamicListener, DynamicResolver {
/**
* Default corner size for the theme.
*/
private static final int CORNER_SIZE_DEFAULT = DynamicUnitUtils.convertDpToPixels(2);
private static final int CORNER_SIZE_DEFAULT =
DynamicUnitUtils.convertDpToPixels(Theme.Corner.DEFAULT);

/**
* Singleton instance of {@link DynamicTheme}.
Expand Down Expand Up @@ -1192,6 +1194,16 @@ public void setLocalVersion(@Version int version) {
? (Context) getLocalListener() : getLocalListener().getContext();
}

/**
* Returns the currently used context.
* <p>Generally, either application or an activity.
*
* @return The currently used context.
*/
private @NonNull Context getResolvedContext() {
return getLocalContext() != null ? getLocalContext() : getContext();
}

/**
* Get the power manager used by the application.
*
Expand Down Expand Up @@ -1407,16 +1419,6 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
mDefaultLocalTheme = null;
}

/**
* Generates default theme according to the current settings.
*
* @return The generated default theme.
*/
public @NonNull DynamicAppTheme generateDefaultTheme() {
return new DynamicAppTheme().setBackgroundColor(
getDefault().getBackgroundColor(), false);
}

/**
* Generates default background color according to the application theme.
*
Expand All @@ -1429,6 +1431,43 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
? R.color.ads_window_background : R.color.ads_window_background_light);
}

/**
* Try to get the corner radius for the widget background from the system.
*
* @param fallback The fallback radius to be used in case of any issues.
*
* @return The corner radius for the widget background from the system.
*/
@TargetApi(Build.VERSION_CODES.S)
public @Px int getWidgetCornerRadius(int fallback) {
if (DynamicSdkUtils.is31()) {
return Math.min(getContext().getResources().getDimensionPixelOffset(
android.R.dimen.system_app_widget_background_radius),
DynamicUnitUtils.convertDpToPixels(Theme.Corner.MAX));
}

return fallback;
}

/**
* Returns the default contrast with color to tint the background aware views accordingly.
*
* @return The default contrast with color.
*/
public @ColorInt int getDefaultContrastWith() {
return get().getBackgroundColor();
}

/**
* Generates default theme according to the current settings.
*
* @return The generated default theme.
*/
public @NonNull DynamicAppTheme generateDefaultTheme() {
return new DynamicAppTheme().setBackgroundColor(
getDefault().getBackgroundColor(), false);
}

/**
* Generates stroke color according to the supplied color.
*
Expand Down Expand Up @@ -1491,25 +1530,6 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
return Dynamic.getTintColor(color);
}

/**
* Returns the currently used context.
* <p>Generally, either application or an activity.
*
* @return The currently used context.
*/
private @NonNull Context getResolvedContext() {
return getLocalContext() != null ? getLocalContext() : getContext();
}

/**
* Returns the default contrast with color to tint the background aware views accordingly.
*
* @return The default contrast with color.
*/
public @ColorInt int getDefaultContrastWith() {
return get().getBackgroundColor();
}

/**
* Returns the resolver used by the dynamic theme.
*
Expand Down

0 comments on commit 11bce49

Please sign in to comment.