Added a call back to reshare.

This commit is contained in:
Sadique Ali 2013-07-20 15:52:51 -07:00
parent 07691ae544
commit db10f0d860
6 changed files with 73 additions and 20 deletions

View File

@ -17,7 +17,8 @@
android:theme="@style/AppTheme" >
<activity
android:name="in.sdqali.sharewithtitle.MainActivity"
android:label="@string/app_name" >
android:label="@string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />

View File

@ -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));
}
}

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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<String, Void, String> {
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("<head>.*?<title>(.*?)</title>.*?</head>", 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();
}
}
}

View File

@ -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);
}