Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.preferences; |
michael@0 | 6 | |
michael@0 | 7 | import org.json.JSONException; |
michael@0 | 8 | import org.json.JSONObject; |
michael@0 | 9 | import org.mozilla.gecko.R; |
michael@0 | 10 | import org.mozilla.gecko.favicons.Favicons; |
michael@0 | 11 | import org.mozilla.gecko.favicons.decoders.FaviconDecoder; |
michael@0 | 12 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 13 | import org.mozilla.gecko.widget.FaviconView; |
michael@0 | 14 | |
michael@0 | 15 | import android.app.AlertDialog; |
michael@0 | 16 | import android.content.Context; |
michael@0 | 17 | import android.graphics.Bitmap; |
michael@0 | 18 | import android.graphics.drawable.BitmapDrawable; |
michael@0 | 19 | import android.text.SpannableString; |
michael@0 | 20 | import android.util.Log; |
michael@0 | 21 | import android.view.View; |
michael@0 | 22 | import android.widget.Toast; |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Represents an element in the list of search engines on the preferences menu. |
michael@0 | 26 | */ |
michael@0 | 27 | public class SearchEnginePreference extends CustomListPreference { |
michael@0 | 28 | protected String LOGTAG = "SearchEnginePreference"; |
michael@0 | 29 | |
michael@0 | 30 | protected static final int INDEX_REMOVE_BUTTON = 1; |
michael@0 | 31 | |
michael@0 | 32 | // The icon to display in the prompt when clicked. |
michael@0 | 33 | private BitmapDrawable mPromptIcon; |
michael@0 | 34 | |
michael@0 | 35 | // The bitmap backing the drawable above - needed separately for the FaviconView. |
michael@0 | 36 | private Bitmap mIconBitmap; |
michael@0 | 37 | |
michael@0 | 38 | private FaviconView mFaviconView; |
michael@0 | 39 | |
michael@0 | 40 | public SearchEnginePreference(Context context, SearchPreferenceCategory parentCategory) { |
michael@0 | 41 | super(context, parentCategory); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Called by Android when we're bound to the custom view. Allows us to set the custom properties |
michael@0 | 46 | * of our custom view elements as we desire (We can now use findViewById on them). |
michael@0 | 47 | * |
michael@0 | 48 | * @param view The view instance for this Preference object. |
michael@0 | 49 | */ |
michael@0 | 50 | @Override |
michael@0 | 51 | protected void onBindView(View view) { |
michael@0 | 52 | super.onBindView(view); |
michael@0 | 53 | |
michael@0 | 54 | // Set the icon in the FaviconView. |
michael@0 | 55 | mFaviconView = ((FaviconView) view.findViewById(R.id.search_engine_icon)); |
michael@0 | 56 | mFaviconView.updateAndScaleImage(mIconBitmap, getTitle().toString()); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | @Override |
michael@0 | 60 | protected int getPreferenceLayoutResource() { |
michael@0 | 61 | return R.layout.preference_search_engine; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Returns the strings to be displayed in the dialog. |
michael@0 | 66 | */ |
michael@0 | 67 | @Override |
michael@0 | 68 | protected String[] createDialogItems() { |
michael@0 | 69 | return new String[] { LABEL_SET_AS_DEFAULT, |
michael@0 | 70 | LABEL_REMOVE }; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | @Override |
michael@0 | 74 | public void showDialog() { |
michael@0 | 75 | // If this is the last engine, then we are the default, and none of the options |
michael@0 | 76 | // on this menu can do anything. |
michael@0 | 77 | if (mParentCategory.getPreferenceCount() == 1) { |
michael@0 | 78 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 79 | @Override |
michael@0 | 80 | public void run() { |
michael@0 | 81 | Toast.makeText(getContext(), R.string.pref_search_last_toast, Toast.LENGTH_SHORT).show(); |
michael@0 | 82 | } |
michael@0 | 83 | }); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | super.showDialog(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @Override |
michael@0 | 91 | protected void configureDialogBuilder(AlertDialog.Builder builder) { |
michael@0 | 92 | // Copy the icon from this object to the prompt we produce. We lazily create the drawable, |
michael@0 | 93 | // as the user may not ever actually tap this object. |
michael@0 | 94 | if (mPromptIcon == null && mIconBitmap != null) { |
michael@0 | 95 | mPromptIcon = new BitmapDrawable(getContext().getResources(), mFaviconView.getBitmap()); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | builder.setIcon(mPromptIcon); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | @Override |
michael@0 | 102 | protected void onDialogIndexClicked(int index) { |
michael@0 | 103 | switch (index) { |
michael@0 | 104 | case INDEX_SET_DEFAULT_BUTTON: |
michael@0 | 105 | mParentCategory.setDefault(this); |
michael@0 | 106 | break; |
michael@0 | 107 | |
michael@0 | 108 | case INDEX_REMOVE_BUTTON: |
michael@0 | 109 | mParentCategory.uninstall(this); |
michael@0 | 110 | break; |
michael@0 | 111 | |
michael@0 | 112 | default: |
michael@0 | 113 | Log.w(LOGTAG, "Selected index out of range."); |
michael@0 | 114 | break; |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Configure this Preference object from the Gecko search engine JSON object. |
michael@0 | 120 | * @param geckoEngineJSON The Gecko-formatted JSON object representing the search engine. |
michael@0 | 121 | * @throws JSONException If the JSONObject is invalid. |
michael@0 | 122 | */ |
michael@0 | 123 | public void setSearchEngineFromJSON(JSONObject geckoEngineJSON) throws JSONException { |
michael@0 | 124 | final String engineName = geckoEngineJSON.getString("name"); |
michael@0 | 125 | final SpannableString titleSpannable = new SpannableString(engineName); |
michael@0 | 126 | |
michael@0 | 127 | setTitle(titleSpannable); |
michael@0 | 128 | |
michael@0 | 129 | final String iconURI = geckoEngineJSON.getString("iconURI"); |
michael@0 | 130 | // Keep a reference to the bitmap - we'll need it later in onBindView. |
michael@0 | 131 | try { |
michael@0 | 132 | final int desiredWidth; |
michael@0 | 133 | if (mFaviconView != null) { |
michael@0 | 134 | desiredWidth = mFaviconView.getWidth(); |
michael@0 | 135 | } else { |
michael@0 | 136 | // largestFaviconSize is initialized when Favicons is attached to a |
michael@0 | 137 | // context, which occurs during GeckoApp.onCreate. That might not |
michael@0 | 138 | // ever happen (leaving it at 0), so we fall back. |
michael@0 | 139 | if (Favicons.largestFaviconSize == 0) { |
michael@0 | 140 | desiredWidth = 128; |
michael@0 | 141 | } else { |
michael@0 | 142 | desiredWidth = Favicons.largestFaviconSize; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // TODO: use the cache. Bug 961600. |
michael@0 | 147 | mIconBitmap = FaviconDecoder.getMostSuitableBitmapFromDataURI(iconURI, desiredWidth); |
michael@0 | 148 | |
michael@0 | 149 | } catch (IllegalArgumentException e) { |
michael@0 | 150 | Log.e(LOGTAG, "IllegalArgumentException creating Bitmap. Most likely a zero-length bitmap.", e); |
michael@0 | 151 | } catch (NullPointerException e) { |
michael@0 | 152 | Log.e(LOGTAG, "NullPointerException creating Bitmap. Most likely a zero-length bitmap.", e); |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | } |