| 1 |
/* |
| 2 |
* Copyright 2012 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.notificationstudio.editor; |
| 18 |
|
| 19 |
import android.util.TypedValue; |
| 20 |
import android.view.View; |
| 21 |
import android.view.ViewGroup; |
| 22 |
import android.widget.AdapterView; |
| 23 |
import android.widget.AdapterView.OnItemSelectedListener; |
| 24 |
import android.widget.ArrayAdapter; |
| 25 |
import android.widget.Spinner; |
| 26 |
import android.widget.TextView; |
| 27 |
|
| 28 |
import com.android.notificationstudio.R; |
| 29 |
import com.android.notificationstudio.editor.Editors.Editor; |
| 30 |
import com.android.notificationstudio.model.EditableItem; |
| 31 |
|
| 32 |
public class DropDownEditor implements Editor { |
| 33 |
|
| 34 |
public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) { |
| 35 |
final Spinner dropDownEditor = (Spinner) v.findViewById(R.id.drop_down_editor); |
| 36 |
dropDownEditor.setVisibility(View.VISIBLE); |
| 37 |
Integer[] values = item.getAvailableValuesInteger(); |
| 38 |
final float textSize = v.getResources().getDimensionPixelSize(R.dimen.editor_text_size); |
| 39 |
final int p = v.getResources().getDimensionPixelSize(R.dimen.editor_drop_down_padding); |
| 40 |
final int p2 = p * 2; |
| 41 |
final ArrayAdapter<Integer> adapter = |
| 42 |
new ArrayAdapter<Integer>(v.getContext(), android.R.layout.simple_spinner_item, values) { |
| 43 |
|
| 44 |
@Override |
| 45 |
public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 46 |
TextView v = (TextView) super.getDropDownView(position, convertView, parent); |
| 47 |
v.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); |
| 48 |
v.setPadding(p2, p2, p2, p2); |
| 49 |
v.setText(v.getResources().getString(getItem(position))); |
| 50 |
return v; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 55 |
TextView v = (TextView) super.getView(position, convertView, parent); |
| 56 |
v.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); |
| 57 |
v.setPadding(p, p, p, p); |
| 58 |
v.setText(v.getResources().getString(getItem(position))); |
| 59 |
return v; |
| 60 |
} |
| 61 |
}; |
| 62 |
dropDownEditor.setAdapter(adapter); |
| 63 |
Runnable updateSelection = new Runnable() { |
| 64 |
public void run() { |
| 65 |
dropDownEditor.setSelection(adapter.getPosition(item.getValueInt())); |
| 66 |
}}; |
| 67 |
if (item.hasValue()) |
| 68 |
updateSelection.run(); |
| 69 |
dropDownEditor.setOnItemSelectedListener(new OnItemSelectedListener(){ |
| 70 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 71 |
Object oldValue = item.getValue(); |
| 72 |
Object newValue = adapter.getItem(position); |
| 73 |
if (newValue.equals(oldValue)) |
| 74 |
return; |
| 75 |
item.setValue(newValue); |
| 76 |
afterChange.run(); |
| 77 |
} |
| 78 |
public void onNothingSelected(AdapterView<?> parent) { |
| 79 |
// noop |
| 80 |
}}); |
| 81 |
return updateSelection; |
| 82 |
} |
| 83 |
|
| 84 |
} |