Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt caching behaviour for dark/light theme switching #463

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package com.google.androidbrowserhelper.trusted.splashscreens;

import android.annotation.SuppressLint;
import android.app.UiModeManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
Expand Down Expand Up @@ -68,6 +70,25 @@ public SplashImageTransferTask(Context context, Bitmap bitmap, String authority,
mProviderPackage = providerPackage;
}

/**
* Get the current theme mode.
*
* @param context Context to get the UiModeManager service.
* @return The current theme mode, either "light" or "dark".
*/
private String getCurrentThemeMode(Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
return "dark";
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
default:
return "light";
}
}

/**
* Executes the task. Should be called only once.
* @param callback {@link Callback} to be called when done.
Expand Down Expand Up @@ -104,8 +125,13 @@ protected Boolean doInBackground(Void... args) {
File file = new File(dir, FILE_NAME);
SharedPreferences prefs =
mContext.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);

long lastUpdateTime = getLastAppUpdateTime();
if (file.exists() && lastUpdateTime == prefs.getLong(PREF_LAST_UPDATE_TIME, 0)) {
String currentThemeMode = getCurrentThemeMode(mContext);
long savedLastUpdateTime = prefs.getLong(PREF_LAST_UPDATE_TIME, 0);
String savedThemeMode = prefs.getString("themeMode", "light"); // Default to light for compatibility
khmyznikov marked this conversation as resolved.
Show resolved Hide resolved

if (file.exists() && lastUpdateTime == savedLastUpdateTime && currentThemeMode.equals(savedThemeMode)) {
// Don't overwrite existing file, if it was saved later than the last time app was
// updated
return transferToCustomTabsProvider(file);
Expand All @@ -114,7 +140,10 @@ protected Boolean doInBackground(Void... args) {
if (isCancelled()) return false;
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
prefs.edit().putLong(PREF_LAST_UPDATE_TIME, lastUpdateTime).commit();
prefs.edit()
.putLong(PREF_LAST_UPDATE_TIME, lastUpdateTime)
.putString("themeMode", currentThemeMode)
.apply();

if (isCancelled()) return false;
return transferToCustomTabsProvider(file);
Expand Down
Loading