diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index c3b2eeb..8f9c48e 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -53,6 +53,7 @@ import android.view.View.OnFocusChangeListener; import android.view.View.OnKeyListener; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; +import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewParent; @@ -120,6 +121,7 @@ import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.UrlUtils; import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.WebUtils; +import acr.browser.lightning.view.Handlers; import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.SearchView; import butterknife.Bind; @@ -179,6 +181,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private int mIconColor; private int mDisabledIconColor; private int mCurrentUiColor = Color.BLACK; + private long mKeyDownStartTime; private String mSearchText; private String mUntitledTitle; private String mCameraPhotoPath; @@ -676,10 +679,21 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements && (Build.MANUFACTURER.compareTo("LGE") == 0)) { // Workaround for stupid LG devices that crash return true; + } else if (keyCode == KeyEvent.KEYCODE_BACK) { + mKeyDownStartTime = System.currentTimeMillis(); + Handlers.MAIN.postDelayed(mLongPressBackRunnable, ViewConfiguration.getLongPressTimeout()); } return super.onKeyDown(keyCode, event); } + private final Runnable mLongPressBackRunnable = new Runnable() { + @Override + public void run() { + final LightningView currentTab = mTabsManager.getCurrentTab(); + showCloseDialog(mTabsManager.positionOf(currentTab)); + } + }; + @Override public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_MENU) @@ -688,6 +702,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements // Workaround for stupid LG devices that crash openOptionsMenu(); return true; + } else if (keyCode == KeyEvent.KEYCODE_BACK) { + Handlers.MAIN.removeCallbacks(mLongPressBackRunnable); + if ((System.currentTimeMillis() - mKeyDownStartTime) > ViewConfiguration.getLongPressTimeout()) { + return true; + } } return super.onKeyUp(keyCode, event); } @@ -1096,15 +1115,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mSuggestionsAdapter.clearCache(); } - @Override - public boolean onKeyLongPress(int keyCode, KeyEvent event) { - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (keyCode == KeyEvent.KEYCODE_BACK) { - showCloseDialog(mTabsManager.positionOf(currentTab)); - } - return true; - } - @Override public void onConfigurationChanged(final Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/app/src/main/java/acr/browser/lightning/view/Handlers.java b/app/src/main/java/acr/browser/lightning/view/Handlers.java new file mode 100644 index 0000000..1572b39 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/view/Handlers.java @@ -0,0 +1,24 @@ +package acr.browser.lightning.view; + +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.NonNull; + +/** + * Simple holder for useful handlers that + * will live for the duration of the app. + */ +public final class Handlers { + + private Handlers() {} + + static { + if (Looper.getMainLooper() == null) { + Looper.prepareMainLooper(); + } + } + + @NonNull + public static final Handler MAIN = new Handler(Looper.getMainLooper()); + +}