Bug 910186 - Long-press on search providers in settings only triggers touch up. r=sriram
authorChenxia Liu <liuche@mozilla.com>
Tue, 24 Sep 2013 17:51:18 -0700
changeset 148404 53a5f19728e5464b0ac58112a5a50633fcd5d7a6
parent 148403 47b85389c55c5e9e022a8e56de420201d2ea99dd
child 148549 e3be443ea09325e1655a7a9a9fd2e360c31b56d6
push id2822
push usercliu@mozilla.com
push dateWed, 25 Sep 2013 00:53:00 +0000
treeherderfx-team@53a5f19728e5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssriram
bugs910186
milestone27.0a1
Bug 910186 - Long-press on search providers in settings only triggers touch up. r=sriram
mobile/android/base/GeckoPreferences.java
mobile/android/base/preferences/SearchEnginePreference.java
--- a/mobile/android/base/GeckoPreferences.java
+++ b/mobile/android/base/GeckoPreferences.java
@@ -3,16 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 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;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
 
 import android.app.Activity;
@@ -39,18 +40,21 @@ import android.preference.PreferenceScre
 import android.preference.TwoStatePreference;
 import android.text.Editable;
 import android.text.InputType;
 import android.text.TextUtils;
 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;
 import java.util.List;
 
 public class GeckoPreferences
     extends PreferenceActivity
     implements OnPreferenceChangeListener, GeckoEventListener, GeckoActivityStatus
@@ -82,26 +86,36 @@ public class GeckoPreferences
     public static String PREFS_RESTORE_SESSION = NON_PREF_PREFIX + "restoreSession3";
 
     // These values are chosen to be distinct from other Activity constants.
     private static int REQUEST_CODE_PREF_SCREEN = 5;
     private static int RESULT_CODE_EXIT_SETTINGS = 6;
 
     @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();
         }
 
         super.onCreate(savedInstanceState);
 
         // 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)) {
                 // Fetch resource id from intent.
                 String resourceName = intentExtras.getString(INTENT_EXTRA_RESOURCES);
                 if (resourceName != null) {
                     res = getResources().getIdentifier(resourceName, "xml", getPackageName());
                     if (res == 0) {
@@ -114,16 +128,35 @@ public class GeckoPreferences
                 Log.e(LOGTAG, "Displaying default settings.");
                 res = R.xml.preferences;
             }
             addPreferencesFromResource(res);
         }
 
         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);
 
         // If launched from notification, explicitly cancel the notification.
         if (intentExtras != null && intentExtras.containsKey(DataReportingNotification.ALERT_NAME_DATAREPORTING_NOTIFICATION)) {
             NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
             notificationManager.cancel(DataReportingNotification.ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode());
         }
--- a/mobile/android/base/preferences/SearchEnginePreference.java
+++ b/mobile/android/base/preferences/SearchEnginePreference.java
@@ -11,27 +11,29 @@ import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.preference.Preference;
 import android.text.SpannableString;
 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;
 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.
     public static final int INDEX_SET_DEFAULT_BUTTON = 0;
     public static final int INDEX_REMOVE_BUTTON = 1;
 
     // Cache label to avoid repeated use of the resource system.
     public final String LABEL_IS_DEFAULT;
@@ -99,16 +101,23 @@ public class SearchEnginePreference exte
     @Override
     protected void onBindView(View view) {
         super.onBindView(view);
         // Set the icon in the FaviconView.
         mFaviconView = ((FaviconView) view.findViewById(R.id.search_engine_icon));
         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.
      * @throws JSONException If the JSONObject is invalid.
      */
     public void setSearchEngineFromJSON(JSONObject geckoEngineJSON) throws JSONException {
         final String engineName = geckoEngineJSON.getString("name");
         final SpannableString titleSpannable = new SpannableString(engineName);