From 0934516a87f8ba8f0c7acab8a45437414568f936 Mon Sep 17 00:00:00 2001 From: Sam Steele Date: Thu, 19 Feb 2015 06:26:59 -0800 Subject: [PATCH] workaround for the hardware menu button on some buggy LG devices --- build.gradle | 2 +- .../android/activity/BaseActivity.java | 43 +++++++++++++++++++ .../android/activity/MainActivity.java | 3 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d5761b781..12c209559 100644 --- a/build.gradle +++ b/build.gradle @@ -56,7 +56,7 @@ android { defaultConfig { versionCode 27 versionName "1.18" - minSdkVersion 10 + minSdkVersion 9 targetSdkVersion 21 applicationId "com.irccloud.android" testApplicationId "com.irccloud.android.test" diff --git a/src/com/irccloud/android/activity/BaseActivity.java b/src/com/irccloud/android/activity/BaseActivity.java index 718e4346a..2c5b56958 100644 --- a/src/com/irccloud/android/activity/BaseActivity.java +++ b/src/com/irccloud/android/activity/BaseActivity.java @@ -20,6 +20,7 @@ import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.*; import android.app.AlertDialog; @@ -36,15 +37,34 @@ import com.irccloud.android.R; import com.irccloud.android.data.ServersDataSource; +import java.lang.reflect.Field; + public class BaseActivity extends ActionBarActivity implements NetworkConnection.IRCEventHandler{ NetworkConnection conn; private View dialogTextPrompt; @Override protected void onCreate(Bundle savedInstanceState) { + if (isMenuWorkaroundRequired()) { + forceOverflowMenu(); + } super.onCreate(savedInstanceState); } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return (keyCode == KeyEvent.KEYCODE_MENU && isMenuWorkaroundRequired()) || super.onKeyDown(keyCode, event); + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_MENU && isMenuWorkaroundRequired()) { + openOptionsMenu(); + return true; + } + return super.onKeyUp(keyCode, event); + } + public View getDialogTextPrompt() { if(dialogTextPrompt == null) dialogTextPrompt = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_textprompt,null); @@ -376,4 +396,27 @@ public void onClick(DialogInterface dialog, int which) { } return super.onOptionsItemSelected(item); } + + //Work around for buggy LG devices, see https://code.google.com/p/android/issues/detail?id=78154 + public static boolean isMenuWorkaroundRequired() { + return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT && + Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1 && + ("LGE".equalsIgnoreCase(Build.MANUFACTURER) || "E6710".equalsIgnoreCase(Build.DEVICE)); + } + + /** + * Modified from: http://stackoverflow.com/a/13098824 + */ + public void forceOverflowMenu() { + try { + ViewConfiguration config = ViewConfiguration.get(this); + Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); + if(menuKeyField != null) { + menuKeyField.setAccessible(true); + menuKeyField.setBoolean(config, false); + } + } catch (IllegalAccessException | NoSuchFieldException e) { + Log.w("IRCCloud", "Failed to force overflow menu."); + } + } } diff --git a/src/com/irccloud/android/activity/MainActivity.java b/src/com/irccloud/android/activity/MainActivity.java index 7db9faa30..bdd422b9d 100644 --- a/src/com/irccloud/android/activity/MainActivity.java +++ b/src/com/irccloud/android/activity/MainActivity.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.reflect.Field; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; @@ -224,7 +225,7 @@ public View getView(int position, View convertView, ViewGroup parent) { private int greyColor = 0; private HashMap pendingEvents = new HashMap(); - + @SuppressLint("NewApi") @SuppressWarnings({ "deprecation", "unchecked" }) @Override