Skip to content

Commit

Permalink
hide voice input if service not available
Browse files Browse the repository at this point in the history
Signed-off-by: parneet-guraya <[email protected]>
  • Loading branch information
parneet-guraya committed Oct 27, 2024
1 parent 522f1fe commit b6641e7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
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)
}

/**
* 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 @@ -60,27 +60,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 @@ -274,13 +282,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.exifinterface.media.ExifInterface;
import androidx.recyclerview.widget.LinearLayoutManager;
import fr.free.nrw.commons.CameraPosition;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.LocationPicker.LocationPicker;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.MainActivity;
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

0 comments on commit b6641e7

Please sign in to comment.