mobile/android/base/prompts/IconGridInput.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/prompts/IconGridInput.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,165 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.prompts;
    1.10 +
    1.11 +import java.util.ArrayList;
    1.12 +import java.util.List;
    1.13 +
    1.14 +import org.json.JSONArray;
    1.15 +import org.json.JSONObject;
    1.16 +import org.mozilla.gecko.GeckoAppShell;
    1.17 +import org.mozilla.gecko.R;
    1.18 +import org.mozilla.gecko.gfx.BitmapUtils;
    1.19 +
    1.20 +import android.content.Context;
    1.21 +import android.graphics.drawable.Drawable;
    1.22 +import android.text.TextUtils;
    1.23 +import android.view.Display;
    1.24 +import android.view.LayoutInflater;
    1.25 +import android.view.View;
    1.26 +import android.view.ViewGroup;
    1.27 +import android.view.WindowManager;
    1.28 +import android.widget.AdapterView;
    1.29 +import android.widget.AdapterView.OnItemClickListener;
    1.30 +import android.widget.ArrayAdapter;
    1.31 +import android.widget.GridView;
    1.32 +import android.widget.ImageView;
    1.33 +import android.widget.TextView;
    1.34 +
    1.35 +public class IconGridInput extends PromptInput implements OnItemClickListener {
    1.36 +    public static final String INPUT_TYPE = "icongrid";
    1.37 +    public static final String LOGTAG = "GeckoIconGridInput";
    1.38 +
    1.39 +    private ArrayAdapter<IconGridItem> mAdapter; // An adapter holding a list of items to show in the grid
    1.40 +
    1.41 +    private static int mColumnWidth = -1;  // The maximum width of columns
    1.42 +    private static int mMaxColumns = -1;  // The maximum number of columns to show
    1.43 +    private static int mIconSize = -1;    // Size of icons in the grid
    1.44 +    private int mSelected = -1;           // Current selection
    1.45 +    private JSONArray mArray;
    1.46 +
    1.47 +    public IconGridInput(JSONObject obj) {
    1.48 +        super(obj);
    1.49 +        mArray = obj.optJSONArray("items");
    1.50 +    }
    1.51 +
    1.52 +    @Override
    1.53 +    public View getView(Context context) throws UnsupportedOperationException {
    1.54 +        if (mColumnWidth < 0) {
    1.55 +            // getColumnWidth isn't available on pre-ICS, so we pull it out and assign it here
    1.56 +            mColumnWidth = context.getResources().getDimensionPixelSize(R.dimen.icongrid_columnwidth);
    1.57 +        }
    1.58 +
    1.59 +        if (mIconSize < 0) {
    1.60 +            mIconSize = GeckoAppShell.getPreferredIconSize();
    1.61 +        }
    1.62 +
    1.63 +        if (mMaxColumns < 0) {
    1.64 +            mMaxColumns = context.getResources().getInteger(R.integer.max_icon_grid_columns);
    1.65 +        }
    1.66 +
    1.67 +        // TODO: Dynamically handle size changes
    1.68 +        final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    1.69 +        final Display display = wm.getDefaultDisplay();
    1.70 +        final int screenWidth = display.getWidth();
    1.71 +        int maxColumns = Math.min(mMaxColumns, screenWidth / mColumnWidth);
    1.72 +
    1.73 +        final GridView view = (GridView) LayoutInflater.from(context).inflate(R.layout.icon_grid, null, false);
    1.74 +        view.setColumnWidth(mColumnWidth);
    1.75 +
    1.76 +        final ArrayList<IconGridItem> items = new ArrayList<IconGridItem>(mArray.length());
    1.77 +        for (int i = 0; i < mArray.length(); i++) {
    1.78 +            IconGridItem item = new IconGridItem(context, mArray.optJSONObject(i));
    1.79 +            items.add(item);
    1.80 +            if (item.selected) {
    1.81 +                mSelected = i;
    1.82 +                view.setSelection(i);
    1.83 +            }
    1.84 +        }
    1.85 +
    1.86 +        view.setNumColumns(Math.min(items.size(), maxColumns));
    1.87 +        view.setOnItemClickListener(this);
    1.88 +
    1.89 +        mAdapter = new IconGridAdapter(context, -1, items);
    1.90 +        view.setAdapter(mAdapter);
    1.91 +        mView = view;
    1.92 +        return mView;
    1.93 +    }
    1.94 +
    1.95 +    @Override
    1.96 +    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    1.97 +        mSelected = position;
    1.98 +        notifyListeners(Integer.toString(position));
    1.99 +    }
   1.100 +
   1.101 +    @Override
   1.102 +    public Object getValue() {
   1.103 +        return new Integer(mSelected);
   1.104 +    }
   1.105 +
   1.106 +    @Override
   1.107 +    public boolean getScrollable() {
   1.108 +        return true;
   1.109 +    }
   1.110 +
   1.111 +    private class IconGridAdapter extends ArrayAdapter<IconGridItem> {
   1.112 +        public IconGridAdapter(Context context, int resource, List<IconGridItem> items) {
   1.113 +            super(context, resource, items);
   1.114 +        }
   1.115 +
   1.116 +        @Override
   1.117 +        public View getView(int position, View convert, ViewGroup parent) {
   1.118 +            final Context context = parent.getContext();
   1.119 +            if (convert == null) {
   1.120 +                convert = LayoutInflater.from(context).inflate(R.layout.icon_grid_item, parent, false);
   1.121 +            }
   1.122 +            bindView(convert, context, position);
   1.123 +            return convert;
   1.124 +        }
   1.125 +
   1.126 +        private void bindView(View v, Context c, int position) {
   1.127 +            final IconGridItem item = getItem(position);
   1.128 +            final TextView text1 = (TextView) v.findViewById(android.R.id.text1);
   1.129 +            text1.setText(item.label);
   1.130 +
   1.131 +            final TextView text2 = (TextView) v.findViewById(android.R.id.text2);
   1.132 +            if (TextUtils.isEmpty(item.description)) {
   1.133 +                text2.setVisibility(View.GONE);
   1.134 +            } else {
   1.135 +                text2.setVisibility(View.VISIBLE);
   1.136 +                text2.setText(item.description);
   1.137 +            }
   1.138 +
   1.139 +            final ImageView icon = (ImageView) v.findViewById(R.id.icon);
   1.140 +            icon.setImageDrawable(item.icon);
   1.141 +            ViewGroup.LayoutParams lp = icon.getLayoutParams();
   1.142 +            lp.width = lp.height = mIconSize;
   1.143 +        }
   1.144 +    }
   1.145 + 
   1.146 +    private class IconGridItem {
   1.147 +        final String label;
   1.148 +        final String description;
   1.149 +        final boolean selected;
   1.150 +        Drawable icon;
   1.151 +
   1.152 +        public IconGridItem(final Context context, final JSONObject obj) {
   1.153 +            label = obj.optString("name");
   1.154 +            final String iconUrl = obj.optString("iconUri");
   1.155 +            description = obj.optString("description");
   1.156 +            selected = obj.optBoolean("selected");
   1.157 +
   1.158 +            BitmapUtils.getDrawable(context, iconUrl, new BitmapUtils.BitmapLoader() {
   1.159 +                public void onBitmapFound(Drawable d) {
   1.160 +                    icon = d;
   1.161 +                    if (mAdapter != null) {
   1.162 +                        mAdapter.notifyDataSetChanged();
   1.163 +                    }
   1.164 +                }
   1.165 +            });
   1.166 +        }
   1.167 +    }
   1.168 +}

mercurial