Skip to content

Commit

Permalink
add BroadcastReceiver to listen to changes in battery save mode and r…
Browse files Browse the repository at this point in the history
…eload the activity if received
  • Loading branch information
seelchen committed Nov 18, 2023
1 parent bf312a3 commit 41533d4
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.amaze.filemanager.ui.activities.superclasses;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_COLORED_NAVIGATION;

import com.amaze.filemanager.R;
Expand All @@ -34,10 +35,15 @@
import com.readystatesoftware.systembartint.SystemBarTintManager;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
Expand All @@ -53,11 +59,20 @@
/** Created by arpitkh996 on 03-03-2016. */
public class ThemedActivity extends PreferenceActivity {
private int uiModeNight = -1;
private final BroadcastReceiver powerModeReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent i) {
recreate();
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

registerPowerModeReceiver();

// setting window background color instead of each item, in order to reduce pixel overdraw
if (getAppTheme().equals(AppTheme.LIGHT)) {
getWindow().setBackgroundDrawableResource(android.R.color.white);
Expand Down Expand Up @@ -309,4 +324,24 @@ protected void onResume() {
uiModeNight = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
setTheme();
}

@Override
protected void onDestroy() {
super.onDestroy();

unregisterPowerModeReceiver();
}

private void registerPowerModeReceiver() {
if (SDK_INT >= LOLLIPOP) {
registerReceiver(
powerModeReceiver, new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
}
}

private void unregisterPowerModeReceiver() {
if (SDK_INT >= LOLLIPOP) {
unregisterReceiver(powerModeReceiver);
}
}
}

0 comments on commit 41533d4

Please sign in to comment.