diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-08-10 16:10:32 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-08-10 16:10:32 +0200 |
commit | 8e16ad08c883615f64c21fcc4c54c1a565bf0b1b (patch) | |
tree | 0cdf31c66ae5a065e30ab7184943ed8710211d13 /src/de/danoeh/antennapod/activity | |
parent | dddc01e2e4911e0e07eb68c797472255414b6f7f (diff) | |
parent | 26fad34b6501adba7720c8728732d18bc613472b (diff) | |
download | AntennaPod-8e16ad08c883615f64c21fcc4c54c1a565bf0b1b.zip |
Merge branch 'feedmanager_removal' into develop
Conflicts:
src/de/danoeh/antennapod/activity/DownloadLogActivity.java
src/de/danoeh/antennapod/activity/FeedItemlistActivity.java
src/de/danoeh/antennapod/activity/ItemviewActivity.java
src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java
src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java
src/de/danoeh/antennapod/activity/PreferenceActivity.java
src/de/danoeh/antennapod/fragment/EpisodesFragment.java
src/de/danoeh/antennapod/fragment/FeedlistFragment.java
src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java
src/de/danoeh/antennapod/fragment/ItemlistFragment.java
Diffstat (limited to 'src/de/danoeh/antennapod/activity')
15 files changed, 1426 insertions, 1251 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(); + } + } + }; } diff --git a/src/de/danoeh/antennapod/activity/DownloadLogActivity.java b/src/de/danoeh/antennapod/activity/DownloadLogActivity.java index 262e0241a..949834596 100644 --- a/src/de/danoeh/antennapod/activity/DownloadLogActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadLogActivity.java @@ -1,27 +1,31 @@ package de.danoeh.antennapod.activity; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.DownloadLogAdapter; import de.danoeh.antennapod.feed.EventDistributor; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.service.download.DownloadStatus; +import de.danoeh.antennapod.storage.DBReader; + +import java.util.List; /** - * Displays completed and failed downloads in a list. The data comes from the - * FeedManager. + * Displays completed and failed downloads in a list. */ public class DownloadLogActivity extends ActionBarActivity { private static final String TAG = "DownloadLogActivity"; - DownloadLogAdapter dla; - FeedManager manager; + private List<DownloadStatus> downloadLog; + private DownloadLogAdapter dla; private ListView listview; @@ -30,13 +34,14 @@ public class DownloadLogActivity extends ActionBarActivity { setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); setContentView(R.layout.listview_activity); - manager = FeedManager.getInstance(); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + listview = (ListView) findViewById(R.layout.listview_activity); - dla = new DownloadLogAdapter(this); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + dla = new DownloadLogAdapter(this, itemAccess); listview.setAdapter(dla); + loadData(); } @Override @@ -69,12 +74,48 @@ public class DownloadLogActivity extends ActionBarActivity { return true; } + private void loadData() { + AsyncTask<Void, Void, List<DownloadStatus>> loadTask = new AsyncTask<Void, Void, List<DownloadStatus>>() { + @Override + protected List<DownloadStatus> doInBackground(Void... voids) { + return DBReader.getDownloadLog(DownloadLogActivity.this); + } + + @Override + protected void onPostExecute(List<DownloadStatus> downloadStatuses) { + super.onPostExecute(downloadStatuses); + if (downloadStatuses != null) { + downloadLog = downloadStatuses; + if (dla != null) { + dla.notifyDataSetChanged(); + } + } else { + Log.e(TAG, "Could not load download log"); + } + } + }; + loadTask.execute(); + } + + private DownloadLogAdapter.ItemAccess itemAccess = new DownloadLogAdapter.ItemAccess() { + + @Override + public int getCount() { + return (downloadLog != null) ? downloadLog.size() : 0; + } + + @Override + public DownloadStatus getItem(int position) { + return (downloadLog != null) ? downloadLog.get(position) : null; + } + }; + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { @Override public void update(EventDistributor eventDistributor, Integer arg) { if ((arg & EventDistributor.DOWNLOADLOG_UPDATE) != 0) { - dla.notifyDataSetChanged(); + loadData(); } } }; diff --git a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java index eb9dd05f9..4a8a2f1f8 100644 --- a/src/de/danoeh/antennapod/activity/FeedInfoActivity.java +++ b/src/de/danoeh/antennapod/activity/FeedInfoActivity.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.activity; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; @@ -14,8 +15,8 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.ImageLoader; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.LangUtils; import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler; @@ -41,47 +42,66 @@ public class FeedInfoActivity extends ActionBarActivity { setContentView(R.layout.feedinfo); getSupportActionBar().setDisplayHomeAsUpEnabled(true); long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1); - FeedManager manager = FeedManager.getInstance(); - feed = manager.getFeed(feedId); - if (feed != null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Language is " + feed.getLanguage()); - if (AppConfig.DEBUG) - Log.d(TAG, "Author is " + feed.getAuthor()); - imgvCover = (ImageView) findViewById(R.id.imgvCover); - txtvTitle = (TextView) findViewById(R.id.txtvTitle); - txtvDescription = (TextView) findViewById(R.id.txtvDescription); - txtvLanguage = (TextView) findViewById(R.id.txtvLanguage); - txtvAuthor = (TextView) findViewById(R.id.txtvAuthor); - imgvCover.post(new Runnable() { + + AsyncTask<Long, Void, Feed> loadTask = new AsyncTask<Long, Void, Feed>() { - @Override - public void run() { - ImageLoader.getInstance().loadThumbnailBitmap( - feed.getImage(), imgvCover); - } - }); - - txtvTitle.setText(feed.getTitle()); - txtvDescription.setText(feed.getDescription()); - if (feed.getAuthor() != null) { - txtvAuthor.setText(feed.getAuthor()); - } - if (feed.getLanguage() != null) { - txtvLanguage.setText(LangUtils.getLanguageString(feed - .getLanguage())); + @Override + protected Feed doInBackground(Long... params) { + return DBReader.getFeed(FeedInfoActivity.this, params[0]); } - } else { - Log.e(TAG, "Activity was started with invalid arguments"); - } + @Override + protected void onPostExecute(Feed result) { + super.onPostExecute(result); + if (result != null) { + feed = result; + if (feed != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Language is " + feed.getLanguage()); + if (AppConfig.DEBUG) + Log.d(TAG, "Author is " + feed.getAuthor()); + imgvCover = (ImageView) findViewById(R.id.imgvCover); + txtvTitle = (TextView) findViewById(R.id.txtvTitle); + txtvDescription = (TextView) findViewById(R.id.txtvDescription); + txtvLanguage = (TextView) findViewById(R.id.txtvLanguage); + txtvAuthor = (TextView) findViewById(R.id.txtvAuthor); + imgvCover.post(new Runnable() { + + @Override + public void run() { + ImageLoader.getInstance().loadThumbnailBitmap( + feed.getImage(), imgvCover); + } + }); + + txtvTitle.setText(feed.getTitle()); + txtvDescription.setText(feed.getDescription()); + if (feed.getAuthor() != null) { + txtvAuthor.setText(feed.getAuthor()); + } + if (feed.getLanguage() != null) { + txtvLanguage.setText(LangUtils + .getLanguageString(feed.getLanguage())); + } + supportInvalidateOptionsMenu(); + } + } else { + Log.e(TAG, "Activity was started with invalid arguments"); + } + } + }; + loadTask.execute(feedId); } @Override public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = new MenuInflater(this); - inflater.inflate(R.menu.feedinfo, menu); - return true; + if (feed != null) { + MenuInflater inflater = new MenuInflater(this); + inflater.inflate(R.menu.feedinfo, menu); + return true; + } else { + return false; + } } @Override @@ -104,7 +124,8 @@ public class FeedInfoActivity extends ActionBarActivity { return FeedMenuHandler.onOptionsItemClicked(this, item, feed); } catch (DownloadRequestException e) { e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, e.getMessage()); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, + e.getMessage()); } return super.onOptionsItemSelected(item); } diff --git a/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java b/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java index a009d4912..64e4f2d30 100644 --- a/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java +++ b/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java @@ -4,9 +4,11 @@ import android.annotation.SuppressLint; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.util.Log; @@ -14,137 +16,173 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.Window; +import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FeedRemover; import de.danoeh.antennapod.dialog.ConfirmationDialog; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.ExternalPlayerFragment; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.util.StorageUtils; import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler; -/** Displays a List of FeedItems */ +/** + * Displays a List of FeedItems + */ public class FeedItemlistActivity extends ActionBarActivity { - private static final String TAG = "FeedItemlistActivity"; - - private FeedManager manager; - - /** The feed which the activity displays */ - private Feed feed; - private ItemlistFragment filf; - private ExternalPlayerFragment externalPlayerFragment; - - @Override - public void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); - StorageUtils.checkStorageAvailability(this); - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.feeditemlist_activity); - - manager = FeedManager.getInstance(); - long feedId = getIntent().getLongExtra( - FeedlistFragment.EXTRA_SELECTED_FEED, -1); - if (feedId == -1) - Log.e(TAG, "Received invalid feed selection."); - - feed = manager.getFeed(feedId); - setTitle(feed.getTitle()); - - FragmentManager fragmentManager = getSupportFragmentManager(); - FragmentTransaction fT = fragmentManager.beginTransaction(); - - filf = ItemlistFragment.newInstance(feed.getId()); - fT.replace(R.id.feeditemlistFragment, filf); - - externalPlayerFragment = new ExternalPlayerFragment(); - fT.replace(R.id.playerFragment, externalPlayerFragment); - fT.commit(); - - } - - @Override - protected void onResume() { - super.onResume(); - StorageUtils.checkStorageAvailability(this); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - TypedArray drawables = obtainStyledAttributes(new int[] { R.attr.action_search }); - menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) - .setIcon(drawables.getDrawable(0)) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); - return FeedMenuHandler - .onCreateOptionsMenu(new MenuInflater(this), menu); - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - return FeedMenuHandler.onPrepareOptionsMenu(menu, feed); - } - - @SuppressLint("NewApi") - @Override - public boolean onOptionsItemSelected(MenuItem item) { - try { - if (FeedMenuHandler.onOptionsItemClicked(this, item, feed)) { - filf.getListAdapter().notifyDataSetChanged(); - } else { - switch (item.getItemId()) { - case R.id.remove_item: - final FeedRemover remover = new FeedRemover( - FeedItemlistActivity.this, feed) { - @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); - finish(); - } - }; - ConfirmationDialog conDialog = new ConfirmationDialog(this, - R.string.remove_feed_label, - R.string.feed_delete_confirmation_msg) { - - @Override - public void onConfirmButtonPressed( - DialogInterface dialog) { - dialog.dismiss(); - remover.executeAsync(); - } - }; - conDialog.createNewDialog().show(); - break; - case R.id.search_item: - onSearchRequested(); - break; - case android.R.id.home: - Intent intent = new Intent(this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(intent); - break; - } - } - } catch (DownloadRequestException e) { - e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, - e.getMessage()); - } - return true; - } - - @Override - public boolean onSearchRequested() { - Bundle bundle = new Bundle(); - bundle.putLong(SearchActivity.EXTRA_FEED_ID, feed.getId()); - startSearch(null, false, bundle, false); - return true; - } + private static final String TAG = "FeedItemlistActivity"; + + /** + * The feed which the activity displays + */ + private Feed feed; + private ItemlistFragment filf; + private ExternalPlayerFragment externalPlayerFragment; + + @Override + public void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); + StorageUtils.checkStorageAvailability(this); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.feeditemlist_activity); + + long feedId = getIntent().getLongExtra( + FeedlistFragment.EXTRA_SELECTED_FEED, -1); + if (feedId == -1) { + Log.e(TAG, "Received invalid feed selection."); + } else { + loadData(feedId); + } + + } + + private void loadData(long id) { + AsyncTask<Long, Void, Feed> loadTask = new AsyncTask<Long, Void, Feed>() { + + @Override + protected Feed doInBackground(Long... longs) { + if (AppConfig.DEBUG) + Log.d(TAG, "Loading feed data in background"); + return DBReader.getFeed(FeedItemlistActivity.this, longs[0]); + } + + @Override + protected void onPostExecute(Feed result) { + super.onPostExecute(result); + if (result != null) { + if (AppConfig.DEBUG) Log.d(TAG, "Finished loading feed data"); + feed = result; + setTitle(feed.getTitle()); + + FragmentManager fragmentManager = getSupportFragmentManager(); + FragmentTransaction fT = fragmentManager.beginTransaction(); + + filf = ItemlistFragment.newInstance(feed.getId()); + fT.replace(R.id.feeditemlistFragment, filf); + + externalPlayerFragment = new ExternalPlayerFragment(); + fT.replace(R.id.playerFragment, externalPlayerFragment); + fT.commit(); + supportInvalidateOptionsMenu(); + } else { + Log.e(TAG, "Error: Feed was null"); + } + } + }; + loadTask.execute(id); + } + + @Override + protected void onResume() { + super.onResume(); + StorageUtils.checkStorageAvailability(this); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if (feed != null) { + TypedArray drawables = obtainStyledAttributes(new int[]{R.attr.action_search}); + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) + .setIcon(drawables.getDrawable(0)), + MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); + return FeedMenuHandler + .onCreateOptionsMenu(new MenuInflater(this), menu); + } else { + return false; + } + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + return FeedMenuHandler.onPrepareOptionsMenu(menu, feed); + } + + @SuppressLint("NewApi") + @Override + public boolean onOptionsItemSelected(MenuItem item) { + try { + if (FeedMenuHandler.onOptionsItemClicked(this, item, feed)) { + filf.getListAdapter().notifyDataSetChanged(); + } else { + switch (item.getItemId()) { + case R.id.remove_item: + final FeedRemover remover = new FeedRemover( + FeedItemlistActivity.this, feed) { + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + finish(); + } + }; + ConfirmationDialog conDialog = new ConfirmationDialog(this, + R.string.remove_feed_label, + R.string.feed_delete_confirmation_msg) { + + @Override + public void onConfirmButtonPressed( + DialogInterface dialog) { + dialog.dismiss(); + remover.executeAsync(); + } + }; + conDialog.createNewDialog().show(); + break; + case R.id.search_item: + onSearchRequested(); + break; + case android.R.id.home: + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + break; + } + } + } catch (DownloadRequestException e) { + e.printStackTrace(); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, + e.getMessage()); + } + return true; + } + + @Override + public boolean onSearchRequested() { + if (feed != null) { + Bundle bundle = new Bundle(); + bundle.putLong(SearchActivity.EXTRA_FEED_ID, feed.getId()); + startSearch(null, false, bundle, false); + return true; + } else { + return false; + } + } } diff --git a/src/de/danoeh/antennapod/activity/ItemviewActivity.java b/src/de/danoeh/antennapod/activity/ItemviewActivity.java index 8488d7e79..43eea93e4 100644 --- a/src/de/danoeh/antennapod/activity/ItemviewActivity.java +++ b/src/de/danoeh/antennapod/activity/ItemviewActivity.java @@ -1,7 +1,6 @@ package de.danoeh.antennapod.activity; -import java.text.DateFormat; - +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; @@ -17,38 +16,43 @@ import android.widget.TextView; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; -import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedManager; -import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemDescriptionFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.DownloadRequestException; +import de.danoeh.antennapod.util.QueueAccess; import de.danoeh.antennapod.util.StorageUtils; import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler; +import java.text.DateFormat; + /** Displays a single FeedItem and provides various actions */ public class ItemviewActivity extends ActionBarActivity { private static final String TAG = "ItemviewActivity"; - private FeedManager manager; - private FeedItem item; + private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED | EventDistributor.DOWNLOAD_QUEUED; - // Widgets - private TextView txtvTitle; - private TextView txtvPublished; + private FeedItem item; @Override public void onCreate(Bundle savedInstanceState) { setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); - manager = FeedManager.getInstance(); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); getSupportActionBar().setDisplayShowTitleEnabled(false); - extractFeeditem(); - populateUI(); + EventDistributor.getInstance().register(contentUpdate); + + long itemId = getIntent().getLongExtra( + ItemlistFragment.EXTRA_SELECTED_FEEDITEM, -1); + if (itemId == -1) { + Log.e(TAG, "Received invalid selection of either feeditem or feed."); + } else { + loadData(itemId); + } } @Override @@ -65,28 +69,38 @@ public class ItemviewActivity extends ActionBarActivity { Log.d(TAG, "Stopping Activity"); } - /** Extracts FeedItem object the activity is supposed to display */ - private void extractFeeditem() { - long itemId = getIntent().getLongExtra( - ItemlistFragment.EXTRA_SELECTED_FEEDITEM, -1); - long feedId = getIntent().getLongExtra( - FeedlistFragment.EXTRA_SELECTED_FEED, -1); - if (itemId == -1 || feedId == -1) { - Log.e(TAG, "Received invalid selection of either feeditem or feed."); - } - Feed feed = manager.getFeed(feedId); - item = manager.getFeedItem(itemId, feed); - if (AppConfig.DEBUG) - Log.d(TAG, "Title of item is " + item.getTitle()); - if (AppConfig.DEBUG) - Log.d(TAG, "Title of feed is " + item.getFeed().getTitle()); - } + private void loadData(long itemId) { + AsyncTask<Long, Void, FeedItem> loadTask = new AsyncTask<Long, Void, FeedItem>() { + + @Override + protected FeedItem doInBackground(Long... longs) { + return DBReader.getFeedItem(ItemviewActivity.this, longs[0]); + } + + @Override + protected void onPostExecute(FeedItem feedItem) { + super.onPostExecute(feedItem); + if (feedItem != null && feedItem.getFeed() != null) { + item = feedItem; + populateUI(); + supportInvalidateOptionsMenu(); + } else { + if (feedItem == null) { + Log.e(TAG, "Error: FeedItem was null"); + } else if (feedItem.getFeed() == null) { + Log.e(TAG, "Error: Feed was null"); + } + } + } + }; + loadTask.execute(itemId); + } private void populateUI() { getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.feeditemview); - txtvTitle = (TextView) findViewById(R.id.txtvItemname); - txtvPublished = (TextView) findViewById(R.id.txtvPublished); + TextView txtvTitle = (TextView) findViewById(R.id.txtvItemname); + TextView txtvPublished = (TextView) findViewById(R.id.txtvPublished); setTitle(item.getFeed().getTitle()); txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate() @@ -105,9 +119,13 @@ public class ItemviewActivity extends ActionBarActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.feeditem, menu); - return true; + if (item != null) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.feeditem, menu); + return true; + } else { + return false; + } } @Override @@ -133,13 +151,28 @@ public class ItemviewActivity extends ActionBarActivity { @Override public boolean onPrepareOptionsMenu(final Menu menu) { return FeedItemMenuHandler.onPrepareMenu( - new FeedItemMenuHandler.MenuInterface() { + new FeedItemMenuHandler.MenuInterface() { - @Override - public void setItemVisibility(int id, boolean visible) { - menu.findItem(id).setVisible(visible); - } - }, item, true); + @Override + public void setItemVisibility(int id, boolean visible) { + menu.findItem(id).setVisible(visible); + } + }, item, true, QueueAccess.NotInQueueAccess()); } + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { + + @Override + public void update(EventDistributor eventDistributor, Integer arg) { + if ((EVENTS & arg) != 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received contentUpdate Intent."); + if (item != null) { + loadData(item.getId()); + } + } + } + }; + + } diff --git a/src/de/danoeh/antennapod/activity/MainActivity.java b/src/de/danoeh/antennapod/activity/MainActivity.java index ae592fbdc..92b56461c 100644 --- a/src/de/danoeh/antennapod/activity/MainActivity.java +++ b/src/de/danoeh/antennapod/activity/MainActivity.java @@ -20,13 +20,14 @@ import android.view.Window; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.EventDistributor; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.EpisodesFragment; import de.danoeh.antennapod.fragment.ExternalPlayerFragment; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.service.download.DownloadService; +import de.danoeh.antennapod.storage.DBReader; +import de.danoeh.antennapod.storage.DBTasks; import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.StorageUtils; @@ -37,7 +38,6 @@ public class MainActivity extends ActionBarActivity { private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED | EventDistributor.DOWNLOAD_QUEUED; - private FeedManager manager; private ViewPager viewpager; private TabsAdapter pagerAdapter; private ExternalPlayerFragment externalPlayerFragment; @@ -49,7 +49,6 @@ public class MainActivity extends ActionBarActivity { setTheme(UserPreferences.getTheme()); super.onCreate(savedInstanceState); StorageUtils.checkStorageAvailability(this); - manager = FeedManager.getInstance(); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); @@ -78,7 +77,7 @@ public class MainActivity extends ActionBarActivity { if (!appLaunched && getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN)) { appLaunched = true; - if (manager.getUnreadItemsSize(true) > 0) { + if (DBReader.getNumberOfUnreadItems(this) > 0) { // select 'episodes' tab getSupportActionBar().setSelectedNavigationItem(1); } @@ -140,7 +139,7 @@ public class MainActivity extends ActionBarActivity { startActivity(new Intent(this, AddFeedActivity.class)); return true; case R.id.all_feed_refresh: - manager.refreshAllFeeds(this); + DBTasks.refreshAllFeeds(this, null); return true; case R.id.show_downloads: startActivity(new Intent(this, DownloadActivity.class)); @@ -171,9 +170,6 @@ public class MainActivity extends ActionBarActivity { } else { refreshAll.setVisible(true); } - - boolean hasFeeds = manager.getFeedsSize() > 0; - menu.findItem(R.id.all_feed_refresh).setVisible(hasFeeds); return true; } diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java index fe9eaed62..b5b694995 100644 --- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -20,7 +20,6 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FlattrClickWorker; import de.danoeh.antennapod.dialog.TimeDialog; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.util.Converter; @@ -38,8 +37,6 @@ public abstract class MediaplayerActivity extends ActionBarActivity implements OnSeekBarChangeListener { private static final String TAG = "MediaplayerActivity"; - protected FeedManager manager; - protected PlaybackController controller; protected TextView txtvPosition; @@ -148,7 +145,6 @@ public abstract class MediaplayerActivity extends ActionBarActivity StorageUtils.checkStorageAvailability(this); orientation = getResources().getConfiguration().orientation; - manager = FeedManager.getInstance(); getWindow().setFormat(PixelFormat.TRANSPARENT); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } diff --git a/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java index ae8b62669..d4b4597f2 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.activity; import java.util.Date; +import java.util.List; import android.annotation.SuppressLint; import android.content.Intent; @@ -24,11 +25,11 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.MiroGuideItemlistAdapter; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.miroguide.conn.MiroGuideException; import de.danoeh.antennapod.miroguide.conn.MiroGuideService; import de.danoeh.antennapod.miroguide.model.MiroGuideChannel; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBReader; import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequester; @@ -39,144 +40,161 @@ import de.danoeh.antennapod.storage.DownloadRequester; public class MiroGuideChannelViewActivity extends ActionBarActivity { private static final String TAG = "MiroGuideChannelViewActivity"; - public static final String EXTRA_CHANNEL_ID = "id"; - public static final String EXTRA_CHANNEL_URL = "url"; - - private RelativeLayout layoutContent; - private ProgressBar progLoading; - private TextView txtvTitle; - private TextView txtVDescription; - private ListView listEntries; - - private long channelId; - private String channelUrl; - private MiroGuideChannel channel; - - @Override - protected void onPause() { - super.onPause(); - channelLoader.cancel(true); - } - - @SuppressLint("NewApi") - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.miroguide_channelview); - - layoutContent = (RelativeLayout) findViewById(R.id.layout_content); - progLoading = (ProgressBar) findViewById(R.id.progLoading); - txtvTitle = (TextView) findViewById(R.id.txtvTitle); - txtVDescription = (TextView) findViewById(R.id.txtvDescription); - listEntries = (ListView) findViewById(R.id.itemlist); - - channelId = getIntent().getLongExtra(EXTRA_CHANNEL_ID, -1); - channelUrl = getIntent().getStringExtra(EXTRA_CHANNEL_URL); - - if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - channelLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } else { - channelLoader.execute(); - } - - } - - /** Is used to load channel information asynchronously. */ - private AsyncTask<Void, Void, Void> channelLoader = new AsyncTask<Void, Void, Void>() { - private static final String TAG = "ChannelLoader"; - private Exception exception; - - @Override - protected Void doInBackground(Void... params) { - if (AppConfig.DEBUG) - Log.d(TAG, "Starting background task"); - MiroGuideService service = new MiroGuideService(); - try { - channel = service.getChannel(channelId); - } catch (MiroGuideException e) { - e.printStackTrace(); - exception = e; - } - return null; - } - - @SuppressLint("NewApi") - @Override - protected void onPostExecute(Void result) { - if (AppConfig.DEBUG) - Log.d(TAG, "Loading finished"); - if (exception == null) { - txtvTitle.setText(channel.getName()); - txtVDescription.setText(channel.getDescription()); - - MiroGuideItemlistAdapter listAdapter = new MiroGuideItemlistAdapter( - MiroGuideChannelViewActivity.this, 0, - channel.getItems()); - listEntries.setAdapter(listAdapter); - progLoading.setVisibility(View.GONE); - layoutContent.setVisibility(View.VISIBLE); - supportInvalidateOptionsMenu(); - } else { - finish(); - } - } - - }; - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = new MenuInflater(this); - inflater.inflate(R.menu.channelview, menu); - return true; - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - boolean channelLoaded = channel != null; - boolean beingDownloaded = channelLoaded - && DownloadRequester.getInstance().isDownloadingFile( - channel.getDownloadUrl()); - boolean notAdded = channelLoaded - && !((FeedManager.getInstance().feedExists( - channel.getDownloadUrl()) || beingDownloaded)); - menu.findItem(R.id.add_feed).setVisible(notAdded); - menu.findItem(R.id.visit_website_item).setVisible( - channelLoaded && channel.getWebsiteUrl() != null); - return true; - } - - @SuppressLint("NewApi") - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.visit_website_item: - Uri uri = Uri.parse(channel.getWebsiteUrl()); - startActivity(new Intent(Intent.ACTION_VIEW, uri)); - return true; - case R.id.add_feed: - try { - DownloadRequester.getInstance().downloadFeed( - this, - new Feed(channel.getDownloadUrl(), new Date(), channel - .getName())); - } catch (DownloadRequestException e) { - e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, - e.getMessage()); - } - Toast toast = Toast.makeText(this, R.string.miro_feed_added, - Toast.LENGTH_LONG); - toast.show(); - supportInvalidateOptionsMenu(); - return true; - default: - return false; - } - } + public static final String EXTRA_CHANNEL_ID = "id"; + public static final String EXTRA_CHANNEL_URL = "url"; + + private RelativeLayout layoutContent; + private ProgressBar progLoading; + private TextView txtvTitle; + private TextView txtVDescription; + private ListView listEntries; + + private long channelId; + private String channelUrl; + private MiroGuideChannel channel; + private volatile List<Feed> feeds; + + @Override + protected void onPause() { + super.onPause(); + channelLoader.cancel(true); + } + + @SuppressLint("NewApi") + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.miroguide_channelview); + + layoutContent = (RelativeLayout) findViewById(R.id.layout_content); + progLoading = (ProgressBar) findViewById(R.id.progLoading); + txtvTitle = (TextView) findViewById(R.id.txtvTitle); + txtVDescription = (TextView) findViewById(R.id.txtvDescription); + listEntries = (ListView) findViewById(R.id.itemlist); + + channelId = getIntent().getLongExtra(EXTRA_CHANNEL_ID, -1); + channelUrl = getIntent().getStringExtra(EXTRA_CHANNEL_URL); + + if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { + channelLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } else { + channelLoader.execute(); + } + + } + + /** + * Is used to load channel information asynchronously. + */ + private AsyncTask<Void, Void, Void> channelLoader = new AsyncTask<Void, Void, Void>() { + private static final String TAG = "ChannelLoader"; + private Exception exception; + + @Override + protected Void doInBackground(Void... params) { + if (AppConfig.DEBUG) + Log.d(TAG, "Starting background task"); + feeds = DBReader.getFeedList(MiroGuideChannelViewActivity.this); + MiroGuideService service = new MiroGuideService(); + try { + channel = service.getChannel(channelId); + } catch (MiroGuideException e) { + e.printStackTrace(); + exception = e; + } + return null; + } + + @SuppressLint("NewApi") + @Override + protected void onPostExecute(Void result) { + if (AppConfig.DEBUG) + Log.d(TAG, "Loading finished"); + if (exception == null) { + txtvTitle.setText(channel.getName()); + txtVDescription.setText(channel.getDescription()); + + MiroGuideItemlistAdapter listAdapter = new MiroGuideItemlistAdapter( + MiroGuideChannelViewActivity.this, 0, + channel.getItems()); + listEntries.setAdapter(listAdapter); + progLoading.setVisibility(View.GONE); + layoutContent.setVisibility(View.VISIBLE); + supportInvalidateOptionsMenu(); + } else { + finish(); + } + } + + }; + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = new MenuInflater(this); + inflater.inflate(R.menu.channelview, menu); + return true; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + boolean channelLoaded = channel != null; + boolean beingDownloaded = channelLoaded + && DownloadRequester.getInstance().isDownloadingFile( + channel.getDownloadUrl()); + boolean notAdded = channelLoaded + && !((feedExists( + channel.getDownloadUrl()) || beingDownloaded)); + menu.findItem(R.id.add_feed).setVisible(notAdded); + menu.findItem(R.id.visit_website_item).setVisible( + channelLoaded && channel.getWebsiteUrl() != null); + return true; + } + + @SuppressLint("NewApi") + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.visit_website_item: + Uri uri = Uri.parse(channel.getWebsiteUrl()); + startActivity(new Intent(Intent.ACTION_VIEW, uri)); + return true; + case R.id.add_feed: + try { + DownloadRequester.getInstance().downloadFeed( + this, + new Feed(channel.getDownloadUrl(), new Date(), channel + .getName())); + } catch (DownloadRequestException e) { + e.printStackTrace(); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, + e.getMessage()); + } + Toast toast = Toast.makeText(this, R.string.miro_feed_added, + Toast.LENGTH_LONG); + toast.show(); + supportInvalidateOptionsMenu(); + return true; + default: + return false; + } + } + + private boolean feedExists(String downloadUrl) { + if (feeds == null) { + return false; + } + + for (Feed feed : feeds) { + if (feed.getDownload_url().equals(downloadUrl)) { + return true; + } + } + return false; + } } diff --git a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java index 2d73525c0..99da9944f 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideMainActivity.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; @@ -24,126 +25,126 @@ import de.danoeh.antennapod.preferences.UserPreferences; * Shows a list of available categories and offers a search button. If the user * selects a category, the MiroGuideCategoryActivity is started. */ -public class MiroGuideMainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{ - private static final String TAG = "MiroGuideMainActivity"; +public class MiroGuideMainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { + private static final String TAG = "MiroGuideMainActivity"; - private static String[] categories; - private ArrayAdapter<String> listAdapter; + private static String[] categories; + private ArrayAdapter<String> listAdapter; - private TextView txtvStatus; + private TextView txtvStatus; private ListView listView; - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.miroguide_categorylist); + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.miroguide_categorylist); - txtvStatus = (TextView) findViewById(android.R.id.empty); + txtvStatus = (TextView) findViewById(android.R.id.empty); listView = (ListView) findViewById(android.R.id.list); listView.setOnItemClickListener(this); - } - - @Override - protected void onPause() { - super.onPause(); - } - - @Override - protected void onResume() { - super.onResume(); - if (categories != null) { - createAdapter(); - } else { - loadCategories(); - } - } - - private void createAdapter() { - if (categories != null) { - listAdapter = new ArrayAdapter<String>(this, - android.R.layout.simple_list_item_1, categories); - txtvStatus.setText(R.string.no_items_label); - listView.setAdapter(listAdapter); - } - } - - /** - * Launches an AsyncTask to load the available categories in the background. - */ - @SuppressLint("NewApi") - private void loadCategories() { - AsyncTask<Void, Void, Void> listLoader = new AsyncTask<Void, Void, Void>() { - - private String[] c; - private MiroGuideException exception; - - @Override - protected void onPostExecute(Void result) { - if (exception == null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Successfully loaded categories"); - categories = c; - createAdapter(); - } else { - Log.e(TAG, "Error happened while trying to load categories"); - txtvStatus.setText(exception.getMessage()); - } - } - - @Override - protected void onPreExecute() { - txtvStatus.setText(R.string.loading_categories_label); - } - - @Override - protected Void doInBackground(Void... params) { - MiroGuideService service = new MiroGuideService(); - try { - c = service.getCategories(); - } catch (MiroGuideException e) { - e.printStackTrace(); - exception = e; - } finally { - service.close(); - } - return null; - } - - }; - - if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - listLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } else { - listLoader.execute(); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) - .setIcon( - obtainStyledAttributes( - new int[] { R.attr.action_search }) - .getDrawable(0)) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.search_item: - onSearchRequested(); - return true; - default: - return false; - } - } + } + + @Override + protected void onPause() { + super.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + if (categories != null) { + createAdapter(); + } else { + loadCategories(); + } + } + + private void createAdapter() { + if (categories != null) { + listAdapter = new ArrayAdapter<String>(this, + android.R.layout.simple_list_item_1, categories); + txtvStatus.setText(R.string.no_items_label); + listView.setAdapter(listAdapter); + } + } + + /** + * Launches an AsyncTask to load the available categories in the background. + */ + @SuppressLint("NewApi") + private void loadCategories() { + AsyncTask<Void, Void, Void> listLoader = new AsyncTask<Void, Void, Void>() { + + private String[] c; + private MiroGuideException exception; + + @Override + protected void onPostExecute(Void result) { + if (exception == null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Successfully loaded categories"); + categories = c; + createAdapter(); + } else { + Log.e(TAG, "Error happened while trying to load categories"); + txtvStatus.setText(exception.getMessage()); + } + } + + @Override + protected void onPreExecute() { + txtvStatus.setText(R.string.loading_categories_label); + } + + @Override + protected Void doInBackground(Void... params) { + MiroGuideService service = new MiroGuideService(); + try { + c = service.getCategories(); + } catch (MiroGuideException e) { + e.printStackTrace(); + exception = e; + } finally { + service.close(); + } + return null; + } + + }; + + if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { + listLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } else { + listLoader.execute(); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) + .setIcon( + obtainStyledAttributes( + new int[]{R.attr.action_search}) + .getDrawable(0)), + MenuItem.SHOW_AS_ACTION_IF_ROOM); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.search_item: + onSearchRequested(); + return true; + default: + return false; + } + } @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { diff --git a/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java b/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java index cada0e4ef..4ea0b1699 100644 --- a/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java +++ b/src/de/danoeh/antennapod/activity/MiroGuideSearchActivity.java @@ -4,6 +4,7 @@ import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.util.Log; @@ -19,71 +20,71 @@ import de.danoeh.antennapod.preferences.UserPreferences; * uses a MiroGuideChannelListFragment to display the results. */ public class MiroGuideSearchActivity extends ActionBarActivity { - private static final String TAG = "MiroGuideSearchActivity"; + private static final String TAG = "MiroGuideSearchActivity"; - private MiroGuideChannellistFragment listFragment; + private MiroGuideChannellistFragment listFragment; - @Override - protected void onCreate(Bundle arg0) { - setTheme(UserPreferences.getTheme()); - super.onCreate(arg0); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.miroguidesearch); - } + @Override + protected void onCreate(Bundle arg0) { + setTheme(UserPreferences.getTheme()); + super.onCreate(arg0); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.miroguidesearch); + } - @Override - protected void onResume() { - super.onResume(); - Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - getSupportActionBar() - .setSubtitle( - getString(R.string.search_term_label) + "\"" - + query + "\""); - handleSearchRequest(query); - } - } + @Override + protected void onResume() { + super.onResume(); + Intent intent = getIntent(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + getSupportActionBar() + .setSubtitle( + getString(R.string.search_term_label) + "\"" + + query + "\""); + handleSearchRequest(query); + } + } - private void handleSearchRequest(String query) { - if (AppConfig.DEBUG) - Log.d(TAG, "Performing search"); - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - listFragment = MiroGuideChannellistFragment.newInstance("name", query, - "name"); - ft.replace(R.id.channellistFragment, listFragment); - ft.commit(); - } + private void handleSearchRequest(String query) { + if (AppConfig.DEBUG) + Log.d(TAG, "Performing search"); + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + listFragment = MiroGuideChannellistFragment.newInstance("name", query, + "name"); + ft.replace(R.id.channellistFragment, listFragment); + ft.commit(); + } - @Override - protected void onNewIntent(Intent intent) { - setIntent(intent); - } + @Override + protected void onNewIntent(Intent intent) { + setIntent(intent); + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) - .setIcon( - obtainStyledAttributes( - new int[] { R.attr.action_search }) - .getDrawable(0)) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) + .setIcon( + obtainStyledAttributes( + new int[]{R.attr.action_search}) + .getDrawable(0)), + MenuItem.SHOW_AS_ACTION_IF_ROOM); - return true; - } + return true; + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.search_item: - onSearchRequested(); - return true; - default: - return false; - } - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.search_item: + onSearchRequested(); + return true; + default: + return false; + } + } } diff --git a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java index bfe94fa48..cb1c66cab 100644 --- a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java +++ b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java @@ -20,9 +20,10 @@ import android.widget.ProgressBar; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.service.download.DownloadRequest; +import de.danoeh.antennapod.service.download.DownloadStatus; import de.danoeh.antennapod.service.download.Downloader; import de.danoeh.antennapod.service.download.DownloaderCallback; import de.danoeh.antennapod.service.download.HttpDownloader; @@ -44,7 +45,7 @@ import de.danoeh.antennapod.util.URLChecker; public abstract class OnlineFeedViewActivity extends ActionBarActivity { private static final String TAG = "OnlineFeedViewActivity"; private static final String ARG_FEEDURL = "arg.feedurl"; - + public static final int RESULT_ERROR = 2; private Feed feed; @@ -69,7 +70,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { @Override protected void onStop() { super.onStop(); - if (downloader != null && downloader.getStatus().isDone() == false) { + if (downloader != null && !downloader.isFinished()) { downloader.cancel(); } } @@ -81,7 +82,7 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { @Override public void run() { - DownloadStatus status = downloader.getStatus(); + DownloadStatus status = downloader.getResult(); if (status != null) { if (!status.isCancelled()) { if (status.isSuccessful()) { @@ -117,10 +118,13 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { FileNameGenerator.generateFileName(feed.getDownload_url())) .toString(); feed.setFile_url(fileUrl); - DownloadStatus status = new DownloadStatus(feed, "OnlineFeed"); + DownloadRequest request = new DownloadRequest(feed.getFile_url(), + feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED); + /* TODO update HttpDownloader httpDownloader = new HttpDownloader(downloaderCallback, - status); + request); httpDownloader.start(); + */ } /** Displays a progress indicator. */ @@ -215,13 +219,14 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { } else { builder.setMessage(R.string.error_msg_prefix); } - builder.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } - }); + builder.setNeutralButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { diff --git a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java index e44fdd478..77dae303e 100644 --- a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java +++ b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java @@ -1,31 +1,41 @@ package de.danoeh.antennapod.activity; import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import android.support.v7.app.ActionBarActivity; import android.view.*; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.mobeta.android.dslv.DragSortListView; - import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.ImageLoader; import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBReader; +import de.danoeh.antennapod.storage.DBTasks; +import de.danoeh.antennapod.storage.DBWriter; import de.danoeh.antennapod.util.UndoBarController; +import java.util.List; + public class OrganizeQueueActivity extends ActionBarActivity implements UndoBarController.UndoListener { private static final String TAG = "OrganizeQueueActivity"; private static final int MENU_ID_ACCEPT = 2; + private List<FeedItem> queue; + private OrganizeAdapter adapter; private UndoBarController undoBarController; @@ -42,13 +52,37 @@ public class OrganizeQueueActivity extends ActionBarActivity implements listView.setDropListener(dropListener); listView.setRemoveListener(removeListener); - adapter = new OrganizeAdapter(this); - listView.setAdapter(adapter); - + loadData(); undoBarController = new UndoBarController(findViewById(R.id.undobar), this); } + private void loadData() { + AsyncTask<Void, Void, List<FeedItem>> loadTask = new AsyncTask<Void, Void, List<FeedItem>>() { + + @Override + protected List<FeedItem> doInBackground(Void... voids) { + return DBReader.getQueue(OrganizeQueueActivity.this); + } + + @Override + protected void onPostExecute(List<FeedItem> feedItems) { + super.onPostExecute(feedItems); + if (feedItems != null) { + queue = feedItems; + if (adapter == null) { + adapter = new OrganizeAdapter(OrganizeQueueActivity.this); + listView.setAdapter(adapter); + } + adapter.notifyDataSetChanged(); + } else { + Log.e(TAG, "Queue was null"); + } + } + }; + loadTask.execute(); + } + @Override protected void onPause() { super.onPause(); @@ -58,8 +92,7 @@ public class OrganizeQueueActivity extends ActionBarActivity implements @Override protected void onStop() { super.onStop(); - FeedManager.getInstance().autodownloadUndownloadedItems( - getApplicationContext()); + DBTasks.autodownloadUndownloadedItems(getApplicationContext()); } @Override @@ -73,9 +106,7 @@ public class OrganizeQueueActivity extends ActionBarActivity implements @Override public void update(EventDistributor eventDistributor, Integer arg) { if (((EventDistributor.QUEUE_UPDATE | EventDistributor.FEED_LIST_UPDATE) & arg) != 0) { - if (adapter != null) { - adapter.notifyDataSetChanged(); - } + loadData(); } } }; @@ -84,9 +115,10 @@ public class OrganizeQueueActivity extends ActionBarActivity implements @Override public void drop(int from, int to) { - FeedManager manager = FeedManager.getInstance(); - manager.moveQueueItem(OrganizeQueueActivity.this, from, to, false); - adapter.notifyDataSetChanged(); + final FeedItem item = queue.remove(from); + queue.add(to, item); + adapter.notifyDataSetChanged(); + DBWriter.moveQueueItem(OrganizeQueueActivity.this, from, to, true); } }; @@ -94,9 +126,8 @@ public class OrganizeQueueActivity extends ActionBarActivity implements @Override public void remove(int which) { - FeedManager manager = FeedManager.getInstance(); FeedItem item = (FeedItem) listView.getAdapter().getItem(which); - manager.removeQueueItem(OrganizeQueueActivity.this, item, false); + DBWriter.removeQueueItem(OrganizeQueueActivity.this, item.getId(), true); undoBarController.showUndoBar(false, getString(R.string.removed_from_queue), new UndoToken(item, which)); @@ -124,22 +155,18 @@ public class OrganizeQueueActivity extends ActionBarActivity implements public void onUndo(Parcelable token) { // Perform the undo UndoToken undoToken = (UndoToken) token; - FeedItem feedItem = undoToken.getFeedItem(); + long itemId = undoToken.getFeedItemId(); int position = undoToken.getPosition(); - - FeedManager manager = FeedManager.getInstance(); - manager.addQueueItemAt(OrganizeQueueActivity.this, feedItem, position, - false); + DBWriter.addQueueItemAt(OrganizeQueueActivity.this, itemId, position, false); } private static class OrganizeAdapter extends BaseAdapter { - private Context context; - private FeedManager manager = FeedManager.getInstance(); + private OrganizeQueueActivity organizeQueueActivity; - public OrganizeAdapter(Context context) { + public OrganizeAdapter(OrganizeQueueActivity organizeQueueActivity) { super(); - this.context = context; + this.organizeQueueActivity = organizeQueueActivity; } @Override @@ -149,7 +176,7 @@ public class OrganizeQueueActivity extends ActionBarActivity implements if (convertView == null) { holder = new Holder(); - LayoutInflater inflater = (LayoutInflater) context + LayoutInflater inflater = (LayoutInflater) organizeQueueActivity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate( R.layout.organize_queue_listitem, null); @@ -186,13 +213,20 @@ public class OrganizeQueueActivity extends ActionBarActivity implements @Override public int getCount() { - int queueSize = manager.getQueueSize(true); - return queueSize; + if (organizeQueueActivity.queue != null) { + return organizeQueueActivity.queue.size(); + } else { + return 0; + } } @Override public FeedItem getItem(int position) { - return manager.getQueueItemAtIndex(position, true); + if (organizeQueueActivity.queue != null) { + return organizeQueueActivity.queue.get(position); + } else { + return null; + } } @Override @@ -208,7 +242,6 @@ public class OrganizeQueueActivity extends ActionBarActivity implements private int position; public UndoToken(FeedItem item, int position) { - FeedManager manager = FeedManager.getInstance(); this.itemId = item.getId(); this.feedId = item.getFeed().getId(); this.position = position; @@ -240,9 +273,8 @@ public class OrganizeQueueActivity extends ActionBarActivity implements out.writeInt(position); } - public FeedItem getFeedItem() { - FeedManager manager = FeedManager.getInstance(); - return manager.getFeedItem(itemId, feedId); + public long getFeedItemId() { + return itemId; } public int getPosition() { diff --git a/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java b/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java index 4f4da71e2..f329581e5 100644 --- a/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java +++ b/src/de/danoeh/antennapod/activity/PlaybackHistoryActivity.java @@ -11,9 +11,9 @@ import android.view.Menu; import android.view.MenuItem; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.storage.DBWriter; public class PlaybackHistoryActivity extends ActionBarActivity { private static final String TAG = "PlaybackHistoryActivity"; @@ -36,7 +36,7 @@ public class PlaybackHistoryActivity extends ActionBarActivity { startActivity(intent); return true; case R.id.clear_history_item: - FeedManager.getInstance().clearPlaybackHistory(this); + DBWriter.clearPlaybackHistory(this); return true; } return super.onOptionsItemSelected(item); diff --git a/src/de/danoeh/antennapod/activity/PreferenceActivity.java b/src/de/danoeh/antennapod/activity/PreferenceActivity.java index afd4bd433..880724c28 100644 --- a/src/de/danoeh/antennapod/activity/PreferenceActivity.java +++ b/src/de/danoeh/antennapod/activity/PreferenceActivity.java @@ -25,29 +25,30 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.FlattrClickWorker; import de.danoeh.antennapod.asynctask.OpmlExportWorker; -import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.util.flattr.FlattrUtils; -/** The main preference activity */ +/** + * The main preference activity + */ public class PreferenceActivity extends android.preference.PreferenceActivity { - private static final String TAG = "PreferenceActivity"; + private static final String TAG = "PreferenceActivity"; - private static final String PREF_FLATTR_THIS_APP = "prefFlattrThisApp"; - private static final String PREF_FLATTR_AUTH = "pref_flattr_authenticate"; - private static final String PREF_FLATTR_REVOKE = "prefRevokeAccess"; - private static final String PREF_OPML_EXPORT = "prefOpmlExport"; - private static final String PREF_ABOUT = "prefAbout"; - private static final String PREF_CHOOSE_DATA_DIR = "prefChooseDataDir"; - private static final String AUTO_DL_PREF_SCREEN = "prefAutoDownloadSettings"; + private static final String PREF_FLATTR_THIS_APP = "prefFlattrThisApp"; + private static final String PREF_FLATTR_AUTH = "pref_flattr_authenticate"; + private static final String PREF_FLATTR_REVOKE = "prefRevokeAccess"; + private static final String PREF_OPML_EXPORT = "prefOpmlExport"; + private static final String PREF_ABOUT = "prefAbout"; + private static final String PREF_CHOOSE_DATA_DIR = "prefChooseDataDir"; + private static final String AUTO_DL_PREF_SCREEN = "prefAutoDownloadSettings"; - private CheckBoxPreference[] selectedNetworks; + private CheckBoxPreference[] selectedNetworks; - @SuppressWarnings("deprecation") - @Override - public void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); + @SuppressWarnings("deprecation") + @Override + public void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); if (android.os.Build.VERSION.SDK_INT >= 11) { getActionBar().setDisplayHomeAsUpEnabled(true); @@ -57,332 +58,319 @@ public class PreferenceActivity extends android.preference.PreferenceActivity { findPreference(PREF_FLATTR_THIS_APP).setOnPreferenceClickListener( new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - new FlattrClickWorker(PreferenceActivity.this, - FlattrUtils.APP_URL).executeAsync(); - - return true; - } - }); - - findPreference(PREF_FLATTR_REVOKE).setOnPreferenceClickListener( - new OnPreferenceClickListener() { - - @Override - public boolean onPreferenceClick(Preference preference) { - FlattrUtils.revokeAccessToken(PreferenceActivity.this); - checkItemVisibility(); - return true; - } - - }); - - findPreference(PREF_ABOUT).setOnPreferenceClickListener( - new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new FlattrClickWorker(PreferenceActivity.this, + FlattrUtils.APP_URL).executeAsync(); + + return true; + } + }); + + findPreference(PREF_FLATTR_REVOKE).setOnPreferenceClickListener( + new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + FlattrUtils.revokeAccessToken(PreferenceActivity.this); + checkItemVisibility(); + return true; + } + + }); + + findPreference(PREF_ABOUT).setOnPreferenceClickListener( + new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + PreferenceActivity.this.startActivity(new Intent( + PreferenceActivity.this, AboutActivity.class)); + return true; + } + + }); + + findPreference(PREF_OPML_EXPORT).setOnPreferenceClickListener( + new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + new OpmlExportWorker(PreferenceActivity.this) + .executeAsync(); + + return true; + } + }); + + findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener( + new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + startActivityForResult( + new Intent(PreferenceActivity.this, + DirectoryChooserActivity.class), + DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED); + return true; + } + }); + findPreference(UserPreferences.PREF_THEME) + .setOnPreferenceChangeListener( + new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange( + Preference preference, Object newValue) { + Intent i = getIntent(); + i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK + | Intent.FLAG_ACTIVITY_NEW_TASK); + finish(); + startActivity(i); + return true; + } + }); + findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER) + .setOnPreferenceChangeListener( + new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange( + Preference preference, Object newValue) { + if (newValue instanceof Boolean) { + setSelectedNetworksEnabled((Boolean) newValue); + return true; + } else { + return false; + } + } + }); + findPreference(UserPreferences.PREF_EPISODE_CACHE_SIZE) + .setOnPreferenceChangeListener( + new OnPreferenceChangeListener() { + + + @Override + public boolean onPreferenceChange(Preference preference, Object o) { + checkItemVisibility(); + return true; + } + }); + buildUpdateIntervalPreference(); + buildAutodownloadSelectedNetworsPreference(); + setSelectedNetworksEnabled(UserPreferences + .isEnableAutodownloadWifiFilter()); + + } + + private void buildUpdateIntervalPreference() { + ListPreference pref = (ListPreference) findPreference(UserPreferences.PREF_UPDATE_INTERVAL); + String[] values = getResources().getStringArray( + R.array.update_intervall_values); + String[] entries = new String[values.length]; + for (int x = 0; x < values.length; x++) { + Integer v = Integer.parseInt(values[x]); + switch (v) { + case 0: + entries[x] = getString(R.string.pref_update_interval_hours_manual); + break; + case 1: + entries[x] = v + + " " + + getString(R.string.pref_update_interval_hours_singular); + break; + default: + entries[x] = v + " " + + getString(R.string.pref_update_interval_hours_plural); + break; + + } + } + pref.setEntries(entries); - @Override - public boolean onPreferenceClick(Preference preference) { - PreferenceActivity.this.startActivity(new Intent( - PreferenceActivity.this, AboutActivity.class)); - return true; - } + } - }); + private void setSelectedNetworksEnabled(boolean b) { + if (selectedNetworks != null) { + for (Preference p : selectedNetworks) { + p.setEnabled(b); + } + } + } + + @Override + protected void onResume() { + super.onResume(); + checkItemVisibility(); + setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize()); + setDataFolderText(); + } + + @SuppressWarnings("deprecation") + private void checkItemVisibility() { + + boolean hasFlattrToken = FlattrUtils.hasToken(); + + findPreference(PREF_FLATTR_AUTH).setEnabled(!hasFlattrToken); + findPreference(PREF_FLATTR_REVOKE).setEnabled(hasFlattrToken); + + findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER) + .setEnabled(UserPreferences.isEnableAutodownload()); + setSelectedNetworksEnabled(UserPreferences.isEnableAutodownload() + && UserPreferences.isEnableAutodownloadWifiFilter()); + + } + + private void setEpisodeCacheSizeText(int cacheSize) { + String s; + if (cacheSize == getResources().getInteger( + R.integer.episode_cache_size_unlimited)) { + s = getString(R.string.pref_episode_cache_unlimited); + } else { + s = Integer.toString(cacheSize) + + getString(R.string.episodes_suffix); + } + findPreference(UserPreferences.PREF_EPISODE_CACHE_SIZE).setSummary(s); + } + + private void setDataFolderText() { + File f = UserPreferences.getDataFolder(this, null); + if (f != null) { + findPreference(PREF_CHOOSE_DATA_DIR) + .setSummary(f.getAbsolutePath()); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + 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); + startActivity(intent); + break; + default: + return false; + } + return true; + } + + @Override + protected void onApplyThemeResource(Theme theme, int resid, boolean first) { + theme.applyStyle(UserPreferences.getTheme(), true); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) { + String dir = data + .getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR); + if (AppConfig.DEBUG) + Log.d(TAG, "Setting data folder"); + UserPreferences.setDataFolder(dir); + } + } - findPreference(PREF_OPML_EXPORT).setOnPreferenceClickListener( - new OnPreferenceClickListener() { + private void buildAutodownloadSelectedNetworsPreference() { + if (selectedNetworks != null) { + clearAutodownloadSelectedNetworsPreference(); + } + // get configured networks + WifiManager wifiservice = (WifiManager) getSystemService(Context.WIFI_SERVICE); + List<WifiConfiguration> networks = wifiservice.getConfiguredNetworks(); + + if (networks != null) { + selectedNetworks = new CheckBoxPreference[networks.size()]; + List<String> prefValues = Arrays.asList(UserPreferences + .getAutodownloadSelectedNetworks()); + PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); + OnPreferenceClickListener clickListener = new OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + if (preference instanceof CheckBoxPreference) { + String key = preference.getKey(); + ArrayList<String> prefValuesList = new ArrayList<String>( + Arrays.asList(UserPreferences + .getAutodownloadSelectedNetworks())); + boolean newValue = ((CheckBoxPreference) preference) + .isChecked(); + if (AppConfig.DEBUG) + Log.d(TAG, "Selected network " + key + + ". New state: " + newValue); + + int index = prefValuesList.indexOf(key); + if (index >= 0 && newValue == false) { + // remove network + prefValuesList.remove(index); + } else if (index < 0 && newValue == true) { + prefValuesList.add(key); + } + + UserPreferences.setAutodownloadSelectedNetworks( + PreferenceActivity.this, prefValuesList + .toArray(new String[prefValuesList + .size()])); + return true; + } else { + return false; + } + } + }; + // create preference for each known network. attach listener and set + // value + for (int i = 0; i < networks.size(); i++) { + WifiConfiguration config = networks.get(i); + + CheckBoxPreference pref = new CheckBoxPreference(this); + String key = Integer.toString(config.networkId); + pref.setTitle(config.SSID); + pref.setKey(key); + pref.setOnPreferenceClickListener(clickListener); + pref.setPersistent(false); + pref.setChecked(prefValues.contains(key)); + selectedNetworks[i] = pref; + prefScreen.addPreference(pref); + } + } else { + Log.e(TAG, "Couldn't get list of configure Wi-Fi networks"); + } + } - @Override - public boolean onPreferenceClick(Preference preference) { - if (FeedManager.getInstance().getFeedsSize() > 0) { - new OpmlExportWorker(PreferenceActivity.this) - .executeAsync(); - } - return true; - } - }); - - findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener( - new OnPreferenceClickListener() { + private void clearAutodownloadSelectedNetworsPreference() { + if (selectedNetworks != null) { + PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); - @Override - public boolean onPreferenceClick(Preference preference) { - startActivityForResult( - new Intent(PreferenceActivity.this, - DirectoryChooserActivity.class), - DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED); - return true; - } - }); - findPreference(UserPreferences.PREF_THEME) - .setOnPreferenceChangeListener( - new OnPreferenceChangeListener() { - - @Override - public boolean onPreferenceChange( - Preference preference, Object newValue) { - Intent i = getIntent(); - i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK - | Intent.FLAG_ACTIVITY_NEW_TASK); - finish(); - startActivity(i); - return true; - } - }); - findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER) - .setOnPreferenceChangeListener( - new OnPreferenceChangeListener() { - - @Override - public boolean onPreferenceChange( - Preference preference, Object newValue) { - if (newValue instanceof Boolean) { - setSelectedNetworksEnabled((Boolean) newValue); - return true; - } else { - return false; - } - } - }); - findPreference(UserPreferences.PREF_EPISODE_CACHE_SIZE) - .setOnPreferenceChangeListener( - new OnPreferenceChangeListener() { - - @Override - public boolean onPreferenceChange( - Preference preference, Object newValue) { - if (newValue instanceof String) { - setEpisodeCacheSizeText(Integer - .valueOf((String) newValue)); - } - return true; - } - }); - findPreference(UserPreferences.PREF_ENABLE_AUTODL) - .setOnPreferenceClickListener(new OnPreferenceClickListener() { - - @Override - public boolean onPreferenceClick(Preference preference) { - checkItemVisibility(); - return true; - } - }); - buildUpdateIntervalPreference(); - buildAutodownloadSelectedNetworsPreference(); - setSelectedNetworksEnabled(UserPreferences - .isEnableAutodownloadWifiFilter()); - - } - - private void buildUpdateIntervalPreference() { - ListPreference pref = (ListPreference) findPreference(UserPreferences.PREF_UPDATE_INTERVAL); - String[] values = getResources().getStringArray( - R.array.update_intervall_values); - String[] entries = new String[values.length]; - for (int x = 0; x < values.length; x++) { - Integer v = Integer.parseInt(values[x]); - switch (v) { - case 0: - entries[x] = getString(R.string.pref_update_interval_hours_manual); - break; - case 1: - entries[x] = v - + " " - + getString(R.string.pref_update_interval_hours_singular); - break; - default: - entries[x] = v + " " - + getString(R.string.pref_update_interval_hours_plural); - break; - - } - } - pref.setEntries(entries); - - } - - private void setSelectedNetworksEnabled(boolean b) { - if (selectedNetworks != null) { - for (Preference p : selectedNetworks) { - p.setEnabled(b); - } - } - } - - @Override - protected void onResume() { - super.onResume(); - checkItemVisibility(); - setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize()); - setDataFolderText(); - } - - @SuppressWarnings("deprecation") - private void checkItemVisibility() { - - boolean hasFlattrToken = FlattrUtils.hasToken(); - - findPreference(PREF_FLATTR_AUTH).setEnabled(!hasFlattrToken); - findPreference(PREF_FLATTR_REVOKE).setEnabled(hasFlattrToken); - - findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER) - .setEnabled(UserPreferences.isEnableAutodownload()); - setSelectedNetworksEnabled(UserPreferences.isEnableAutodownload() - && UserPreferences.isEnableAutodownloadWifiFilter()); - - } - - private void setEpisodeCacheSizeText(int cacheSize) { - String s; - if (cacheSize == getResources().getInteger( - R.integer.episode_cache_size_unlimited)) { - s = getString(R.string.pref_episode_cache_unlimited); - } else { - s = Integer.toString(cacheSize) - + getString(R.string.episodes_suffix); - } - findPreference(UserPreferences.PREF_EPISODE_CACHE_SIZE).setSummary(s); - } - - private void setDataFolderText() { - File f = UserPreferences.getDataFolder(this, null); - if (f != null) { - findPreference(PREF_CHOOSE_DATA_DIR) - .setSummary(f.getAbsolutePath()); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - 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); - startActivity(intent); - break; - default: - return false; - } - return true; - } - - @Override - protected void onApplyThemeResource(Theme theme, int resid, boolean first) { - theme.applyStyle(UserPreferences.getTheme(), true); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) { - String dir = data - .getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR); - if (AppConfig.DEBUG) - Log.d(TAG, "Setting data folder"); - UserPreferences.setDataFolder(dir); - } - } - - private void buildAutodownloadSelectedNetworsPreference() { - if (selectedNetworks != null) { - clearAutodownloadSelectedNetworsPreference(); - } - // get configured networks - WifiManager wifiservice = (WifiManager) getSystemService(Context.WIFI_SERVICE); - List<WifiConfiguration> networks = wifiservice.getConfiguredNetworks(); - - if (networks != null) { - selectedNetworks = new CheckBoxPreference[networks.size()]; - List<String> prefValues = Arrays.asList(UserPreferences - .getAutodownloadSelectedNetworks()); - PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); - OnPreferenceClickListener clickListener = new OnPreferenceClickListener() { - - @Override - public boolean onPreferenceClick(Preference preference) { - if (preference instanceof CheckBoxPreference) { - String key = preference.getKey(); - ArrayList<String> prefValuesList = new ArrayList<String>( - Arrays.asList(UserPreferences - .getAutodownloadSelectedNetworks())); - boolean newValue = ((CheckBoxPreference) preference) - .isChecked(); - if (AppConfig.DEBUG) - Log.d(TAG, "Selected network " + key - + ". New state: " + newValue); - - int index = prefValuesList.indexOf(key); - if (index >= 0 && newValue == false) { - // remove network - prefValuesList.remove(index); - } else if (index < 0 && newValue == true) { - prefValuesList.add(key); - } - - UserPreferences.setAutodownloadSelectedNetworks( - PreferenceActivity.this, prefValuesList - .toArray(new String[prefValuesList - .size()])); - return true; - } else { - return false; - } - } - }; - // create preference for each known network. attach listener and set - // value - for (int i = 0; i < networks.size(); i++) { - WifiConfiguration config = networks.get(i); - - CheckBoxPreference pref = new CheckBoxPreference(this); - String key = Integer.toString(config.networkId); - pref.setTitle(config.SSID); - pref.setKey(key); - pref.setOnPreferenceClickListener(clickListener); - pref.setPersistent(false); - pref.setChecked(prefValues.contains(key)); - selectedNetworks[i] = pref; - prefScreen.addPreference(pref); - } - } else { - Log.e(TAG, "Couldn't get list of configure Wi-Fi networks"); - } - } - - private void clearAutodownloadSelectedNetworsPreference() { - if (selectedNetworks != null) { - PreferenceScreen prefScreen = (PreferenceScreen) findPreference(AUTO_DL_PREF_SCREEN); - - for (int i = 0; i < selectedNetworks.length; i++) { - if (selectedNetworks[i] != null) { - prefScreen.removePreference(selectedNetworks[i]); - } - } - } - } - - @SuppressWarnings("deprecation") - @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, - Preference preference) { - super.onPreferenceTreeClick(preferenceScreen, preference); - if (preference != null) - if (preference instanceof PreferenceScreen) - if (((PreferenceScreen) preference).getDialog() != null) - ((PreferenceScreen) preference) - .getDialog() - .getWindow() - .getDecorView() - .setBackgroundDrawable( - this.getWindow().getDecorView() - .getBackground().getConstantState() - .newDrawable()); - return false; - } + for (int i = 0; i < selectedNetworks.length; i++) { + if (selectedNetworks[i] != null) { + prefScreen.removePreference(selectedNetworks[i]); + } + } + } + } + + @SuppressWarnings("deprecation") + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, + Preference preference) { + super.onPreferenceTreeClick(preferenceScreen, preference); + if (preference != null) + if (preference instanceof PreferenceScreen) + if (((PreferenceScreen) preference).getDialog() != null) + ((PreferenceScreen) preference) + .getDialog() + .getWindow() + .getDecorView() + .setBackgroundDrawable( + this.getWindow().getDecorView() + .getBackground().getConstantState() + .newDrawable()); + return false; + } } diff --git a/src/de/danoeh/antennapod/activity/SearchActivity.java b/src/de/danoeh/antennapod/activity/SearchActivity.java index feb9ba328..b6bdab83c 100644 --- a/src/de/danoeh/antennapod/activity/SearchActivity.java +++ b/src/de/danoeh/antennapod/activity/SearchActivity.java @@ -1,11 +1,12 @@ package de.danoeh.antennapod.activity; -import java.util.ArrayList; +import java.util.List; import android.annotation.SuppressLint; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; @@ -20,150 +21,152 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.SearchlistAdapter; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedManager; -import de.danoeh.antennapod.feed.FeedSearcher; +import de.danoeh.antennapod.storage.FeedSearcher; import de.danoeh.antennapod.feed.SearchResult; import de.danoeh.antennapod.fragment.FeedlistFragment; import de.danoeh.antennapod.fragment.ItemlistFragment; import de.danoeh.antennapod.preferences.UserPreferences; -/** Displays the results when the user searches for FeedItems or Feeds. */ -public class SearchActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{ - private static final String TAG = "SearchActivity"; +/** + * Displays the results when the user searches for FeedItems or Feeds. + */ +public class SearchActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { + private static final String TAG = "SearchActivity"; - public static final String EXTRA_FEED_ID = "de.danoeh.antennapod.searchactivity.extra.feedId"; + public static final String EXTRA_FEED_ID = "de.danoeh.antennapod.searchactivity.extra.feedId"; - private SearchlistAdapter searchAdapter; - private ArrayList<SearchResult> content; + private SearchlistAdapter searchAdapter; + private List<SearchResult> content; - /** Feed that is being searched or null if the search is global. */ - private Feed selectedFeed; + /** + * ID of the feed that is being searched or null if the search is global. + */ + private long feedID; private ListView listView; - private TextView txtvStatus; + private TextView txtvStatus; - @Override - protected void onCreate(Bundle savedInstanceState) { - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.searchlist); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.searchlist); listView = (ListView) findViewById(android.R.id.list); - txtvStatus = (TextView) findViewById(android.R.id.empty); + txtvStatus = (TextView) findViewById(android.R.id.empty); listView.setOnItemClickListener(this); } - @Override - protected void onNewIntent(Intent intent) { - setIntent(intent); - } - - @Override - protected void onResume() { - super.onResume(); - Intent intent = getIntent(); - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - Bundle extra = intent.getBundleExtra(SearchManager.APP_DATA); - if (extra != null) { - if (AppConfig.DEBUG) - Log.d(TAG, "Found bundle extra"); - long feedId = extra.getLong(EXTRA_FEED_ID); - selectedFeed = FeedManager.getInstance().getFeed(feedId); - } - if (AppConfig.DEBUG) - Log.d(TAG, "Starting search"); - String query = intent.getStringExtra(SearchManager.QUERY); - getSupportActionBar() - .setSubtitle( - getString(R.string.search_term_label) + "\"" - + query + "\""); - handleSearchRequest(query); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) - .setIcon( - obtainStyledAttributes( - new int[] { R.attr.action_search }) - .getDrawable(0)) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - 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); - startActivity(intent); - return true; - case R.id.search_item: - onSearchRequested(); - return true; - default: - return false; - } - } - - @Override - public boolean onSearchRequested() { - Bundle extra = null; - if (selectedFeed != null) { - extra = new Bundle(); - extra.putLong(EXTRA_FEED_ID, selectedFeed.getId()); - } - startSearch(null, false, extra, false); - return true; - } - - @SuppressLint({ "NewApi", "NewApi" }) - private void handleSearchRequest(final String query) { - if (searchAdapter != null) { - searchAdapter.clear(); - searchAdapter.notifyDataSetChanged(); - } - txtvStatus.setText(R.string.search_status_searching); - - Thread thread = new Thread() { - - @Override - public void run() { - Log.d(TAG, "Starting background work"); - final ArrayList<SearchResult> result = FeedSearcher - .performSearch(SearchActivity.this, query, selectedFeed); - if (SearchActivity.this != null) { - SearchActivity.this.runOnUiThread(new Runnable() { - - @Override - public void run() { - if (AppConfig.DEBUG) - Log.d(TAG, "Background work finished"); - if (AppConfig.DEBUG) - Log.d(TAG, "Found " + result.size() - + " results"); - content = result; - - searchAdapter = new SearchlistAdapter( - SearchActivity.this, 0, content); - listView.setAdapter(searchAdapter); - searchAdapter.notifyDataSetChanged(); - if (content.isEmpty()) { - txtvStatus - .setText(R.string.search_status_no_results); - } - } - }); - } - } - }; - thread.start(); - - } + @Override + protected void onNewIntent(Intent intent) { + setIntent(intent); + } + + @Override + protected void onResume() { + super.onResume(); + Intent intent = getIntent(); + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + Bundle extra = intent.getBundleExtra(SearchManager.APP_DATA); + if (extra != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Found bundle extra"); + feedID = extra.getLong(EXTRA_FEED_ID); + } + if (AppConfig.DEBUG) + Log.d(TAG, "Starting search"); + String query = intent.getStringExtra(SearchManager.QUERY); + getSupportActionBar() + .setSubtitle( + getString(R.string.search_term_label) + "\"" + + query + "\""); + handleSearchRequest(query); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label) + .setIcon( + obtainStyledAttributes( + new int[]{R.attr.action_search}) + .getDrawable(0)), + (MenuItem.SHOW_AS_ACTION_IF_ROOM)); + 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); + startActivity(intent); + return true; + case R.id.search_item: + onSearchRequested(); + return true; + default: + return false; + } + } + + @Override + public boolean onSearchRequested() { + Bundle extra = null; + if (feedID != 0) { + extra = new Bundle(); + extra.putLong(EXTRA_FEED_ID, feedID); + } + startSearch(null, false, extra, false); + return true; + } + + @SuppressLint({"NewApi", "NewApi"}) + private void handleSearchRequest(final String query) { + if (searchAdapter != null) { + searchAdapter.clear(); + searchAdapter.notifyDataSetChanged(); + } + txtvStatus.setText(R.string.search_status_searching); + + Thread thread = new Thread() { + + @Override + public void run() { + Log.d(TAG, "Starting background work"); + final List<SearchResult> result = FeedSearcher + .performSearch(SearchActivity.this, query, feedID); + if (SearchActivity.this != null) { + SearchActivity.this.runOnUiThread(new Runnable() { + + @Override + public void run() { + if (AppConfig.DEBUG) + Log.d(TAG, "Background work finished"); + if (AppConfig.DEBUG) + Log.d(TAG, "Found " + result.size() + + " results"); + content = result; + + searchAdapter = new SearchlistAdapter( + SearchActivity.this, 0, content); + listView.setAdapter(searchAdapter); + searchAdapter.notifyDataSetChanged(); + if (content.isEmpty()) { + txtvStatus + .setText(R.string.search_status_no_results); + } + } + }); + } + } + }; + thread.start(); + + } @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { |