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 org.mozilla.gecko.R; |
michael@0 | 9 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 10 | |
michael@0 | 11 | import org.json.JSONArray; |
michael@0 | 12 | import org.json.JSONObject; |
michael@0 | 13 | import org.json.JSONException; |
michael@0 | 14 | |
michael@0 | 15 | import android.content.Context; |
michael@0 | 16 | import android.os.Build; |
michael@0 | 17 | import android.util.Log; |
michael@0 | 18 | import android.view.View; |
michael@0 | 19 | import android.view.LayoutInflater; |
michael@0 | 20 | import android.widget.AdapterView; |
michael@0 | 21 | import android.widget.Button; |
michael@0 | 22 | import android.widget.FrameLayout; |
michael@0 | 23 | import android.widget.LinearLayout; |
michael@0 | 24 | import android.widget.LinearLayout.LayoutParams; |
michael@0 | 25 | import android.widget.ListView; |
michael@0 | 26 | import android.widget.TabHost; |
michael@0 | 27 | import android.widget.TabWidget; |
michael@0 | 28 | import android.widget.TextView; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.LinkedHashMap; |
michael@0 | 31 | |
michael@0 | 32 | public class TabInput extends PromptInput implements AdapterView.OnItemClickListener { |
michael@0 | 33 | public static final String INPUT_TYPE = "tabs"; |
michael@0 | 34 | public static final String LOGTAG = "GeckoTabInput"; |
michael@0 | 35 | |
michael@0 | 36 | /* Keeping the order of this in sync with the JSON is important. */ |
michael@0 | 37 | final private LinkedHashMap<String, PromptListItem[]> mTabs; |
michael@0 | 38 | |
michael@0 | 39 | private TabHost mHost; |
michael@0 | 40 | private int mPosition; |
michael@0 | 41 | |
michael@0 | 42 | public TabInput(JSONObject obj) { |
michael@0 | 43 | super(obj); |
michael@0 | 44 | mTabs = new LinkedHashMap<String, PromptListItem[]>(); |
michael@0 | 45 | try { |
michael@0 | 46 | JSONArray tabs = obj.getJSONArray("items"); |
michael@0 | 47 | for (int i = 0; i < tabs.length(); i++) { |
michael@0 | 48 | JSONObject tab = tabs.getJSONObject(i); |
michael@0 | 49 | String title = tab.getString("label"); |
michael@0 | 50 | JSONArray items = tab.getJSONArray("items"); |
michael@0 | 51 | mTabs.put(title, PromptListItem.getArray(items)); |
michael@0 | 52 | } |
michael@0 | 53 | } catch(JSONException ex) { |
michael@0 | 54 | Log.e(LOGTAG, "Exception", ex); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | @Override |
michael@0 | 59 | public View getView(final Context context) throws UnsupportedOperationException { |
michael@0 | 60 | final LayoutInflater inflater = LayoutInflater.from(context); |
michael@0 | 61 | mHost = (TabHost) inflater.inflate(R.layout.tab_prompt_input, null); |
michael@0 | 62 | mHost.setup(); |
michael@0 | 63 | |
michael@0 | 64 | for (String title : mTabs.keySet()) { |
michael@0 | 65 | final TabHost.TabSpec spec = mHost.newTabSpec(title); |
michael@0 | 66 | spec.setContent(new TabHost.TabContentFactory() { |
michael@0 | 67 | @Override |
michael@0 | 68 | public View createTabContent(final String tag) { |
michael@0 | 69 | PromptListAdapter adapter = new PromptListAdapter(context, android.R.layout.simple_list_item_1, mTabs.get(tag)); |
michael@0 | 70 | ListView listView = new ListView(context); |
michael@0 | 71 | listView.setCacheColorHint(0); |
michael@0 | 72 | listView.setOnItemClickListener(TabInput.this); |
michael@0 | 73 | listView.setAdapter(adapter); |
michael@0 | 74 | return listView; |
michael@0 | 75 | } |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | // On older android versions, we use a custom style for the tabs. |
michael@0 | 79 | if (Build.VERSION.SDK_INT < 11) { |
michael@0 | 80 | TextView textview = (TextView) inflater.inflate(R.layout.tab_prompt_tab, null); |
michael@0 | 81 | textview.setText(title); |
michael@0 | 82 | spec.setIndicator(textview); |
michael@0 | 83 | } else { |
michael@0 | 84 | spec.setIndicator(title); |
michael@0 | 85 | } |
michael@0 | 86 | mHost.addTab(spec); |
michael@0 | 87 | } |
michael@0 | 88 | mView = mHost; |
michael@0 | 89 | return mHost; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | @Override |
michael@0 | 93 | public Object getValue() { |
michael@0 | 94 | JSONObject obj = new JSONObject(); |
michael@0 | 95 | try { |
michael@0 | 96 | obj.put("tab", mHost.getCurrentTab()); |
michael@0 | 97 | obj.put("item", mPosition); |
michael@0 | 98 | } catch(JSONException ex) { } |
michael@0 | 99 | |
michael@0 | 100 | return obj; |
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 | @Override |
michael@0 | 109 | public boolean canApplyInputStyle() { |
michael@0 | 110 | return false; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
michael@0 | 115 | ThreadUtils.assertOnUiThread(); |
michael@0 | 116 | mPosition = position; |
michael@0 | 117 | notifyListeners(Integer.toString(position)); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | } |