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 org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONObject; michael@0: import org.json.JSONException; michael@0: michael@0: import android.content.Context; michael@0: import android.os.Build; michael@0: import android.util.Log; michael@0: import android.view.View; michael@0: import android.view.LayoutInflater; michael@0: import android.widget.AdapterView; michael@0: import android.widget.Button; michael@0: import android.widget.FrameLayout; michael@0: import android.widget.LinearLayout; michael@0: import android.widget.LinearLayout.LayoutParams; michael@0: import android.widget.ListView; michael@0: import android.widget.TabHost; michael@0: import android.widget.TabWidget; michael@0: import android.widget.TextView; michael@0: michael@0: import java.util.LinkedHashMap; michael@0: michael@0: public class TabInput extends PromptInput implements AdapterView.OnItemClickListener { michael@0: public static final String INPUT_TYPE = "tabs"; michael@0: public static final String LOGTAG = "GeckoTabInput"; michael@0: michael@0: /* Keeping the order of this in sync with the JSON is important. */ michael@0: final private LinkedHashMap mTabs; michael@0: michael@0: private TabHost mHost; michael@0: private int mPosition; michael@0: michael@0: public TabInput(JSONObject obj) { michael@0: super(obj); michael@0: mTabs = new LinkedHashMap(); michael@0: try { michael@0: JSONArray tabs = obj.getJSONArray("items"); michael@0: for (int i = 0; i < tabs.length(); i++) { michael@0: JSONObject tab = tabs.getJSONObject(i); michael@0: String title = tab.getString("label"); michael@0: JSONArray items = tab.getJSONArray("items"); michael@0: mTabs.put(title, PromptListItem.getArray(items)); michael@0: } michael@0: } catch(JSONException ex) { michael@0: Log.e(LOGTAG, "Exception", ex); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public View getView(final Context context) throws UnsupportedOperationException { michael@0: final LayoutInflater inflater = LayoutInflater.from(context); michael@0: mHost = (TabHost) inflater.inflate(R.layout.tab_prompt_input, null); michael@0: mHost.setup(); michael@0: michael@0: for (String title : mTabs.keySet()) { michael@0: final TabHost.TabSpec spec = mHost.newTabSpec(title); michael@0: spec.setContent(new TabHost.TabContentFactory() { michael@0: @Override michael@0: public View createTabContent(final String tag) { michael@0: PromptListAdapter adapter = new PromptListAdapter(context, android.R.layout.simple_list_item_1, mTabs.get(tag)); michael@0: ListView listView = new ListView(context); michael@0: listView.setCacheColorHint(0); michael@0: listView.setOnItemClickListener(TabInput.this); michael@0: listView.setAdapter(adapter); michael@0: return listView; michael@0: } michael@0: }); michael@0: michael@0: // On older android versions, we use a custom style for the tabs. michael@0: if (Build.VERSION.SDK_INT < 11) { michael@0: TextView textview = (TextView) inflater.inflate(R.layout.tab_prompt_tab, null); michael@0: textview.setText(title); michael@0: spec.setIndicator(textview); michael@0: } else { michael@0: spec.setIndicator(title); michael@0: } michael@0: mHost.addTab(spec); michael@0: } michael@0: mView = mHost; michael@0: return mHost; michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: JSONObject obj = new JSONObject(); michael@0: try { michael@0: obj.put("tab", mHost.getCurrentTab()); michael@0: obj.put("item", mPosition); michael@0: } catch(JSONException ex) { } michael@0: michael@0: return obj; michael@0: } michael@0: michael@0: @Override michael@0: public boolean getScrollable() { michael@0: return true; michael@0: } michael@0: michael@0: @Override michael@0: public boolean canApplyInputStyle() { michael@0: return false; michael@0: } michael@0: michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: ThreadUtils.assertOnUiThread(); michael@0: mPosition = position; michael@0: notifyListeners(Integer.toString(position)); michael@0: } michael@0: michael@0: }