From db10f0d86006e9fd567c0507e6c142beac218127 Mon Sep 17 00:00:00 2001 From: Sadique Ali Date: Sat, 20 Jul 2013 15:52:51 -0700 Subject: [PATCH] Added a call back to reshare. --- ShareWithTitle/src/main/AndroidManifest.xml | 3 +- .../sdqali/sharewithtitle/MainActivity.java | 8 +-- .../sharewithtitle/ReshareCallback.java | 49 +++++++++++++++++++ .../sharewithtitle/TitleGrabCallback.java | 6 +-- .../sdqali/sharewithtitle/TitleRetriever.java | 21 ++++---- .../sharewithtitle/UpdateViewCallback.java | 6 +-- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/ReshareCallback.java diff --git a/ShareWithTitle/src/main/AndroidManifest.xml b/ShareWithTitle/src/main/AndroidManifest.xml index 2664039..8aba449 100644 --- a/ShareWithTitle/src/main/AndroidManifest.xml +++ b/ShareWithTitle/src/main/AndroidManifest.xml @@ -17,7 +17,8 @@ android:theme="@style/AppTheme" > + android:label="@string/app_name" + android:noHistory="true"> diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java index 1511caf..6407639 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java @@ -19,18 +19,20 @@ public class MainActivity extends Activity { setContentView(R.layout.activity_main); Intent intent = getIntent(); + intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + + String action = intent.getAction(); final TextView textView = (TextView) findViewById(R.id.greet_text); textView.setText(""); - final View progressBar = findViewById(R.id.progressBar); + final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); if(action.equals(Intent.ACTION_SEND)) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); Log.d("Share With Title", "Received shared text: " + sharedText); - - new TitleRetriever(sharedText).retrieve(new UpdateViewCallback(getApplicationContext(), textView, progressBar)); + new TitleRetriever(sharedText).retrieve(new ReshareCallback(this, progressBar)); } } diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/ReshareCallback.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/ReshareCallback.java new file mode 100644 index 0000000..d72e305 --- /dev/null +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/ReshareCallback.java @@ -0,0 +1,49 @@ +package in.sdqali.sharewithtitle; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.Toast; + +/** + * Created by sdqali on 7/20/13. + */ +public class ReshareCallback implements TitleGrabCallback{ + private Activity activity; + private Context applicationContext; + private ProgressBar progressBar; + + public ReshareCallback(Activity mainActivity, ProgressBar progressBar) { + this.activity = mainActivity; + this.progressBar = progressBar; + } + + @Override + public void onSuccess(String titleAndLink) { + progressBar.setVisibility(View.VISIBLE); + Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); + + sharingIntent.setType("text/plain"); + sharingIntent.putExtra(Intent.EXTRA_TEXT, titleAndLink); + activity.startActivity(Intent.createChooser(sharingIntent, "Share via")); + } + + @Override + public void onProgress() { + progressBar.animate(); + } + + @Override + public void cleanUp() { + progressBar.setVisibility(View.GONE); + } + + @Override + public void showError(String errorMessage) { + int duration = Toast.LENGTH_SHORT; + Toast toast = Toast.makeText(activity.getApplicationContext(), errorMessage, duration); + toast.show(); + } +} diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleGrabCallback.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleGrabCallback.java index ab33e08..c2e41df 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleGrabCallback.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleGrabCallback.java @@ -4,8 +4,8 @@ package in.sdqali.sharewithtitle; * Created by sdqali on 7/20/13. */ public interface TitleGrabCallback { - public void update(String title); - public void showProgress(); - public void finish(); + public void onSuccess(String title); + public void onProgress(); + public void cleanUp(); public void showError(String errorMessage); } diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java index 0a1b708..13ef954 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java @@ -17,11 +17,11 @@ public class TitleRetriever { this.urlText = url; } - public void retrieve(TitleGrabCallback viewUpdater) { + public void retrieve(TitleGrabCallback callback) { if(isValidUrl(urlText)) { - new DownloadTaskNew(viewUpdater).execute(urlText); + new DownloadTaskNew(callback).execute(urlText); } else { - viewUpdater.showError("Not a valid url. Could not load title!"); + callback.showError("Not a valid url. Could not load title!"); } } @@ -30,16 +30,16 @@ public class TitleRetriever { } private class DownloadTaskNew extends AsyncTask { - private TitleGrabCallback viewUpdater; + private TitleGrabCallback callback; private final PageDownloader pageDownloader = new PageDownloader(); - public DownloadTaskNew(TitleGrabCallback viewUpdater) { - this.viewUpdater = viewUpdater; + public DownloadTaskNew(TitleGrabCallback callback) { + this.callback = callback; } @Override protected String doInBackground(String... urls) { - viewUpdater.showProgress(); + callback.onProgress(); return pageDownloader.downloadUrl(urls[0]); } @@ -49,13 +49,14 @@ public class TitleRetriever { Pattern p = Pattern.compile(".*?(.*?).*?", Pattern.DOTALL); Matcher m = p.matcher(rawHtml); String title; - viewUpdater.update(urlText); + String output = urlText; while (m.find()) { title = m.group(1); Log.d("Share With Title", "Title: " + title); - viewUpdater.update(title); + output = title + " " + urlText; } - viewUpdater.finish(); + callback.onSuccess(output); + callback.cleanUp(); } } } diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java index 1326c94..8368751 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java @@ -21,18 +21,18 @@ class UpdateViewCallback implements TitleGrabCallback { } @Override - public void update(String title) { + public void onSuccess(String title) { textView.setText(title); progressBar.setVisibility(View.VISIBLE); } @Override - public void showProgress() { + public void onProgress() { progressBar.animate(); } @Override - public void finish() { + public void cleanUp() { progressBar.setVisibility(View.GONE); }