15
15
package com .google .androidbrowserhelper .trusted .splashscreens ;
16
16
17
17
import android .annotation .SuppressLint ;
18
+ import android .app .UiModeManager ;
18
19
import android .content .Context ;
19
20
import android .content .SharedPreferences ;
20
21
import android .content .pm .PackageManager ;
22
+ import android .content .res .Configuration ;
21
23
import android .graphics .Bitmap ;
22
24
import android .os .AsyncTask ;
23
25
import android .util .Log ;
@@ -68,6 +70,25 @@ public SplashImageTransferTask(Context context, Bitmap bitmap, String authority,
68
70
mProviderPackage = providerPackage ;
69
71
}
70
72
73
+ /**
74
+ * Get the current theme mode.
75
+ *
76
+ * @param context Context to get the UiModeManager service.
77
+ * @return The current theme mode, either "light" or "dark".
78
+ */
79
+ private String getCurrentThemeMode (Context context ) {
80
+ UiModeManager uiModeManager = (UiModeManager ) context .getSystemService (Context .UI_MODE_SERVICE );
81
+ int nightModeFlags = context .getResources ().getConfiguration ().uiMode & Configuration .UI_MODE_NIGHT_MASK ;
82
+ switch (nightModeFlags ) {
83
+ case Configuration .UI_MODE_NIGHT_YES :
84
+ return "dark" ;
85
+ case Configuration .UI_MODE_NIGHT_NO :
86
+ case Configuration .UI_MODE_NIGHT_UNDEFINED :
87
+ default :
88
+ return "light" ;
89
+ }
90
+ }
91
+
71
92
/**
72
93
* Executes the task. Should be called only once.
73
94
* @param callback {@link Callback} to be called when done.
@@ -104,8 +125,13 @@ protected Boolean doInBackground(Void... args) {
104
125
File file = new File (dir , FILE_NAME );
105
126
SharedPreferences prefs =
106
127
mContext .getSharedPreferences (PREFS_FILE , Context .MODE_PRIVATE );
128
+
107
129
long lastUpdateTime = getLastAppUpdateTime ();
108
- if (file .exists () && lastUpdateTime == prefs .getLong (PREF_LAST_UPDATE_TIME , 0 )) {
130
+ String currentThemeMode = getCurrentThemeMode (mContext );
131
+ long savedLastUpdateTime = prefs .getLong (PREF_LAST_UPDATE_TIME , 0 );
132
+ String savedThemeMode = prefs .getString ("themeMode" , "light" ); // Default to light for compatibility
133
+
134
+ if (file .exists () && lastUpdateTime == savedLastUpdateTime && currentThemeMode .equals (savedThemeMode )) {
109
135
// Don't overwrite existing file, if it was saved later than the last time app was
110
136
// updated
111
137
return transferToCustomTabsProvider (file );
@@ -114,7 +140,10 @@ protected Boolean doInBackground(Void... args) {
114
140
if (isCancelled ()) return false ;
115
141
mBitmap .compress (Bitmap .CompressFormat .PNG , 100 , os );
116
142
os .flush ();
117
- prefs .edit ().putLong (PREF_LAST_UPDATE_TIME , lastUpdateTime ).commit ();
143
+ prefs .edit ()
144
+ .putLong (PREF_LAST_UPDATE_TIME , lastUpdateTime )
145
+ .putString ("themeMode" , currentThemeMode )
146
+ .apply ();
118
147
119
148
if (isCancelled ()) return false ;
120
149
return transferToCustomTabsProvider (file );
0 commit comments