diff --git a/android/app/src/main/java/app/organicmaps/MwmActivity.java b/android/app/src/main/java/app/organicmaps/MwmActivity.java index bb5bbdea18..b5222b23ef 100644 --- a/android/app/src/main/java/app/organicmaps/MwmActivity.java +++ b/android/app/src/main/java/app/organicmaps/MwmActivity.java @@ -157,7 +157,11 @@ public class MwmActivity extends BaseMwmFragmentActivity EditorHostFragment.class.getName(), ReportFragment.class.getName() }; - public static final int REQ_CODE_DRIVING_OPTIONS = 6; + public final ActivityResultLauncher startDrivingOptionsForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> + { + if( activityResult.getResultCode() == Activity.RESULT_OK) + rebuildLastRoute(); + }); private static final String MAIN_MENU_ID = "MAIN_MENU_BOTTOM_SHEET"; private static final String LAYERS_MENU_ID = "LAYERS_MENU_BOTTOM_SHEET"; @@ -614,7 +618,7 @@ public class MwmActivity extends BaseMwmFragmentActivity if (!mIsTabletLayout) { - mRoutingPlanInplaceController = new RoutingPlanInplaceController(this, this, this); + mRoutingPlanInplaceController = new RoutingPlanInplaceController(this, startDrivingOptionsForResult, this, this); removeCurrentFragment(false); } @@ -1018,18 +1022,6 @@ public class MwmActivity extends BaseMwmFragmentActivity mPowerSaveDisclaimerShown = savedInstanceState.getBoolean(POWER_SAVE_DISCLAIMER_SHOWN, false); } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) - { - super.onActivityResult(requestCode, resultCode, data); - - if (resultCode != Activity.RESULT_OK) - return; - - if (requestCode == REQ_CODE_DRIVING_OPTIONS) - rebuildLastRoute(); - } - private void rebuildLastRoute() { RoutingController.get().attach(this); @@ -1693,7 +1685,7 @@ public class MwmActivity extends BaseMwmFragmentActivity mAlertDialog = new MaterialAlertDialogBuilder(this, R.style.MwmTheme_AlertDialog) .setTitle(R.string.unable_to_calc_alert_title) .setMessage(R.string.unable_to_calc_alert_subtitle) - .setPositiveButton(R.string.settings, (dialog, which) -> DrivingOptionsActivity.start(this)) + .setPositiveButton(R.string.settings, (dialog, which) -> DrivingOptionsActivity.start(this, startDrivingOptionsForResult)) .setNegativeButton(R.string.cancel, null) .setOnDismissListener(dialog -> mAlertDialog = null) .show(); diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesFragment.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesFragment.java index 2b6a487b61..cc31cfb4cf 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesFragment.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategoriesFragment.java @@ -15,6 +15,7 @@ import android.view.View; import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.CallSuper; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; @@ -57,9 +58,6 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment startBookmarkListForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + if( activityResult.getResultCode() == Activity.RESULT_OK) + onDeleteActionSelected(getSelectedCategory()); + }); + + private final ActivityResultLauncher startImportDirectoryForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> + { + if( activityResult.getResultCode() == Activity.RESULT_OK) + onImportDirectoryResult(activityResult.getData()); + }); + + private final ActivityResultLauncher startBookmarkSettingsForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + // not handled at the moment + }); + + @Override @LayoutRes protected int getLayoutRes() @@ -259,7 +273,7 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment onDeleteActionSelected(getSelectedCategory()); - case REQ_CODE_IMPORT_DIRECTORY -> - { - if (data == null) - throw new AssertionError("Data is null"); + if (data == null) + throw new AssertionError("Data is null"); - final Context context = requireActivity(); - final Uri rootUri = data.getData(); - final ProgressDialog dialog = new ProgressDialog(context, R.style.MwmTheme_ProgressDialog); - dialog.setMessage(getString(R.string.wait_several_minutes)); - dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); - dialog.setIndeterminate(true); - dialog.setCancelable(false); - dialog.show(); - Logger.d(TAG, "Importing bookmarks from " + rootUri); - MwmApplication app = MwmApplication.from(context); - final File tempDir = new File(StorageUtils.getTempPath(app)); - final ContentResolver resolver = context.getContentResolver(); - ThreadPool.getStorage().execute(() -> { - AtomicInteger found = new AtomicInteger(0); - StorageUtils.listContentProviderFilesRecursively( + final Context context = requireActivity(); + final Uri rootUri = data.getData(); + final ProgressDialog dialog = new ProgressDialog(context, R.style.MwmTheme_ProgressDialog); + dialog.setMessage(getString(R.string.wait_several_minutes)); + dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + dialog.setIndeterminate(true); + dialog.setCancelable(false); + dialog.show(); + Logger.d(TAG, "Importing bookmarks from " + rootUri); + MwmApplication app = MwmApplication.from(context); + final File tempDir = new File(StorageUtils.getTempPath(app)); + final ContentResolver resolver = context.getContentResolver(); + ThreadPool.getStorage().execute(() -> { + AtomicInteger found = new AtomicInteger(0); + StorageUtils.listContentProviderFilesRecursively( resolver, rootUri, uri -> { if (BookmarkManager.INSTANCE.importBookmarksFile(resolver, uri, tempDir)) found.incrementAndGet(); }); - UiThread.run(() -> { - if (dialog.isShowing()) - dialog.dismiss(); - int found_val = found.get(); - String message = context.getResources().getQuantityString( + UiThread.run(() -> { + if (dialog.isShowing()) + dialog.dismiss(); + int found_val = found.get(); + String message = context.getResources().getQuantityString( R.plurals.bookmarks_detect_message, found_val, found_val); - Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show(); - }); - }); - } - default -> throw new AssertionError("Invalid requestCode: " + requestCode); - } + Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show(); + }); + }); } @Override diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategorySettingsActivity.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategorySettingsActivity.java index ad8df7db69..e96c65bdc0 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategorySettingsActivity.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkCategorySettingsActivity.java @@ -2,6 +2,7 @@ package app.organicmaps.bookmarks; import android.content.Intent; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; @@ -11,7 +12,6 @@ import app.organicmaps.bookmarks.data.BookmarkCategory; public class BookmarkCategorySettingsActivity extends BaseMwmFragmentActivity { - public static final int REQUEST_CODE = 107; public static final String EXTRA_BOOKMARK_CATEGORY = "bookmark_category"; @Override @@ -32,11 +32,11 @@ public class BookmarkCategorySettingsActivity extends BaseMwmFragmentActivity return BookmarkCategorySettingsFragment.class; } - public static void startForResult(@NonNull Fragment fragment, + public static void startForResult(@NonNull Fragment fragment, ActivityResultLauncher startBookmarkSettingsForResult, @NonNull BookmarkCategory category) { android.content.Intent intent = new Intent(fragment.requireActivity(), BookmarkCategorySettingsActivity.class) .putExtra(EXTRA_BOOKMARK_CATEGORY, category); - fragment.startActivityForResult(intent, REQUEST_CODE); + startBookmarkSettingsForResult.launch(intent); } } diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java index 98b4758139..353df29639 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarkListActivity.java @@ -3,6 +3,7 @@ package app.organicmaps.bookmarks; import android.content.Intent; import android.os.Bundle; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.CallSuper; import androidx.annotation.NonNull; import androidx.annotation.StyleRes; @@ -57,11 +58,11 @@ public class BookmarkListActivity extends BaseToolbarActivity return R.layout.bookmarks_activity; } - static void startForResult(@NonNull Fragment fragment, @NonNull BookmarkCategory category) + static void startForResult(@NonNull Fragment fragment, ActivityResultLauncher startBookmarkListForResult, @NonNull BookmarkCategory category) { Bundle args = new Bundle(); Intent intent = new Intent(fragment.requireActivity(), BookmarkListActivity.class); intent.putExtra(BookmarksListFragment.EXTRA_CATEGORY, category); - fragment.startActivityForResult(intent, BookmarkCategoriesFragment.REQ_CODE_DELETE_CATEGORY); + startBookmarkListForResult.launch(intent); } } diff --git a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarksListFragment.java b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarksListFragment.java index b7aa7a2fc2..7a59c0a7fe 100644 --- a/android/app/src/main/java/app/organicmaps/bookmarks/BookmarksListFragment.java +++ b/android/app/src/main/java/app/organicmaps/bookmarks/BookmarksListFragment.java @@ -14,6 +14,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.CallSuper; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -74,6 +75,15 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment shareLauncher; + private final ActivityResultLauncher startBookmarkListForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + System.out.println("resultCode: " + activityResult.getResultCode()); + handleActivityResult(); + }); + + private final ActivityResultLauncher startBookmarkSettingsForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + System.out.println("resultCode: " + activityResult.getResultCode()); + handleActivityResult(); + }); @SuppressWarnings("NotNullFieldNotInitialized") @NonNull @@ -140,7 +150,9 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment BookmarkListActivity.startForResult(this, item)); + adapter.setOnClickListener((v, item) -> { + BookmarkListActivity.startForResult(this, startBookmarkListForResult, item); + }); return adapter; } @@ -756,7 +768,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment startVoiceRecognitionForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> mToolbarController.onVoiceRecognitionResult(activityResult)); + private final RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) @@ -210,13 +214,6 @@ public class DownloaderFragment extends BaseMwmRecyclerFragment startDrivingOptionsForResult, @NonNull RoutingPlanInplaceController.RoutingPlanListener routingPlanListener, @Nullable RoutingBottomMenuListener listener) { @@ -103,7 +106,7 @@ public class RoutingPlanController extends ToolbarController View btn = mDrivingOptionsBtnContainer.findViewById(R.id.driving_options_btn); mDrivingOptionsImage = mFrame.findViewById(R.id.driving_options_btn_img); - btn.setOnClickListener(v -> DrivingOptionsActivity.start(requireActivity())); + btn.setOnClickListener(v -> DrivingOptionsActivity.start(requireActivity(), startDrivingOptionsForResult)); mDriverOptionsLayoutListener = new SelfTerminatedDrivingOptionsLayoutListener(); mAnimToggle = MwmApplication.from(activity.getApplicationContext()) .getResources().getInteger(R.integer.anim_default); diff --git a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanFragment.java b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanFragment.java index 23987e71d2..4a54c0f3c1 100644 --- a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanFragment.java +++ b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanFragment.java @@ -7,9 +7,9 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.fragment.app.FragmentActivity; import app.organicmaps.Framework; +import app.organicmaps.MwmActivity; import app.organicmaps.R; import app.organicmaps.base.BaseMwmFragment; @@ -21,15 +21,9 @@ public class RoutingPlanFragment extends BaseMwmFragment @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - final FragmentActivity activity = requireActivity(); + final MwmActivity activity = (MwmActivity) requireActivity(); View res = inflater.inflate(R.layout.fragment_routing, container, false); - RoutingBottomMenuListener listener = null; - if (activity instanceof RoutingBottomMenuListener) - listener = (RoutingBottomMenuListener) activity; - - RoutingPlanInplaceController.RoutingPlanListener planListener = - (RoutingPlanInplaceController.RoutingPlanListener) activity; - mPlanController = new RoutingPlanController(res, activity, planListener, listener); + mPlanController = new RoutingPlanController(res, activity, activity.startDrivingOptionsForResult, activity, activity); return res; } diff --git a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanInplaceController.java b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanInplaceController.java index a6f8db5c81..d71c973329 100644 --- a/android/app/src/main/java/app/organicmaps/routing/RoutingPlanInplaceController.java +++ b/android/app/src/main/java/app/organicmaps/routing/RoutingPlanInplaceController.java @@ -2,8 +2,10 @@ package app.organicmaps.routing; import android.animation.Animator; import android.animation.ValueAnimator; +import android.content.Intent; import android.os.Bundle; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -20,10 +22,11 @@ public class RoutingPlanInplaceController extends RoutingPlanController private Animator mAnimator; public RoutingPlanInplaceController(@NonNull MwmActivity activity, + ActivityResultLauncher startDrivingOptionsForResult, @NonNull RoutingPlanListener routingPlanListener, @NonNull RoutingBottomMenuListener listener) { - super(activity.findViewById(R.id.routing_plan_frame), activity, routingPlanListener, listener); + super(activity.findViewById(R.id.routing_plan_frame), activity, startDrivingOptionsForResult, routingPlanListener, listener); mRoutingPlanListener = routingPlanListener; } diff --git a/android/app/src/main/java/app/organicmaps/search/SearchFragment.java b/android/app/src/main/java/app/organicmaps/search/SearchFragment.java index 054560b4cc..33c7ccc802 100644 --- a/android/app/src/main/java/app/organicmaps/search/SearchFragment.java +++ b/android/app/src/main/java/app/organicmaps/search/SearchFragment.java @@ -11,6 +11,8 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.CallSuper; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -115,10 +117,9 @@ public class SearchFragment extends BaseMwmFragment } @Override - @SuppressWarnings("deprecation") // https://github.com/organicmaps/organicmaps/issues/3630 - protected void startVoiceRecognition(Intent intent, int code) + protected void startVoiceRecognition(Intent intent) { - startActivityForResult(intent, code); + startVoiceRecognitionForResult.launch(intent); } @Override @@ -170,6 +171,11 @@ public class SearchFragment extends BaseMwmFragment private String mInitialLocale; private boolean mInitialSearchOnMap = false; + private final ActivityResultLauncher startVoiceRecognitionForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> + { + mToolbarController.onVoiceRecognitionResult(activityResult); + }); + private final LocationListener mLocationListener = new LocationListener() { @Override @@ -529,14 +535,6 @@ public class SearchFragment extends BaseMwmFragment mToolbarController.showProgress(true); } - @Override - @SuppressWarnings("deprecation") // https://github.com/organicmaps/organicmaps/issues/3630 - public void onActivityResult(int requestCode, int resultCode, Intent data) - { - super.onActivityResult(requestCode, resultCode, data); - mToolbarController.onActivityResult(requestCode, resultCode, data); - } - @Override public boolean onBackPressed() { diff --git a/android/app/src/main/java/app/organicmaps/settings/DrivingOptionsActivity.java b/android/app/src/main/java/app/organicmaps/settings/DrivingOptionsActivity.java index 94b7cd1a94..b98fe46f3f 100644 --- a/android/app/src/main/java/app/organicmaps/settings/DrivingOptionsActivity.java +++ b/android/app/src/main/java/app/organicmaps/settings/DrivingOptionsActivity.java @@ -3,10 +3,9 @@ package app.organicmaps.settings; import android.app.Activity; import android.content.Intent; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; - -import app.organicmaps.MwmActivity; import app.organicmaps.base.BaseMwmFragmentActivity; public class DrivingOptionsActivity extends BaseMwmFragmentActivity @@ -17,9 +16,9 @@ public class DrivingOptionsActivity extends BaseMwmFragmentActivity return DrivingOptionsFragment.class; } - public static void start(@NonNull Activity activity) + public static void start(@NonNull Activity activity, ActivityResultLauncher startDrivingOptionsForResult) { Intent intent = new Intent(activity, DrivingOptionsActivity.class); - activity.startActivityForResult(intent, MwmActivity.REQ_CODE_DRIVING_OPTIONS); + startDrivingOptionsForResult.launch(intent); } } diff --git a/android/app/src/main/java/app/organicmaps/settings/VoiceInstructionsSettingsFragment.java b/android/app/src/main/java/app/organicmaps/settings/VoiceInstructionsSettingsFragment.java index a22f74d630..7fd38e86f7 100644 --- a/android/app/src/main/java/app/organicmaps/settings/VoiceInstructionsSettingsFragment.java +++ b/android/app/src/main/java/app/organicmaps/settings/VoiceInstructionsSettingsFragment.java @@ -1,5 +1,6 @@ package app.organicmaps.settings; +import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.res.Configuration; @@ -11,6 +12,8 @@ import android.text.style.ForegroundColorSpan; import android.view.View; import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; @@ -35,7 +38,6 @@ import java.util.Map; public class VoiceInstructionsSettingsFragment extends BaseXmlSettingsFragment { - private static final int REQUEST_INSTALL_DATA = 1; @NonNull @SuppressWarnings("NotNullFieldNotInitialized") @@ -67,6 +69,14 @@ public class VoiceInstructionsSettingsFragment extends BaseXmlSettingsFragment private LanguageData mCurrentLanguage; private String mSelectedLanguage; + private final ActivityResultLauncher startInstallDataIntentForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + + if(activityResult.getResultCode() == Activity.RESULT_OK) + { + onInstallDataResult(); + } + }); + private final Preference.OnPreferenceChangeListener mEnabledListener = (preference, newValue) -> { final boolean set = (Boolean) newValue; if (!set) @@ -107,8 +117,7 @@ public class VoiceInstructionsSettingsFragment extends BaseXmlSettingsFragment if (lang.downloaded) setLanguage(lang); else - UiUtils.startActivityForResult(VoiceInstructionsSettingsFragment.this, - new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA), REQUEST_INSTALL_DATA); + UiUtils.startActivityForResult(startInstallDataIntentForResult, new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA)); return false; }; @@ -181,21 +190,13 @@ public class VoiceInstructionsSettingsFragment extends BaseXmlSettingsFragment updateTts(); } - @Override - @SuppressWarnings("deprecation") // https://github.com/organicmaps/organicmaps/issues/3630 - public void onActivityResult(int requestCode, int resultCode, Intent data) + private void onInstallDataResult() { - // Do not check resultCode here as it is always RESULT_CANCELED - super.onActivityResult(requestCode, resultCode, data); + updateTts(); - if (requestCode == REQUEST_INSTALL_DATA) - { - updateTts(); - - LanguageData lang = mLanguages.get(mSelectedLanguage); - if (lang != null && lang.downloaded) - setLanguage(lang); - } + LanguageData lang = mLanguages.get(mSelectedLanguage); + if (lang != null && lang.downloaded) + setLanguage(lang); } private void enableListeners(boolean enable) diff --git a/android/app/src/main/java/app/organicmaps/util/UiUtils.java b/android/app/src/main/java/app/organicmaps/util/UiUtils.java index 3bd2609007..bfb1eba977 100644 --- a/android/app/src/main/java/app/organicmaps/util/UiUtils.java +++ b/android/app/src/main/java/app/organicmaps/util/UiUtils.java @@ -18,6 +18,8 @@ import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.AnyRes; import androidx.annotation.AttrRes; import androidx.annotation.ColorInt; @@ -32,7 +34,6 @@ import androidx.core.graphics.Insets; import androidx.core.view.WindowCompat; import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsControllerCompat; -import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; import app.organicmaps.MwmApplication; import app.organicmaps.R; @@ -362,10 +363,9 @@ public final class UiUtils } } - @SuppressWarnings("deprecation") // https://github.com/organicmaps/organicmaps/issues/3630 - public static void startActivityForResult(@NonNull Fragment fragment, @NonNull Intent intent, int requestCode) + public static void startActivityForResult(ActivityResultLauncher startForResult, @NonNull Intent intent) { - fragment.startActivityForResult(intent, requestCode); + startForResult.launch(intent); } // utility class diff --git a/android/app/src/main/java/app/organicmaps/widget/SearchToolbarController.java b/android/app/src/main/java/app/organicmaps/widget/SearchToolbarController.java index c6fc6468ef..434d714389 100644 --- a/android/app/src/main/java/app/organicmaps/widget/SearchToolbarController.java +++ b/android/app/src/main/java/app/organicmaps/widget/SearchToolbarController.java @@ -10,6 +10,7 @@ import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; +import androidx.activity.result.ActivityResult; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; @@ -22,7 +23,6 @@ import com.google.android.material.textfield.TextInputEditText; public class SearchToolbarController extends ToolbarController implements View.OnClickListener { - private static final int REQUEST_VOICE_RECOGNITION = 0xCA11; @Nullable private final View mToolbarContainer; @NonNull @@ -107,7 +107,7 @@ public class SearchToolbarController extends ToolbarController implements View.O clear(); } - protected void startVoiceRecognition(Intent intent, int code) + protected void startVoiceRecognition(Intent intent) { throw new RuntimeException("To be used startVoiceRecognition() must be implemented by descendant class"); } @@ -130,7 +130,7 @@ public class SearchToolbarController extends ToolbarController implements View.O try { startVoiceRecognition(InputUtils.createIntentForVoiceRecognition( - requireActivity().getString(getVoiceInputPrompt())), REQUEST_VOICE_RECOGNITION); + requireActivity().getString(getVoiceInputPrompt()))); } catch (ActivityNotFoundException e) { @@ -210,14 +210,18 @@ public class SearchToolbarController extends ToolbarController implements View.O UiUtils.showIf(show, mSearchContainer); } - public void onActivityResult(int requestCode, int resultCode, Intent data) + public void onVoiceRecognitionResult(ActivityResult activityResult) { - if (requestCode == REQUEST_VOICE_RECOGNITION && resultCode == Activity.RESULT_OK) - { - String result = InputUtils.getBestRecognitionResult(data); - if (!TextUtils.isEmpty(result)) - setQuery(result); - } + if(activityResult.getResultCode() == Activity.RESULT_OK) + { + if (activityResult.getData() == null) + { + return; + } + String recognitionResult = InputUtils.getBestRecognitionResult(activityResult.getData()); + if (!TextUtils.isEmpty(recognitionResult)) + setQuery(recognitionResult); + } } public void setHint(@StringRes int hint)