Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.prompts; |
michael@0 | 7 | |
michael@0 | 8 | import java.util.ArrayList; |
michael@0 | 9 | import java.util.List; |
michael@0 | 10 | |
michael@0 | 11 | import org.json.JSONArray; |
michael@0 | 12 | import org.json.JSONObject; |
michael@0 | 13 | import org.mozilla.gecko.GeckoAppShell; |
michael@0 | 14 | import org.mozilla.gecko.R; |
michael@0 | 15 | import org.mozilla.gecko.gfx.BitmapUtils; |
michael@0 | 16 | |
michael@0 | 17 | import android.content.Context; |
michael@0 | 18 | import android.graphics.drawable.Drawable; |
michael@0 | 19 | import android.text.TextUtils; |
michael@0 | 20 | import android.view.Display; |
michael@0 | 21 | import android.view.LayoutInflater; |
michael@0 | 22 | import android.view.View; |
michael@0 | 23 | import android.view.ViewGroup; |
michael@0 | 24 | import android.view.WindowManager; |
michael@0 | 25 | import android.widget.AdapterView; |
michael@0 | 26 | import android.widget.AdapterView.OnItemClickListener; |
michael@0 | 27 | import android.widget.ArrayAdapter; |
michael@0 | 28 | import android.widget.GridView; |
michael@0 | 29 | import android.widget.ImageView; |
michael@0 | 30 | import android.widget.TextView; |
michael@0 | 31 | |
michael@0 | 32 | public class IconGridInput extends PromptInput implements OnItemClickListener { |
michael@0 | 33 | public static final String INPUT_TYPE = "icongrid"; |
michael@0 | 34 | public static final String LOGTAG = "GeckoIconGridInput"; |
michael@0 | 35 | |
michael@0 | 36 | private ArrayAdapter<IconGridItem> mAdapter; // An adapter holding a list of items to show in the grid |
michael@0 | 37 | |
michael@0 | 38 | private static int mColumnWidth = -1; // The maximum width of columns |
michael@0 | 39 | private static int mMaxColumns = -1; // The maximum number of columns to show |
michael@0 | 40 | private static int mIconSize = -1; // Size of icons in the grid |
michael@0 | 41 | private int mSelected = -1; // Current selection |
michael@0 | 42 | private JSONArray mArray; |
michael@0 | 43 | |
michael@0 | 44 | public IconGridInput(JSONObject obj) { |
michael@0 | 45 | super(obj); |
michael@0 | 46 | mArray = obj.optJSONArray("items"); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | @Override |
michael@0 | 50 | public View getView(Context context) throws UnsupportedOperationException { |
michael@0 | 51 | if (mColumnWidth < 0) { |
michael@0 | 52 | // getColumnWidth isn't available on pre-ICS, so we pull it out and assign it here |
michael@0 | 53 | mColumnWidth = context.getResources().getDimensionPixelSize(R.dimen.icongrid_columnwidth); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | if (mIconSize < 0) { |
michael@0 | 57 | mIconSize = GeckoAppShell.getPreferredIconSize(); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if (mMaxColumns < 0) { |
michael@0 | 61 | mMaxColumns = context.getResources().getInteger(R.integer.max_icon_grid_columns); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // TODO: Dynamically handle size changes |
michael@0 | 65 | final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
michael@0 | 66 | final Display display = wm.getDefaultDisplay(); |
michael@0 | 67 | final int screenWidth = display.getWidth(); |
michael@0 | 68 | int maxColumns = Math.min(mMaxColumns, screenWidth / mColumnWidth); |
michael@0 | 69 | |
michael@0 | 70 | final GridView view = (GridView) LayoutInflater.from(context).inflate(R.layout.icon_grid, null, false); |
michael@0 | 71 | view.setColumnWidth(mColumnWidth); |
michael@0 | 72 | |
michael@0 | 73 | final ArrayList<IconGridItem> items = new ArrayList<IconGridItem>(mArray.length()); |
michael@0 | 74 | for (int i = 0; i < mArray.length(); i++) { |
michael@0 | 75 | IconGridItem item = new IconGridItem(context, mArray.optJSONObject(i)); |
michael@0 | 76 | items.add(item); |
michael@0 | 77 | if (item.selected) { |
michael@0 | 78 | mSelected = i; |
michael@0 | 79 | view.setSelection(i); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | view.setNumColumns(Math.min(items.size(), maxColumns)); |
michael@0 | 84 | view.setOnItemClickListener(this); |
michael@0 | 85 | |
michael@0 | 86 | mAdapter = new IconGridAdapter(context, -1, items); |
michael@0 | 87 | view.setAdapter(mAdapter); |
michael@0 | 88 | mView = view; |
michael@0 | 89 | return mView; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | @Override |
michael@0 | 93 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
michael@0 | 94 | mSelected = position; |
michael@0 | 95 | notifyListeners(Integer.toString(position)); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | @Override |
michael@0 | 99 | public Object getValue() { |
michael@0 | 100 | return new Integer(mSelected); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | @Override |
michael@0 | 104 | public boolean getScrollable() { |
michael@0 | 105 | return true; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | private class IconGridAdapter extends ArrayAdapter<IconGridItem> { |
michael@0 | 109 | public IconGridAdapter(Context context, int resource, List<IconGridItem> items) { |
michael@0 | 110 | super(context, resource, items); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | public View getView(int position, View convert, ViewGroup parent) { |
michael@0 | 115 | final Context context = parent.getContext(); |
michael@0 | 116 | if (convert == null) { |
michael@0 | 117 | convert = LayoutInflater.from(context).inflate(R.layout.icon_grid_item, parent, false); |
michael@0 | 118 | } |
michael@0 | 119 | bindView(convert, context, position); |
michael@0 | 120 | return convert; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | private void bindView(View v, Context c, int position) { |
michael@0 | 124 | final IconGridItem item = getItem(position); |
michael@0 | 125 | final TextView text1 = (TextView) v.findViewById(android.R.id.text1); |
michael@0 | 126 | text1.setText(item.label); |
michael@0 | 127 | |
michael@0 | 128 | final TextView text2 = (TextView) v.findViewById(android.R.id.text2); |
michael@0 | 129 | if (TextUtils.isEmpty(item.description)) { |
michael@0 | 130 | text2.setVisibility(View.GONE); |
michael@0 | 131 | } else { |
michael@0 | 132 | text2.setVisibility(View.VISIBLE); |
michael@0 | 133 | text2.setText(item.description); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | final ImageView icon = (ImageView) v.findViewById(R.id.icon); |
michael@0 | 137 | icon.setImageDrawable(item.icon); |
michael@0 | 138 | ViewGroup.LayoutParams lp = icon.getLayoutParams(); |
michael@0 | 139 | lp.width = lp.height = mIconSize; |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | private class IconGridItem { |
michael@0 | 144 | final String label; |
michael@0 | 145 | final String description; |
michael@0 | 146 | final boolean selected; |
michael@0 | 147 | Drawable icon; |
michael@0 | 148 | |
michael@0 | 149 | public IconGridItem(final Context context, final JSONObject obj) { |
michael@0 | 150 | label = obj.optString("name"); |
michael@0 | 151 | final String iconUrl = obj.optString("iconUri"); |
michael@0 | 152 | description = obj.optString("description"); |
michael@0 | 153 | selected = obj.optBoolean("selected"); |
michael@0 | 154 | |
michael@0 | 155 | BitmapUtils.getDrawable(context, iconUrl, new BitmapUtils.BitmapLoader() { |
michael@0 | 156 | public void onBitmapFound(Drawable d) { |
michael@0 | 157 | icon = d; |
michael@0 | 158 | if (mAdapter != null) { |
michael@0 | 159 | mAdapter.notifyDataSetChanged(); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | }); |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | } |