1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/prompts/TabInput.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 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 org.mozilla.gecko.R; 1.12 +import org.mozilla.gecko.util.ThreadUtils; 1.13 + 1.14 +import org.json.JSONArray; 1.15 +import org.json.JSONObject; 1.16 +import org.json.JSONException; 1.17 + 1.18 +import android.content.Context; 1.19 +import android.os.Build; 1.20 +import android.util.Log; 1.21 +import android.view.View; 1.22 +import android.view.LayoutInflater; 1.23 +import android.widget.AdapterView; 1.24 +import android.widget.Button; 1.25 +import android.widget.FrameLayout; 1.26 +import android.widget.LinearLayout; 1.27 +import android.widget.LinearLayout.LayoutParams; 1.28 +import android.widget.ListView; 1.29 +import android.widget.TabHost; 1.30 +import android.widget.TabWidget; 1.31 +import android.widget.TextView; 1.32 + 1.33 +import java.util.LinkedHashMap; 1.34 + 1.35 +public class TabInput extends PromptInput implements AdapterView.OnItemClickListener { 1.36 + public static final String INPUT_TYPE = "tabs"; 1.37 + public static final String LOGTAG = "GeckoTabInput"; 1.38 + 1.39 + /* Keeping the order of this in sync with the JSON is important. */ 1.40 + final private LinkedHashMap<String, PromptListItem[]> mTabs; 1.41 + 1.42 + private TabHost mHost; 1.43 + private int mPosition; 1.44 + 1.45 + public TabInput(JSONObject obj) { 1.46 + super(obj); 1.47 + mTabs = new LinkedHashMap<String, PromptListItem[]>(); 1.48 + try { 1.49 + JSONArray tabs = obj.getJSONArray("items"); 1.50 + for (int i = 0; i < tabs.length(); i++) { 1.51 + JSONObject tab = tabs.getJSONObject(i); 1.52 + String title = tab.getString("label"); 1.53 + JSONArray items = tab.getJSONArray("items"); 1.54 + mTabs.put(title, PromptListItem.getArray(items)); 1.55 + } 1.56 + } catch(JSONException ex) { 1.57 + Log.e(LOGTAG, "Exception", ex); 1.58 + } 1.59 + } 1.60 + 1.61 + @Override 1.62 + public View getView(final Context context) throws UnsupportedOperationException { 1.63 + final LayoutInflater inflater = LayoutInflater.from(context); 1.64 + mHost = (TabHost) inflater.inflate(R.layout.tab_prompt_input, null); 1.65 + mHost.setup(); 1.66 + 1.67 + for (String title : mTabs.keySet()) { 1.68 + final TabHost.TabSpec spec = mHost.newTabSpec(title); 1.69 + spec.setContent(new TabHost.TabContentFactory() { 1.70 + @Override 1.71 + public View createTabContent(final String tag) { 1.72 + PromptListAdapter adapter = new PromptListAdapter(context, android.R.layout.simple_list_item_1, mTabs.get(tag)); 1.73 + ListView listView = new ListView(context); 1.74 + listView.setCacheColorHint(0); 1.75 + listView.setOnItemClickListener(TabInput.this); 1.76 + listView.setAdapter(adapter); 1.77 + return listView; 1.78 + } 1.79 + }); 1.80 + 1.81 + // On older android versions, we use a custom style for the tabs. 1.82 + if (Build.VERSION.SDK_INT < 11) { 1.83 + TextView textview = (TextView) inflater.inflate(R.layout.tab_prompt_tab, null); 1.84 + textview.setText(title); 1.85 + spec.setIndicator(textview); 1.86 + } else { 1.87 + spec.setIndicator(title); 1.88 + } 1.89 + mHost.addTab(spec); 1.90 + } 1.91 + mView = mHost; 1.92 + return mHost; 1.93 + } 1.94 + 1.95 + @Override 1.96 + public Object getValue() { 1.97 + JSONObject obj = new JSONObject(); 1.98 + try { 1.99 + obj.put("tab", mHost.getCurrentTab()); 1.100 + obj.put("item", mPosition); 1.101 + } catch(JSONException ex) { } 1.102 + 1.103 + return obj; 1.104 + } 1.105 + 1.106 + @Override 1.107 + public boolean getScrollable() { 1.108 + return true; 1.109 + } 1.110 + 1.111 + @Override 1.112 + public boolean canApplyInputStyle() { 1.113 + return false; 1.114 + } 1.115 + 1.116 + @Override 1.117 + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 1.118 + ThreadUtils.assertOnUiThread(); 1.119 + mPosition = position; 1.120 + notifyListeners(Integer.toString(position)); 1.121 + } 1.122 + 1.123 +}