Attempt to stop services when switching users, to conserve memory
@@ -19,8 +19,11 @@ import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
+import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
@@ -47,6 +50,30 @@ public class NotificationService extends Service {
return START_STICKY;
}
+ BroadcastReceiver userForegroundReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ boolean isHidden = U.getSharedPreferences(context).getBoolean("is_hidden", false);
+ if(!isHidden) {
+ startService(new Intent(context, TaskbarService.class));
+ startService(new Intent(context, StartMenuService.class));
+ startService(new Intent(context, DashboardService.class));
+ }
+ }
+ };
+
+ BroadcastReceiver userBackgroundReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ boolean isHidden = U.getSharedPreferences(context).getBoolean("is_hidden", false);
+ if(!isHidden) {
+ stopService(new Intent(context, TaskbarService.class));
+ stopService(new Intent(context, StartMenuService.class));
+ stopService(new Intent(context, DashboardService.class));
+ }
+ }
+ };
+
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate() {
@@ -83,6 +110,9 @@ public class NotificationService extends Service {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID, QuickSettingsTileService.class.getName()));
+
+ registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
+ registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
} else {
pref.edit().putBoolean("taskbar_active", false).apply();
@@ -107,5 +137,8 @@ public class NotificationService extends Service {
}
super.onDestroy();
+
+ unregisterReceiver(userForegroundReceiver);
+ unregisterReceiver(userBackgroundReceiver);
}
}
