michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.prompts; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.gfx.BitmapUtils; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.text.TextUtils; michael@0: import android.view.Display; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.view.WindowManager; michael@0: import android.widget.AdapterView; michael@0: import android.widget.AdapterView.OnItemClickListener; michael@0: import android.widget.ArrayAdapter; michael@0: import android.widget.GridView; michael@0: import android.widget.ImageView; michael@0: import android.widget.TextView; michael@0: michael@0: public class IconGridInput extends PromptInput implements OnItemClickListener { michael@0: public static final String INPUT_TYPE = "icongrid"; michael@0: public static final String LOGTAG = "GeckoIconGridInput"; michael@0: michael@0: private ArrayAdapter mAdapter; // An adapter holding a list of items to show in the grid michael@0: michael@0: private static int mColumnWidth = -1; // The maximum width of columns michael@0: private static int mMaxColumns = -1; // The maximum number of columns to show michael@0: private static int mIconSize = -1; // Size of icons in the grid michael@0: private int mSelected = -1; // Current selection michael@0: private JSONArray mArray; michael@0: michael@0: public IconGridInput(JSONObject obj) { michael@0: super(obj); michael@0: mArray = obj.optJSONArray("items"); michael@0: } michael@0: michael@0: @Override michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: if (mColumnWidth < 0) { michael@0: // getColumnWidth isn't available on pre-ICS, so we pull it out and assign it here michael@0: mColumnWidth = context.getResources().getDimensionPixelSize(R.dimen.icongrid_columnwidth); michael@0: } michael@0: michael@0: if (mIconSize < 0) { michael@0: mIconSize = GeckoAppShell.getPreferredIconSize(); michael@0: } michael@0: michael@0: if (mMaxColumns < 0) { michael@0: mMaxColumns = context.getResources().getInteger(R.integer.max_icon_grid_columns); michael@0: } michael@0: michael@0: // TODO: Dynamically handle size changes michael@0: final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); michael@0: final Display display = wm.getDefaultDisplay(); michael@0: final int screenWidth = display.getWidth(); michael@0: int maxColumns = Math.min(mMaxColumns, screenWidth / mColumnWidth); michael@0: michael@0: final GridView view = (GridView) LayoutInflater.from(context).inflate(R.layout.icon_grid, null, false); michael@0: view.setColumnWidth(mColumnWidth); michael@0: michael@0: final ArrayList items = new ArrayList(mArray.length()); michael@0: for (int i = 0; i < mArray.length(); i++) { michael@0: IconGridItem item = new IconGridItem(context, mArray.optJSONObject(i)); michael@0: items.add(item); michael@0: if (item.selected) { michael@0: mSelected = i; michael@0: view.setSelection(i); michael@0: } michael@0: } michael@0: michael@0: view.setNumColumns(Math.min(items.size(), maxColumns)); michael@0: view.setOnItemClickListener(this); michael@0: michael@0: mAdapter = new IconGridAdapter(context, -1, items); michael@0: view.setAdapter(mAdapter); michael@0: mView = view; michael@0: return mView; michael@0: } michael@0: michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: mSelected = position; michael@0: notifyListeners(Integer.toString(position)); michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: return new Integer(mSelected); michael@0: } michael@0: michael@0: @Override michael@0: public boolean getScrollable() { michael@0: return true; michael@0: } michael@0: michael@0: private class IconGridAdapter extends ArrayAdapter { michael@0: public IconGridAdapter(Context context, int resource, List items) { michael@0: super(context, resource, items); michael@0: } michael@0: michael@0: @Override michael@0: public View getView(int position, View convert, ViewGroup parent) { michael@0: final Context context = parent.getContext(); michael@0: if (convert == null) { michael@0: convert = LayoutInflater.from(context).inflate(R.layout.icon_grid_item, parent, false); michael@0: } michael@0: bindView(convert, context, position); michael@0: return convert; michael@0: } michael@0: michael@0: private void bindView(View v, Context c, int position) { michael@0: final IconGridItem item = getItem(position); michael@0: final TextView text1 = (TextView) v.findViewById(android.R.id.text1); michael@0: text1.setText(item.label); michael@0: michael@0: final TextView text2 = (TextView) v.findViewById(android.R.id.text2); michael@0: if (TextUtils.isEmpty(item.description)) { michael@0: text2.setVisibility(View.GONE); michael@0: } else { michael@0: text2.setVisibility(View.VISIBLE); michael@0: text2.setText(item.description); michael@0: } michael@0: michael@0: final ImageView icon = (ImageView) v.findViewById(R.id.icon); michael@0: icon.setImageDrawable(item.icon); michael@0: ViewGroup.LayoutParams lp = icon.getLayoutParams(); michael@0: lp.width = lp.height = mIconSize; michael@0: } michael@0: } michael@0: michael@0: private class IconGridItem { michael@0: final String label; michael@0: final String description; michael@0: final boolean selected; michael@0: Drawable icon; michael@0: michael@0: public IconGridItem(final Context context, final JSONObject obj) { michael@0: label = obj.optString("name"); michael@0: final String iconUrl = obj.optString("iconUri"); michael@0: description = obj.optString("description"); michael@0: selected = obj.optBoolean("selected"); michael@0: michael@0: BitmapUtils.getDrawable(context, iconUrl, new BitmapUtils.BitmapLoader() { michael@0: public void onBitmapFound(Drawable d) { michael@0: icon = d; michael@0: if (mAdapter != null) { michael@0: mAdapter.notifyDataSetChanged(); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: } michael@0: }