2121
2222import android .app .Activity ;
2323import android .content .Context ;
24+ import android .content .res .Configuration ;
25+ import android .content .res .Resources ;
2426import android .content .res .TypedArray ;
2527import android .util .TypedValue ;
26- import android .view .ContextThemeWrapper ;
2728
2829import androidx .annotation .AttrRes ;
2930import androidx .annotation .Nullable ;
3940import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
4041
4142public final class ThemeHelper {
42- private ThemeHelper () { }
43+ private ThemeHelper () {
44+ }
4345
4446 /**
4547 * Apply the selected theme (on NewPipe settings) in the context
@@ -70,31 +72,12 @@ public static void setTheme(final Context context, final int serviceId) {
7072 * @return whether the light theme is selected
7173 */
7274 public static boolean isLightThemeSelected (final Context context ) {
73- return getSelectedThemeString (context ).equals (context .getResources ()
74- .getString (R .string .light_theme_key ));
75- }
76-
77-
78- /**
79- * Create and return a wrapped context with the default selected theme set.
80- *
81- * @param baseContext the base context for the wrapper
82- * @return a wrapped-styled context
83- */
84- public static Context getThemedContext (final Context baseContext ) {
85- return new ContextThemeWrapper (baseContext , getThemeForService (baseContext , -1 ));
86- }
75+ final String selectedThemeKey = getSelectedThemeKey (context );
76+ final Resources res = context .getResources ();
8777
88- /**
89- * Return the selected theme without being styled to any service.
90- * See {@link #getThemeForService(Context, int)}.
91- *
92- * @param context context to get the selected theme
93- * @return the selected style (the default one)
94- */
95- @ StyleRes
96- public static int getDefaultTheme (final Context context ) {
97- return getThemeForService (context , -1 );
78+ return selectedThemeKey .equals (res .getString (R .string .light_theme_key ))
79+ || (selectedThemeKey .equals (res .getString (R .string .auto_device_theme_key ))
80+ && !isDeviceDarkThemeEnabled (context ));
9881 }
9982
10083 /**
@@ -130,71 +113,60 @@ public static int getMinWidthDialogTheme(final Context context) {
130113 */
131114 @ StyleRes
132115 public static int getThemeForService (final Context context , final int serviceId ) {
133- final String lightTheme = context .getResources ().getString (R .string .light_theme_key );
134- final String darkTheme = context .getResources ().getString (R .string .dark_theme_key );
135- final String blackTheme = context .getResources ().getString (R .string .black_theme_key );
136-
137- final String selectedTheme = getSelectedThemeString (context );
138-
139- int defaultTheme = R .style .DarkTheme ;
140- if (selectedTheme .equals (lightTheme )) {
141- defaultTheme = R .style .LightTheme ;
142- } else if (selectedTheme .equals (blackTheme )) {
143- defaultTheme = R .style .BlackTheme ;
144- } else if (selectedTheme .equals (darkTheme )) {
145- defaultTheme = R .style .DarkTheme ;
116+ final Resources res = context .getResources ();
117+ final String lightThemeKey = res .getString (R .string .light_theme_key );
118+ final String blackThemeKey = res .getString (R .string .black_theme_key );
119+ final String automaticDeviceThemeKey = res .getString (R .string .auto_device_theme_key );
120+
121+ final String selectedThemeKey = getSelectedThemeKey (context );
122+
123+ int baseTheme = R .style .DarkTheme ; // default to dark theme
124+ if (selectedThemeKey .equals (lightThemeKey )) {
125+ baseTheme = R .style .LightTheme ;
126+ } else if (selectedThemeKey .equals (blackThemeKey )) {
127+ baseTheme = R .style .BlackTheme ;
128+ } else if (selectedThemeKey .equals (automaticDeviceThemeKey )) {
129+
130+ if (isDeviceDarkThemeEnabled (context )) {
131+ // use the dark theme variant preferred by the user
132+ final String selectedNightThemeKey = getSelectedNightThemeKey (context );
133+ if (selectedNightThemeKey .equals (blackThemeKey )) {
134+ baseTheme = R .style .BlackTheme ;
135+ } else {
136+ baseTheme = R .style .DarkTheme ;
137+ }
138+ } else {
139+ // there is only one day theme
140+ baseTheme = R .style .LightTheme ;
141+ }
146142 }
147143
148144 if (serviceId <= -1 ) {
149- return defaultTheme ;
145+ return baseTheme ;
150146 }
151147
152148 final StreamingService service ;
153149 try {
154150 service = NewPipe .getService (serviceId );
155151 } catch (final ExtractionException ignored ) {
156- return defaultTheme ;
152+ return baseTheme ;
157153 }
158154
159- String themeName = "DarkTheme" ;
160- if (selectedTheme . equals ( lightTheme ) ) {
155+ String themeName = "DarkTheme" ; // default
156+ if (baseTheme == R . style . LightTheme ) {
161157 themeName = "LightTheme" ;
162- } else if (selectedTheme . equals ( blackTheme ) ) {
158+ } else if (baseTheme == R . style . BlackTheme ) {
163159 themeName = "BlackTheme" ;
164- } else if (selectedTheme .equals (darkTheme )) {
165- themeName = "DarkTheme" ;
166160 }
167161
168162 themeName += "." + service .getServiceInfo ().getName ();
169- final int resourceId = context
170- .getResources ()
163+ final int resourceId = context .getResources ()
171164 .getIdentifier (themeName , "style" , context .getPackageName ());
172165
173166 if (resourceId > 0 ) {
174167 return resourceId ;
175168 }
176-
177- return defaultTheme ;
178- }
179-
180- @ StyleRes
181- public static int getSettingsThemeStyle (final Context context ) {
182- final String lightTheme = context .getResources ().getString (R .string .light_theme_key );
183- final String darkTheme = context .getResources ().getString (R .string .dark_theme_key );
184- final String blackTheme = context .getResources ().getString (R .string .black_theme_key );
185-
186- final String selectedTheme = getSelectedThemeString (context );
187-
188- if (selectedTheme .equals (lightTheme )) {
189- return R .style .LightSettingsTheme ;
190- } else if (selectedTheme .equals (blackTheme )) {
191- return R .style .BlackSettingsTheme ;
192- } else if (selectedTheme .equals (darkTheme )) {
193- return R .style .DarkSettingsTheme ;
194- } else {
195- // Fallback
196- return R .style .DarkSettingsTheme ;
197- }
169+ return baseTheme ;
198170 }
199171
200172 /**
@@ -229,18 +201,27 @@ public static int resolveColorFromAttr(final Context context, @AttrRes final int
229201 return value .data ;
230202 }
231203
232- private static String getSelectedThemeString (final Context context ) {
204+ private static String getSelectedThemeKey (final Context context ) {
233205 final String themeKey = context .getString (R .string .theme_key );
234206 final String defaultTheme = context .getResources ().getString (R .string .default_theme_value );
235207 return PreferenceManager .getDefaultSharedPreferences (context )
236208 .getString (themeKey , defaultTheme );
237209 }
238210
211+ private static String getSelectedNightThemeKey (final Context context ) {
212+ final String nightThemeKey = context .getString (R .string .night_theme_key );
213+ final String defaultNightTheme = context .getResources ()
214+ .getString (R .string .default_night_theme_value );
215+ return PreferenceManager .getDefaultSharedPreferences (context )
216+ .getString (nightThemeKey , defaultNightTheme );
217+ }
218+
239219 /**
240220 * Sets the title to the activity, if the activity is an {@link AppCompatActivity} and has an
241221 * action bar.
222+ *
242223 * @param activity the activity to set the title of
243- * @param title the title to set to the activity
224+ * @param title the title to set to the activity
244225 */
245226 public static void setTitleToAppCompatActivity (@ Nullable final Activity activity ,
246227 final CharSequence title ) {
@@ -251,4 +232,27 @@ public static void setTitleToAppCompatActivity(@Nullable final Activity activity
251232 }
252233 }
253234 }
235+
236+ /**
237+ * Get the device theme
238+ * <p>
239+ * It will return true if the device 's theme is dark, false otherwise.
240+ * <p>
241+ * From https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#java
242+ *
243+ * @param context the context to use
244+ * @return true:dark theme, false:light or unknown
245+ */
246+ public static boolean isDeviceDarkThemeEnabled (final Context context ) {
247+ final int deviceTheme = context .getResources ().getConfiguration ().uiMode
248+ & Configuration .UI_MODE_NIGHT_MASK ;
249+ switch (deviceTheme ) {
250+ case Configuration .UI_MODE_NIGHT_YES :
251+ return true ;
252+ case Configuration .UI_MODE_NIGHT_UNDEFINED :
253+ case Configuration .UI_MODE_NIGHT_NO :
254+ default :
255+ return false ;
256+ }
257+ }
254258}
0 commit comments