diff options
Diffstat (limited to 'apps/NotificationStudio/src/com/android/notificationstudio/editor')
9 files changed, 659 insertions, 0 deletions
diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/BitmapEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/BitmapEditor.java new file mode 100644 index 000000000..04194acc3 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/BitmapEditor.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.widget.ImageView; + +import com.android.notificationstudio.R; + +public class BitmapEditor extends IconEditor { + + @Override + protected void setImage(ImageView imageView, Object value) { + imageView.setImageBitmap((Bitmap) value); + } + + protected int getIconSize(Resources res) { + return res.getDimensionPixelSize(R.dimen.editor_icon_size_large); + } + +} diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/BooleanEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/BooleanEditor.java new file mode 100644 index 000000000..6a6a8cfbd --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/BooleanEditor.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.view.View; +import android.view.ViewStub; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.Switch; + +import com.android.notificationstudio.R; +import com.android.notificationstudio.editor.Editors.Editor; +import com.android.notificationstudio.model.EditableItem; + +public class BooleanEditor implements Editor { + + public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { + final ViewStub booleanEditorStub = (ViewStub) v.findViewById(R.id.boolean_editor_stub); + booleanEditorStub.setLayoutResource(R.layout.boolean_editor); + final Switch booleanEditor = (Switch) booleanEditorStub.inflate(); + Runnable updateSwitch = new Runnable() { + public void run() { + booleanEditor.setChecked(item.hasValue() && item.getValueBool()); + }}; + booleanEditor.setVisibility(View.VISIBLE); + updateSwitch.run(); + booleanEditor.setOnCheckedChangeListener(new OnCheckedChangeListener(){ + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + item.setValue(isChecked); + afterChange.run(); + }}); + return updateSwitch; + } + +}
\ No newline at end of file diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/DateTimeEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/DateTimeEditor.java new file mode 100644 index 000000000..8089de139 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/DateTimeEditor.java @@ -0,0 +1,139 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.app.Activity; +import android.app.DatePickerDialog; +import android.app.DatePickerDialog.OnDateSetListener; +import android.app.Dialog; +import android.app.DialogFragment; +import android.app.FragmentTransaction; +import android.app.TimePickerDialog; +import android.app.TimePickerDialog.OnTimeSetListener; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.DatePicker; +import android.widget.TimePicker; + +import com.android.notificationstudio.R; +import com.android.notificationstudio.editor.Editors.Editor; +import com.android.notificationstudio.model.EditableItem; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateTimeEditor implements Editor { + private static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy/MM/dd"); + private static final SimpleDateFormat HH_MM_SS = new SimpleDateFormat("HH:mm:ss"); + + @SuppressWarnings("deprecation") + public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { + + final Button dateButton = (Button) v.findViewById(R.id.date_button); + final Button timeButton = (Button) v.findViewById(R.id.time_button); + final Button resetButton = (Button) v.findViewById(R.id.reset_button); + + int vPad = v.getResources().getDimensionPixelSize(R.dimen.editor_datetime_padding_v); + int hPad = v.getResources().getDimensionPixelSize(R.dimen.editor_datetime_padding_h); + for (Button b : new Button[] { dateButton, timeButton, resetButton }) { + b.setVisibility(View.VISIBLE); + b.setPadding(hPad, vPad, hPad, vPad); + } + + final Runnable updateButtonText = new Runnable() { + public void run() { + Date d = getDateTime(item); + String dateString = YYYY_MM_DD.format(d); + dateButton.setText(dateString); + String timeString = HH_MM_SS.format(d); + timeButton.setText(timeString); + }}; + updateButtonText.run(); + + // wire up date button + DialogFragment datePickerFragment = new DialogFragment() { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Date d = getDateTime(item); + OnDateSetListener onDateSet = new OnDateSetListener() { + public void onDateSet(DatePicker view, int year, + int monthOfYear, int dayOfMonth) { + Date d = getDateTime(item); + d.setYear(year - 1900); + d.setMonth(monthOfYear); + d.setDate(dayOfMonth); + item.setValue(d.getTime()); + updateButtonText.run(); + afterChange.run(); + } + }; + return new DatePickerDialog(getActivity(), onDateSet, + d.getYear() + 1900, d.getMonth(), d.getDate()); + } + }; + Activity activity = (Activity) v.getContext(); + launchDialogOnClick(activity, "datePicker", dateButton, datePickerFragment); + + // wire up time button + DialogFragment timePickerFragment = new DialogFragment() { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Date d = getDateTime(item); + OnTimeSetListener onTimeSet = new OnTimeSetListener() { + public void onTimeSet(TimePicker view, int hourOfDay, + int minute) { + Date d = getDateTime(item); + d.setHours(hourOfDay); + d.setMinutes(minute); + item.setValue(d.getTime()); + updateButtonText.run(); + afterChange.run(); + } + }; + return new TimePickerDialog(getActivity(), + onTimeSet, d.getHours(), d.getMinutes(), true); + } + }; + launchDialogOnClick(activity, "timePicker", timeButton, timePickerFragment); + + // wire up reset button + resetButton.setOnClickListener(new OnClickListener(){ + public void onClick(View v) { + item.setValue(null); + updateButtonText.run(); + afterChange.run(); + }}); + return updateButtonText; + } + + private static Date getDateTime(EditableItem item) { + long value = item.hasValue() ? item.getValueLong() : System.currentTimeMillis(); + return new Date(value); + } + + private static void launchDialogOnClick(final Activity activity, + final String tag, Button button, final DialogFragment fragment) { + button.setOnClickListener(new OnClickListener(){ + public void onClick(View v) { + FragmentTransaction ft = activity.getFragmentManager().beginTransaction(); + fragment.show(ft, tag); + }}); + } + +} diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/DropDownEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/DropDownEditor.java new file mode 100644 index 000000000..64490fafe --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/DropDownEditor.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.util.TypedValue; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; +import android.widget.ArrayAdapter; +import android.widget.Spinner; +import android.widget.TextView; + +import com.android.notificationstudio.R; +import com.android.notificationstudio.editor.Editors.Editor; +import com.android.notificationstudio.model.EditableItem; + +public class DropDownEditor implements Editor { + + public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { + final Spinner dropDownEditor = (Spinner) v.findViewById(R.id.drop_down_editor); + dropDownEditor.setVisibility(View.VISIBLE); + Integer[] values = item.getAvailableValuesInteger(); + final float textSize = v.getResources().getDimensionPixelSize(R.dimen.editor_text_size); + final int p = v.getResources().getDimensionPixelSize(R.dimen.editor_drop_down_padding); + final int p2 = p * 2; + final ArrayAdapter<Integer> adapter = + new ArrayAdapter<Integer>(v.getContext(), android.R.layout.simple_spinner_item, values) { + + @Override + public View getDropDownView(int position, View convertView, ViewGroup parent) { + TextView v = (TextView) super.getDropDownView(position, convertView, parent); + v.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); + v.setPadding(p2, p2, p2, p2); + v.setText(v.getResources().getString(getItem(position))); + return v; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + TextView v = (TextView) super.getView(position, convertView, parent); + v.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); + v.setPadding(p, p, p, p); + v.setText(v.getResources().getString(getItem(position))); + return v; + } + }; + dropDownEditor.setAdapter(adapter); + Runnable updateSelection = new Runnable() { + public void run() { + dropDownEditor.setSelection(adapter.getPosition(item.getValueInt())); + }}; + if (item.hasValue()) + updateSelection.run(); + dropDownEditor.setOnItemSelectedListener(new OnItemSelectedListener(){ + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + Object oldValue = item.getValue(); + Object newValue = adapter.getItem(position); + if (newValue.equals(oldValue)) + return; + item.setValue(newValue); + afterChange.run(); + } + public void onNothingSelected(AdapterView<?> parent) { + // noop + }}); + return updateSelection; + } + +}
\ No newline at end of file diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/Editors.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/Editors.java new file mode 100644 index 000000000..b7268f111 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/Editors.java @@ -0,0 +1,100 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.os.Build; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.notificationstudio.NotificationStudioActivity; +import com.android.notificationstudio.R; +import com.android.notificationstudio.model.EditableItem; +import com.android.notificationstudio.model.EditableItemConstants; + +import java.util.HashMap; +import java.util.Map; + +public class Editors implements EditableItemConstants { + + public interface Editor { + Runnable bindEditor(View v, EditableItem item, Runnable afterChange); + } + + private static final Map<Integer, Editor> EDITORS = editors(); + private static Runnable sUpdatePreset; + + private static Map<Integer, Editor> editors() { + Map<Integer, Editor> editors = new HashMap<Integer, Editor>(); + editors.put(TYPE_RESOURCE_ID, new IconEditor()); + editors.put(TYPE_TEXT, new TextEditor()); + editors.put(TYPE_INT, new IntEditor()); + if (Build.VERSION.SDK_INT >= 14) // switch 14, progress 14, uses chron 16 + editors.put(TYPE_BOOLEAN, new BooleanEditor()); + editors.put(TYPE_DROP_DOWN, new DropDownEditor()); + editors.put(TYPE_BITMAP, new BitmapEditor()); + if (Build.VERSION.SDK_INT >= 11) // fragments 11, when 11 + editors.put(TYPE_DATETIME, new DateTimeEditor()); + editors.put(TYPE_TEXT_LINES, new LinesEditor()); + return editors; + } + + public static View newEditor(final NotificationStudioActivity activity, + final ViewGroup parent, final EditableItem item) { + final View editorView = activity.getLayoutInflater().inflate(R.layout.editable_item, null); + ((TextView) editorView.findViewById(R.id.caption)).setText(item.getCaption(activity)); + + // bind visibility + editorView.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE); + item.setVisibilityListener(new Runnable(){ + public void run() { + editorView.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE); + }}); + + // bind type-specific behavior + Editor editor = EDITORS.get(item.getType()); + if (editor == null) + return null; + Runnable updater = editor.bindEditor(editorView, item, new Runnable() { + public void run() { + if (item.equals(EditableItem.PRESET)) { + updateEditors(parent); + } else { + EditableItem.PRESET.setValue(PRESET_CUSTOM); + sUpdatePreset.run(); + } + activity.refreshNotification(); + }}); + + // store the updater as the view tag + editorView.setTag(updater); + if (item.equals(EditableItem.PRESET)) + sUpdatePreset = updater; + + return editorView; + } + + private static void updateEditors(ViewGroup parent) { + for (int i = 0; i < parent.getChildCount(); i++) { + Object childTag = parent.getChildAt(i).getTag(); + if (childTag instanceof Runnable) { + ((Runnable) childTag).run(); + } + } + } + +} diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/IconEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/IconEditor.java new file mode 100644 index 000000000..71e900567 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/IconEditor.java @@ -0,0 +1,118 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.content.res.Resources; +import android.view.MotionEvent; +import android.view.SoundEffectConstants; +import android.view.View; +import android.view.View.OnTouchListener; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.HorizontalScrollView; +import android.widget.ImageView; +import android.widget.ImageView.ScaleType; +import android.widget.LinearLayout; + +import com.android.notificationstudio.R; +import com.android.notificationstudio.editor.Editors.Editor; +import com.android.notificationstudio.model.EditableItem; + +public class IconEditor implements Editor { + + public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { + final LinearLayout iconEditor = (LinearLayout) v.findViewById(R.id.icon_editor_layout); + final HorizontalScrollView scroller = + (HorizontalScrollView) v.findViewById(R.id.icon_editor_scroller); + scroller.setVisibility(View.VISIBLE); + + final Object[] displayValues = getAvailableValuesForDisplay(item); + final Runnable updateSelection = new Runnable() { + public void run() { + for (int i=0;i<iconEditor.getChildCount();i++) { + View imageViewHolder = iconEditor.getChildAt(i); + Object iconResId = imageViewHolder.getTag(); + boolean selected = item.hasValue() && item.getValue().equals(iconResId) || + !item.hasValue() && iconResId == null; + imageViewHolder.setSelected(selected); + if (selected) { + int x = imageViewHolder.getLeft(); + if (x < scroller.getScrollX() || + x > scroller.getScrollX() + scroller.getWidth()) { + scroller.scrollTo(imageViewHolder.getLeft(), 0); + } + } + } + }}; + + int iconSize = getIconSize(v.getResources()); + int outerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_outer_margin); + int innerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_inner_margin); + + for (final Object iconResId : displayValues) { + final FrameLayout imageViewHolder = new FrameLayout(v.getContext()); + imageViewHolder.setTag(iconResId); + final ImageView imageView = new ImageView(v.getContext()); + imageView.setScaleType(ScaleType.CENTER); + imageView.setOnTouchListener(new OnTouchListener(){ + public boolean onTouch(View v, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_UP) { + v.playSoundEffect(SoundEffectConstants.CLICK); + item.setValue(iconResId); + updateSelection.run(); + afterChange.run(); + } + return true; + }}); + + imageViewHolder.setBackgroundResource(R.drawable.icon_bg); + if (iconResId != null) + setImage(imageView, iconResId); + + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(iconSize, iconSize); + lp.bottomMargin = lp.topMargin = lp.leftMargin = lp.rightMargin = outerMargin; + imageViewHolder.setLayoutParams(lp); + + FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT); + flp.bottomMargin = flp.topMargin = flp.leftMargin = flp.rightMargin = innerMargin; + imageView.setLayoutParams(flp); + + imageViewHolder.addView(imageView); + iconEditor.addView(imageViewHolder); + } + updateSelection.run(); + return updateSelection; + } + + protected int getIconSize(Resources res) { + return res.getDimensionPixelSize(R.dimen.editor_icon_size_small); + } + + protected void setImage(ImageView imageView, Object value) { + imageView.setImageResource((Integer) value); + } + + private Object[] getAvailableValuesForDisplay(EditableItem item) { + Object[] avail = item.getAvailableValues(); + Object[] rt = new Object[avail.length + 1]; + System.arraycopy(avail, 0, rt, 1, avail.length); + return rt; + } + +}
\ No newline at end of file diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/IntEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/IntEditor.java new file mode 100644 index 000000000..393863eb1 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/IntEditor.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.text.InputType; + +public class IntEditor extends TextEditor { + + @Override + protected Object parseValue(String str) { + return str == null ? null : Integer.parseInt(str); + } + + protected int getInputType() { + return InputType.TYPE_CLASS_NUMBER; + } + +}
\ No newline at end of file diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/LinesEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/LinesEditor.java new file mode 100644 index 000000000..5173a992f --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/LinesEditor.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.text.InputType; + +public class LinesEditor extends TextEditor { + + @Override + protected int getInputType() { + return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE; + } + +} diff --git a/apps/NotificationStudio/src/com/android/notificationstudio/editor/TextEditor.java b/apps/NotificationStudio/src/com/android/notificationstudio/editor/TextEditor.java new file mode 100644 index 000000000..aa38eec34 --- /dev/null +++ b/apps/NotificationStudio/src/com/android/notificationstudio/editor/TextEditor.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.notificationstudio.editor; + +import android.text.Editable; +import android.text.InputType; +import android.text.TextWatcher; +import android.view.View; +import android.widget.EditText; + +import com.android.notificationstudio.R; +import com.android.notificationstudio.editor.Editors.Editor; +import com.android.notificationstudio.model.EditableItem; + +public class TextEditor implements Editor { + + public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { + final EditText textEditor = (EditText) v.findViewById(R.id.text_editor); + textEditor.setVisibility(View.VISIBLE); + textEditor.setInputType(getInputType()); + Runnable updateEditText = new Runnable() { + public void run() { + textEditor.setText(item.getValue() == null ? "" : item.getValue().toString()); + }}; + if (item.hasValue()) + updateEditText.run(); + textEditor.addTextChangedListener(new TextWatcher() { + public void afterTextChanged(Editable s) { + Object newVal = parseValue(s.length() == 0 ? null : s.toString()); + if (equal(newVal, item.getValue())) + return; + item.setValue(newVal); + afterChange.run(); + } + + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // noop + } + + public void onTextChanged(CharSequence s, int start, int before, int count) { + // noop + } + }); + return updateEditText; + } + + protected int getInputType() { + return InputType.TYPE_CLASS_TEXT; + } + + protected Object parseValue(String str) { + return str; + } + + private static boolean equal(Object a, Object b) { + return a == b || (a != null && a.equals(b)); + } + +}
\ No newline at end of file |