From c4060ef50d36be176bc4975a7bcaf424ae0638c8 Mon Sep 17 00:00:00 2001 From: Chenxia Liu Date: Tue, 24 Sep 2013 17:51:18 -0700 Subject: [PATCH] Bug 910186 - Long-press on search providers in settings only triggers touch up. r=sriram --- mobile/android/base/GeckoPreferences.java | 35 ++++++++++++++++++- .../preferences/SearchEnginePreference.java | 11 +++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/mobile/android/base/GeckoPreferences.java b/mobile/android/base/GeckoPreferences.java index ce5761efd5c..17dd4c65e8c 100644 --- a/mobile/android/base/GeckoPreferences.java +++ b/mobile/android/base/GeckoPreferences.java @@ -8,6 +8,7 @@ package org.mozilla.gecko; import org.mozilla.gecko.background.announcements.AnnouncementsConstants; import org.mozilla.gecko.background.common.GlobalConstants; import org.mozilla.gecko.background.healthreport.HealthReportConstants; +import org.mozilla.gecko.preferences.SearchEnginePreference; import org.mozilla.gecko.util.GeckoEventListener; import org.mozilla.gecko.GeckoPreferenceFragment; import org.mozilla.gecko.util.ThreadUtils; @@ -44,8 +45,11 @@ import android.text.TextWatcher; import android.util.Log; import android.view.MenuItem; import android.view.View; +import android.widget.AdapterView; import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.ListAdapter; +import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; @@ -87,9 +91,15 @@ public class GeckoPreferences @Override protected void onCreate(Bundle savedInstanceState) { - // For fragment-capable devices, display the default fragment if no explicit fragment to show. + + // For Android v11+ where we use Fragments (v11+ only due to bug 866352), + // check that PreferenceActivity.EXTRA_SHOW_FRAGMENT has been set + // (or set it) before super.onCreate() is called so Android can display + // the correct Fragment resource. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && !getIntent().hasExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT)) { + // Set up the default fragment if there is no explicit fragment to show. setupTopLevelFragmentIntent(); } @@ -97,6 +107,10 @@ public class GeckoPreferences // Use setResourceToOpen to specify these extras. Bundle intentExtras = getIntent().getExtras(); + + // For versions of Android lower than Honeycomb, use xml resources instead of + // Fragments because of an Android bug in ActionBar (described in bug 866352 and + // fixed in bug 833625). if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { int res = 0; if (intentExtras != null && intentExtras.containsKey(INTENT_EXTRA_RESOURCES)) { @@ -119,6 +133,25 @@ public class GeckoPreferences registerEventListener("Sanitize:Finished"); + // Add handling for long-press click. + // This is only for Android 3.0 and below (which use the long-press-context-menu paradigm). + final ListView mListView = getListView(); + mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { + // Call long-click handler if it the item implements it. + final ListAdapter listAdapter = ((ListView) parent).getAdapter(); + final Object listItem = listAdapter.getItem(position); + + // Only SearchEnginePreference handles long clicks. + if (listItem instanceof SearchEnginePreference && listItem instanceof View.OnLongClickListener) { + final View.OnLongClickListener longClickListener = (View.OnLongClickListener) listItem; + return longClickListener.onLongClick(view); + } + return false; + } + }); + if (Build.VERSION.SDK_INT >= 14) getActionBar().setHomeButtonEnabled(true); diff --git a/mobile/android/base/preferences/SearchEnginePreference.java b/mobile/android/base/preferences/SearchEnginePreference.java index 181a196b373..9be33c71632 100644 --- a/mobile/android/base/preferences/SearchEnginePreference.java +++ b/mobile/android/base/preferences/SearchEnginePreference.java @@ -16,8 +16,10 @@ import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; + import org.json.JSONException; import org.json.JSONObject; + import org.mozilla.gecko.R; import org.mozilla.gecko.gfx.BitmapUtils; import org.mozilla.gecko.util.ThreadUtils; @@ -26,7 +28,7 @@ import org.mozilla.gecko.widget.FaviconView; /** * Represents an element in the list of search engines on the preferences menu. */ -public class SearchEnginePreference extends Preference { +public class SearchEnginePreference extends Preference implements View.OnLongClickListener { private static final String LOGTAG = "SearchEnginePreference"; // Indices in button array of the AlertDialog of the three buttons. @@ -104,6 +106,13 @@ public class SearchEnginePreference extends Preference { mFaviconView.updateAndScaleImage(mIconBitmap, getTitle().toString()); } + @Override + public boolean onLongClick(View view) { + // Show the preference dialog on long-press. + showDialog(); + return true; + } + /** * Configure this Preference object from the Gecko search engine JSON object. * @param geckoEngineJSON The Gecko-formatted JSON object representing the search engine.