diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-03-17 17:44:18 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-03-17 17:44:18 +0100 |
commit | f8502b364238eea42482266650e120afd2dbbb35 (patch) | |
tree | 4fa9ba9aeebe2cbf34cd0189e2fd91b0804546b6 /src | |
parent | 0c2d78157c94a7c92cea5d7548332f55f5d976b2 (diff) | |
download | AntennaPod-f8502b364238eea42482266650e120afd2dbbb35.zip |
Added authentication dialog to OnlineFeedViewActivity
Diffstat (limited to 'src')
4 files changed, 174 insertions, 281 deletions
diff --git a/src/de/danoeh/antennapod/activity/AddFeedActivity.java b/src/de/danoeh/antennapod/activity/AddFeedActivity.java index 82e03f122..0fd623813 100644 --- a/src/de/danoeh/antennapod/activity/AddFeedActivity.java +++ b/src/de/danoeh/antennapod/activity/AddFeedActivity.java @@ -1,84 +1,70 @@ package de.danoeh.antennapod.activity; -import java.util.Date; - -import android.support.v4.app.NavUtils; -import android.support.v7.app.ActionBarActivity; -import android.view.Menu; -import android.view.MenuItem; -import de.danoeh.antennapod.activity.gpoddernet.GpodnetMainActivity; -import org.apache.commons.lang3.StringUtils; - -import android.app.AlertDialog; import android.app.ProgressDialog; -import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; - import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.activity.gpoddernet.GpodnetMainActivity; import de.danoeh.antennapod.preferences.UserPreferences; -import de.danoeh.antennapod.storage.DownloadRequestException; -import de.danoeh.antennapod.storage.DownloadRequester; -import de.danoeh.antennapod.util.ConnectionTester; -import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.StorageUtils; -import de.danoeh.antennapod.util.URLChecker; +import org.apache.commons.lang3.StringUtils; -/** Activity for adding a Feed */ +/** + * Activity for adding a Feed + */ public class AddFeedActivity extends ActionBarActivity { - private static final String TAG = "AddFeedActivity"; - - private DownloadRequester requester; + private static final String TAG = "AddFeedActivity"; - private EditText etxtFeedurl; - private Button butBrowseMiroGuide; + private EditText etxtFeedurl; + private Button butBrowseMiroGuide; private Button butBrowserGpoddernet; - private Button butOpmlImport; - private Button butConfirm; - private Button butCancel; - - private ProgressDialog progDialog; - - @Override - protected void onCreate(Bundle savedInstanceState) { - if (AppConfig.DEBUG) - Log.d(TAG, "Was started with Intent " + getIntent().getAction() - + " and Data " + getIntent().getDataString()); - setTheme(UserPreferences.getTheme()); - super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - StorageUtils.checkStorageAvailability(this); - setContentView(R.layout.addfeed); - - requester = DownloadRequester.getInstance(); - progDialog = new ProgressDialog(this); - - etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl); - if (StringUtils.equals(getIntent().getAction(), Intent.ACTION_VIEW)) { - etxtFeedurl.setText(getIntent().getDataString()); - } - - butBrowseMiroGuide = (Button) findViewById(R.id.butBrowseMiroguide); + private Button butOpmlImport; + private Button butConfirm; + private Button butCancel; + + private ProgressDialog progDialog; + + @Override + protected void onCreate(Bundle savedInstanceState) { + if (AppConfig.DEBUG) + Log.d(TAG, "Was started with Intent " + getIntent().getAction() + + " and Data " + getIntent().getDataString()); + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + StorageUtils.checkStorageAvailability(this); + setContentView(R.layout.addfeed); + + progDialog = new ProgressDialog(this); + + etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl); + if (StringUtils.equals(getIntent().getAction(), Intent.ACTION_VIEW)) { + etxtFeedurl.setText(getIntent().getDataString()); + } + + butBrowseMiroGuide = (Button) findViewById(R.id.butBrowseMiroguide); butBrowserGpoddernet = (Button) findViewById(R.id.butBrowseGpoddernet); - butOpmlImport = (Button) findViewById(R.id.butOpmlImport); - butConfirm = (Button) findViewById(R.id.butConfirm); - butCancel = (Button) findViewById(R.id.butCancel); - - butBrowseMiroGuide.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - startActivity(new Intent(AddFeedActivity.this, - MiroGuideMainActivity.class)); - } - }); + butOpmlImport = (Button) findViewById(R.id.butOpmlImport); + butConfirm = (Button) findViewById(R.id.butConfirm); + butCancel = (Button) findViewById(R.id.butCancel); + + butBrowseMiroGuide.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + startActivity(new Intent(AddFeedActivity.this, + MiroGuideMainActivity.class)); + } + }); butBrowserGpoddernet.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { @@ -87,141 +73,81 @@ public class AddFeedActivity extends ActionBarActivity { } }); - butOpmlImport.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - startActivity(new Intent(AddFeedActivity.this, - OpmlImportFromPathActivity.class)); - } - }); - - butConfirm.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - addNewFeed(); - } - }); - - butCancel.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - setResult(RESULT_CANCELED); - finish(); - } - }); - } - - @Override - protected void onResume() { - super.onResume(); - StorageUtils.checkStorageAvailability(this); - Intent intent = getIntent(); - if (intent.getAction() != null - && intent.getAction().equals(Intent.ACTION_SEND)) { - if (AppConfig.DEBUG) - Log.d(TAG, "Resuming with ACTION_SEND intent"); - String text = intent.getStringExtra(Intent.EXTRA_TEXT); - if (text != null) { - etxtFeedurl.setText(text); - } else { - if (AppConfig.DEBUG) - Log.d(TAG, "No text was sent"); - } - } - - } - - @Override - protected void onStop() { - super.onStop(); - if (AppConfig.DEBUG) - Log.d(TAG, "Stopping Activity"); - } - - @Override - protected void onPause() { - super.onPause(); - } - - /** Read the url text field and start downloading a new feed. */ - private void addNewFeed() { - String url = etxtFeedurl.getText().toString(); - url = URLChecker.prepareURL(url); - - if (url != null) { - final Feed feed = new Feed(url, new Date()); - final ConnectionTester conTester = new ConnectionTester(url, - new ConnectionTester.Callback() { - - @Override - public void onConnectionSuccessful() { - try { - requester.downloadFeed(AddFeedActivity.this, - feed); - if (progDialog.isShowing()) { - progDialog.dismiss(); - finish(); - } - } catch (DownloadRequestException e) { - e.printStackTrace(); - onConnectionFailure(DownloadError.ERROR_REQUEST_ERROR); - } - - } - - @Override - public void onConnectionFailure(DownloadError reason) { - handleDownloadError(reason); - } - }); - observeDownload(feed); - new Thread(conTester).start(); - - } - } - - /** Start listening for any intents send by the DownloadService. */ - private void observeDownload(Feed feed) { - progDialog.show(); - progDialog.setMessage(getString(R.string.loading_label)); - } - - private void handleDownloadError(DownloadError reason) { - final AlertDialog errorDialog = new AlertDialog.Builder(this).create(); - errorDialog.setTitle(R.string.error_label); - errorDialog.setMessage(getString(R.string.error_msg_prefix) + " " - + reason.getErrorString(this)); - errorDialog.setButton(getString(android.R.string.ok), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - errorDialog.dismiss(); - } - }); - if (progDialog.isShowing()) { - progDialog.dismiss(); - } - errorDialog.show(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - return true; - } + butOpmlImport.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + startActivity(new Intent(AddFeedActivity.this, + OpmlImportFromPathActivity.class)); + } + }); + + butConfirm.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(AddFeedActivity.this, DefaultOnlineFeedViewActivity.class); + intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, etxtFeedurl.getText().toString()); + intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, getSupportActionBar().getTitle()); + startActivity(intent); + } + }); + + butCancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + setResult(RESULT_CANCELED); + finish(); + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + StorageUtils.checkStorageAvailability(this); + Intent intent = getIntent(); + if (intent.getAction() != null + && intent.getAction().equals(Intent.ACTION_SEND)) { + if (AppConfig.DEBUG) + Log.d(TAG, "Resuming with ACTION_SEND intent"); + String text = intent.getStringExtra(Intent.EXTRA_TEXT); + if (text != null) { + etxtFeedurl.setText(text); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "No text was sent"); + } + } + } + @Override + protected void onStop() { + super.onStop(); + if (AppConfig.DEBUG) + Log.d(TAG, "Stopping Activity"); + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - default: - return false; - } - } + @Override + protected void onPause() { + super.onPause(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + default: + return false; + } + } } diff --git a/src/de/danoeh/antennapod/activity/DefaultOnlineFeedViewActivity.java b/src/de/danoeh/antennapod/activity/DefaultOnlineFeedViewActivity.java index bb56b1d12..5709fc958 100644 --- a/src/de/danoeh/antennapod/activity/DefaultOnlineFeedViewActivity.java +++ b/src/de/danoeh/antennapod/activity/DefaultOnlineFeedViewActivity.java @@ -3,7 +3,6 @@ package de.danoeh.antennapod.activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; -import android.support.v4.app.NavUtils; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; @@ -90,10 +89,11 @@ public class DefaultOnlineFeedViewActivity extends OnlineFeedViewActivity { @Override public void onClick(View v) { try { + Feed f = new Feed(feed.getDownload_url(), new Date(), feed.getTitle()); + f.setPreferences(feed.getPreferences()); DownloadRequester.getInstance().downloadFeed( DefaultOnlineFeedViewActivity.this, - new Feed(feed.getDownload_url(), new Date(), feed - .getTitle())); + f); } catch (DownloadRequestException e) { e.printStackTrace(); DownloadRequestErrorDialogCreator.newRequestErrorDialog(DefaultOnlineFeedViewActivity.this, diff --git a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java index 3af4bee7c..4a70a558a 100644 --- a/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java +++ b/src/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java @@ -1,6 +1,8 @@ package de.danoeh.antennapod.activity; import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; @@ -12,7 +14,9 @@ import android.widget.ProgressBar; import android.widget.RelativeLayout; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.dialog.AuthenticationDialog; import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedPreferences; import de.danoeh.antennapod.preferences.UserPreferences; import de.danoeh.antennapod.service.download.DownloadRequest; import de.danoeh.antennapod.service.download.DownloadStatus; @@ -52,9 +56,9 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { private Downloader downloader; @Override - protected void onCreate(Bundle arg0) { + protected void onCreate(Bundle savedInstanceState) { setTheme(UserPreferences.getTheme()); - super.onCreate(arg0); + super.onCreate(savedInstanceState); if (getIntent() != null && getIntent().hasExtra(ARG_TITLE)) { getSupportActionBar().setTitle(getIntent().getStringExtra(ARG_TITLE)); @@ -69,7 +73,20 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { if (AppConfig.DEBUG) Log.d(TAG, "Activity was started with url " + feedUrl); setLoadingLayout(); - startFeedDownload(feedUrl); + if (savedInstanceState == null) { + startFeedDownload(feedUrl, null, null); + } else { + startFeedDownload(feedUrl, savedInstanceState.getString("username"), savedInstanceState.getString("password")); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (feed != null && feed.getPreferences() != null) { + outState.putString("username", feed.getPreferences().getUsername()); + outState.putString("password", feed.getPreferences().getPassword()); + } } @Override @@ -92,6 +109,10 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { if (!status.isCancelled()) { if (status.isSuccessful()) { parseFeed(); + } else if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) { + Dialog dialog = new FeedViewAuthenticationDialog(OnlineFeedViewActivity.this, + R.string.authentication_notification_title, downloader.getDownloadRequest().getSource()); + dialog.show(); } else { String errorMsg = status.getReason().getErrorString( OnlineFeedViewActivity.this); @@ -113,17 +134,20 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { } - private void startFeedDownload(String url) { + private void startFeedDownload(String url, String username, String password) { if (AppConfig.DEBUG) Log.d(TAG, "Starting feed download"); url = URLChecker.prepareURL(url); feed = new Feed(url, new Date()); + if (username != null && password != null) { + feed.setPreferences(new FeedPreferences(0, true, username, password)); + } String fileUrl = new File(getExternalCacheDir(), FileNameGenerator.generateFileName(feed.getDownload_url())) .toString(); feed.setFile_url(fileUrl); final DownloadRequest request = new DownloadRequest(feed.getFile_url(), - feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED); + feed.getDownload_url(), "OnlineFeed", 0, Feed.FEEDFILETYPE_FEED, username, password); downloader = new HttpDownloader( request); new Thread() { @@ -259,4 +283,25 @@ public abstract class OnlineFeedViewActivity extends ActionBarActivity { builder.show(); } + private class FeedViewAuthenticationDialog extends AuthenticationDialog { + + private String feedUrl; + + public FeedViewAuthenticationDialog(Context context, int titleRes, String feedUrl) { + super(context, titleRes, true, false, null, null); + this.feedUrl = feedUrl; + } + + @Override + protected void onCancelled() { + super.onCancelled(); + finish(); + } + + @Override + protected void onConfirmed(String username, String password, boolean saveUsernamePassword) { + startFeedDownload(feedUrl, username, password); + } + } + } diff --git a/src/de/danoeh/antennapod/util/ConnectionTester.java b/src/de/danoeh/antennapod/util/ConnectionTester.java deleted file mode 100644 index 5d940d9e1..000000000 --- a/src/de/danoeh/antennapod/util/ConnectionTester.java +++ /dev/null @@ -1,78 +0,0 @@ -package de.danoeh.antennapod.util; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - -import android.os.Handler; -import android.util.Log; -import de.danoeh.antennapod.AppConfig; - -/** Tests a connection before downloading something. */ -public class ConnectionTester implements Runnable { - private static final String TAG = "ConnectionTester"; - private String strUrl; - private Callback callback; - private DownloadError reason; - - private Handler handler; - - public ConnectionTester(String url, Callback callback) { - super(); - this.strUrl = url; - this.callback = callback; - this.handler = new Handler(); - } - - @Override - public void run() { - if (AppConfig.DEBUG) - Log.d(TAG, "Testing connection"); - try { - URL url = new URL(strUrl); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.connect(); - handler.post(new Runnable() { - @Override - public void run() { - callback.onConnectionSuccessful(); - } - }); if (AppConfig.DEBUG) - Log.d(TAG, "Connection seems to work"); - } catch (MalformedURLException e) { - e.printStackTrace(); - reason = DownloadError.ERROR_CONNECTION_ERROR; - if (AppConfig.DEBUG) - Log.d(TAG, "Connection failed"); - handler.post(new Runnable() { - @Override - public void run() { - callback.onConnectionFailure(reason); - } - }); - } catch (IOException e) { - e.printStackTrace(); - reason = DownloadError.ERROR_CONNECTION_ERROR; - if (AppConfig.DEBUG) - Log.d(TAG, "Connection failed"); - handler.post(new Runnable() { - @Override - public void run() { - callback.onConnectionFailure(reason); - } - }); - } - } - - public static abstract class Callback { - public abstract void onConnectionSuccessful(); - - public abstract void onConnectionFailure(DownloadError reason); - } - - public DownloadError getReason() { - return reason; - } - -} |