Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide voice input if service not available #5904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/CommonsApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.Intent
import android.database.sqlite.SQLiteException
import android.os.Build
import android.os.Process
import android.speech.SpeechRecognizer
import android.util.Log
import androidx.multidex.MultiDexApplication
import com.facebook.drawee.backends.pipeline.Fresco
Expand Down Expand Up @@ -91,6 +92,13 @@ class CommonsApplication : MultiDexApplication() {
@Inject
lateinit var contributionDao: ContributionDao

/**
* Flag to check if speech recognition is available.
*/
val isVoiceRecognitionAvailable by lazy {
SpeechRecognizer.isRecognitionAvailable(this)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're depending on this call to check if service is available or not. Since, I don't have access to the device, do you mind adding a print statement and see if it returns true, then likely issue is with API.

}

/**
* Used to declare and initialize various components and dependencies
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class DescriptionEditActivity :
savedLanguageValue,
descriptionAndCaptions,
recentLanguagesDao,
voiceInputResultLauncher
voiceInputResultLauncher,
CommonsApplication.instance.isVoiceRecognitionAvailable
)
uploadMediaDetailAdapter.setCallback { titleStringID: Int, messageStringId: Int ->
showInfoAlert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,35 @@ public class UploadMediaDetailAdapter extends
private Activity activity;
private final ActivityResultLauncher<Intent> voiceInputResultLauncher;
private SelectedVoiceIcon selectedVoiceIcon;
boolean isVoiceRecognitionAvailable;

private RowItemDescriptionBinding binding;

public UploadMediaDetailAdapter(Fragment fragment, String savedLanguageValue,
RecentLanguagesDao recentLanguagesDao, ActivityResultLauncher<Intent> voiceInputResultLauncher) {
RecentLanguagesDao recentLanguagesDao,
ActivityResultLauncher<Intent> voiceInputResultLauncher,
boolean isVoiceRecognitionAvailable) {
uploadMediaDetails = new ArrayList<>();
selectedLanguages = new HashMap<>();
this.savedLanguageValue = savedLanguageValue;
this.recentLanguagesDao = recentLanguagesDao;
this.fragment = fragment;
this.voiceInputResultLauncher = voiceInputResultLauncher;
this.isVoiceRecognitionAvailable = isVoiceRecognitionAvailable;
}

public UploadMediaDetailAdapter(Activity activity, final String savedLanguageValue,
List<UploadMediaDetail> uploadMediaDetails, RecentLanguagesDao recentLanguagesDao, ActivityResultLauncher<Intent> voiceInputResultLauncher) {
List<UploadMediaDetail> uploadMediaDetails,
RecentLanguagesDao recentLanguagesDao,
ActivityResultLauncher<Intent> voiceInputResultLauncher,
boolean isVoiceRecognitionAvailable) {
this.uploadMediaDetails = uploadMediaDetails;
selectedLanguages = new HashMap<>();
this.savedLanguageValue = savedLanguageValue;
this.recentLanguagesDao = recentLanguagesDao;
this.activity = activity;
this.voiceInputResultLauncher = voiceInputResultLauncher;
this.isVoiceRecognitionAvailable = isVoiceRecognitionAvailable;
}

public void setCallback(Callback callback) {
Expand Down Expand Up @@ -273,13 +281,15 @@ public void bind(int position) {
selectedVoiceIcon = SelectedVoiceIcon.CAPTION;
startSpeechInput(descriptionLanguages.getText().toString());
});
captionInputLayout.setEndIconVisible(isVoiceRecognitionAvailable);
descInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
descInputLayout.setEndIconDrawable(R.drawable.baseline_keyboard_voice);
descInputLayout.setEndIconOnClickListener(v -> {
currentPosition = position;
selectedVoiceIcon = SelectedVoiceIcon.DESCRIPTION;
startSpeechInput(descriptionLanguages.getText().toString());
});
descInputLayout.setEndIconVisible(isVoiceRecognitionAvailable);

if (position == 0) {
removeButton.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import fr.free.nrw.commons.CameraPosition;
import fr.free.nrw.commons.locationpicker.LocationPicker;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.MainActivity;
import fr.free.nrw.commons.databinding.FragmentUploadMediaDetailFragmentBinding;
Expand Down Expand Up @@ -295,7 +296,8 @@ private void initPresenter() {
*/
private void initRecyclerView() {
uploadMediaDetailAdapter = new UploadMediaDetailAdapter(this,
defaultKvStore.getString(Prefs.DESCRIPTION_LANGUAGE, ""), recentLanguagesDao, voiceInputResultLauncher);
defaultKvStore.getString(Prefs.DESCRIPTION_LANGUAGE, ""), recentLanguagesDao, voiceInputResultLauncher,
CommonsApplication.getInstance().isVoiceRecognitionAvailable());
uploadMediaDetailAdapter.setCallback(this::showInfoAlert);
uploadMediaDetailAdapter.setEventListener(this);
binding.rvDescriptions.setLayoutManager(new LinearLayoutManager(getContext()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class UploadMediaDetailAdapterUnitTest {
uploadMediaDetails = mutableListOf(uploadMediaDetail, uploadMediaDetail)
activity = Robolectric.buildActivity(UploadActivity::class.java).get()
fragment = mock(UploadMediaDetailFragment::class.java)
adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao, mockResultLauncher)
adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao, mockResultLauncher, true)
context = ApplicationProvider.getApplicationContext()
Whitebox.setInternalState(adapter, "uploadMediaDetails", uploadMediaDetails)
Whitebox.setInternalState(adapter, "eventListener", eventListener)
Expand Down
Loading