Move voice add button to action bar

pull/253/head
Alex Baker 10 years ago
parent 1e708455e6
commit b9920fd85d

@ -58,6 +58,7 @@ import org.tasks.ui.NavigationDrawerFragment;
import javax.inject.Inject; import javax.inject.Inject;
import static com.todoroo.astrid.voice.RecognizerApi.RecognizerApiListener; import static com.todoroo.astrid.voice.RecognizerApi.RecognizerApiListener;
import static com.todoroo.astrid.voice.VoiceRecognizer.voiceInputAvailable;
import static org.tasks.ui.NavigationDrawerFragment.OnFilterItemClickedListener; import static org.tasks.ui.NavigationDrawerFragment.OnFilterItemClickedListener;
public class TaskListActivity extends AstridActivity implements OnPageChangeListener, OnFilterItemClickedListener, RecognizerApiListener { 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_clear_completed).setVisible(true);
menu.findItem(R.id.menu_sort).setVisible(false); 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 MenuItem item = menu.findItem(R.id.menu_search);
final SearchView actionView = (SearchView) MenuItemCompat.getActionView(item); final SearchView actionView = (SearchView) MenuItemCompat.getActionView(item);
actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -471,6 +473,9 @@ public class TaskListActivity extends AstridActivity implements OnPageChangeList
case R.id.menu_settings: case R.id.menu_settings:
tlf.showSettings(); tlf.showSettings();
return true; return true;
case R.id.menu_voice_add:
getTaskListFragment().startVoiceRecognition();
return true;
case R.id.menu_sort: case R.id.menu_sort:
AlertDialog dialog = SortSelectionActivity.createDialog( AlertDialog dialog = SortSelectionActivity.createDialog(
this, tlf.hasDraggableOption(), tlf, tlf.getSortFlags(), tlf.getSort()); this, tlf.hasDraggableOption(), tlf, tlf.getSortFlags(), tlf.getSort());
@ -550,7 +555,7 @@ public class TaskListActivity extends AstridActivity implements OnPageChangeList
if (tlf != null) { if (tlf != null) {
QuickAddBar quickAdd = tlf.quickAddBar; QuickAddBar quickAdd = tlf.quickAddBar;
if (quickAdd != null) { if (quickAdd != null) {
VoiceRecognizer vr = quickAdd.getVoiceRecognizer(); VoiceRecognizer vr = getTaskListFragment().getVoiceRecognizer();
if (vr != null) { if (vr != null) {
vr.cancel(); vr.cancel();
} }

@ -73,6 +73,7 @@ import com.todoroo.astrid.tags.TaskToTagMetadata;
import com.todoroo.astrid.timers.TimerPlugin; import com.todoroo.astrid.timers.TimerPlugin;
import com.todoroo.astrid.ui.QuickAddBar; import com.todoroo.astrid.ui.QuickAddBar;
import com.todoroo.astrid.utility.Flags; import com.todoroo.astrid.utility.Flags;
import com.todoroo.astrid.voice.VoiceRecognizer;
import com.todoroo.astrid.widget.TasksWidget; import com.todoroo.astrid.widget.TasksWidget;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -223,6 +224,10 @@ public class TaskListFragment extends InjectingListFragment implements OnSortSel
emptyView.setRefreshing(ongoing); emptyView.setRefreshing(ongoing);
} }
public VoiceRecognizer getVoiceRecognizer() {
return voiceRecognizer;
}
/** /**
* Container Activity must implement this interface and we ensure that it * Container Activity must implement this interface and we ensure that it
* does during the onAttach() callback * does during the onAttach() callback
@ -507,15 +512,28 @@ public class TaskListFragment extends InjectingListFragment implements OnSortSel
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
quickAddBar.setupRecognizerApi(); setupRecognizerApi();
} }
@Override @Override
public void onStop() { public void onStop() {
super.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 @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

@ -36,8 +36,6 @@ import com.todoroo.astrid.gcal.GCalHelper;
import com.todoroo.astrid.repeats.RepeatControlSet; import com.todoroo.astrid.repeats.RepeatControlSet;
import com.todoroo.astrid.service.TaskCreator; import com.todoroo.astrid.service.TaskCreator;
import com.todoroo.astrid.service.TaskService; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -59,7 +57,6 @@ public class QuickAddBar extends LinearLayout {
private static final Logger log = LoggerFactory.getLogger(QuickAddBar.class); private static final Logger log = LoggerFactory.getLogger(QuickAddBar.class);
private ImageButton voiceAddButton;
private ImageButton quickAddButton; private ImageButton quickAddButton;
private EditText quickAddBox; private EditText quickAddBox;
private LinearLayout quickAddControls; private LinearLayout quickAddControls;
@ -75,8 +72,6 @@ public class QuickAddBar extends LinearLayout {
@Inject ActivityPreferences preferences; @Inject ActivityPreferences preferences;
@Inject DateChangedAlerts dateChangedAlerts; @Inject DateChangedAlerts dateChangedAlerts;
private VoiceRecognizer voiceRecognizer;
private TaskListActivity activity; private TaskListActivity activity;
private TaskListFragment fragment; 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 // set listener for extended addbutton
quickAddButton.setOnLongClickListener(new OnLongClickListener() { quickAddButton.setOnLongClickListener(new OnLongClickListener() {
@Override @Override
@ -191,12 +175,6 @@ public class QuickAddBar extends LinearLayout {
} }
}); });
if (VoiceRecognizer.voiceInputAvailable(activity)) {
voiceAddButton.setVisibility(View.VISIBLE);
} else {
voiceAddButton.setVisibility(View.GONE);
}
setUpQuickAddControlSets(); setUpQuickAddControlSets();
} }
@ -321,34 +299,10 @@ public class QuickAddBar extends LinearLayout {
} }
public boolean onActivityResult(int requestCode, int resultCode, Intent data) { public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
// handle the result of voice recognition, put it into the textfield if (requestCode == TaskEditFragment.REQUEST_CODE_CONTACT) {
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) {
return true; return true;
} }
return false; 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();
}
} }

@ -6,6 +6,7 @@
package com.todoroo.astrid.voice; package com.todoroo.astrid.voice;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -56,22 +57,22 @@ public class VoiceRecognizer {
private static VoiceRecognizer instance = null; 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) { synchronized(VoiceRecognizer.class) {
if (instance == null) { if (instance == null) {
instance = new VoiceRecognizer(); instance = new VoiceRecognizer();
} }
} }
if (speechRecordingAvailable(context)) { if (speechRecordingAvailable(activity)) {
if (instance.recognizerApi != null) { if (instance.recognizerApi != null) {
instance.recognizerApi.destroy(); instance.recognizerApi.destroy();
} }
instance.recognizerApi = new RecognizerApi(context); instance.recognizerApi = new RecognizerApi(activity);
instance.recognizerApi.setListener(listener); instance.recognizerApi.setListener(listener);
} else { } else {
instance.voiceInputAssistant = new VoiceInputAssistant(voiceAddButton); instance.voiceInputAssistant = new VoiceInputAssistant(activity);
} }
return instance; return instance;
} }

@ -23,21 +23,6 @@
android:gravity="center_vertical" android:gravity="center_vertical"
android:orientation="horizontal"> android:orientation="horizontal">
<!-- Voice Add Button -->
<ImageButton android:id="@+id/voiceAddButton"
android:layout_width="39dip"
android:layout_height="39dip"
android:layout_gravity="center_vertical"
android:layout_marginLeft="4dip"
android:layout_marginRight="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:layout_weight="1"
android:visibility="gone"
android:background="@android:color/transparent"
android:src="?attr/ic_action_mic"
android:scaleType="center"/>
<!-- Quick Add Task --> <!-- Quick Add Task -->
<EditText android:id="@+id/quickAddText" <EditText android:id="@+id/quickAddText"
android:layout_width="wrap_content" android:layout_width="wrap_content"

@ -3,6 +3,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tasks="http://schemas.android.com/apk/res-auto"> xmlns:tasks="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_voice_add"
android:title="@string/EPr_voiceInputEnabled_title"
android:icon="?attr/ic_action_mic"
tasks:showAsAction="ifRoom"/>
<item <item
android:id="@+id/menu_search" android:id="@+id/menu_search"
android:title="@string/TLA_menu_search" android:title="@string/TLA_menu_search"

Loading…
Cancel
Save