Pulled out the Callback handler.

This commit is contained in:
Sadique Ali 2013-07-20 15:12:13 -07:00
parent bac9149865
commit 07691ae544
2 changed files with 54 additions and 33 deletions

View File

@ -23,52 +23,22 @@ public class MainActivity extends Activity {
final TextView textView = (TextView) findViewById(R.id.greet_text); final TextView textView = (TextView) findViewById(R.id.greet_text);
textView.setText(""); textView.setText("");
final View progressBar = (ProgressBar)findViewById(R.id.progressBar); final View progressBar = findViewById(R.id.progressBar);
if(action.equals(Intent.ACTION_SEND)) { if(action.equals(Intent.ACTION_SEND)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d("Share With Title", "Received shared text: " + sharedText); Log.d("Share With Title", "Received shared text: " + sharedText);
new TitleRetriever(sharedText).retrieve(new TitleGrabCallback() { new TitleRetriever(sharedText).retrieve(new UpdateViewCallback(getApplicationContext(), textView, progressBar));
@Override
public void update(String title) {
textView.setText(title);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void showProgress() {
progressBar.animate();
}
@Override
public void finish() {
progressBar.setVisibility(View.GONE);
}
@Override
public void showError(String errorMessage) {
showToast(errorMessage);
}
});
} }
} }
private void showToast(String text) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); getMenuInflater().inflate(R.menu.main, menu);
return true; return true;
} }
} }

View File

@ -0,0 +1,51 @@
package in.sdqali.sharewithtitle;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by sdqali on 7/20/13.
*/
class UpdateViewCallback implements TitleGrabCallback {
private Context mainActivityContext;
private final TextView textView;
private final View progressBar;
public UpdateViewCallback(Context context, TextView textView, View progressBar) {
mainActivityContext = context;
this.textView = textView;
this.progressBar = progressBar;
}
@Override
public void update(String title) {
textView.setText(title);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void showProgress() {
progressBar.animate();
}
@Override
public void finish() {
progressBar.setVisibility(View.GONE);
}
@Override
public void showError(String errorMessage) {
showToast(errorMessage);
}
private void showToast(String text) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(mainActivityContext, text, duration);
toast.show();
}
}