Bug 910186 - Long-press on search providers in settings only triggers touch up. r=sriram
--- 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);