From 07691ae544162a08ece945b26a24244e495aedc7 Mon Sep 17 00:00:00 2001 From: Sadique Ali Date: Sat, 20 Jul 2013 15:12:13 -0700 Subject: [PATCH] Pulled out the Callback handler. --- .../sdqali/sharewithtitle/MainActivity.java | 36 ++----------- .../sharewithtitle/UpdateViewCallback.java | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java index 46f680b..1511caf 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java @@ -23,52 +23,22 @@ public class MainActivity extends Activity { final TextView textView = (TextView) findViewById(R.id.greet_text); textView.setText(""); - final View progressBar = (ProgressBar)findViewById(R.id.progressBar); + final View 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 TitleGrabCallback() { - @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); - } - }); + new TitleRetriever(sharedText).retrieve(new UpdateViewCallback(getApplicationContext(), textView, progressBar)); } } - private void showToast(String text) { - Context context = getApplicationContext(); - int duration = Toast.LENGTH_SHORT; - - Toast toast = Toast.makeText(context, text, duration); - toast.show(); - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } - + } diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java new file mode 100644 index 0000000..1326c94 --- /dev/null +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/UpdateViewCallback.java @@ -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(); + } + +}