diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java index 270a66e8c..46f14be0c 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java @@ -58,6 +58,7 @@ import org.tasks.ui.NavigationDrawerFragment; import javax.inject.Inject; import static com.todoroo.astrid.voice.RecognizerApi.RecognizerApiListener; +import static com.todoroo.astrid.voice.VoiceRecognizer.voiceInputAvailable; import static org.tasks.ui.NavigationDrawerFragment.OnFilterItemClickedListener; public class TaskListActivity extends AstridActivity implements OnPageChangeListener, OnFilterItemClickedListener, RecognizerApiListener { @@ -158,6 +159,7 @@ public class TaskListActivity extends AstridActivity implements OnPageChangeList menu.findItem(R.id.menu_clear_completed).setVisible(true); menu.findItem(R.id.menu_sort).setVisible(false); } + menu.findItem(R.id.menu_voice_add).setVisible(voiceInputAvailable(this)); final MenuItem item = menu.findItem(R.id.menu_search); final SearchView actionView = (SearchView) MenuItemCompat.getActionView(item); actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @@ -471,6 +473,9 @@ public class TaskListActivity extends AstridActivity implements OnPageChangeList case R.id.menu_settings: tlf.showSettings(); return true; + case R.id.menu_voice_add: + getTaskListFragment().startVoiceRecognition(); + return true; case R.id.menu_sort: AlertDialog dialog = SortSelectionActivity.createDialog( this, tlf.hasDraggableOption(), tlf, tlf.getSortFlags(), tlf.getSort()); @@ -550,7 +555,7 @@ public class TaskListActivity extends AstridActivity implements OnPageChangeList if (tlf != null) { QuickAddBar quickAdd = tlf.quickAddBar; if (quickAdd != null) { - VoiceRecognizer vr = quickAdd.getVoiceRecognizer(); + VoiceRecognizer vr = getTaskListFragment().getVoiceRecognizer(); if (vr != null) { vr.cancel(); } diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java index 17db16bfa..4d8a21c04 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java @@ -73,6 +73,7 @@ import com.todoroo.astrid.tags.TaskToTagMetadata; import com.todoroo.astrid.timers.TimerPlugin; import com.todoroo.astrid.ui.QuickAddBar; import com.todoroo.astrid.utility.Flags; +import com.todoroo.astrid.voice.VoiceRecognizer; import com.todoroo.astrid.widget.TasksWidget; import org.slf4j.Logger; @@ -223,6 +224,10 @@ public class TaskListFragment extends InjectingListFragment implements OnSortSel emptyView.setRefreshing(ongoing); } + public VoiceRecognizer getVoiceRecognizer() { + return voiceRecognizer; + } + /** * Container Activity must implement this interface and we ensure that it * does during the onAttach() callback @@ -507,15 +512,28 @@ public class TaskListFragment extends InjectingListFragment implements OnSortSel @Override public void onStart() { super.onStart(); - quickAddBar.setupRecognizerApi(); + setupRecognizerApi(); } @Override public void onStop() { super.onStop(); - quickAddBar.destroyRecognizerApi(); + destroyRecognizerApi(); + } + + private VoiceRecognizer voiceRecognizer; + + public void setupRecognizerApi() { + voiceRecognizer = VoiceRecognizer.instantiateVoiceRecognizer(getActivity(), (TaskListActivity) getActivity()); + } + + public void destroyRecognizerApi() { + voiceRecognizer.destroyRecognizerApi(); } + public void startVoiceRecognition() { + voiceRecognizer.startVoiceRecognition(getActivity(), this); + } @Override public void onResume() { super.onResume(); diff --git a/astrid/src/main/java/com/todoroo/astrid/ui/QuickAddBar.java b/astrid/src/main/java/com/todoroo/astrid/ui/QuickAddBar.java index 51a715a05..1e7bf4670 100644 --- a/astrid/src/main/java/com/todoroo/astrid/ui/QuickAddBar.java +++ b/astrid/src/main/java/com/todoroo/astrid/ui/QuickAddBar.java @@ -36,8 +36,6 @@ import com.todoroo.astrid.gcal.GCalHelper; import com.todoroo.astrid.repeats.RepeatControlSet; import com.todoroo.astrid.service.TaskCreator; import com.todoroo.astrid.service.TaskService; -import com.todoroo.astrid.utility.Flags; -import com.todoroo.astrid.voice.VoiceRecognizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +57,6 @@ public class QuickAddBar extends LinearLayout { private static final Logger log = LoggerFactory.getLogger(QuickAddBar.class); - private ImageButton voiceAddButton; private ImageButton quickAddButton; private EditText quickAddBox; private LinearLayout quickAddControls; @@ -75,8 +72,6 @@ public class QuickAddBar extends LinearLayout { @Inject ActivityPreferences preferences; @Inject DateChangedAlerts dateChangedAlerts; - private VoiceRecognizer voiceRecognizer; - private TaskListActivity activity; private TaskListFragment fragment; @@ -165,17 +160,6 @@ public class QuickAddBar extends LinearLayout { } }); - // prepare and set listener for voice add button - voiceAddButton = (ImageButton) findViewById( - R.id.voiceAddButton); - - voiceAddButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - startVoiceRecognition(); - } - }); - // set listener for extended addbutton quickAddButton.setOnLongClickListener(new OnLongClickListener() { @Override @@ -191,12 +175,6 @@ public class QuickAddBar extends LinearLayout { } }); - if (VoiceRecognizer.voiceInputAvailable(activity)) { - voiceAddButton.setVisibility(View.VISIBLE); - } else { - voiceAddButton.setVisibility(View.GONE); - } - setUpQuickAddControlSets(); } @@ -321,34 +299,10 @@ public class QuickAddBar extends LinearLayout { } public boolean onActivityResult(int requestCode, int resultCode, Intent data) { - // handle the result of voice recognition, put it into the textfield - if (voiceRecognizer.handleActivityResult(requestCode, resultCode, data, quickAddBox)) { - // if user wants, create the task directly (with defaultvalues) - // after saying it - quickAddTask(quickAddBox.getText().toString(), true); - - // the rest of onActivityResult is totally unrelated to - // voicerecognition, so bail out - return true; - } else if (requestCode == TaskEditFragment.REQUEST_CODE_CONTACT) { + if (requestCode == TaskEditFragment.REQUEST_CODE_CONTACT) { return true; } return false; } - - public VoiceRecognizer getVoiceRecognizer() { - return voiceRecognizer; - } - public void startVoiceRecognition() { - voiceRecognizer.startVoiceRecognition(activity, fragment); - } - - public void setupRecognizerApi() { - voiceRecognizer = VoiceRecognizer.instantiateVoiceRecognizer(activity, activity, voiceAddButton); - } - - public void destroyRecognizerApi() { - voiceRecognizer.destroyRecognizerApi(); - } } diff --git a/astrid/src/main/java/com/todoroo/astrid/voice/VoiceRecognizer.java b/astrid/src/main/java/com/todoroo/astrid/voice/VoiceRecognizer.java index 87b1479ca..5849d124c 100644 --- a/astrid/src/main/java/com/todoroo/astrid/voice/VoiceRecognizer.java +++ b/astrid/src/main/java/com/todoroo/astrid/voice/VoiceRecognizer.java @@ -6,6 +6,7 @@ package com.todoroo.astrid.voice; import android.annotation.TargetApi; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -56,22 +57,22 @@ public class VoiceRecognizer { private static VoiceRecognizer instance = null; - public static VoiceRecognizer instantiateVoiceRecognizer(Context context, RecognizerApiListener listener, ImageButton voiceAddButton) { + public static VoiceRecognizer instantiateVoiceRecognizer(Activity activity, RecognizerApiListener listener) { synchronized(VoiceRecognizer.class) { if (instance == null) { instance = new VoiceRecognizer(); } } - if (speechRecordingAvailable(context)) { + if (speechRecordingAvailable(activity)) { if (instance.recognizerApi != null) { instance.recognizerApi.destroy(); } - instance.recognizerApi = new RecognizerApi(context); + instance.recognizerApi = new RecognizerApi(activity); instance.recognizerApi.setListener(listener); } else { - instance.voiceInputAssistant = new VoiceInputAssistant(voiceAddButton); + instance.voiceInputAssistant = new VoiceInputAssistant(activity); } return instance; } diff --git a/astrid/src/main/res/layout/quick_add_bar.xml b/astrid/src/main/res/layout/quick_add_bar.xml index 533d236b7..dcf84d7a2 100644 --- a/astrid/src/main/res/layout/quick_add_bar.xml +++ b/astrid/src/main/res/layout/quick_add_bar.xml @@ -23,21 +23,6 @@ android:gravity="center_vertical" android:orientation="horizontal"> - - - +