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 java.text.SimpleDateFormat; michael@0: import java.util.Calendar; michael@0: import java.util.GregorianCalendar; michael@0: michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.widget.AllCapsTextView; michael@0: import org.mozilla.gecko.widget.DateTimePicker; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.Configuration; michael@0: import android.os.Build; michael@0: import android.text.Html; michael@0: import android.text.InputType; michael@0: import android.text.TextUtils; michael@0: import android.text.format.DateFormat; michael@0: import android.util.Log; michael@0: import android.view.View; michael@0: import android.view.ViewGroup.LayoutParams; michael@0: import android.view.inputmethod.InputMethodManager; michael@0: import android.widget.ArrayAdapter; michael@0: import android.widget.CheckBox; michael@0: import android.widget.DatePicker; michael@0: import android.widget.EditText; michael@0: import android.widget.LinearLayout; michael@0: import android.widget.Spinner; michael@0: import android.widget.TextView; michael@0: import android.widget.TimePicker; michael@0: michael@0: public class PromptInput { michael@0: protected final String mLabel; michael@0: protected final String mType; michael@0: protected final String mId; michael@0: protected final String mValue; michael@0: protected OnChangeListener mListener; michael@0: protected View mView; michael@0: public static final String LOGTAG = "GeckoPromptInput"; michael@0: michael@0: public interface OnChangeListener { michael@0: public void onChange(PromptInput input); michael@0: } michael@0: michael@0: public void setListener(OnChangeListener listener) { michael@0: mListener = listener; michael@0: } michael@0: michael@0: public static class EditInput extends PromptInput { michael@0: protected final String mHint; michael@0: protected final boolean mAutofocus; michael@0: public static final String INPUT_TYPE = "textbox"; michael@0: michael@0: public EditInput(JSONObject object) { michael@0: super(object); michael@0: mHint = object.optString("hint"); michael@0: mAutofocus = object.optBoolean("autofocus"); michael@0: } michael@0: michael@0: public View getView(final Context context) throws UnsupportedOperationException { michael@0: EditText input = new EditText(context); michael@0: input.setInputType(InputType.TYPE_CLASS_TEXT); michael@0: input.setText(mValue); michael@0: michael@0: if (!TextUtils.isEmpty(mHint)) { michael@0: input.setHint(mHint); michael@0: } michael@0: michael@0: if (mAutofocus) { michael@0: input.setOnFocusChangeListener(new View.OnFocusChangeListener() { michael@0: @Override michael@0: public void onFocusChange(View v, boolean hasFocus) { michael@0: if (hasFocus) { michael@0: ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v, 0); michael@0: } michael@0: } michael@0: }); michael@0: input.requestFocus(); michael@0: } michael@0: michael@0: mView = (View)input; michael@0: return mView; michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: EditText edit = (EditText)mView; michael@0: return edit.getText(); michael@0: } michael@0: } michael@0: michael@0: public static class NumberInput extends EditInput { michael@0: public static final String INPUT_TYPE = "number"; michael@0: public NumberInput(JSONObject obj) { michael@0: super(obj); michael@0: } michael@0: michael@0: public View getView(final Context context) throws UnsupportedOperationException { michael@0: EditText input = (EditText) super.getView(context); michael@0: input.setRawInputType(Configuration.KEYBOARD_12KEY); michael@0: input.setInputType(InputType.TYPE_CLASS_NUMBER | michael@0: InputType.TYPE_NUMBER_FLAG_SIGNED); michael@0: return input; michael@0: } michael@0: } michael@0: michael@0: public static class PasswordInput extends EditInput { michael@0: public static final String INPUT_TYPE = "password"; michael@0: public PasswordInput(JSONObject obj) { michael@0: super(obj); michael@0: } michael@0: michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: EditText input = (EditText) super.getView(context); michael@0: input.setInputType(InputType.TYPE_CLASS_TEXT | michael@0: InputType.TYPE_TEXT_VARIATION_PASSWORD | michael@0: InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); michael@0: return input; michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: EditText edit = (EditText)mView; michael@0: return edit.getText(); michael@0: } michael@0: } michael@0: michael@0: public static class CheckboxInput extends PromptInput { michael@0: public static final String INPUT_TYPE = "checkbox"; michael@0: private boolean mChecked; michael@0: michael@0: public CheckboxInput(JSONObject obj) { michael@0: super(obj); michael@0: mChecked = obj.optBoolean("checked"); michael@0: } michael@0: michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: CheckBox checkbox = new CheckBox(context); michael@0: checkbox.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); michael@0: checkbox.setText(mLabel); michael@0: checkbox.setChecked(mChecked); michael@0: mView = (View)checkbox; michael@0: return mView; michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: CheckBox checkbox = (CheckBox)mView; michael@0: return checkbox.isChecked() ? Boolean.TRUE : Boolean.FALSE; michael@0: } michael@0: } michael@0: michael@0: public static class DateTimeInput extends PromptInput { michael@0: public static final String[] INPUT_TYPES = new String[] { michael@0: "date", michael@0: "week", michael@0: "time", michael@0: "datetime-local", michael@0: "datetime", michael@0: "month" michael@0: }; michael@0: michael@0: public DateTimeInput(JSONObject obj) { michael@0: super(obj); michael@0: } michael@0: michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: if (mType.equals("date")) { michael@0: try { michael@0: DateTimePicker input = new DateTimePicker(context, "yyyy-MM-dd", mValue, michael@0: DateTimePicker.PickersState.DATE); michael@0: input.toggleCalendar(true); michael@0: mView = (View)input; michael@0: } catch (UnsupportedOperationException ex) { michael@0: // We can't use our custom version of the DatePicker widget because the sdk is too old. michael@0: // But we can fallback on the native one. michael@0: DatePicker input = new DatePicker(context); michael@0: try { michael@0: if (!TextUtils.isEmpty(mValue)) { michael@0: GregorianCalendar calendar = new GregorianCalendar(); michael@0: calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mValue)); michael@0: input.updateDate(calendar.get(Calendar.YEAR), michael@0: calendar.get(Calendar.MONTH), michael@0: calendar.get(Calendar.DAY_OF_MONTH)); michael@0: } michael@0: } catch (Exception e) { michael@0: Log.e(LOGTAG, "error parsing format string: " + e); michael@0: } michael@0: mView = (View)input; michael@0: } michael@0: } else if (mType.equals("week")) { michael@0: DateTimePicker input = new DateTimePicker(context, "yyyy-'W'ww", mValue, michael@0: DateTimePicker.PickersState.WEEK); michael@0: mView = (View)input; michael@0: } else if (mType.equals("time")) { michael@0: TimePicker input = new TimePicker(context); michael@0: input.setIs24HourView(DateFormat.is24HourFormat(context)); michael@0: michael@0: GregorianCalendar calendar = new GregorianCalendar(); michael@0: if (!TextUtils.isEmpty(mValue)) { michael@0: try { michael@0: calendar.setTime(new SimpleDateFormat("HH:mm").parse(mValue)); michael@0: } catch (Exception e) { } michael@0: } michael@0: input.setCurrentHour(calendar.get(GregorianCalendar.HOUR_OF_DAY)); michael@0: input.setCurrentMinute(calendar.get(GregorianCalendar.MINUTE)); michael@0: mView = (View)input; michael@0: } else if (mType.equals("datetime-local") || mType.equals("datetime")) { michael@0: DateTimePicker input = new DateTimePicker(context, "yyyy-MM-dd HH:mm", mValue, michael@0: DateTimePicker.PickersState.DATETIME); michael@0: input.toggleCalendar(true); michael@0: mView = (View)input; michael@0: } else if (mType.equals("month")) { michael@0: DateTimePicker input = new DateTimePicker(context, "yyyy-MM", mValue, michael@0: DateTimePicker.PickersState.MONTH); michael@0: mView = (View)input; michael@0: } michael@0: return mView; michael@0: } michael@0: michael@0: private static String formatDateString(String dateFormat, Calendar calendar) { michael@0: return new SimpleDateFormat(dateFormat).format(calendar.getTime()); michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: if (Build.VERSION.SDK_INT < 11 && mType.equals("date")) { michael@0: // We can't use the custom DateTimePicker with a sdk older than 11. michael@0: // Fallback on the native DatePicker. michael@0: DatePicker dp = (DatePicker)mView; michael@0: GregorianCalendar calendar = michael@0: new GregorianCalendar(dp.getYear(),dp.getMonth(),dp.getDayOfMonth()); michael@0: return formatDateString("yyyy-MM-dd",calendar); michael@0: } else if (mType.equals("time")) { michael@0: TimePicker tp = (TimePicker)mView; michael@0: GregorianCalendar calendar = michael@0: new GregorianCalendar(0,0,0,tp.getCurrentHour(),tp.getCurrentMinute()); michael@0: return formatDateString("HH:mm",calendar); michael@0: } else { michael@0: DateTimePicker dp = (DateTimePicker)mView; michael@0: GregorianCalendar calendar = new GregorianCalendar(); michael@0: calendar.setTimeInMillis(dp.getTimeInMillis()); michael@0: if (mType.equals("date")) { michael@0: return formatDateString("yyyy-MM-dd",calendar); michael@0: } else if (mType.equals("week")) { michael@0: return formatDateString("yyyy-'W'ww",calendar); michael@0: } else if (mType.equals("datetime-local")) { michael@0: return formatDateString("yyyy-MM-dd HH:mm",calendar); michael@0: } else if (mType.equals("datetime")) { michael@0: calendar.set(GregorianCalendar.ZONE_OFFSET,0); michael@0: calendar.setTimeInMillis(dp.getTimeInMillis()); michael@0: return formatDateString("yyyy-MM-dd HH:mm",calendar); michael@0: } else if (mType.equals("month")) { michael@0: return formatDateString("yyyy-MM",calendar); michael@0: } michael@0: } michael@0: return super.getValue(); michael@0: } michael@0: } michael@0: michael@0: public static class MenulistInput extends PromptInput { michael@0: public static final String INPUT_TYPE = "menulist"; michael@0: private static String[] mListitems; michael@0: private static int mSelected; michael@0: michael@0: public Spinner spinner; michael@0: public AllCapsTextView textView; michael@0: michael@0: public MenulistInput(JSONObject obj) { michael@0: super(obj); michael@0: mListitems = Prompt.getStringArray(obj, "values"); michael@0: mSelected = obj.optInt("selected"); michael@0: } michael@0: michael@0: public View getView(final Context context) throws UnsupportedOperationException { michael@0: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { michael@0: spinner = new Spinner(context); michael@0: } else { michael@0: spinner = new Spinner(context, Spinner.MODE_DIALOG); michael@0: } michael@0: try { michael@0: if (mListitems.length > 0) { michael@0: ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, mListitems); michael@0: adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); michael@0: michael@0: spinner.setAdapter(adapter); michael@0: spinner.setSelection(mSelected); michael@0: } michael@0: } catch(Exception ex) { } michael@0: michael@0: if (!TextUtils.isEmpty(mLabel)) { michael@0: LinearLayout container = new LinearLayout(context); michael@0: container.setOrientation(LinearLayout.VERTICAL); michael@0: michael@0: textView = new AllCapsTextView(context, null); michael@0: textView.setText(mLabel); michael@0: container.addView(textView); michael@0: michael@0: container.addView(spinner); michael@0: return container; michael@0: } michael@0: michael@0: return spinner; michael@0: } michael@0: michael@0: @Override michael@0: public Object getValue() { michael@0: return new Integer(spinner.getSelectedItemPosition()); michael@0: } michael@0: } michael@0: michael@0: public static class LabelInput extends PromptInput { michael@0: public static final String INPUT_TYPE = "label"; michael@0: public LabelInput(JSONObject obj) { michael@0: super(obj); michael@0: } michael@0: michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: // not really an input, but a way to add labels and such to the dialog michael@0: TextView view = new TextView(context); michael@0: view.setText(Html.fromHtml(mLabel)); michael@0: mView = view; michael@0: return mView; michael@0: } michael@0: } michael@0: michael@0: public PromptInput(JSONObject obj) { michael@0: mLabel = obj.optString("label"); michael@0: mType = obj.optString("type"); michael@0: String id = obj.optString("id"); michael@0: mId = TextUtils.isEmpty(id) ? mType : id; michael@0: mValue = obj.optString("value"); michael@0: } michael@0: michael@0: public static PromptInput getInput(JSONObject obj) { michael@0: String type = obj.optString("type"); michael@0: if (EditInput.INPUT_TYPE.equals(type)) { michael@0: return new EditInput(obj); michael@0: } else if (NumberInput.INPUT_TYPE.equals(type)) { michael@0: return new NumberInput(obj); michael@0: } else if (PasswordInput.INPUT_TYPE.equals(type)) { michael@0: return new PasswordInput(obj); michael@0: } else if (CheckboxInput.INPUT_TYPE.equals(type)) { michael@0: return new CheckboxInput(obj); michael@0: } else if (MenulistInput.INPUT_TYPE.equals(type)) { michael@0: return new MenulistInput(obj); michael@0: } else if (LabelInput.INPUT_TYPE.equals(type)) { michael@0: return new LabelInput(obj); michael@0: } else if (IconGridInput.INPUT_TYPE.equals(type)) { michael@0: return new IconGridInput(obj); michael@0: } else if (ColorPickerInput.INPUT_TYPE.equals(type)) { michael@0: return new ColorPickerInput(obj); michael@0: } else if (TabInput.INPUT_TYPE.equals(type)) { michael@0: return new TabInput(obj); michael@0: } else { michael@0: for (String dtType : DateTimeInput.INPUT_TYPES) { michael@0: if (dtType.equals(type)) { michael@0: return new DateTimeInput(obj); michael@0: } michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: public View getView(Context context) throws UnsupportedOperationException { michael@0: return null; michael@0: } michael@0: michael@0: public String getId() { michael@0: return mId; michael@0: } michael@0: michael@0: public Object getValue() { michael@0: return null; michael@0: } michael@0: michael@0: public boolean getScrollable() { michael@0: return false; michael@0: } michael@0: michael@0: public boolean canApplyInputStyle() { michael@0: return true; michael@0: } michael@0: michael@0: protected void notifyListeners(String val) { michael@0: if (mListener != null) { michael@0: mListener.onChange(this); michael@0: } michael@0: } michael@0: }