diff options
Diffstat (limited to 'src/de/danoeh/antennapod/activity/DownloadActivity.java')
-rw-r--r-- | src/de/danoeh/antennapod/activity/DownloadActivity.java | 424 |
1 files changed, 213 insertions, 211 deletions
diff --git a/src/de/danoeh/antennapod/activity/DownloadActivity.java b/src/de/danoeh/antennapod/activity/DownloadActivity.java index 6618b21d6..40c75d336 100644 --- a/src/de/danoeh/antennapod/activity/DownloadActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadActivity.java @@ -11,6 +11,7 @@ import android.content.res.TypedArray; import android.os.AsyncTask; import android.os.Bundle; import android.os.IBinder; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.view.ActionMode; import android.util.Log; @@ -25,8 +26,8 @@ import android.widget.ListView; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.DownloadlistAdapter; -import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.service.download.DownloadRequest; import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.storage.DownloadRequester; @@ -35,226 +36,227 @@ import de.danoeh.antennapod.storage.DownloadRequester; * objects created by a DownloadObserver. */ public class DownloadActivity extends ActionBarActivity implements - ActionMode.Callback { + ActionMode.Callback { - private static final String TAG = "DownloadActivity"; - private static final int MENU_SHOW_LOG = 0; - private static final int MENU_CANCEL_ALL_DOWNLOADS = 1; - private DownloadlistAdapter dla; - private DownloadRequester requester; + private static final String TAG = "DownloadActivity"; + private static final int MENU_SHOW_LOG = 0; + private static final int MENU_CANCEL_ALL_DOWNLOADS = 1; + private DownloadlistAdapter dla; + private DownloadRequester requester; - private ActionMode mActionMode; - private DownloadStatus selectedDownload; + private ActionMode mActionMode; + private DownloadRequest selectedDownload; - private DownloadService downloadService = null; - boolean mIsBound; + private DownloadService downloadService = null; + boolean mIsBound; - private AsyncTask<Void, Void, Void> contentRefresher; + private AsyncTask<Void, Void, Void> contentRefresher; private ListView listview; - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); setContentView(R.layout.listview_activity); listview = (ListView) findViewById(R.id.listview); - if (AppConfig.DEBUG) - Log.d(TAG, "Creating Activity"); - requester = DownloadRequester.getInstance(); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - } - - @Override - protected void onPause() { - super.onPause(); - unbindService(mConnection); - unregisterReceiver(contentChanged); - } - - @Override - protected void onResume() { - super.onResume(); - registerReceiver(contentChanged, new IntentFilter( - DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED)); - bindService(new Intent(this, DownloadService.class), mConnection, 0); - startContentRefresher(); - if (dla != null) { - dla.notifyDataSetChanged(); - } - } - - @Override - protected void onStop() { - super.onStop(); - if (AppConfig.DEBUG) - Log.d(TAG, "Stopping Activity"); - stopContentRefresher(); - } - - private ServiceConnection mConnection = new ServiceConnection() { - public void onServiceDisconnected(ComponentName className) { - downloadService = null; - mIsBound = false; - Log.i(TAG, "Closed connection with DownloadService."); - } - - public void onServiceConnected(ComponentName name, IBinder service) { - downloadService = ((DownloadService.LocalBinder) service) - .getService(); - mIsBound = true; - if (AppConfig.DEBUG) - Log.d(TAG, "Connection to service established"); - dla = new DownloadlistAdapter(DownloadActivity.this, 0, - downloadService.getDownloads()); - listview.setAdapter(dla); - dla.notifyDataSetChanged(); - } - }; - - @SuppressLint("NewApi") - private void startContentRefresher() { - if (contentRefresher != null) { - contentRefresher.cancel(true); - } - contentRefresher = new AsyncTask<Void, Void, Void>() { - private final int WAITING_INTERVALL = 1000; - - @Override - protected void onProgressUpdate(Void... values) { - super.onProgressUpdate(values); - if (dla != null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Refreshing content automatically"); - dla.notifyDataSetChanged(); - } - } - - @Override - protected Void doInBackground(Void... params) { - while (!isCancelled()) { - try { - Thread.sleep(WAITING_INTERVALL); - publishProgress(); - } catch (InterruptedException e) { - return null; - } - } - return null; - } - }; - if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - contentRefresher.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } else { - contentRefresher.execute(); - } - } - - private void stopContentRefresher() { - if (contentRefresher != null) { - contentRefresher.cancel(true); - } - } - - @Override - protected void onPostCreate(Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - listview.setOnItemLongClickListener(new OnItemLongClickListener() { - - @Override - public boolean onItemLongClick(AdapterView<?> arg0, View view, - int position, long id) { - DownloadStatus selection = dla.getItem(position).getStatus(); - if (selection != null && mActionMode != null) { - mActionMode.finish(); - } - dla.setSelectedItemIndex(position); - selectedDownload = selection; - mActionMode = startSupportActionMode(DownloadActivity.this); - return true; - } - - }); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - menu.add(Menu.NONE, MENU_SHOW_LOG, Menu.NONE, - R.string.show_download_log).setShowAsAction( - MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); - menu.add(Menu.NONE, MENU_CANCEL_ALL_DOWNLOADS, Menu.NONE, - R.string.cancel_all_downloads_label).setShowAsAction( - MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - Intent intent = new Intent(this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP - | Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - break; - case MENU_SHOW_LOG: - startActivity(new Intent(this, DownloadLogActivity.class)); - break; - case MENU_CANCEL_ALL_DOWNLOADS: - requester.cancelAllDownloads(this); - break; - } - return true; - } - - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - if (!selectedDownload.isDone()) { - TypedArray drawables = obtainStyledAttributes(new int[] { R.attr.navigation_cancel }); - menu.add(Menu.NONE, R.id.cancel_download_item, Menu.NONE, - R.string.cancel_download_label).setIcon( - drawables.getDrawable(0)); - } - return true; - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return false; - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - boolean handled = false; - switch (item.getItemId()) { - case R.id.cancel_download_item: - requester.cancelDownload(this, selectedDownload.getFeedFile()); - handled = true; - break; - } - mActionMode.finish(); - return handled; - } - - @Override - public void onDestroyActionMode(ActionMode mode) { - mActionMode = null; - selectedDownload = null; - dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE); - } - - private BroadcastReceiver contentChanged = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - if (dla != null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Refreshing content"); - dla.notifyDataSetChanged(); - } - } - }; + if (AppConfig.DEBUG) + Log.d(TAG, "Creating Activity"); + requester = DownloadRequester.getInstance(); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + } + + @Override + protected void onPause() { + super.onPause(); + unbindService(mConnection); + unregisterReceiver(contentChanged); + } + + @Override + protected void onResume() { + super.onResume(); + registerReceiver(contentChanged, new IntentFilter( + DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED)); + bindService(new Intent(this, DownloadService.class), mConnection, 0); + startContentRefresher(); + if (dla != null) { + dla.notifyDataSetChanged(); + } + } + + @Override + protected void onStop() { + super.onStop(); + if (AppConfig.DEBUG) + Log.d(TAG, "Stopping Activity"); + stopContentRefresher(); + } + + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceDisconnected(ComponentName className) { + downloadService = null; + mIsBound = false; + Log.i(TAG, "Closed connection with DownloadService."); + } + + public void onServiceConnected(ComponentName name, IBinder service) { + downloadService = ((DownloadService.LocalBinder) service) + .getService(); + mIsBound = true; + if (AppConfig.DEBUG) + Log.d(TAG, "Connection to service established"); + dla = new DownloadlistAdapter(DownloadActivity.this, 0, + downloadService.getDownloads()); + listview.setAdapter(dla); + dla.notifyDataSetChanged(); + } + }; + + @SuppressLint("NewApi") + private void startContentRefresher() { + if (contentRefresher != null) { + contentRefresher.cancel(true); + } + contentRefresher = new AsyncTask<Void, Void, Void>() { + private final int WAITING_INTERVALL = 1000; + + @Override + protected void onProgressUpdate(Void... values) { + super.onProgressUpdate(values); + if (dla != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Refreshing content automatically"); + dla.notifyDataSetChanged(); + } + } + + @Override + protected Void doInBackground(Void... params) { + while (!isCancelled()) { + try { + Thread.sleep(WAITING_INTERVALL); + publishProgress(); + } catch (InterruptedException e) { + return null; + } + } + return null; + } + }; + if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { + contentRefresher.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } else { + contentRefresher.execute(); + } + } + + private void stopContentRefresher() { + if (contentRefresher != null) { + contentRefresher.cancel(true); + } + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + listview.setOnItemLongClickListener(new OnItemLongClickListener() { + + @Override + public boolean onItemLongClick(AdapterView<?> arg0, View view, + int position, long id) { + DownloadRequest selection = dla.getItem(position) + .getDownloadRequest(); + if (selection != null && mActionMode != null) { + mActionMode.finish(); + } + dla.setSelectedItemIndex(position); + selectedDownload = selection; + mActionMode = startSupportActionMode(DownloadActivity.this); + return true; + } + + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, MENU_SHOW_LOG, Menu.NONE, + R.string.show_download_log), + MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, MENU_CANCEL_ALL_DOWNLOADS, Menu.NONE, + R.string.cancel_all_downloads_label), + MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + break; + case MENU_SHOW_LOG: + startActivity(new Intent(this, DownloadLogActivity.class)); + break; + case MENU_CANCEL_ALL_DOWNLOADS: + requester.cancelAllDownloads(this); + break; + } + return true; + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + if (selectedDownload != null) { + TypedArray drawables = obtainStyledAttributes(new int[]{R.attr.navigation_cancel}); + menu.add(Menu.NONE, R.id.cancel_download_item, Menu.NONE, + R.string.cancel_download_label).setIcon( + drawables.getDrawable(0)); + } + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + boolean handled = false; + switch (item.getItemId()) { + case R.id.cancel_download_item: + requester.cancelDownload(this, selectedDownload.getSource()); + handled = true; + break; + } + mActionMode.finish(); + return handled; + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + mActionMode = null; + selectedDownload = null; + dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE); + } + + private BroadcastReceiver contentChanged = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + if (dla != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Refreshing content"); + dla.notifyDataSetChanged(); + } + } + }; } |