3232import androidx .annotation .NonNull ;
3333import androidx .annotation .Nullable ;
3434import androidx .annotation .StyleRes ;
35- import java .util .Arrays ;
36- import java .util .HashSet ;
37- import java .util .Set ;
35+ import java .lang .reflect .Method ;
36+ import java .util .Collections ;
37+ import java .util .HashMap ;
38+ import java .util .Map ;
3839
3940/**
4041 * Utility for applying dynamic colors to application/activities.
@@ -43,14 +44,66 @@ public class DynamicColors {
4344 private static final int [] DYNAMIC_COLOR_THEME_OVERLAY_ATTRIBUTE =
4445 new int [] { R .attr .dynamicColorThemeOverlay };
4546
46- private static final Set <String > DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS =
47- new HashSet <>(Arrays .asList (
48- "oppo" , "realme" , "oneplus" , "vivo" , "xiaomi" , "motorola" , "itel" , "tecno mobile limited" ,
49- "infinix mobility limited" , "hmd global" , "sharp" , "sony" , "tcl" , "lenovo" , "google" ,
50- "robolectric" ));
47+ private static final DeviceSupportCondition DEFAULT_DEVICE_SUPPORT_CONDITION =
48+ new DeviceSupportCondition () {
49+ @ Override
50+ public boolean isSupported () {
51+ return true ;
52+ }
53+ };
54+
55+ @ SuppressLint ("PrivateApi" )
56+ private static final DeviceSupportCondition SAMSUNG_DEVICE_SUPPORT_CONDITION =
57+ new DeviceSupportCondition () {
58+ private Long version ;
59+
60+ @ Override
61+ public boolean isSupported () {
62+ if (version == null ) {
63+ try {
64+ Method method = Build .class .getDeclaredMethod ("getLong" , String .class );
65+ method .setAccessible (true );
66+ version = (long ) method .invoke (null , "ro.build.version.oneui" );
67+ } catch (Exception e ) {
68+ version = -1L ;
69+ }
70+ }
71+ return version >= 40100L ;
72+ }
73+ };
74+
75+ private static final Map <String , DeviceSupportCondition > DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS ;
76+
77+ static {
78+ Map <String , DeviceSupportCondition > deviceMap = new HashMap <>();
79+ deviceMap .put ("oppo" , DEFAULT_DEVICE_SUPPORT_CONDITION );
80+ deviceMap .put ("realme" , DEFAULT_DEVICE_SUPPORT_CONDITION );
81+ deviceMap .put ("oneplus" , DEFAULT_DEVICE_SUPPORT_CONDITION );
82+ deviceMap .put ("vivo" , DEFAULT_DEVICE_SUPPORT_CONDITION );
83+ deviceMap .put ("xiaomi" , DEFAULT_DEVICE_SUPPORT_CONDITION );
84+ deviceMap .put ("motorola" , DEFAULT_DEVICE_SUPPORT_CONDITION );
85+ deviceMap .put ("itel" , DEFAULT_DEVICE_SUPPORT_CONDITION );
86+ deviceMap .put ("tecno mobile limited" , DEFAULT_DEVICE_SUPPORT_CONDITION );
87+ deviceMap .put ("infinix mobility limited" , DEFAULT_DEVICE_SUPPORT_CONDITION );
88+ deviceMap .put ("hmd global" , DEFAULT_DEVICE_SUPPORT_CONDITION );
89+ deviceMap .put ("sharp" , DEFAULT_DEVICE_SUPPORT_CONDITION );
90+ deviceMap .put ("sony" , DEFAULT_DEVICE_SUPPORT_CONDITION );
91+ deviceMap .put ("tcl" , DEFAULT_DEVICE_SUPPORT_CONDITION );
92+ deviceMap .put ("lenovo" , DEFAULT_DEVICE_SUPPORT_CONDITION );
93+ deviceMap .put ("google" , DEFAULT_DEVICE_SUPPORT_CONDITION );
94+ deviceMap .put ("robolectric" , DEFAULT_DEVICE_SUPPORT_CONDITION );
95+ deviceMap .put ("samsung" , SAMSUNG_DEVICE_SUPPORT_CONDITION );
96+ DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS = Collections .unmodifiableMap (deviceMap );
97+ }
98+
99+ private static final Map <String , DeviceSupportCondition > DYNAMIC_COLOR_SUPPORTED_BRANDS ;
51100
52- private static final Set <String > DYNAMIC_COLOR_SUPPORTED_BRANDS =
53- new HashSet <>(Arrays .asList ("jio" ));
101+ static {
102+ Map <String , DeviceSupportCondition > deviceMap = new HashMap <>();
103+ deviceMap .put ("asus" , DEFAULT_DEVICE_SUPPORT_CONDITION );
104+ deviceMap .put ("jio" , DEFAULT_DEVICE_SUPPORT_CONDITION );
105+ DYNAMIC_COLOR_SUPPORTED_BRANDS = Collections .unmodifiableMap (deviceMap );
106+ }
54107
55108 private static final int USE_DEFAULT_THEME_OVERLAY = 0 ;
56109
@@ -223,9 +276,15 @@ public static Context wrapContextIfAvailable(
223276 @ SuppressLint ("DefaultLocale" )
224277 @ ChecksSdkIntAtLeast (api = VERSION_CODES .S )
225278 public static boolean isDynamicColorAvailable () {
226- return VERSION .SDK_INT >= VERSION_CODES .S
227- && (DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS .contains (Build .MANUFACTURER .toLowerCase ())
228- || DYNAMIC_COLOR_SUPPORTED_BRANDS .contains (Build .BRAND .toLowerCase ()));
279+ if (VERSION .SDK_INT < VERSION_CODES .S ) {
280+ return false ;
281+ }
282+ DeviceSupportCondition deviceSupportCondition =
283+ DYNAMIC_COLOR_SUPPORTED_MANUFACTURERS .get (Build .MANUFACTURER .toLowerCase ());
284+ if (deviceSupportCondition == null ) {
285+ deviceSupportCondition = DYNAMIC_COLOR_SUPPORTED_BRANDS .get (Build .BRAND .toLowerCase ());
286+ }
287+ return deviceSupportCondition != null && deviceSupportCondition .isSupported ();
229288 }
230289
231290 private static int getDefaultThemeOverlay (@ NonNull Context context ) {
@@ -286,4 +345,8 @@ public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bun
286345 @ Override
287346 public void onActivityDestroyed (@ NonNull Activity activity ) {}
288347 }
348+
349+ private interface DeviceSupportCondition {
350+ boolean isSupported ();
351+ }
289352}
0 commit comments