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