From e6d1081a68ac9b0a28953a372b81736d56cb5e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6rkem=20M=C3=BClayim?= Date: Thu, 10 May 2018 22:29:53 +0300 Subject: [PATCH 1/2] Added Voicer functionality to new slideshow button. --- app/build.gradle | 8 ++- .../java/com/deitel/slideshow/Slideshow.java | 52 +++++++++++++++++-- settings.gradle | 4 +- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index df7f914..64e1b9b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.deitel.slideshow" - minSdkVersion 8 + minSdkVersion 21 targetSdkVersion 8 } @@ -16,4 +16,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } + + dependencies { + compile (project(':voicer')) { + transitive = false + } + } } diff --git a/app/src/main/java/com/deitel/slideshow/Slideshow.java b/app/src/main/java/com/deitel/slideshow/Slideshow.java index 1bccd39..5c87863 100644 --- a/app/src/main/java/com/deitel/slideshow/Slideshow.java +++ b/app/src/main/java/com/deitel/slideshow/Slideshow.java @@ -17,6 +17,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.provider.MediaStore; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; @@ -33,13 +34,23 @@ import android.widget.TextView; import android.widget.Toast; -public class Slideshow extends ListActivity { +import com.miyagilabs.voicer.InitListener; +import com.miyagilabs.voicer.Voicer; +import com.miyagilabs.voicer.VoicerFactory; +import com.miyagilabs.voicer.annotation.Voice; +import com.miyagilabs.voicer.tts.SpeakerException; +import com.miyagilabs.voicer.tts.VirtualAssistant; +import com.miyagilabs.voicer.util.VoicerListener; +import com.miyagilabs.voicer.widget.Toaster; + +public class Slideshow extends ListActivity implements InitListener { // used when adding slideshow name as an extra to an Intent public static final String NAME_EXTRA = "NAME"; static List slideshowList; // List of slideshows private ListView slideshowListView; // this ListActivity's ListView private SlideshowAdapter slideshowAdapter; // adapter for the ListView + private Voicer mVoicer; // called when the activity is first created @Override @@ -60,6 +71,18 @@ public void onCreate(Bundle savedInstanceState) { builder.show(); } // end method onCreate + @Override + protected void onResume() { + VoicerFactory.fakeVoicer(this, this); + super.onResume(); + } + + @Override + protected void onPause() { + mVoicer.shutdown(); + super.onPause(); + } + // create the Activity's menu from a menu resource XML file @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -75,6 +98,12 @@ public boolean onCreateOptionsMenu(Menu menu) { // handle choice from options menu @Override public boolean onOptionsItemSelected(MenuItem item) { + newSlideShow(); + return super.onOptionsItemSelected(item); // call super's method + } // end method onOptionsItemSelected + + @Voice(commands = "new slideshow") + private void newSlideShow() { // get a reference to the LayoutInflater service LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE); @@ -119,9 +148,7 @@ public void onClick(DialogInterface dialog, int whichButton) { inputDialog.setNegativeButton(R.string.button_cancel, null); inputDialog.show(); - - return super.onOptionsItemSelected(item); // call super's method - } // end method onOptionsItemSelected + } // refresh ListView after slideshow editing is complete @Override @@ -131,6 +158,23 @@ protected void onActivityResult(int requestCode, int resultCode, slideshowAdapter.notifyDataSetChanged(); // refresh the adapter } // end method onActivityResult + @Override + public void onInit(Voicer voicer, int status) { + mVoicer = voicer; + mVoicer.register(this); + mVoicer.addVoicerListener(new Toaster(this)); + new Thread(new Runnable() { + @Override + public void run() { + try { + mVoicer.addVoicerListener(new VirtualAssistant(Slideshow.this)); + } catch (SpeakerException | InterruptedException ex) { + Log.e(getClass().getName(), null, ex); + } + } + }).start(); + } + // Class for implementing the "ViewHolder pattern" // for better ListView performance private static class ViewHolder { diff --git a/settings.gradle b/settings.gradle index e7b4def..f3b2588 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,3 @@ -include ':app' +include ':app', ':voicer' + +project (':voicer').projectDir = new File('../voicerframework/voicer') \ No newline at end of file From e6279437f6a9924ddaa5d30729ebe36b654c402d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6rkem=20M=C3=BClayim?= Date: Thu, 10 May 2018 22:41:57 +0300 Subject: [PATCH 2/2] Removed redundant thread. --- .../java/com/deitel/slideshow/Slideshow.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/deitel/slideshow/Slideshow.java b/app/src/main/java/com/deitel/slideshow/Slideshow.java index 5c87863..7415ed0 100644 --- a/app/src/main/java/com/deitel/slideshow/Slideshow.java +++ b/app/src/main/java/com/deitel/slideshow/Slideshow.java @@ -16,6 +16,7 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Looper; import android.provider.MediaStore; import android.util.Log; import android.view.Gravity; @@ -163,16 +164,11 @@ public void onInit(Voicer voicer, int status) { mVoicer = voicer; mVoicer.register(this); mVoicer.addVoicerListener(new Toaster(this)); - new Thread(new Runnable() { - @Override - public void run() { - try { - mVoicer.addVoicerListener(new VirtualAssistant(Slideshow.this)); - } catch (SpeakerException | InterruptedException ex) { - Log.e(getClass().getName(), null, ex); - } - } - }).start(); + try { + mVoicer.addVoicerListener(new VirtualAssistant(Slideshow.this)); + } catch (SpeakerException | InterruptedException ex) { + Log.e(getClass().getName(), null, ex); + } } // Class for implementing the "ViewHolder pattern"