Skip to content

Commit

Permalink
Improve restart launcher method
Browse files Browse the repository at this point in the history
  • Loading branch information
divadsn committed Mar 28, 2018
1 parent fc400d6 commit d228973
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
36 changes: 25 additions & 11 deletions app/src/main/java/ch/deletescape/lawnchair/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.UserHandle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -108,6 +109,7 @@
import ch.deletescape.lawnchair.shortcuts.DeepShortcutManager;
import ch.deletescape.lawnchair.shortcuts.ShortcutInfoCompat;
import ch.deletescape.lawnchair.util.IconNormalizer;
import ch.deletescape.lawnchair.util.LooperExecutor;
import ch.deletescape.lawnchair.util.PackageManagerHelper;

/**
Expand All @@ -117,6 +119,7 @@ public final class Utilities {

private static final String TAG = "Launcher.Utilities";
private static final String LAUNCHER_RESTART_KEY = "launcher_restart_key";
private static final int WAIT_BEFORE_RESTART = 250;

private static final Rect sOldBounds = new Rect();
private static final Canvas sCanvas = new Canvas();
Expand Down Expand Up @@ -1178,20 +1181,31 @@ public static boolean checkOutdatedLawnfeed(Context context) {
return false;
}

public static void restartLauncher(Context context) {
PackageManager pm = context.getPackageManager();
Intent startActivity = pm.getLaunchIntentForPackage(context.getPackageName());
public static void restartLauncher(final Context context) {
new LooperExecutor(LauncherModel.getWorkerLooper()).execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(WAIT_BEFORE_RESTART);
} catch (Exception e) {
Log.e("SettingsActivity", "Error waiting", e);
}

startActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)
.setPackage(context.getPackageName())
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Create a pending intent so the application is restarted after System.exit(0) was called.
// We use an AlarmManager to call this intent in 100ms
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, startActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
// Create a pending intent so the application is restarted after Process.killProcess() was called.
// We use an AlarmManager to call this intent in 50ms
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 50, pendingIntent);

// Kill the application
System.exit(0);
// Kill the application
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}

public static int getNumberOfHotseatRows(Context context){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package ch.deletescape.lawnchair.util;

import android.os.Handler;
import android.os.Looper;

import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Extension of {@link AbstractExecutorService} which executed on a provided looper.
*/
public class LooperExecutor extends AbstractExecutorService {

private final Handler mHandler;

public LooperExecutor(Looper looper) {
mHandler = new Handler(looper);
}

@Override
public void execute(Runnable runnable) {
if (mHandler.getLooper() == Looper.myLooper()) {
runnable.run();
} else {
mHandler.post(runnable);
}
}

/**
* Not supported and throws an exception when used.
*/
@Override
@Deprecated
public void shutdown() {
throw new UnsupportedOperationException();
}

/**
* Not supported and throws an exception when used.
*/
@Override
@Deprecated
public List<Runnable> shutdownNow() {
throw new UnsupportedOperationException();
}

@Override
public boolean isShutdown() {
return false;
}

@Override
public boolean isTerminated() {
return false;
}

/**
* Not supported and throws an exception when used.
*/
@Override
@Deprecated
public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
throw new UnsupportedOperationException();
}
}

0 comments on commit d228973

Please sign in to comment.