Skip to content

Commit 11bce49

Browse files
committed
Dynamic theme 4.4.0
Refactor theme fragments. Add support for system corner radius.
1 parent 31d9aaa commit 11bce49

File tree

14 files changed

+410
-244
lines changed

14 files changed

+410
-244
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ buildscript {
2929
'material' : '1.8.0-alpha03',
3030
'preferences' : '2.2.2',
3131
'swiperefresh': '1.1.0',
32-
'theme' : '4.3.1',
32+
'theme' : '4.4.0',
3333
'toasts' : '4.1.2',
3434
'work' : '2.8.0-beta02'
3535
]

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/model/DynamicAppTheme.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,8 @@ public boolean isFontScale() {
13701370

13711371
@Override
13721372
public int getCornerRadius(boolean resolve) {
1373-
if (resolve && getCornerRadius(false) == Theme.Corner.AUTO) {
1373+
if (resolve && (getCornerRadius(false) == Theme.Corner.AUTO
1374+
|| getCornerRadius(false) == Theme.Corner.SYSTEM)) {
13741375
return getThemeFallback(false).getCornerRadius();
13751376
}
13761377

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

13921393
@Override
13931394
public int getCornerSize(boolean resolve) {
1394-
if (!resolve && getCornerRadius(false) == Theme.Corner.AUTO) {
1395+
if (!resolve && (getCornerRadius(false) == Theme.Corner.AUTO
1396+
|| getCornerRadius(false) == Theme.Corner.SYSTEM)) {
13951397
return Theme.Corner.AUTO;
13961398
}
13971399

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

14061408
@Override
14071409
public @NonNull DynamicAppTheme setCornerSize(float cornerSize) {
1408-
return setCornerRadius(cornerSize == Theme.Corner.AUTO ? (int) cornerSize
1409-
: DynamicUnitUtils.convertDpToPixels(cornerSize));
1410+
return setCornerRadius(cornerSize == Theme.Corner.AUTO || cornerSize == Theme.Corner.SYSTEM
1411+
? (int) cornerSize : DynamicUnitUtils.convertDpToPixels(cornerSize));
14101412
}
14111413

14121414
@Override

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/model/DynamicWidgetTheme.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.pranavpandey.android.dynamic.theme.base.WidgetTheme;
3535
import com.pranavpandey.android.dynamic.theme.strategy.ExcludeStrategy;
3636
import com.pranavpandey.android.dynamic.theme.util.DynamicThemeUtils;
37+
import com.pranavpandey.android.dynamic.util.DynamicSdkUtils;
3738

3839
/**
3940
* An app widget theme to store various colors and attributes for app widget which can be
@@ -68,7 +69,8 @@ public DynamicWidgetTheme() {
6869
* @param widgetId The widget id to be used.
6970
*/
7071
public DynamicWidgetTheme(int widgetId) {
71-
this(widgetId, new DynamicAppTheme());
72+
this(widgetId, new DynamicAppTheme().setCornerSize(DynamicSdkUtils.is31()
73+
? Theme.Corner.SYSTEM : Theme.Corner.AUTO));
7274
}
7375

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

323+
@Override
324+
public int getCornerRadius(boolean resolve) {
325+
if (resolve && super.getCornerRadius(false) == Theme.Corner.SYSTEM) {
326+
return DynamicTheme.getInstance().getWidgetCornerRadius(
327+
super.getCornerRadius(true));
328+
}
329+
330+
return super.getCornerRadius(resolve);
331+
}
332+
333+
@Override
334+
public int getCornerSize(boolean resolve) {
335+
if (!resolve && super.getCornerRadius(false) == Theme.Corner.SYSTEM) {
336+
return Theme.Corner.SYSTEM;
337+
}
338+
339+
return super.getCornerSize(resolve);
340+
}
341+
321342
@Override
322343
public @ColorInt int getStrokeColor() {
323344
if (getPrimaryColorDark(false) == Theme.AUTO) {

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/theme/DynamicTheme.java

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import androidx.annotation.ColorInt;
3838
import androidx.annotation.NonNull;
3939
import androidx.annotation.Nullable;
40+
import androidx.annotation.Px;
4041
import androidx.annotation.RestrictTo;
4142
import androidx.annotation.StyleRes;
4243
import androidx.core.app.ActivityCompat;
@@ -229,7 +230,8 @@ public class DynamicTheme implements DynamicListener, DynamicResolver {
229230
/**
230231
* Default corner size for the theme.
231232
*/
232-
private static final int CORNER_SIZE_DEFAULT = DynamicUnitUtils.convertDpToPixels(2);
233+
private static final int CORNER_SIZE_DEFAULT =
234+
DynamicUnitUtils.convertDpToPixels(Theme.Corner.DEFAULT);
233235

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

1197+
/**
1198+
* Returns the currently used context.
1199+
* <p>Generally, either application or an activity.
1200+
*
1201+
* @return The currently used context.
1202+
*/
1203+
private @NonNull Context getResolvedContext() {
1204+
return getLocalContext() != null ? getLocalContext() : getContext();
1205+
}
1206+
11951207
/**
11961208
* Get the power manager used by the application.
11971209
*
@@ -1407,16 +1419,6 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
14071419
mDefaultLocalTheme = null;
14081420
}
14091421

1410-
/**
1411-
* Generates default theme according to the current settings.
1412-
*
1413-
* @return The generated default theme.
1414-
*/
1415-
public @NonNull DynamicAppTheme generateDefaultTheme() {
1416-
return new DynamicAppTheme().setBackgroundColor(
1417-
getDefault().getBackgroundColor(), false);
1418-
}
1419-
14201422
/**
14211423
* Generates default background color according to the application theme.
14221424
*
@@ -1429,6 +1431,43 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
14291431
? R.color.ads_window_background : R.color.ads_window_background_light);
14301432
}
14311433

1434+
/**
1435+
* Try to get the corner radius for the widget background from the system.
1436+
*
1437+
* @param fallback The fallback radius to be used in case of any issues.
1438+
*
1439+
* @return The corner radius for the widget background from the system.
1440+
*/
1441+
@TargetApi(Build.VERSION_CODES.S)
1442+
public @Px int getWidgetCornerRadius(int fallback) {
1443+
if (DynamicSdkUtils.is31()) {
1444+
return Math.min(getContext().getResources().getDimensionPixelOffset(
1445+
android.R.dimen.system_app_widget_background_radius),
1446+
DynamicUnitUtils.convertDpToPixels(Theme.Corner.MAX));
1447+
}
1448+
1449+
return fallback;
1450+
}
1451+
1452+
/**
1453+
* Returns the default contrast with color to tint the background aware views accordingly.
1454+
*
1455+
* @return The default contrast with color.
1456+
*/
1457+
public @ColorInt int getDefaultContrastWith() {
1458+
return get().getBackgroundColor();
1459+
}
1460+
1461+
/**
1462+
* Generates default theme according to the current settings.
1463+
*
1464+
* @return The generated default theme.
1465+
*/
1466+
public @NonNull DynamicAppTheme generateDefaultTheme() {
1467+
return new DynamicAppTheme().setBackgroundColor(
1468+
getDefault().getBackgroundColor(), false);
1469+
}
1470+
14321471
/**
14331472
* Generates stroke color according to the supplied color.
14341473
*
@@ -1491,25 +1530,6 @@ public void onLocalDestroy(@Nullable DynamicListener listener) {
14911530
return Dynamic.getTintColor(color);
14921531
}
14931532

1494-
/**
1495-
* Returns the currently used context.
1496-
* <p>Generally, either application or an activity.
1497-
*
1498-
* @return The currently used context.
1499-
*/
1500-
private @NonNull Context getResolvedContext() {
1501-
return getLocalContext() != null ? getLocalContext() : getContext();
1502-
}
1503-
1504-
/**
1505-
* Returns the default contrast with color to tint the background aware views accordingly.
1506-
*
1507-
* @return The default contrast with color.
1508-
*/
1509-
public @ColorInt int getDefaultContrastWith() {
1510-
return get().getBackgroundColor();
1511-
}
1512-
15131533
/**
15141534
* Returns the resolver used by the dynamic theme.
15151535
*

0 commit comments

Comments
 (0)