OSDN Git Service

ver1.3.0a
[jugglemaster/source.git] / workspace / JuggleMaster / src / com / jm / PatternManager.java
1 package com.jm;
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import android.app.AlertDialog;
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.os.Bundle;
13 import android.text.Editable;
14 import android.text.TextWatcher;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.AdapterView;
18 import android.widget.AdapterView.OnItemClickListener;
19 import android.widget.ArrayAdapter;
20 import android.widget.Button;
21 import android.widget.LinearLayout;
22 import android.widget.ListView;
23 import android.widget.TextView;
24
25 import com.jm.common.Constant;
26 import com.jm.db.Dao;
27 import com.jm.db.PatternFile;
28 import com.jm.pref.EditPrefUtil;
29 import com.jm.utility.BaseActivity;
30 import com.jm.utility.Debug;
31 import com.jm.utility.JmException;
32
33 public class PatternManager extends BaseActivity {
34         private enum Status {
35                 NORMAL, SELECTED, INPUT_FILENAME, UPDATE, NONE
36         };
37
38         private static int selectedIndex = -1;
39         private EditPrefUtil pref = new EditPrefUtil(this);
40         private Button patternAddButton;
41         private Button patternUpdateButton;
42         private Button patternDeleteButton;
43         private Button patternCloseButton;
44         private LinearLayout patternLayout;
45         private TextView patternFileText;
46         private ListView listView;
47         private PatternFile[] list;
48         private Status status = Status.NORMAL;
49         private InputStream inputStream;
50         private int selectedPatternIndex = -1;
51
52         @Override
53         public void onCreate(Bundle savedInstanceState) {
54                 super.onCreate(savedInstanceState);
55                 setContentView(R.layout.pattern_manager);
56
57                 patternAddButton = (Button) findViewById(R.id.patternAddButton);
58                 patternUpdateButton = (Button) findViewById(R.id.patternUpdateButton);
59                 patternDeleteButton = (Button) findViewById(R.id.patternDeleteButton);
60                 patternCloseButton = (Button) findViewById(R.id.patternCloseButton);
61                 listView = (ListView) findViewById(R.id.patternList);
62                 patternLayout = (LinearLayout) findViewById(R.id.patternLayout);
63                 patternFileText = (TextView) findViewById(R.id.patternFileText);
64
65                 listView.setOnItemClickListener(new OnItemClickListener() {
66                         @Override
67                         public void onItemClick(AdapterView<?> parent, View view,
68                                         int position, long id) {
69                                 listViewOnItemClick(parent, view, position, id);
70                         }
71                 });
72
73                 patternAddButton.setOnClickListener(new OnClickListener() {
74                         @Override
75                         public void onClick(View view) {
76                                 patternAddButtonOnClick();
77                         }
78                 });
79                 patternUpdateButton.setOnClickListener(new OnClickListener() {
80                         @Override
81                         public void onClick(View view) {
82                                 patternUpdateButtonOnClick();
83                         }
84                 });
85                 patternDeleteButton.setOnClickListener(new OnClickListener() {
86                         @Override
87                         public void onClick(View view) {
88                                 patternDeleteButtonOnClick();
89                         }
90                 });
91                 patternCloseButton.setOnClickListener(new OnClickListener() {
92                         @Override
93                         public void onClick(View view) {
94                                 patternCloseButtonOnClick();
95                         }
96                 });
97                 patternFileText.addTextChangedListener(new TextWatcher() {
98                         @Override
99                         public void afterTextChanged(Editable arg0) {
100
101                         }
102
103                         @Override
104                         public void beforeTextChanged(CharSequence arg0, int arg1,
105                                         int arg2, int arg3) {
106
107                         }
108
109                         @Override
110                         public void onTextChanged(CharSequence arg0, int arg1, int arg2,
111                                         int arg3) {
112                                 setStatus();
113                         }
114                 });
115                 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
116
117                 selectedIndex = pref.getInt(Constant.PREF_PATTERN_FILE_INDEX);
118                 setStatus((selectedIndex >= 0) ? Status.SELECTED : Status.NORMAL);
119                 updateList();
120
121                 selectedPatternIndex = pref
122                                 .getInt(Constant.PREF_SELECTED_PATTERN_INDEX);
123         }
124
125         @Override
126         protected void onStart() {
127                 super.onStart();
128
129                 selectedIndex = pref.getInt(Constant.PREF_PATTERN_FILE_INDEX);
130         }
131
132         @Override
133         protected void onPause() {
134                 pref.put(Constant.PREF_PATTERN_FILE_INDEX, selectedIndex);
135                 pref.update();
136
137                 super.onPause();
138         }
139
140         protected void onActivityResult(int requestCode, int resultCode,
141                         Intent intent) {
142                 super.onActivityResult(requestCode, resultCode, intent);
143
144                 if (resultCode != RESULT_OK) {
145                         return;
146                 }
147
148                 switch (requestCode) {
149                 case 0:
150                         Uri uri = intent.getData();
151                         try {
152                                 inputStream = getContentResolver().openInputStream(uri);
153                                 patternFileText.setText("");
154                                 setStatus(Status.INPUT_FILENAME);
155                         } catch (FileNotFoundException e) {
156                                 Debug.d(this, e.getMessage());
157                         }
158                         break;
159                 }
160         }
161
162         private void updateList() {
163                 Dao dao = Dao.getInstance();
164                 updateList(dao);
165         }
166
167         private void updateList(Dao dao) {
168                 List<PatternFile> l = dao.getPatternFile();
169                 if (l == null) {
170                         return;
171                 }
172                 list = l.toArray(new PatternFile[0]);
173
174                 ArrayAdapter<PatternFile> adapter = new ArrayAdapter<PatternFile>(this,
175                                 R.layout.simple_list_item_2, R.id.text3, list);
176                 listView.setAdapter(adapter);
177
178                 setStatus();
179         }
180
181         private void listViewOnItemClick(AdapterView<?> parent, View view,
182                         int position, long id) {
183                 selectedIndex = position;
184                 setStatus(Status.SELECTED);
185         }
186
187         private void patternAddButtonOnClick() {
188                 switch (status) {
189                 case NORMAL:
190                 case SELECTED:
191                         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
192
193                         intent.setType("text/*");
194                         try {
195                                 startActivityForResult(intent, 0);
196                         }
197                         catch (Exception e){
198                                 // 起動失敗
199                                 Debug.d(this, e.getMessage());
200                         }
201                         break;
202                 case INPUT_FILENAME:
203                         if (inputStream == null) {
204                                 return;
205                         }
206                         String text = patternFileText.getText().toString();
207                         Dao dao = Dao.getInstance();
208                         dao.insertPatternList(inputStream, text, true);
209                         updateList();
210                         inputStream = null;
211                         selectedIndex = -1;
212                         setStatus(Status.NORMAL);
213                         break;
214                 default:
215                         break;
216                 }
217         }
218
219         private void patternUpdateButtonOnClick() {
220                 if (selectedIndex < 0) {
221                         return;
222                 }
223
224                 setStatus(Status.UPDATE);
225
226                 PatternFile p = list[selectedIndex];
227                 long pid = p.getId();
228
229                 Intent intent = new Intent();
230                 intent.putExtra(Constant.EXTRAS_ID, pid);
231                 setResult(RESULT_OK, intent);
232
233                 finish();
234         }
235
236         private void patternDeleteButtonOnClick() {
237                 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
238                 alertDialogBuilder.setTitle(getString(R.string.title_delete));
239                 alertDialogBuilder
240                                 .setMessage(getString(R.string.message_delete_patternfile));
241                 alertDialogBuilder.setPositiveButton(android.R.string.ok,
242                                 new DialogInterface.OnClickListener() {
243                                         @Override
244                                         public void onClick(DialogInterface dialog, int which) {
245                                                 if (selectedIndex < 0) {
246                                                         return;
247                                                 }
248
249                                                 PatternFile p = list[selectedIndex];
250                                                 long pid = p.getId();
251
252                                                 Dao dao = Dao.getInstance();
253                                                 try {
254                                                         dao.deletePatternFile(pid);
255                                                         selectedIndex = -1;
256                                                 } catch (JmException e) {
257                                                         Debug.d(this, e.getMessage());
258                                                         return;
259                                                 }
260
261                                                 setStatus(Status.NORMAL);
262                                                 updateList();
263                                         }
264                                 });
265                 alertDialogBuilder.setNeutralButton(android.R.string.no,
266                                 new DialogInterface.OnClickListener() {
267                                         @Override
268                                         public void onClick(DialogInterface dialog, int which) {
269                                                 setStatus(Status.SELECTED);
270                                         }
271                                 });
272                 alertDialogBuilder.setCancelable(true);
273                 AlertDialog alertDialog = alertDialogBuilder.create();
274                 alertDialog.show();
275         }
276
277         private void patternCloseButtonOnClick() {
278                 finish();
279         }
280
281         private void setStatus() {
282                 setStatus(Status.NONE);
283         }
284
285         private void setStatus(Status s) {
286                 if (s != Status.NONE) {
287                         status = s;
288                 }
289
290                 switch (status) {
291                 case NORMAL:
292                         patternLayout.setVisibility(View.GONE);
293                         listView.setVisibility(View.VISIBLE);
294                         listView.setEnabled(true);
295                         patternAddButton.setEnabled(true);
296                         patternUpdateButton.setEnabled(false);
297                         patternDeleteButton.setEnabled(false);
298                         patternCloseButton.setEnabled(true);
299                         break;
300                 case SELECTED:
301                         patternLayout.setVisibility(View.GONE);
302                         listView.setVisibility(View.VISIBLE);
303                         listView.setEnabled(true);
304                         patternAddButton.setEnabled(true);
305                         patternUpdateButton.setEnabled(true);
306                         boolean b = false;
307                         if (list != null && selectedIndex >= 0) {
308                                 PatternFile p = list[selectedIndex];
309                                 long pid = p.getId();
310                                 if (pid != selectedPatternIndex && p.isWritable()) {
311                                         b = true;
312                                 }
313                         }
314                         patternDeleteButton.setEnabled(b);
315                         patternCloseButton.setEnabled(true);
316                         break;
317                 case INPUT_FILENAME:
318                         patternLayout.setVisibility(View.VISIBLE);
319                         listView.setVisibility(View.INVISIBLE);
320                         listView.setEnabled(false);
321                         patternAddButton.setEnabled(patternFileText.getText().length() > 0);
322                         patternUpdateButton.setEnabled(false);
323                         patternDeleteButton.setEnabled(false);
324                         patternCloseButton.setEnabled(true);
325                         break;
326                 case UPDATE:
327                         patternLayout.setVisibility(View.GONE);
328                         listView.setVisibility(View.VISIBLE);
329                         listView.setEnabled(false);
330                         patternAddButton.setEnabled(false);
331                         patternUpdateButton.setEnabled(false);
332                         patternDeleteButton.setEnabled(false);
333                         patternCloseButton.setEnabled(false);
334                         break;
335                 default:
336                         break;
337                 }
338         }
339
340         public static void init(Context context) {
341                 EditPrefUtil pref = new EditPrefUtil(context);
342                 pref.put(Constant.PREF_PATTERN_FILE_INDEX, -1);
343                 pref.update();
344
345                 selectedIndex = -1;
346         }
347 }