• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Navigation voice changer app for Android


Commit MetaInfo

Revision61f6ae454c79f71f72e62a4a396f342f3212af83 (tree)
Time2013-07-31 04:18:06
AuthorHMML <hmml3939@gmai...>
CommiterHMML

Log Message

Context menu on download list.

Change Summary

Incremental Difference

--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -37,5 +37,6 @@
3737 <string name="download_progress">Donwloading...</string>
3838 <string name="err_no_sd">No external storage is detected. You need to check your SD card or storage setting.</string>
3939 <string name="err_no_sd_title">Medium Error</string>
40+ <string name="c_menu_download">Download</string>
4041
4142 </resources>
--- a/src/jp/nekoteki/android/navivoicechanger/DownloadActivity.java
+++ b/src/jp/nekoteki/android/navivoicechanger/DownloadActivity.java
@@ -15,12 +15,15 @@ import android.os.AsyncTask;
1515 import android.os.Bundle;
1616 import android.app.Activity;
1717 import android.util.Log;
18+import android.view.ContextMenu;
1819 import android.view.Menu;
1920 import android.view.MenuItem;
2021 import android.view.View;
2122 import android.view.ViewGroup;
23+import android.view.ContextMenu.ContextMenuInfo;
2224 import android.view.ViewGroup.LayoutParams;
2325 import android.widget.AbsListView.OnScrollListener;
26+import android.widget.AdapterView.AdapterContextMenuInfo;
2427 import android.widget.AbsListView;
2528 import android.widget.AdapterView;
2629 import android.widget.BaseAdapter;
@@ -28,6 +31,7 @@ import android.widget.LinearLayout;
2831 import android.widget.ListView;
2932 import android.widget.RelativeLayout;
3033 import android.widget.TextView;
34+import android.widget.Toast;
3135 import android.support.v4.app.NavUtils;
3236 import android.annotation.TargetApi;
3337 import android.content.Context;
@@ -65,6 +69,13 @@ public class DownloadActivity extends Activity {
6569 return ((RemoteVoiceData) this.getItem(position)).getId();
6670 }
6771
72+ public RemoteVoiceData getItemById(int id) {
73+ for (RemoteVoiceData rvd: this.list) {
74+ if (rvd.getId() == id) return rvd;
75+ }
76+ return null;
77+ }
78+
6879 public void reset() {
6980 this.cur_page = 1;
7081 this.list = new ArrayList<RemoteVoiceData>();
@@ -135,7 +146,7 @@ public class DownloadActivity extends Activity {
135146 for (VoiceData vd: locals) {
136147 if (vd.getId() != rvd.getId())
137148 continue;
138- rvd.setDownloaded(true);
149+ rvd.setVoiceData(vd);
139150 break;
140151 }
141152 this.adapter.list.add(rvd);
@@ -179,8 +190,14 @@ public class DownloadActivity extends Activity {
179190
180191 }
181192
193+ private static final int C_MENU_PREVIEW = 0;
194+ private static final int C_MENU_DOWNLOAD = 1;
195+ private static final int C_MENU_INSTALL = 2;
196+ private static final int C_MENU_RATE = 3;
197+ private static final int C_MENU_DELETE = 4;
198+
182199 public View list_footer_marker = null;
183- public RemoteVoiceDataAdapter vd_list_adapter;
200+ public RemoteVoiceDataAdapter rvd_list_adapter;
184201 public List<VoiceData> voice_data_list;
185202
186203 @Override
@@ -190,13 +207,13 @@ public class DownloadActivity extends Activity {
190207 // Show the Up button in the action bar.
191208 setupActionBar();
192209
193- this.vd_list_adapter = new RemoteVoiceDataAdapter(this);
210+ this.rvd_list_adapter = new RemoteVoiceDataAdapter(this);
194211 this.list_footer_marker = getLayoutInflater().inflate(R.layout.list_progress_footer, null);
195- this.voice_data_list = VoiceData.scanVoiceData(this);
212+ this.scanVoiceData();
196213
197214 ListView lv = (ListView) findViewById(R.id.download_item_list);
198215 lv.addFooterView(this.list_footer_marker);
199- lv.setAdapter(this.vd_list_adapter);
216+ lv.setAdapter(this.rvd_list_adapter);
200217 lv.setOnScrollListener(new OnScrollListener() {
201218 @Override
202219 public void onScrollStateChanged(AbsListView view, int scrollState) {
@@ -207,51 +224,23 @@ public class DownloadActivity extends Activity {
207224 public void onScroll(AbsListView view, int firstVisibleItem,
208225 int visibleItemCount, int totalItemCount) {
209226 if (totalItemCount == firstVisibleItem + visibleItemCount)
210- ((DownloadActivity) view.getContext()).vd_list_adapter.loadList(view);
227+ ((DownloadActivity) view.getContext()).rvd_list_adapter.loadList(view);
211228 }
212229 });
213- lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
214230
231+ registerForContextMenu(lv);
232+
233+ lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
215234 @Override
216235 public void onItemClick(AdapterView<?> list, View item, int pos, long id) {
217- RemoteVoiceData rvd = (RemoteVoiceData) list.getAdapter().getItem(pos);
218- if (rvd.isDownloaded()) return; // TODO: Show dialog to choice to go to install activity or re-download.
219-
220- ((DownloadActivity) list.getContext()).setDownloadOverlay(true);
221-
222- new AsyncTask<Object, Void, Boolean>() {
223- protected DownloadActivity context;
224-// protected int pos;
225-// protected long id;
226- protected RemoteVoiceData rvd;
227-
228- @Override
229- protected Boolean doInBackground(Object... params) {
230- this.context = (DownloadActivity) params[0];
231- this.rvd = (RemoteVoiceData) params[1];
232-// this.pos = ((Integer) params[2]).intValue();
233-// this.id = ((Long) params[3]).longValue();
234- try {
235- rvd.download(context);
236- } catch (Exception e) {
237- e.printStackTrace();
238- return false;
239- }
240- return true;
241- }
242-
243- protected void onPostExecute(Boolean flag) {
244- if (flag) {
245- this.rvd.setDownloaded(true);
246- context.vd_list_adapter.notifyDataSetChanged();
247- }
248- context.setDownloadOverlay(false);
249- }
250-
251- }.execute(list.getContext(), rvd, pos, id);
236+ item.performLongClick();
252237 }
253238 });
254239 }
240+
241+ protected void scanVoiceData() {
242+ this.voice_data_list = VoiceData.scanVoiceData(getApplicationContext());
243+ }
255244
256245 /**
257246 * Set up the {@link android.app.ActionBar}, if the API is available.
@@ -299,4 +288,79 @@ public class DownloadActivity extends Activity {
299288 public void goInstallListFromMenu(MenuItem item) {
300289 startActivity(new Intent(DownloadActivity.this, InstallListActivity.class));
301290 }
291+
292+ public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
293+ super.onCreateContextMenu(menu, view, info);
294+ AdapterContextMenuInfo ainfo = (AdapterContextMenuInfo) info;
295+ ListView listView = (ListView)view;
296+
297+ RemoteVoiceData rvd = (RemoteVoiceData) listView.getItemAtPosition(ainfo.position);
298+ menu.setHeaderTitle(rvd.getTitle());
299+ menu.add(rvd.getId(), C_MENU_PREVIEW, 0, R.string.c_menu_preview);
300+ menu.add(rvd.getId(), C_MENU_DOWNLOAD, 0, R.string.c_menu_download);
301+ menu.add(rvd.getId(), C_MENU_INSTALL, 0, R.string.c_menu_install);
302+ menu.add(rvd.getId(), C_MENU_RATE, 0, R.string.c_menu_rate);
303+ menu.add(rvd.getId(), C_MENU_DELETE, 0, R.string.c_menu_delete);
304+
305+ if (rvd.isDownloaded()) {
306+ menu.getItem(C_MENU_DOWNLOAD).setEnabled(false);
307+ } else {
308+ menu.getItem(C_MENU_INSTALL).setEnabled(false);
309+ menu.getItem(C_MENU_DELETE).setEnabled(false);
310+ menu.getItem(C_MENU_RATE).setEnabled(false);
311+ }
312+ }
313+
314+ public boolean onContextItemSelected(MenuItem item) {
315+ RemoteVoiceData rvd = this.rvd_list_adapter.getItemById(item.getGroupId());
316+ if (rvd == null) return true;
317+ switch (item.getItemId()) {
318+ case C_MENU_PREVIEW:
319+ rvd.playPreview(this.getApplicationContext());
320+ break;
321+ case C_MENU_DOWNLOAD:
322+ if (rvd.isDownloaded()) return true;
323+ this.setDownloadOverlay(true);
324+
325+ new AsyncTask<Object, Void, Boolean>() {
326+ protected DownloadActivity context;
327+ protected RemoteVoiceData rvd;
328+
329+ @Override
330+ protected Boolean doInBackground(Object... params) {
331+ this.context = (DownloadActivity) params[0];
332+ this.rvd = (RemoteVoiceData) params[1];
333+ try {
334+ rvd.download(context);
335+ } catch (Exception e) {
336+ e.printStackTrace();
337+ return false;
338+ }
339+ return true;
340+ }
341+
342+ protected void onPostExecute(Boolean flag) {
343+ if (this.rvd.isDownloaded())
344+ context.rvd_list_adapter.notifyDataSetChanged();
345+ context.setDownloadOverlay(false);
346+ }
347+ }.execute(this, rvd);
348+ break;
349+ case C_MENU_INSTALL:
350+ if (rvd.isDownloaded())
351+ rvd.getVoiceData().installAndShowResults(this);
352+ break;
353+ case C_MENU_DELETE:
354+ rvd.delete();
355+ this.rvd_list_adapter.notifyDataSetChanged();
356+ Toast.makeText(this, R.string.voice_deleted, Toast.LENGTH_SHORT).show();
357+ break;
358+ case C_MENU_RATE:
359+ // TODO: implement!
360+ Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT).show();
361+ break;
362+ }
363+ return true;
364+ }
365+
302366 }
--- a/src/jp/nekoteki/android/navivoicechanger/InstallListActivity.java
+++ b/src/jp/nekoteki/android/navivoicechanger/InstallListActivity.java
@@ -172,12 +172,12 @@ public class InstallListActivity extends Activity {
172172 menu.setHeaderTitle(vd.getTitle());
173173 menu.add(vd.getId(), C_MENU_PREVIEW, 0, R.string.c_menu_preview);
174174 menu.add(vd.getId(), C_MENU_INSTALL, 0, R.string.c_menu_install);
175- menu.add(vd.getId(), C_MENU_RATE, 0, R.string.c_menu_rate);
176- menu.add(vd.getId(), C_MENU_DELETE, 0, R.string.c_menu_delete);
175+ menu.add(vd.getId(), C_MENU_RATE, 0, R.string.c_menu_rate);
176+ menu.add(vd.getId(), C_MENU_DELETE, 0, R.string.c_menu_delete);
177177
178178 if (vd.getId() < 1) {
179- menu.getItem(2).setEnabled(false);
180- menu.getItem(3).setEnabled(false);
179+ menu.getItem(C_MENU_RATE).setEnabled(false);
180+ menu.getItem(C_MENU_DELETE).setEnabled(false);
181181 }
182182 }
183183
--- a/src/jp/nekoteki/android/navivoicechanger/RemoteVoiceData.java
+++ b/src/jp/nekoteki/android/navivoicechanger/RemoteVoiceData.java
@@ -9,6 +9,8 @@ import org.apache.http.HttpResponse;
99 import org.apache.http.client.methods.HttpGet;
1010
1111 import android.content.Context;
12+import android.media.MediaPlayer;
13+import android.net.Uri;
1214 import android.net.http.AndroidHttpClient;
1315 import android.util.Log;
1416
@@ -22,13 +24,16 @@ public class RemoteVoiceData {
2224 protected String unit;
2325 protected String author;
2426 protected String description;
25- protected boolean downloaded;
27+ protected VoiceData voice_data;
2628
27- public void setDownloaded(boolean f) {
28- this.downloaded = f;
29+ public VoiceData getVoiceData() {
30+ return voice_data;
31+ }
32+ public void setVoiceData(VoiceData v) {
33+ this.voice_data = v;
2934 }
3035 public boolean isDownloaded() {
31- return downloaded;
36+ return this.voice_data != null;
3237 }
3338 public int getId() {
3439 return id;
@@ -94,6 +99,7 @@ public class RemoteVoiceData {
9499 this.downloadFile(this.ini_url, new File(datadir, VoiceData.DATA_INI));
95100 this.downloadFile(this.archive_url, new File(datadir, VoiceData.ARCHIVE_FILENAME));
96101 this.downloadFile(this.preview_url, new File(datadir, VoiceData.PREVIEW_FILESNAME));
102+ this.setVoiceData(new VoiceData(datadir, context));
97103 }
98104
99105 protected void downloadFile(String url, File file) throws IOException {
@@ -116,4 +122,20 @@ public class RemoteVoiceData {
116122 is.close();
117123 }
118124 }
125+
126+ public void delete() {
127+ if (!this.isDownloaded()) return;
128+ this.getVoiceData().delete();
129+ this.setVoiceData(null);
130+ }
131+
132+ public void playPreview(Context context) {
133+ if (this.isDownloaded()) {
134+ this.getVoiceData().playPreview();
135+ } else {
136+ MediaPlayer player = MediaPlayer.create(context, Uri.parse(this.getPreview_url()));
137+ if (player != null)
138+ player.start();
139+ }
140+ }
119141 }