Révision | 66e9850b98a642c3c11b5b2724db7c38718fa5a2 (tree) |
---|---|
l'heure | 2013-04-20 10:46:11 |
Auteur | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
IntentDetailActivity にアクション編集機能を実装
@@ -12,4 +12,5 @@ | ||
12 | 12 | |
13 | 13 | * ResolveInfoListActivity |
14 | 14 | ** 各項目にデフォルト設定済みかどうかが分かるようにしたい |
15 | +** 可能かどうか調べる | |
15 | 16 |
@@ -60,8 +60,9 @@ | ||
60 | 60 | <activity android:name=".common.EditTextActivity" /> |
61 | 61 | |
62 | 62 | <!-- content provider --> |
63 | - <provider android:name=".intent.provider.IntentProvider" | |
64 | - android:authorities="org.routine_work.intent_laboratory.intentsprovider" | |
63 | + <provider | |
64 | + android:name=".intent.provider.IntentProvider" | |
65 | + android:authorities="org.routine_work.intent_laboratory.intentsprovider" | |
65 | 66 | /> |
66 | 67 | |
67 | 68 | </application> |
@@ -13,14 +13,31 @@ | ||
13 | 13 | android:id="@+id/home_button" |
14 | 14 | android:src="?attr/ic_home" |
15 | 15 | /> |
16 | + <!-- View Mode --> | |
16 | 17 | <TextView |
17 | 18 | style="@style/actionbar_title" |
19 | + android:id="@+id/title_textview" | |
18 | 20 | android:text="@string/select_action" |
19 | 21 | /> |
20 | 22 | <ImageButton |
21 | 23 | style="@style/actionbar_right_button" |
22 | - android:id="@+id/ok_button" | |
23 | - android:src="?attr/ic_ok" | |
24 | + android:id="@+id/search_button" | |
25 | + android:src="?attr/ic_search" | |
26 | + /> | |
27 | + <!-- Search Mode --> | |
28 | + <EditText | |
29 | + style="@style/actionbar_edittext" | |
30 | + android:id="@+id/search_edittext" | |
31 | + android:hint="@string/search_action_hint" | |
32 | + android:inputType="text" | |
33 | + android:visibility="gone" | |
34 | + android:imeOptions="actionSearch" | |
35 | + /> | |
36 | + <ImageButton | |
37 | + style="@style/actionbar_right_button" | |
38 | + android:id="@+id/cancel_search_button" | |
39 | + android:src="?attr/ic_clear" | |
40 | + android:visibility="gone" | |
24 | 41 | /> |
25 | 42 | </LinearLayout> |
26 | 43 |
@@ -17,11 +17,13 @@ | ||
17 | 17 | style="@style/actionbar_title" |
18 | 18 | android:text="@string/edit_intent" |
19 | 19 | /> |
20 | + <!-- | |
20 | 21 | <ImageButton |
21 | 22 | style="@style/actionbar_right_button" |
22 | 23 | android:id="@+id/ok_button" |
23 | 24 | android:src="?attr/ic_ok" |
24 | 25 | /> |
26 | + --> | |
25 | 27 | </LinearLayout> |
26 | 28 | |
27 | 29 | <!-- list --> |
@@ -49,5 +49,7 @@ | ||
49 | 49 | <!-- ActionList --> |
50 | 50 | <string name="action_list">Action List</string> |
51 | 51 | <string name="select_action">Select Action</string> |
52 | + <string name="search_action_hint">Select action</string> | |
53 | + <string name="search_action_description">Select action</string> | |
52 | 54 | |
53 | 55 | </resources> |
@@ -3,33 +3,70 @@ package org.routine_work.intent_lab.action; | ||
3 | 3 | import android.app.ListActivity; |
4 | 4 | import android.content.Intent; |
5 | 5 | import android.os.Bundle; |
6 | +import android.view.KeyEvent; | |
6 | 7 | import android.view.View; |
7 | 8 | import android.view.View.OnClickListener; |
9 | +import android.view.inputmethod.EditorInfo; | |
10 | +import android.widget.AdapterView; | |
11 | +import android.widget.EditText; | |
8 | 12 | import android.widget.ImageButton; |
9 | 13 | import android.widget.ListView; |
10 | 14 | import android.widget.SimpleAdapter; |
15 | +import android.widget.TextView; | |
11 | 16 | import java.lang.reflect.Field; |
12 | 17 | import java.lang.reflect.Modifier; |
13 | 18 | import java.util.ArrayList; |
14 | 19 | import java.util.HashMap; |
15 | 20 | import java.util.List; |
16 | 21 | import java.util.Map; |
22 | +import java.util.Map.Entry; | |
17 | 23 | import org.routine_work.intent_lab.R; |
24 | +import org.routine_work.utils.IMEUtils; | |
18 | 25 | import org.routine_work.utils.Log; |
19 | 26 | |
20 | 27 | /** |
21 | 28 | * 端末のOSの android.content.Intent クラスに定義されているアクションの一覧を表示する. |
22 | 29 | * 他のクラスで定義されているアクションは表示されない. |
30 | + * | |
23 | 31 | * @author sawai |
24 | 32 | */ |
25 | 33 | public class ActionListActivity extends ListActivity |
26 | - implements OnClickListener | |
34 | + implements OnClickListener, | |
35 | + AdapterView.OnItemClickListener, | |
36 | + TextView.OnEditorActionListener | |
27 | 37 | { |
38 | + | |
28 | 39 | public static final String EXTRA_ACTION_STRING = "EXTRA_ACTION_NAME"; |
29 | 40 | private static final String LOG_TAG = "intent-lab"; |
30 | 41 | private static final String KEY_ACTION_NAME = "ActionName"; |
31 | 42 | private static final String KEY_ACTION_STRING = "ActionValue"; |
43 | + private static final String SAVE_KEY_ACTION_MODE = "actionMode"; | |
32 | 44 | private static final List<Map<String, String>> ACTION_DATA_LIST; |
45 | + private static final int ACTION_MODE_NORMAL = 0; | |
46 | + private static final int ACTION_MODE_SEARCH = 1; | |
47 | + private static final Map<Integer, Boolean>[] ACTION_ITEM_VISIBILITY = new Map[] | |
48 | + { | |
49 | + new HashMap<Integer, Boolean>() | |
50 | + { // view mode | |
51 | + | |
52 | + { | |
53 | + this.put(R.id.title_textview, true); | |
54 | + this.put(R.id.search_button, true); | |
55 | + this.put(R.id.search_edittext, false); | |
56 | + this.put(R.id.cancel_search_button, false); | |
57 | + } | |
58 | + }, | |
59 | + new HashMap<Integer, Boolean>() | |
60 | + { // search mode | |
61 | + | |
62 | + { | |
63 | + this.put(R.id.title_textview, false); | |
64 | + this.put(R.id.search_button, false); | |
65 | + this.put(R.id.search_edittext, true); | |
66 | + this.put(R.id.cancel_search_button, true); | |
67 | + } | |
68 | + }, | |
69 | + }; | |
33 | 70 | private static final String[] MAPPING_FROM = new String[] |
34 | 71 | { |
35 | 72 | KEY_ACTION_NAME, |
@@ -40,7 +77,9 @@ public class ActionListActivity extends ListActivity | ||
40 | 77 | android.R.id.text1, |
41 | 78 | android.R.id.text2, |
42 | 79 | }; |
80 | + private int actionMode = -1; | |
43 | 81 | private ListView listView; |
82 | + private EditText searchEditText; | |
44 | 83 | |
45 | 84 | static |
46 | 85 | { |
@@ -80,66 +119,71 @@ public class ActionListActivity extends ListActivity | ||
80 | 119 | } |
81 | 120 | } |
82 | 121 | |
83 | - /** Called when the activity is first created. */ | |
122 | + /** | |
123 | + * Called when the activity is first created. | |
124 | + */ | |
84 | 125 | @Override |
85 | - public void onCreate(Bundle savedInstanceState) | |
126 | + protected void onCreate(Bundle savedInstanceState) | |
86 | 127 | { |
87 | 128 | Log.d(LOG_TAG, "ActionListActivity#onCreate() Hello"); |
88 | 129 | super.onCreate(savedInstanceState); |
89 | 130 | setContentView(R.layout.action_list_activity); |
90 | 131 | |
91 | - Intent intent = getIntent(); | |
92 | - String action = intent.getAction(); | |
93 | - String actionString = intent.getStringExtra(EXTRA_ACTION_STRING); | |
94 | - Log.d(LOG_TAG, "action => " + action); | |
95 | 132 | |
96 | 133 | listView = (ListView) findViewById(android.R.id.list); |
97 | - ImageButton okButton = (ImageButton) findViewById(R.id.ok_button); | |
98 | 134 | ImageButton homeButton = (ImageButton) findViewById(R.id.home_button); |
135 | + homeButton.setOnClickListener(this); | |
99 | 136 | |
100 | - if (Intent.ACTION_PICK.equals(action)) | |
101 | - { | |
102 | - Log.d(LOG_TAG, "ActionListActivity#onCreate() ACTION_PICK"); | |
137 | + // view mode items | |
138 | + ImageButton searchButton = (ImageButton) findViewById(R.id.search_button); | |
139 | + searchButton.setOnClickListener(this); | |
140 | + | |
141 | + // search mode items | |
142 | + searchEditText = (EditText) findViewById(R.id.search_edittext); | |
143 | + searchEditText.setOnEditorActionListener(this); | |
144 | + ImageButton cancelSearchButton = (ImageButton) findViewById(R.id.cancel_search_button); | |
145 | + cancelSearchButton.setOnClickListener(this); | |
103 | 146 | |
104 | - SimpleAdapter simpleAdapter = new SimpleAdapter(this, ACTION_DATA_LIST, | |
105 | - R.layout.action_list_item_single_choice, | |
106 | - MAPPING_FROM, MAPPING_TO); | |
107 | - setListAdapter(simpleAdapter); | |
147 | + SimpleAdapter simpleAdapter = new SimpleAdapter(this, | |
148 | + ACTION_DATA_LIST, | |
149 | + R.layout.action_list_item, | |
150 | + MAPPING_FROM, MAPPING_TO); | |
151 | + setListAdapter(simpleAdapter); | |
108 | 152 | |
109 | - listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); | |
110 | - setItemCheckedByActionString(actionString); | |
153 | + listView.setChoiceMode(ListView.CHOICE_MODE_NONE); | |
154 | + listView.setOnItemClickListener(this); | |
111 | 155 | |
112 | - okButton.setOnClickListener(this); | |
113 | - okButton.setVisibility(View.VISIBLE); | |
114 | - homeButton.setOnClickListener(this); | |
115 | - homeButton.setVisibility(View.VISIBLE); | |
156 | + if (savedInstanceState != null) | |
157 | + { | |
158 | + int mode = savedInstanceState.getInt(SAVE_KEY_ACTION_MODE); | |
159 | + setActionMode(mode); | |
116 | 160 | } |
117 | 161 | else |
118 | 162 | { |
119 | - Log.d(LOG_TAG, "ActionListActivity#onCreate() ACTION_VIEW"); | |
120 | - | |
121 | - SimpleAdapter simpleAdapter = new SimpleAdapter(this, ACTION_DATA_LIST, | |
122 | - R.layout.action_list_item, | |
123 | - MAPPING_FROM, MAPPING_TO); | |
124 | - setListAdapter(simpleAdapter); | |
125 | - | |
126 | - listView.setChoiceMode(ListView.CHOICE_MODE_NONE); | |
127 | - | |
128 | - okButton.setVisibility(View.GONE); | |
129 | - homeButton.setOnClickListener(this); | |
130 | - homeButton.setVisibility(View.VISIBLE); | |
163 | + setActionMode(ACTION_MODE_NORMAL); | |
131 | 164 | } |
132 | 165 | |
133 | 166 | Log.d(LOG_TAG, "ActionListActivity#onCreate() Bye"); |
134 | 167 | } |
135 | 168 | |
169 | + @Override | |
170 | + protected void onSaveInstanceState(Bundle outState) | |
171 | + { | |
172 | + outState.putInt(SAVE_KEY_ACTION_MODE, actionMode); | |
173 | + super.onSaveInstanceState(outState); | |
174 | + } | |
175 | + | |
176 | + @Override | |
136 | 177 | public void onClick(View view) |
137 | 178 | { |
138 | 179 | int id = view.getId(); |
139 | 180 | switch (id) |
140 | 181 | { |
141 | - case R.id.ok_button: | |
142 | - ok(); | |
182 | + case R.id.search_button: | |
183 | + enterSearchMode(); | |
184 | + break; | |
185 | + case R.id.cancel_search_button: | |
186 | + leaveSearchMode(); | |
143 | 187 | break; |
144 | 188 | case R.id.home_button: |
145 | 189 | cancel(); |
@@ -148,23 +192,29 @@ public class ActionListActivity extends ListActivity | ||
148 | 192 | } |
149 | 193 | } |
150 | 194 | |
151 | - private void ok() | |
195 | + public void onItemClick(AdapterView<?> parent, View view, int position, long id) | |
152 | 196 | { |
153 | - int position = listView.getCheckedItemPosition(); | |
154 | - Log.d(LOG_TAG, "checkedItemPosition => " + position); | |
155 | - if (position >= 0) | |
156 | - { | |
157 | - Map<String, String> data = ACTION_DATA_LIST.get(position); | |
158 | - String actionString = data.get(KEY_ACTION_STRING); | |
159 | - Intent resultIntent = new Intent(); | |
160 | - resultIntent.putExtra(EXTRA_ACTION_STRING, actionString); | |
161 | - setResult(RESULT_OK, resultIntent); | |
162 | - } | |
163 | - else | |
197 | + Log.v(LOG_TAG, "Hello"); | |
198 | + Log.v(LOG_TAG, "position => " + position + ", id => " + id); | |
199 | + | |
200 | + Map<String, String> actionData = ACTION_DATA_LIST.get(position); | |
201 | + String actionString = actionData.get(KEY_ACTION_STRING); | |
202 | + Intent resultIntent = new Intent(); | |
203 | + resultIntent.putExtra(EXTRA_ACTION_STRING, actionString); | |
204 | + setResult(RESULT_OK, resultIntent); | |
205 | + finish(); | |
206 | + | |
207 | + Log.v(LOG_TAG, "Bye"); | |
208 | + } | |
209 | + | |
210 | + // TextView.OnEditorActionListener | |
211 | + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) | |
212 | + { | |
213 | + if (actionId == EditorInfo.IME_ACTION_SEARCH) | |
164 | 214 | { |
165 | - setResult(RESULT_CANCELED); | |
215 | + IMEUtils.hideSoftKeyboardWindow(this, v); | |
166 | 216 | } |
167 | - finish(); | |
217 | + return true; | |
168 | 218 | } |
169 | 219 | |
170 | 220 | private void cancel() |
@@ -173,26 +223,59 @@ public class ActionListActivity extends ListActivity | ||
173 | 223 | finish(); |
174 | 224 | } |
175 | 225 | |
176 | - private void setItemCheckedByActionString(String actionString) | |
226 | + private void setActionMode(int newActionMode) | |
177 | 227 | { |
178 | - Log.d(LOG_TAG, "ActionListActivity#setItemCheckedByActionString() Hello"); | |
179 | - Log.d(LOG_TAG, " actionString => " + actionString); | |
180 | - if (actionString == null || actionString.length() == 0) | |
181 | - { | |
182 | - return; | |
183 | - } | |
228 | + Log.v(LOG_TAG, "Hello"); | |
184 | 229 | |
185 | - int size = ACTION_DATA_LIST.size(); | |
186 | - for (int i = 0; i < size; i++) | |
230 | + if (actionMode != newActionMode) | |
187 | 231 | { |
188 | - Map<String, String> actionData = ACTION_DATA_LIST.get(i); | |
189 | - String string = actionData.get(KEY_ACTION_STRING); | |
190 | - if (string.equals(actionString)) | |
232 | + actionMode = newActionMode; | |
233 | + Log.d(LOG_TAG, "update actionMode => " + actionMode); | |
234 | + | |
235 | + // update visibility | |
236 | + for (Entry<Integer, Boolean> e : ACTION_ITEM_VISIBILITY[actionMode].entrySet()) | |
191 | 237 | { |
192 | - Log.d(LOG_TAG, " found! i => " + i); | |
193 | - listView.setItemChecked(i, true); | |
194 | - break; | |
238 | + View actionItemView = findViewById(e.getKey()); | |
239 | + if (actionItemView != null) | |
240 | + { | |
241 | + if (e.getValue()) | |
242 | + { | |
243 | + actionItemView.setVisibility(View.VISIBLE); | |
244 | + } | |
245 | + else | |
246 | + { | |
247 | + actionItemView.setVisibility(View.GONE); | |
248 | + } | |
249 | + } | |
195 | 250 | } |
196 | 251 | } |
252 | + | |
253 | + Log.v(LOG_TAG, "Bye"); | |
254 | + } | |
255 | + | |
256 | + private void leaveSearchMode() | |
257 | + { | |
258 | + Log.v(LOG_TAG, "Hello"); | |
259 | + | |
260 | + if (actionMode != ACTION_MODE_NORMAL) | |
261 | + { | |
262 | + searchEditText.setText(null); | |
263 | + getListView().requestFocus(); | |
264 | + setActionMode(ACTION_MODE_NORMAL); | |
265 | + } | |
266 | + | |
267 | + Log.v(LOG_TAG, "Bye"); | |
268 | + } | |
269 | + | |
270 | + private void enterSearchMode() | |
271 | + { | |
272 | + Log.v(LOG_TAG, "Hello"); | |
273 | + if (actionMode != ACTION_MODE_SEARCH) | |
274 | + { | |
275 | + setActionMode(ACTION_MODE_SEARCH); | |
276 | + IMEUtils.requestKeyboardFocus(searchEditText); | |
277 | + searchEditText.requestFocus(); | |
278 | + } | |
279 | + Log.v(LOG_TAG, "Bye"); | |
197 | 280 | } |
198 | 281 | } |
@@ -19,6 +19,7 @@ import android.widget.TextView; | ||
19 | 19 | import android.widget.Toast; |
20 | 20 | import java.util.Set; |
21 | 21 | import org.routine_work.intent_lab.R; |
22 | +import org.routine_work.intent_lab.action.ActionListActivity; | |
22 | 23 | import org.routine_work.intent_lab.common.EditTextActivity; |
23 | 24 | import org.routine_work.intent_lab.intent.provider.IntentStore; |
24 | 25 | import org.routine_work.utils.Log; |
@@ -66,12 +67,17 @@ public class IntentDetailActivity extends ListActivity | ||
66 | 67 | { |
67 | 68 | Log.d(LOG_TAG, "onItemClick() : Hello"); |
68 | 69 | Log.d(LOG_TAG, "position => " + position); |
70 | + | |
69 | 71 | switch (position) |
70 | 72 | { |
71 | 73 | case POSITION_NAME: |
72 | 74 | startNameEditActivity(); |
73 | 75 | break; |
76 | + case POSITION_ACTION: | |
77 | + startActionPickActivity(); | |
78 | + break; | |
74 | 79 | } |
80 | + | |
75 | 81 | Log.d(LOG_TAG, "onItemClick() : Bye"); |
76 | 82 | } |
77 | 83 |
@@ -124,6 +130,14 @@ public class IntentDetailActivity extends ListActivity | ||
124 | 130 | intentDetailListAdapter.notifyDataSetChanged(); |
125 | 131 | } |
126 | 132 | break; |
133 | + case REQUEST_CODE_EDIT_INTENT_ACTION: | |
134 | + if (resultCode == RESULT_OK) | |
135 | + { | |
136 | + String actionString = data.getStringExtra(ActionListActivity.EXTRA_ACTION_STRING); | |
137 | + targetIntent.setAction(actionString); | |
138 | + intentDetailListAdapter.notifyDataSetChanged(); | |
139 | + } | |
140 | + break; | |
127 | 141 | default: |
128 | 142 | super.onActivityResult(requestCode, resultCode, data); |
129 | 143 | } |
@@ -166,6 +180,15 @@ public class IntentDetailActivity extends ListActivity | ||
166 | 180 | startActivityForResult(intent, REQUEST_CODE_EDIT_INTENT_NAME); |
167 | 181 | } |
168 | 182 | |
183 | + private void startActionPickActivity() | |
184 | + { | |
185 | + Intent intent = new Intent(this, ActionListActivity.class); | |
186 | + intent.setAction(Intent.ACTION_PICK); | |
187 | + String actionSring = targetIntent.getAction(); | |
188 | + intent.putExtra(ActionListActivity.EXTRA_ACTION_STRING, actionSring); | |
189 | + startActivityForResult(intent, REQUEST_CODE_EDIT_INTENT_ACTION); | |
190 | + } | |
191 | + | |
169 | 192 | class IntentDetailListAdapter extends BaseAdapter |
170 | 193 | { |
171 | 194 |
@@ -88,7 +88,6 @@ public class IMEUtils | ||
88 | 88 | public static void requestKeyboardFocusByClick(EditText editText) |
89 | 89 | { |
90 | 90 | requestKeyboardFocusByClick(editText, 200); |
91 | - | |
92 | 91 | } |
93 | 92 | |
94 | 93 | public static void requestKeyboardFocusByClick(EditText editText, long delayInMillis) |