Skip to content

Commit

Permalink
fix Live wallpaper mode unable to update wallpaper on Android 12
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoheng committed Jul 4, 2022
1 parent 01454e9 commit 4da714d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
applicationId "me.liaoheng.wallpaper"
versionCode 10948
versionName "1.9.48"
versionCode 10949
versionName "1.9.49"
}

def isSigning = true
Expand Down Expand Up @@ -62,7 +62,7 @@ android {
}
}

def firebaseCore = "com.google.firebase:firebase-analytics:20.0.2"
def firebaseCore = "com.google.firebase:firebase-analytics:21.0.0"

dependencies {
testImplementation deps.robolectric
Expand All @@ -85,7 +85,7 @@ dependencies {
compileOnly firebaseCore
}

implementation 'io.sentry:sentry-android-core:5.7.0'
implementation 'io.sentry:sentry-android-core:6.1.4'
implementation 'org.conscrypt:conscrypt-android:2.5.2'

implementation deps.gson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
Expand Down Expand Up @@ -66,8 +69,6 @@ public class LiveWallpaperService extends WallpaperService {
private LiveWallpaperBroadcastReceiver mReceiver;
private SetWallpaperServiceHelper mServiceHelper;
private CompositeDisposable mLoadWallpaperDisposable;
private HandlerHelper mCheckHandlerHelper;
private Runnable mCheckRunnable;
private final long mCheckPeriodic = Constants.DEF_LIVE_WALLPAPER_CHECK_PERIODIC;

@Override
Expand All @@ -87,7 +88,6 @@ public void onReceive(Context context, Intent intent) {
LogDebugFileUtils.init(getApplicationContext());
} else if (ENABLE_LIVE_WALLPAPER.equals(intent.getAction())) {
boolean enable = intent.getBooleanExtra(EXTRA_ENABLE_LIVE_WALLPAPER, false);
L.alog().d(TAG, "enable :" + enable);
disable();
if (enable) {
enable();
Expand All @@ -96,26 +96,21 @@ public void onReceive(Context context, Intent intent) {
}
}

private final ScheduledThreadPoolExecutor mPoolExecutor = new ScheduledThreadPoolExecutor(1, r -> {
Thread thread = new Thread(r);
thread.setPriority(Thread.MIN_PRIORITY);
return thread;
});

private final Runnable checkRunnable = this::timing;

@Override
public void onCreate() {
super.onCreate();
L.alog().d(TAG, "onCreate");
LogDebugFileUtils.init(this);
mServiceHelper = new SetWallpaperServiceHelper(this, TAG);
mLoadWallpaperDisposable = new CompositeDisposable();
mCheckHandlerHelper = HandlerHelper.create(TAG, Process.THREAD_PRIORITY_FOREGROUND, msg -> {
L.alog().d(TAG, "HandlerHelper");
if (msg.what == 1) {
Boolean enable = (Boolean) msg.obj;
L.alog().d(TAG, "chekc :" + enable);
disable();
if (enable) {
enable();
}
}
return false;
});
mCheckRunnable = this::timing;
mReceiver = new LiveWallpaperBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(UPDATE_LIVE_WALLPAPER);
Expand All @@ -130,38 +125,28 @@ public void onDestroy() {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
Utils.dispose(mLoadWallpaperDisposable);
disable();
if (mCheckHandlerHelper != null) {
mCheckHandlerHelper.release();
}
//mPoolExecutor.shutdown();
Utils.dispose(mLoadWallpaperDisposable);
super.onDestroy();
}

private void postDelayed() {
if (mCheckHandlerHelper == null) {
return;
}
mCheckHandlerHelper.postDelayed(mCheckRunnable, mCheckPeriodic);
}
private ScheduledFuture<?> mScheduledFuture;

public void enable() {
if (mCheckHandlerHelper == null) {
return;
}
disable();
mCheckHandlerHelper.postDelayed(this::timing, 500);
mScheduledFuture = mPoolExecutor.scheduleAtFixedRate(checkRunnable, 500, mCheckPeriodic,
TimeUnit.MILLISECONDS);
}

private void disable() {
if (mCheckHandlerHelper == null) {
if (mScheduledFuture == null) {
return;
}
mCheckHandlerHelper.removeCallbacks(mCheckRunnable);
mScheduledFuture.cancel(true);
}

private void timing() {
postDelayed();
L.alog().i(TAG, "timing check...");
if (Settings.isEnableLogProvider(this)) {
LogDebugFileUtils.get().i(TAG, "Timing check...");
Expand Down Expand Up @@ -343,7 +328,7 @@ public void onCreate(SurfaceHolder surfaceHolder) {
TAG += UUID.randomUUID();
L.alog().d(TAG, "onCreate");
setOffsetNotificationsEnabled(true);
mDrawHandlerHelper = HandlerHelper.create(TAG, Process.THREAD_PRIORITY_FOREGROUND, null);
mDrawHandlerHelper = HandlerHelper.create(TAG, Process.THREAD_PRIORITY_DEFAULT, null);
mActionHandler = new DelayedHandler(Looper.getMainLooper(), msg -> {
if (msg.what == DOWNLOAD_DRAW) {
downloadWallpaper((DownloadBitmap) msg.obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public static void disabled(Context context, boolean force) {
BingWallpaperAlarmManager.disabled(context);
if (force || Settings.getJobType(context) == Settings.LIVE_WALLPAPER) {
try {
WallpaperManager.getInstance(context).clear();
Observable.just("").subscribeOn(Schedulers.io()).map((Function<String, Object>) s -> {
WallpaperManager.getInstance(context).clear();
return "";
}).subscribe();
} catch (Exception ignored) {
}
}
Expand Down Expand Up @@ -107,7 +110,7 @@ public static boolean enableSystem(Context context) {
boolean enabled = WorkerManager.enabled(context, time);
if (enabled) {
Settings.setJobType(context, Settings.WORKER);
if (BingWallpaperUtils.isEnableLog(context)) {
if (Settings.isEnableLog(context)) {
LogDebugFileUtils.get()
.i(TAG, "Enable scheduler interval time : %s", time);
}
Expand All @@ -121,7 +124,7 @@ public static boolean enableTimer(Context context) {
boolean enabled = BingWallpaperAlarmManager.enabled(context, updateTime);
if (enabled) {
Settings.setJobType(context, Settings.TIMER);
if (BingWallpaperUtils.isEnableLog(context)) {
if (Settings.isEnableLog(context)) {
LogDebugFileUtils.get()
.i(TAG, "Enable timer time : %s", updateTime.toString("HH:mm"));
}
Expand Down Expand Up @@ -158,7 +161,7 @@ public static void onActivityResult(Context context, int requestCode, int result
if (requestCode == LIVE_WALLPAPER_REQUEST_CODE) {
if (Activity.RESULT_OK == resultCode) {
Settings.setJobType(context, Settings.LIVE_WALLPAPER);
if (BingWallpaperUtils.isEnableLog(context)) {
if (Settings.isEnableLog(context)) {
LogDebugFileUtils.get()
.i(TAG, "Enable live wallpaper");
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/me/liaoheng/wallpaper/util/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.liaoheng.wallpaper.util;

import android.os.Build;

import androidx.annotation.IntDef;

import org.joda.time.LocalTime;
Expand All @@ -23,7 +25,9 @@ public interface Constants {

long DEF_SCHEDULER_PERIODIC = 3;//hour
String DEF_TIMER_PERIODIC = new LocalTime(0, 35).toString();
long DEF_LIVE_WALLPAPER_CHECK_PERIODIC = TimeUnit.MINUTES.toMillis(35);
long DEF_LIVE_WALLPAPER_CHECK_PERIODIC =
Build.VERSION.SDK_INT > Build.VERSION_CODES.R ? TimeUnit.MINUTES.toMillis(6)
: TimeUnit.MINUTES.toMillis(35);

String LOCAL_BASE_URL = "https://www.bing.com";
String GLOBAL_BASE_URL = "https://global.bing.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import com.github.liaoheng.common.util.LogFileUtils;

import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
* 不带system log
Expand All @@ -24,20 +26,23 @@ public static LogFileUtils get() {
public static void init(Context context) {
if (Settings.isEnableLogProvider(context)) {
create(context);
}else{
} else {
LogFileUtils.get().close();
}
}

public static void create(Context context) {
try{
Observable.just("").subscribeOn(Schedulers.io()).map((Function<String, Object>) s -> {
LogFileUtils.get().open(context, "log", "");
}catch (IOException ignored){
}
return "";
}).subscribe();
}

public static void destroy() {
LogFileUtils.get().close();
LogFileUtils.get().clearFile();
Observable.just("").subscribeOn(Schedulers.io()).map((Function<String, Object>) s -> {
LogFileUtils.get().close();
LogFileUtils.get().clearFile();
return "";
}).subscribe();
}
}
13 changes: 10 additions & 3 deletions app/src/main/java/me/liaoheng/wallpaper/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.IntDef;
import androidx.preference.PreferenceManager;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;

import androidx.annotation.IntDef;
import androidx.preference.PreferenceManager;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.liaoheng.wallpaper.R;
import me.liaoheng.wallpaper.ui.SettingsActivity;

Expand Down Expand Up @@ -257,7 +261,10 @@ public static String getLastWallpaperImageUrl(Context context) {
public static final String BING_WALLPAPER_JOB_TYPE = "bing_wallpaper_job_type";

public static void setJobType(Context context, @JobType int type) {
SettingTrayPreferences.get(context).put(BING_WALLPAPER_JOB_TYPE, type);
Observable.just(type).subscribeOn(Schedulers.io()).map((Function<Integer, Object>) t -> {
SettingTrayPreferences.get(context).put(BING_WALLPAPER_JOB_TYPE, t);
return "";
}).subscribe();
}

@JobType
Expand Down

0 comments on commit 4da714d

Please sign in to comment.