Change MaterialYouService to foreground service.

This commit is contained in:
Alex Ning 2021-08-04 18:03:39 +08:00
parent 51f6a9613f
commit da7abe1959
6 changed files with 72 additions and 15 deletions

View File

@ -4,6 +4,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.content.ContextCompat;
import ml.docilealligator.infinityforreddit.services.MaterialYouService;
public class WallpaperChangeReceiver extends BroadcastReceiver {
@ -11,6 +13,6 @@ public class WallpaperChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent materialYouIntent = new Intent(context, MaterialYouService.class);
context.startService(materialYouIntent);
ContextCompat.startForegroundService(context, materialYouIntent);
}
}

View File

@ -1,9 +1,18 @@
package ml.docilealligator.infinityforreddit.services;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.concurrent.Executor;
@ -11,12 +20,14 @@ import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils;
import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class MaterialYouService extends IntentService {
public class MaterialYouService extends Service {
@Inject
@Named("default")
@ -38,16 +49,44 @@ public class MaterialYouService extends IntentService {
Executor executor;
public MaterialYouService() {
super("MaterialYouService");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
NotificationUtils.CHANNEL_ID_MATERIAL_YOU,
NotificationUtils.CHANNEL_MATERIAL_YOU,
NotificationManager.IMPORTANCE_LOW
);
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.createNotificationChannel(serviceChannel);
}
Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_MATERIAL_YOU)
.setContentTitle(getString(R.string.material_you_notification_title))
.setContentText(getString(R.string.please_wait))
.setSmallIcon(R.drawable.ic_notification)
.setColor(customThemeWrapper.getColorPrimaryLightTheme())
.build();
startForeground(NotificationUtils.MATERIAL_YOU_NOTIFICATION_ID, notification);
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) {
MaterialYouUtils.changeTheme(this, executor, new Handler(), redditDataRoomDatabase,
customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences,
amoledThemeSharedPreferences);
amoledThemeSharedPreferences, () -> {
stopForeground(true);
stopSelf();
});
}
return START_NOT_STICKY;
}
}

View File

@ -1,5 +1,10 @@
package ml.docilealligator.infinityforreddit.settings;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@ -38,11 +43,6 @@ import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUt
import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
/**
* A simple {@link Fragment} subclass.
*/
@ -182,7 +182,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
MaterialYouUtils.changeTheme(activity, executor, new Handler(),
redditDataRoomDatabase, customThemeWrapper,
lightThemeSharedPreferences, darkThemeSharedPreferences,
amoledThemeSharedPreferences);
amoledThemeSharedPreferences, null);
applyMaterialYouPreference.setVisible(true);
} else {
applyMaterialYouPreference.setVisible(false);
@ -194,7 +194,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
MaterialYouUtils.changeTheme(activity, executor, new Handler(),
redditDataRoomDatabase, customThemeWrapper,
lightThemeSharedPreferences, darkThemeSharedPreferences,
amoledThemeSharedPreferences);
amoledThemeSharedPreferences, null);
return true;
});
}

View File

@ -9,6 +9,7 @@ import android.os.Build;
import android.os.Handler;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import org.greenrobot.eventbus.EventBus;
@ -44,7 +45,8 @@ public class MaterialYouUtils {
CustomThemeWrapper customThemeWrapper,
SharedPreferences lightThemeSharedPreferences,
SharedPreferences darkThemeSharedPreferences,
SharedPreferences amoledThemeSharedPreferences) {
SharedPreferences amoledThemeSharedPreferences,
@Nullable MaterialYouListener materialYouListener) {
executor.execute(() -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
@ -116,7 +118,12 @@ public class MaterialYouUtils {
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences);
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences);
handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent()));
handler.post(() -> {
if (materialYouListener != null) {
materialYouListener.applied();
}
EventBus.getDefault().post(new RecreateActivityEvent());
});
}
}
});
@ -141,4 +148,8 @@ public class MaterialYouUtils {
double luminance = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
return luminance < 0.5 ? Color.BLACK : Color.WHITE;
}
public interface MaterialYouListener {
void applied();
}
}

View File

@ -22,11 +22,14 @@ public class NotificationUtils {
public static final String CHANNEL_DOWNLOAD_IMAGE = "Download Image";
public static final String CHANNEL_ID_DOWNLOAD_GIF = "download_gif";
public static final String CHANNEL_DOWNLOAD_GIF = "Download Gif";
public static final String CHANNEL_ID_MATERIAL_YOU = "material_you";
public static final String CHANNEL_MATERIAL_YOU = "Material You";
public static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000;
public static final int DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID = 20000;
public static final int DOWNLOAD_VIDEO_NOTIFICATION_ID = 30000;
public static final int DOWNLOAD_IMAGE_NOTIFICATION_ID = 40000;
public static final int DOWNLOAD_GIF_NOTIFICATION_ID = 50000;
public static final int MATERIAL_YOU_NOTIFICATION_ID = 60000;
private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0;
private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1;

View File

@ -1162,4 +1162,6 @@
<string name="error_loading_wiki">Error loading wiki</string>
<string name="no_wiki">This subreddit has no wiki page</string>
<string name="material_you_notification_title">Applying Material You</string>
</resources>