Skip to content

Commit

Permalink
Merge pull request #92 from Mahmud0808/master
Browse files Browse the repository at this point in the history
Merge master into fdroid
  • Loading branch information
Mahmud0808 authored Jun 7, 2024
2 parents e6e2302 + de59267 commit be259ce
Show file tree
Hide file tree
Showing 71 changed files with 1,376 additions and 1,700 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ local.properties
/app/release/
/*.hprof
/.idea/deploymentTargetDropDown.xml
/.kotlin/sessions/*.salive
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# ColorBlendr

### An application to modify material you colors of your device.
### Customize Material You colors of your device.
</div>
<p align="center">
Elevate your creativity with effortless material customization. Instantly tweak colors for a personalized touch in just a few taps.
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdk = 31
targetSdk = 34
versionCode = 12
versionName = "v1.4"
versionCode = 13
versionName = "v1.5"
}

buildTypes {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,20 @@
</receiver>

<service
android:name=".service.BackgroundService"
android:name=".service.ScheduledJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />

<service
android:name=".service.AutoStartService"
android:enabled="true"
android:foregroundServiceType="specialUse" />

<receiver
android:name=".service.RestartBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:foregroundServiceType="specialUse"
tools:ignore="ExportedService" />
android:label="RestartServiceWhenStopped"
tools:ignore="ExportedReceiver" />

<provider
android:name=".provider.RemotePrefProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ public class Const {

public static Set<String> EXCLUDED_PREFS_FROM_BACKUP = new HashSet<>(
Arrays.asList(
FIRST_RUN,
PREF_WORKING_METHOD,
MONET_LAST_UPDATED
MONET_LAST_UPDATED,
THEMING_ENABLED,
SHIZUKU_THEMING_ENABLED,
WALLPAPER_COLOR_LIST
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static android.content.Context.MODE_PRIVATE;
import static com.drdisagree.colorblendr.common.Const.EXCLUDED_PREFS_FROM_BACKUP;
import static com.drdisagree.colorblendr.common.Const.THEMING_ENABLED;

import android.content.SharedPreferences;
import android.util.Log;
Expand Down Expand Up @@ -133,6 +134,10 @@ public static void restorePrefs(final @NonNull InputStream inputStream) {

// Retrieve excluded prefs from current prefs
Map<String, Object> excludedPrefs = new HashMap<>();

// Restoring config will enable theming service
excludedPrefs.put(THEMING_ENABLED, true);

for (String excludedPref : EXCLUDED_PREFS_FROM_BACKUP) {
Object prefValue = prefs.getAll().get(excludedPref);
if (prefValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static JSONObject getThemeCustomizationOverlayPackages() {
ColorUtil.intToHexColorNoHash(RPrefs.getInt(MONET_SEED_COLOR, Color.BLUE))
);
}
object.putOpt(APPLIED_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
object.putOpt(APPLIED_TIMESTAMP, System.currentTimeMillis());
} catch (Exception e) {
Log.e(TAG, "getThemeCustomizationOverlayPackages:", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.drdisagree.colorblendr.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -15,7 +16,9 @@

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.drdisagree.colorblendr.BuildConfig;
import com.drdisagree.colorblendr.ColorBlendr;
import com.drdisagree.colorblendr.R;
import com.drdisagree.colorblendr.common.Const;
Expand All @@ -26,20 +29,36 @@
import com.drdisagree.colorblendr.utils.ShizukuUtil;
import com.drdisagree.colorblendr.utils.SystemUtil;

public class BackgroundService extends Service {
import java.util.Timer;
import java.util.TimerTask;

private static final String TAG = BackgroundService.class.getSimpleName();
public class AutoStartService extends Service {

private static final String TAG = AutoStartService.class.getSimpleName();
private static boolean isRunning = false;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "Background Service";
private static BroadcastListener myReceiver;
private NotificationManager notificationManager;

public BackgroundService() {
// for testing background running service
private final boolean TEST_BACKGROUND_SERVICE = false;
private final boolean debugging = BuildConfig.DEBUG && TEST_BACKGROUND_SERVICE;
public int counter = 0;
private Timer timer;
private static final String packageName = ColorBlendr.getAppContext().getPackageName();
public static final String ACTION_FOO = packageName + ".FOO";
public static final String EXTRA_PARAM_A = packageName + ".PARAM_A";

public AutoStartService() {
isRunning = false;
myReceiver = new BroadcastListener();
}

public static boolean isServiceNotRunning() {
return !isRunning;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Expand All @@ -51,11 +70,12 @@ public void onCreate() {
super.onCreate();

if (notificationManager == null) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager = getSystemService(NotificationManager.class);
}

isRunning = true;
createNotificationChannel();
showNotification();
registerReceivers();

if (BroadcastListener.lastOrientation == -1) {
Expand All @@ -65,12 +85,50 @@ public void onCreate() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification();
super.onStartCommand(intent, flags, startId);

if (debugging) {
// for testing background running service
startTimer(this);
}

setupSystemUIRestartListener();

return START_STICKY;
}

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

isRunning = false;
Log.i(TAG, "onDestroy: Service is destroyed :(");

try {
unregisterReceiver(myReceiver);
} catch (Exception ignored) {
// Receiver was probably never registered
}

Intent broadcastIntent = new Intent(this, RestartBroadcastReceiver.class);
sendBroadcast(broadcastIntent);

if (debugging) {
// for testing background running service
stopTimerTask();
}
}

private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
getString(R.string.background_service_notification_channel_title),
NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription(getString(R.string.background_service_notification_channel_text));
notificationManager.createNotificationChannel(channel);
}

private void showNotification() {
Intent notificationIntent = new Intent();
notificationIntent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
Expand All @@ -85,7 +143,7 @@ private void showNotification() {
PendingIntent.FLAG_IMMUTABLE
);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
Notification notification = new NotificationCompat.Builder(
this,
NOTIFICATION_CHANNEL_ID
)
Expand All @@ -95,12 +153,13 @@ private void showNotification() {
.setContentText(getString(R.string.background_service_notification_text))
.setContentIntent(pendingIntent)
.setSound(null, AudioManager.STREAM_NOTIFICATION)
.setColor(ColorUtil.getAccentColor(this));
.setColor(ColorUtil.getAccentColor(this))
.build();

notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
startForeground(NOTIFICATION_ID, notification);
}

@SuppressWarnings("deprecation")
@SuppressWarnings("all")
private void registerReceivers() {
IntentFilter intentFilterWithoutScheme = new IntentFilter();
intentFilterWithoutScheme.addAction(Intent.ACTION_WALLPAPER_CHANGED);
Expand All @@ -121,16 +180,6 @@ private void registerReceivers() {
registerReceiver(myReceiver, intentFilterWithScheme);
}

public void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
getString(R.string.background_service_notification_channel_title),
NotificationManager.IMPORTANCE_LOW
);
channel.setDescription(getString(R.string.background_service_notification_channel_text));
notificationManager.createNotificationChannel(channel);
}

private void setupSystemUIRestartListener() {
if (Const.getWorkingMethod() == Const.WORK_METHOD.ROOT &&
RootConnectionProvider.isNotConnected()
Expand All @@ -139,7 +188,7 @@ private void setupSystemUIRestartListener() {
.runOnSuccess(new MethodInterface() {
@Override
public void run() {
setupSysUIRestartListener();
initSystemUIRestartListener();
}
})
.run();
Expand All @@ -153,34 +202,40 @@ public void run() {
ShizukuConnectionProvider.serviceConnection
);
} else if (Const.getWorkingMethod() == Const.WORK_METHOD.ROOT) {
setupSysUIRestartListener();
initSystemUIRestartListener();
}
}

private void setupSysUIRestartListener() {
private void initSystemUIRestartListener() {
try {
ColorBlendr.getRootConnection().setSystemUIRestartListener();
} catch (RemoteException e) {
Log.e(TAG, "Failed to set SystemUI restart listener", e);
}
}

public static boolean isServiceNotRunning() {
return !isRunning;
public void startTimer(Context context) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG, "Timer is running " + counter++);
broadcastActionTest(context, String.valueOf(counter));
}
}, 1000, 1000);
}

@Override
public void onDestroy() {
isRunning = false;
stopForeground(true);
try {
unregisterReceiver(myReceiver);
} catch (Exception ignored) {
// Receiver was probably never registered
}
Intent broadcastIntent = new Intent(this, ServiceDestroyedListener.class);
sendBroadcast(broadcastIntent);
public static void broadcastActionTest(Context context, String param) {
Intent intent = new Intent(ACTION_FOO);
intent.putExtra(EXTRA_PARAM_A, param);
LocalBroadcastManager bm = LocalBroadcastManager.getInstance(context);
bm.sendBroadcast(intent);
}

super.onDestroy();
public void stopTimerTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.drdisagree.colorblendr.ColorBlendr;
import com.drdisagree.colorblendr.common.Const;
import com.drdisagree.colorblendr.config.RPrefs;
import com.drdisagree.colorblendr.extension.MethodInterface;
import com.drdisagree.colorblendr.provider.RootConnectionProvider;
import com.drdisagree.colorblendr.utils.AppUtil;
import com.drdisagree.colorblendr.utils.OverlayManager;
import com.drdisagree.colorblendr.utils.SystemUtil;
import com.drdisagree.colorblendr.utils.WallpaperUtil;
import com.drdisagree.colorblendr.utils.WallpaperColorUtil;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -52,10 +51,8 @@ public void onReceive(Context context, Intent intent) {
Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())
) {
// Start background service on boot
if (AppUtil.permissionsGranted(context)) {
if (BackgroundService.isServiceNotRunning()) {
context.startService(new Intent(ColorBlendr.getAppContext(), BackgroundService.class));
}
if (AppUtil.permissionsGranted(context) && AutoStartService.isServiceNotRunning()) {
context.startForegroundService(new Intent(context, AutoStartService.class));
}

validateRootAndUpdateColors(context, new MethodInterface() {
Expand All @@ -72,7 +69,7 @@ public void run() {
if (Intent.ACTION_WALLPAPER_CHANGED.equals(intent.getAction()) &&
AppUtil.permissionsGranted(context)
) {
ArrayList<Integer> wallpaperColors = WallpaperUtil.getWallpaperColors(context);
ArrayList<Integer> wallpaperColors = WallpaperColorUtil.getWallpaperColors(context);
RPrefs.putString(WALLPAPER_COLOR_LIST, Const.GSON.toJson(wallpaperColors));

if (!RPrefs.getBoolean(MONET_SEED_COLOR_ENABLED, false)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.drdisagree.colorblendr.service;

import static android.content.Context.JOB_SCHEDULER_SERVICE;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class RestartBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = RestartBroadcastReceiver.class.getSimpleName();
private static JobScheduler jobScheduler;

@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Service Stopped, but this is a never ending service.");
scheduleJob(context);
}

public static void scheduleJob(Context context) {
if (jobScheduler == null) {
jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
}

ComponentName componentName = new ComponentName(context, ScheduledJobService.class);

JobInfo jobInfo = new JobInfo.Builder(1, componentName)
.setOverrideDeadline(0)
.setPersisted(true).build();

jobScheduler.schedule(jobInfo);
}
}
Loading

0 comments on commit be259ce

Please sign in to comment.