Fixed bug on Android Nougat where long press of the back button didn't work as before

This commit is contained in:
Anthony Restaino 2016-09-16 22:04:04 -04:00
parent ef7fb521ff
commit ef482718ae
2 changed files with 43 additions and 9 deletions

View File

@ -53,6 +53,7 @@ import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener; import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener; import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent; 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.UrlUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils; import acr.browser.lightning.utils.WebUtils;
import acr.browser.lightning.view.Handlers;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.SearchView; import acr.browser.lightning.view.SearchView;
import butterknife.Bind; import butterknife.Bind;
@ -179,6 +181,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private int mIconColor; private int mIconColor;
private int mDisabledIconColor; private int mDisabledIconColor;
private int mCurrentUiColor = Color.BLACK; private int mCurrentUiColor = Color.BLACK;
private long mKeyDownStartTime;
private String mSearchText; private String mSearchText;
private String mUntitledTitle; private String mUntitledTitle;
private String mCameraPhotoPath; private String mCameraPhotoPath;
@ -676,10 +679,21 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
&& (Build.MANUFACTURER.compareTo("LGE") == 0)) { && (Build.MANUFACTURER.compareTo("LGE") == 0)) {
// Workaround for stupid LG devices that crash // Workaround for stupid LG devices that crash
return true; return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
mKeyDownStartTime = System.currentTimeMillis();
Handlers.MAIN.postDelayed(mLongPressBackRunnable, ViewConfiguration.getLongPressTimeout());
} }
return super.onKeyDown(keyCode, event); 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 @Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_MENU) if ((keyCode == KeyEvent.KEYCODE_MENU)
@ -688,6 +702,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Workaround for stupid LG devices that crash // Workaround for stupid LG devices that crash
openOptionsMenu(); openOptionsMenu();
return true; 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); return super.onKeyUp(keyCode, event);
} }
@ -1096,15 +1115,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSuggestionsAdapter.clearCache(); 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 @Override
public void onConfigurationChanged(final Configuration newConfig) { public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);

View File

@ -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());
}