Remove voice input assistant

pull/574/head
Alex Baker 7 years ago
parent 559619eadd
commit 5f7e6c109c

@ -11,6 +11,7 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
@ -26,7 +27,6 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
@ -41,7 +41,6 @@ import com.todoroo.astrid.service.TaskCreator;
import com.todoroo.astrid.service.TaskDeleter;
import com.todoroo.astrid.service.TaskDuplicator;
import com.todoroo.astrid.timers.TimerPlugin;
import com.todoroo.astrid.voice.VoiceInputAssistant;
import org.tasks.LocalBroadcastManager;
import org.tasks.R;
@ -55,6 +54,7 @@ import org.tasks.gtasks.SyncAdapterHelper;
import org.tasks.injection.ForActivity;
import org.tasks.injection.FragmentComponent;
import org.tasks.injection.InjectingFragment;
import org.tasks.preferences.Device;
import org.tasks.preferences.Preferences;
import org.tasks.tasklist.TaskListRecyclerAdapter;
import org.tasks.tasklist.ViewHolderFactory;
@ -71,7 +71,6 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import static android.support.v4.content.ContextCompat.getColor;
import static com.todoroo.astrid.voice.VoiceInputAssistant.voiceInputAvailable;
/**
* Primary activity for the Bente application. Shows a list of upcoming tasks
@ -105,13 +104,13 @@ public class TaskListFragment extends InjectingFragment implements
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject DialogBuilder dialogBuilder;
@Inject CheckBoxes checkBoxes;
@Inject VoiceInputAssistant voiceInputAssistant;
@Inject TaskCreator taskCreator;
@Inject protected TaskListDataProvider taskListDataProvider;
@Inject TimerPlugin timerPlugin;
@Inject ViewHolderFactory viewHolderFactory;
@Inject protected Tracker tracker;
@Inject LocalBroadcastManager localBroadcastManager;
@Inject Device device;
@BindView(R.id.swipe_layout) SwipeRefreshLayout swipeRefreshLayout;
@BindView(R.id.swipe_layout_empty) SwipeRefreshLayout emptyRefreshLayout;
@ -246,7 +245,7 @@ public class TaskListFragment extends InjectingFragment implements
hidden.setEnabled(false);
}
menu.findItem(R.id.menu_voice_add).setVisible(voiceInputAvailable(getActivity()));
menu.findItem(R.id.menu_voice_add).setVisible(device.voiceInputAvailable());
final MenuItem item = menu.findItem(R.id.menu_search);
final SearchView actionView = (SearchView) MenuItemCompat.getActionView(item);
actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -276,7 +275,11 @@ public class TaskListFragment extends InjectingFragment implements
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_voice_add:
voiceInputAssistant.startVoiceRecognitionActivity(R.string.voice_create_prompt);
Intent recognition = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognition.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognition.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
recognition.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.voice_create_prompt));
startActivityForResult(recognition, TaskListFragment.VOICE_RECOGNITION_REQUEST_CODE);
return true;
case R.id.menu_sort:
SortDialog.newSortDialog(hasDraggableOption()).show(getChildFragmentManager(), FRAG_TAG_SORT_DIALOG);
@ -502,14 +505,18 @@ public class TaskListFragment extends InjectingFragment implements
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Callback<String> quickAddTask = title -> {
Task task = addTask(title);
List<String> match = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (match != null && match.size() > 0 && match.get(0).length() > 0) {
String recognizedSpeech = match.get(0);
recognizedSpeech = recognizedSpeech.substring(0, 1).toUpperCase() +
recognizedSpeech.substring(1).toLowerCase();
Task task = addTask(recognizedSpeech);
taskCreator.addToCalendar(task);
onTaskListItemClicked(task.getId());
loadTaskListContent();
onTaskCreated(task.getUUID());
};
voiceInputAssistant.handleActivityResult(data, quickAddTask);
}
}
} else if (requestCode == REQUEST_EDIT_FILTER) {
if (resultCode == Activity.RESULT_OK) {

@ -1,113 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.voice;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import com.todoroo.andlib.data.Callback;
import com.todoroo.astrid.activity.TaskListFragment;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* This class handles taking voice-input and appends the text to the registered EditText-instance.
* You can have multiple VoiceInputAssistants per Fragment, just use the additional constructor
* to specify unique requestCodes for the RecognizerIntent (e.g. VoiceInputAssistant.VOICE_RECOGNITION_REQUEST_CODE+i).
* If you have only one VoiceInputAssitant on an Fragment, just use the normal constructor.
* <p>
* You can query voiceinput-capabilities by calling isVoiceInputAvailable() for external checking,
* but the visibility for the microphone-button specified by the constructor is handled in configureMicrophoneButton(int).
*
* @author Arne Jans
*/
@Singleton
public class VoiceInputAssistant {
/** requestcode for activityresult from voicerecognizer-intent */
/**
* Call this to see if your phone supports voiceinput in its current configuration.
* If this method returns false, it could also mean that Google Voicesearch is simply
* not installed.
* If this method returns true, internal use of it enables the registered microphone-button.
*
* @return whether this phone supports voiceinput
*/
public static boolean voiceInputAvailable(Context context) {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
return (activities.size() != 0);
}
/**
* This requestcode is used to differentiate between multiple microphone-buttons on a single fragment.
* Use the mightier constructor to specify your own requestCode in this case for every additional use on an fragment.
* If you only use one microphone-button on an fragment, you can leave it to its default, VOICE_RECOGNITION_REQUEST_CODE.
*/
private final Fragment fragment;
/**
* This constructor can be called from a widget with a voice-button calling a dummy-activity.
*
*/
@Inject
public VoiceInputAssistant(Fragment fragment) {
this.fragment = fragment;
}
/**
* Fire an intent to start the speech recognition activity.
* This is fired by the listener on the microphone-button.
*
* @param prompt Specify the R.string.string_id resource for the prompt-text during voice-recognition here
*/
public void startVoiceRecognitionActivity(int prompt) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, fragment.getString(prompt));
fragment.startActivityForResult(intent, TaskListFragment.VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* This callback-method has to be called from Fragment.onActivityResult within your fragment
* with parameters directly on passthru.<br>
* You can check in your fragment if it was really a RecognizerIntent that was handled here,
* if so, this method returns true. In this case, you should call super.onActivityResult in your
* fragment.onActivityResult.
* <p>
* If this method returns false, then it wasnt a request with a RecognizerIntent, so you can handle
* these other requests as you need.
*
*/
public void handleActivityResult(Intent data, Callback<String> onVoiceRecognition) {
// handle the result of voice recognition, put it into the textfield
// this was handled here, even if voicerecognition fails for any reason
// so your program flow wont get chaotic if you dont explicitly state
// your own requestCodes.
// Fill the quickAddBox-view with the string the recognizer thought it could have heard
ArrayList<String> match = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
// make sure we only do this if there is SomeThing (tm) returned
if (match != null && match.size() > 0 && match.get(0).length() > 0) {
String recognizedSpeech = match.get(0);
recognizedSpeech = recognizedSpeech.substring(0, 1).toUpperCase() +
recognizedSpeech.substring(1).toLowerCase();
onVoiceRecognition.apply(recognizedSpeech);
}
}
}

@ -23,11 +23,8 @@ import org.tasks.ui.GoogleTaskListFragment;
import org.tasks.ui.NavigationDrawerFragment;
import org.tasks.ui.PriorityControlSet;
import javax.inject.Singleton;
import dagger.Subcomponent;
@Singleton
@Subcomponent(modules = FragmentModule.class)
public interface FragmentComponent {

@ -2,8 +2,11 @@ package org.tasks.preferences;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import com.google.common.base.Joiner;
@ -12,6 +15,8 @@ import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.locale.Locale;
import java.util.List;
import javax.inject.Inject;
import timber.log.Timber;
@ -43,6 +48,13 @@ public class Device {
return context.getResources().getBoolean(R.bool.location_enabled);
}
public boolean voiceInputAvailable() {
PackageManager pm = context.getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
return (activities.size() != 0);
}
public String getDebugInfo() {
try {
java.util.Locale appLocale = locale.getLocale();

Loading…
Cancel
Save