Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6e51ae6c authored by Aurélien Pomini's avatar Aurélien Pomini Committed by Automerger Merge Worker
Browse files

Merge "Delay tryToRebind if application exit reason is LMK" am: 20d546c0 am:...

Merge "Delay tryToRebind if application exit reason is LMK" am: 20d546c0 am: 3bb836d9 am: 42359035 am: 17d3c0c4 am: 6e959c15 am: 3de89d2f

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2398974



Change-Id: I48fdae2ac57862d725776efdcf5315539f8ea1e2
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 8c969e8d 3de89d2f
Loading
Loading
Loading
Loading
+58 −13
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.ApplicationExitInfo;
import android.app.ILocalWallpaperColorConsumer;
import android.app.IWallpaperManager;
import android.app.IWallpaperManagerCallback;
@@ -187,6 +188,10 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
    /** Tracks wallpaper being migrated from system+lock to lock when setting static wp. */
    WallpaperDestinationChangeHandler mPendingMigrationViaStatic;

    private static final double LMK_LOW_THRESHOLD_MEMORY_PERCENTAGE = 10;
    private static final int LMK_RECONNECT_REBIND_RETRIES = 3;
    private static final long LMK_RECONNECT_DELAY_MS = 5000;

    /**
     * Minimum time between crashes of a wallpaper service for us to consider
     * restarting it vs. just reverting to the static wallpaper.
@@ -988,6 +993,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        /** Time in milliseconds until we expect the wallpaper to reconnect (unless we're in the
         *  middle of an update). If exceeded, the wallpaper gets reset to the system default. */
        private static final long WALLPAPER_RECONNECT_TIMEOUT_MS = 10000;
        private int mLmkLimitRebindRetries = LMK_RECONNECT_REBIND_RETRIES;

        final WallpaperInfo mInfo;
        IWallpaperService mService;
@@ -1209,6 +1215,36 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                            && mWallpaper.userId == mCurrentUserId
                            && !Objects.equals(mDefaultWallpaperComponent, wpService)
                            && !Objects.equals(mImageWallpaper, wpService)) {
                        List<ApplicationExitInfo> reasonList =
                                mActivityManager.getHistoricalProcessExitReasons(
                                wpService.getPackageName(), 0, 1);
                        int exitReason = ApplicationExitInfo.REASON_UNKNOWN;
                        if (reasonList != null && !reasonList.isEmpty()) {
                            ApplicationExitInfo info = reasonList.get(0);
                            exitReason = info.getReason();
                        }
                        Slog.d(TAG, "exitReason: " + exitReason);
                        // If exit reason is LOW_MEMORY_KILLER
                        // delay the mTryToRebindRunnable for 10s
                        if (exitReason == ApplicationExitInfo.REASON_LOW_MEMORY) {
                            if (isRunningOnLowMemory()) {
                                Slog.i(TAG, "Rebind is delayed due to lmk");
                                mContext.getMainThreadHandler().postDelayed(mTryToRebindRunnable,
                                        LMK_RECONNECT_DELAY_MS);
                                mLmkLimitRebindRetries = LMK_RECONNECT_REBIND_RETRIES;
                            } else {
                                if (mLmkLimitRebindRetries <= 0) {
                                    Slog.w(TAG, "Reverting to built-in wallpaper due to lmk!");
                                    clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId,
                                            null);
                                    mLmkLimitRebindRetries = LMK_RECONNECT_REBIND_RETRIES;
                                    return;
                                }
                                mLmkLimitRebindRetries--;
                                mContext.getMainThreadHandler().postDelayed(mTryToRebindRunnable,
                                        LMK_RECONNECT_DELAY_MS);
                            }
                        } else {
                            // There is a race condition which causes
                            // {@link #mWallpaper.wallpaperUpdating} to be false even if it is
                            // currently updating since the broadcast notifying us is async.
@@ -1225,6 +1261,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                                tryToRebind();
                            }
                        }
                    }
                } else {
                    if (DEBUG_LIVE) {
                        Slog.i(TAG, "Wallpaper changed during disconnect tracking; ignoring");
@@ -1233,6 +1270,14 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            }
        };

        private boolean isRunningOnLowMemory() {
            ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            mActivityManager.getMemoryInfo(memoryInfo);
            double availableMBsInPercentage = memoryInfo.availMem / (double)memoryInfo.totalMem *
                    100.0;
            return availableMBsInPercentage < LMK_LOW_THRESHOLD_MEMORY_PERCENTAGE;
        }

        /**
         * Called by a live wallpaper if its colors have changed.
         * @param primaryColors representation of wallpaper primary colors