From 7abe2833fd18668479fb9e14320a6892b532cf6a Mon Sep 17 00:00:00 2001 From: Alexey Osminin Date: Mon, 7 Dec 2020 12:40:08 +0300 Subject: [PATCH] [android] remove MwmApplication get --- .../jni/com/mapswithme/platform/Platform.cpp | 8 ++++-- .../src/com/mapswithme/maps/MwmActivity.java | 4 +-- .../maps/downloader/DownloaderFragment.java | 2 +- .../maps/location/LocationHelper.java | 4 +-- .../mapswithme/maps/routing/SearchWheel.java | 2 +- .../mapswithme/maps/search/SearchEngine.java | 24 +++++++++++------- .../maps/search/SearchFragment.java | 10 ++++---- .../mapswithme/maps/search/SearchRecents.java | 6 +++-- .../maps/ugc/UGCEditorFragment.java | 2 +- android/src/com/mapswithme/util/Language.java | 4 +-- .../com/mapswithme/util/LocationUtils.java | 25 ++++++------------- .../com/mapswithme/util/log/MemLogging.java | 8 +++--- 12 files changed, 51 insertions(+), 48 deletions(-) diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index 83b7c7a9b8..010a35e216 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -70,8 +70,12 @@ std::string Platform::GetMemoryInfo() const static std::shared_ptr classMemLogging = jni::make_global_ref(env->FindClass("com/mapswithme/util/log/MemLogging")); ASSERT(classMemLogging, ()); - static jmethodID const getMemoryInfoId = jni::GetStaticMethodID(env, static_cast(*classMemLogging), "getMemoryInfo", "()Ljava/lang/String;"); - jstring const memInfoString = (jstring)env->CallStaticObjectMethod(static_cast(*classMemLogging), getMemoryInfoId); + jobject context = android::Platform::Instance().GetContext(); + static jmethodID const getMemoryInfoId = jni::GetStaticMethodID(env, + static_cast(*classMemLogging), "getMemoryInfo", + "(Landroid/content/Context;)Ljava/lang/String;"); + jstring const memInfoString = (jstring)env->CallStaticObjectMethod( + static_cast(*classMemLogging), getMemoryInfoId, context); ASSERT(memInfoString, ()); return jni::ToNativeString(env, memInfoString); diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 2cafd569dc..1adcf81dfc 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -738,7 +738,7 @@ public class MwmActivity extends BaseMwmFragmentActivity // results are no longer needed. SearchEngine.INSTANCE.cancel(); - SearchEngine.INSTANCE.searchInteractive(mSearchController.getQuery(), System.nanoTime(), + SearchEngine.INSTANCE.searchInteractive(this, mSearchController.getQuery(), System.nanoTime(), false /* isMapAndTable */, mFilterController != null ? mFilterController.getFilter() : null, mFilterController != null ? mFilterController.getBookingFilterParams() : null); @@ -1745,7 +1745,7 @@ public class MwmActivity extends BaseMwmFragmentActivity return; request.setPointData(object.getLat(), object.getLon(), object.getTitle(), object.getApiId()); - object.setSubtitle(request.getCallerName(MwmApplication.get()).toString()); + object.setSubtitle(request.getCallerName(MwmApplication.from(this)).toString()); } } diff --git a/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java b/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java index 9c7d8cd151..6800847212 100644 --- a/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java +++ b/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java @@ -75,7 +75,7 @@ public class DownloaderFragment extends BaseMwmRecyclerFragment private final LocationState.LocationPendingTimeoutListener mLocationPendingTimeoutListener = () -> { stop(); - if (LocationUtils.areLocationServicesTurnedOn()) + if (LocationUtils.areLocationServicesTurnedOn(mContext)) notifyLocationNotFound(); }; @@ -469,7 +469,7 @@ public enum LocationHelper implements Initializable addListener(mCoreLocationListener, true); - if (!LocationUtils.checkProvidersAvailability()) + if (!LocationUtils.checkProvidersAvailability(mContext)) { // No need to notify about an error in first run mode if (!mInFirstRun) diff --git a/android/src/com/mapswithme/maps/routing/SearchWheel.java b/android/src/com/mapswithme/maps/routing/SearchWheel.java index 95baa3e24f..b1cebd6daa 100644 --- a/android/src/com/mapswithme/maps/routing/SearchWheel.java +++ b/android/src/com/mapswithme/maps/routing/SearchWheel.java @@ -296,7 +296,7 @@ class SearchWheel implements View.OnClickListener { mCurrentOption = searchOption; final String query = mFrame.getContext().getString(searchOption.mQueryId); - SearchEngine.INSTANCE.searchInteractive(query, System.nanoTime(), false /* isMapAndTable */, + SearchEngine.INSTANCE.searchInteractive(mFrame.getContext(), query, System.nanoTime(), false /* isMapAndTable */, null /* hotelsFilter */, null /* bookingParams */); refreshSearchButtonImage(); diff --git a/android/src/com/mapswithme/maps/search/SearchEngine.java b/android/src/com/mapswithme/maps/search/SearchEngine.java index 17d51c6c0c..3c88f90fbe 100644 --- a/android/src/com/mapswithme/maps/search/SearchEngine.java +++ b/android/src/com/mapswithme/maps/search/SearchEngine.java @@ -1,5 +1,7 @@ package com.mapswithme.maps.search; +import android.content.Context; + import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -145,17 +147,19 @@ public enum SearchEngine implements NativeSearchListener, private native void nativeInit(); /** + * + * @param context * @param timestamp Search results are filtered according to it after multiple requests. * @return whether search was actually started. */ @MainThread - public boolean search(String query, long timestamp, boolean hasLocation, - double lat, double lon, @Nullable HotelsFilter hotelsFilter, - @Nullable BookingFilterParams bookingParams) + public boolean search(@NonNull Context context, String query, long timestamp, boolean hasLocation, + double lat, double lon, @Nullable HotelsFilter hotelsFilter, + @Nullable BookingFilterParams bookingParams) { try { - return nativeRunSearch(query.getBytes("utf-8"), Language.getKeyboardLocale(), + return nativeRunSearch(query.getBytes("utf-8"), Language.getKeyboardLocale(context), timestamp, hasLocation, lat, lon, hotelsFilter, bookingParams); } catch (UnsupportedEncodingException ignored) { } @@ -175,18 +179,20 @@ public enum SearchEngine implements NativeSearchListener, } @MainThread - public void searchInteractive(@NonNull String query, long timestamp, boolean isMapAndTable, - @Nullable HotelsFilter hotelsFilter, @Nullable BookingFilterParams bookingParams) + public void searchInteractive(@NonNull Context context, @NonNull String query, long timestamp, + boolean isMapAndTable, @Nullable HotelsFilter hotelsFilter, + @Nullable BookingFilterParams bookingParams) { - searchInteractive(query, Language.getKeyboardLocale(), timestamp, isMapAndTable, hotelsFilter, bookingParams); + searchInteractive(query, Language.getKeyboardLocale(context), timestamp, isMapAndTable, hotelsFilter, bookingParams); } @MainThread - public static void searchMaps(String query, long timestamp) + public static void searchMaps(@NonNull Context context, String query, long timestamp) { try { - nativeRunSearchMaps(query.getBytes("utf-8"), Language.getKeyboardLocale(), timestamp); + nativeRunSearchMaps(query.getBytes("utf-8"), Language.getKeyboardLocale(context), + timestamp); } catch (UnsupportedEncodingException ignored) { } } diff --git a/android/src/com/mapswithme/maps/search/SearchFragment.java b/android/src/com/mapswithme/maps/search/SearchFragment.java index 4750d4d403..e2cc4f5197 100644 --- a/android/src/com/mapswithme/maps/search/SearchFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchFragment.java @@ -538,7 +538,7 @@ public class SearchFragment extends BaseMwmFragment void showSingleResultOnMap(@NonNull SearchResult result, int resultIndex) { final String query = getQuery(); - SearchRecents.add(query); + SearchRecents.add(query, requireContext()); SearchEngine.INSTANCE.cancel(); if (!RoutingController.get().isWaitingPoiPick()) @@ -556,7 +556,7 @@ public class SearchFragment extends BaseMwmFragment SearchEngine.INSTANCE.cancel(); final String query = getQuery(); - SearchRecents.add(query); + SearchRecents.add(query, requireContext()); mLastQueryTimestamp = System.nanoTime(); HotelsFilter hotelsFilter = null; @@ -569,7 +569,7 @@ public class SearchFragment extends BaseMwmFragment SearchEngine.INSTANCE.searchInteractive( query, !TextUtils.isEmpty(mInitialLocale) - ? mInitialLocale : com.mapswithme.util.Language.getKeyboardLocale(), + ? mInitialLocale : com.mapswithme.util.Language.getKeyboardLocale(requireContext()), mLastQueryTimestamp, false /* isMapAndTable */, hotelsFilter, bookingFilterParams); SearchEngine.INSTANCE.setQuery(query); @@ -621,12 +621,12 @@ public class SearchFragment extends BaseMwmFragment mLastQueryTimestamp = System.nanoTime(); if (isTabletSearch()) { - SearchEngine.INSTANCE.searchInteractive(getQuery(), mLastQueryTimestamp, true /* isMapAndTable */, + SearchEngine.INSTANCE.searchInteractive(requireContext(), getQuery(), mLastQueryTimestamp, true /* isMapAndTable */, hotelsFilter, bookingFilterParams); } else { - if (!SearchEngine.INSTANCE.search(getQuery(), mLastQueryTimestamp, mLastPosition.valid, + if (!SearchEngine.INSTANCE.search(requireContext(), getQuery(), mLastQueryTimestamp, mLastPosition.valid, mLastPosition.lat, mLastPosition.lon, hotelsFilter, bookingFilterParams)) { diff --git a/android/src/com/mapswithme/maps/search/SearchRecents.java b/android/src/com/mapswithme/maps/search/SearchRecents.java index e651e2cace..f9c4df911d 100644 --- a/android/src/com/mapswithme/maps/search/SearchRecents.java +++ b/android/src/com/mapswithme/maps/search/SearchRecents.java @@ -1,6 +1,8 @@ package com.mapswithme.maps.search; import androidx.annotation.NonNull; + +import android.content.Context; import android.text.TextUtils; import android.util.Pair; import com.mapswithme.util.Language; @@ -34,12 +36,12 @@ public final class SearchRecents return sRecents.get(position); } - public static boolean add(@NonNull String query) + public static boolean add(@NonNull String query, @NonNull Context context) { if (TextUtils.isEmpty(query) || sRecents.contains(query)) return false; - nativeAdd(Language.getKeyboardLocale(), query); + nativeAdd(Language.getKeyboardLocale(context), query); refresh(); return true; } diff --git a/android/src/com/mapswithme/maps/ugc/UGCEditorFragment.java b/android/src/com/mapswithme/maps/ugc/UGCEditorFragment.java index cd0d4345e5..ba0befb098 100644 --- a/android/src/com/mapswithme/maps/ugc/UGCEditorFragment.java +++ b/android/src/com/mapswithme/maps/ugc/UGCEditorFragment.java @@ -191,7 +191,7 @@ public class UGCEditorFragment extends BaseToolbarAuthFragment modifiedRatings.toArray(ratings); UGCUpdate update = new UGCUpdate(ratings, mReviewEditText.getText().toString(), System.currentTimeMillis(), Language.getDefaultLocale(), - Language.getKeyboardLocale()); + Language.getKeyboardLocale(requireContext())); FeatureId featureId = getArguments().getParcelable(ARG_FEATURE_ID); if (featureId == null) { diff --git a/android/src/com/mapswithme/util/Language.java b/android/src/com/mapswithme/util/Language.java index bc8f255530..f84abc7d3c 100644 --- a/android/src/com/mapswithme/util/Language.java +++ b/android/src/com/mapswithme/util/Language.java @@ -6,6 +6,7 @@ import android.text.TextUtils; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; +import androidx.annotation.Nullable; import com.mapswithme.maps.MwmApplication; import java.util.Locale; @@ -35,9 +36,8 @@ public class Language // After some testing on Galaxy S4, looks like this method doesn't work on all devices: // sometime it always returns the same value as getDefaultLocale() @NonNull - public static String getKeyboardLocale() + public static String getKeyboardLocale(@NonNull Context context) { - Context context = MwmApplication.get(); InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) diff --git a/android/src/com/mapswithme/util/LocationUtils.java b/android/src/com/mapswithme/util/LocationUtils.java index 8306b3058a..831d97c276 100644 --- a/android/src/com/mapswithme/util/LocationUtils.java +++ b/android/src/com/mapswithme/util/LocationUtils.java @@ -102,9 +102,9 @@ public class LocationUtils @SuppressLint("InlinedApi") @SuppressWarnings("deprecation") - public static boolean areLocationServicesTurnedOn() + public static boolean areLocationServicesTurnedOn(@NonNull Context context) { - final ContentResolver resolver = MwmApplication.get().getContentResolver(); + final ContentResolver resolver = context.getContentResolver(); try { return Settings.Secure.getInt(resolver, Settings.Secure.LOCATION_MODE) @@ -116,9 +116,9 @@ public class LocationUtils } } - private static void logAvailableProviders() + private static void logAvailableProviders(@NonNull Context context) { - LocationManager locMngr = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE); + LocationManager locMngr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); List providers = locMngr.getProviders(true); StringBuilder sb; if (!providers.isEmpty()) @@ -134,20 +134,9 @@ public class LocationUtils LOGGER.i(TAG, sb.toString()); } - /** - * - * Use {@link #checkProvidersAvailability(Application)} instead. - */ - @SuppressWarnings("DeprecatedIsStillUsed") - @Deprecated - public static boolean checkProvidersAvailability() + public static boolean checkProvidersAvailability(@NonNull Context context) { - return checkProvidersAvailability(MwmApplication.get()); - } - - public static boolean checkProvidersAvailability(@NonNull Application application) - { - LocationManager locationManager = (LocationManager) application.getSystemService(Context.LOCATION_SERVICE); + LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (locationManager == null) { LOGGER.e(TAG, "This device doesn't support the location service."); @@ -156,7 +145,7 @@ public class LocationUtils boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); - LocationUtils.logAvailableProviders(); + LocationUtils.logAvailableProviders(context); return networkEnabled || gpsEnabled; } } diff --git a/android/src/com/mapswithme/util/log/MemLogging.java b/android/src/com/mapswithme/util/log/MemLogging.java index 8bb7e8927d..28e028ced5 100644 --- a/android/src/com/mapswithme/util/log/MemLogging.java +++ b/android/src/com/mapswithme/util/log/MemLogging.java @@ -4,18 +4,20 @@ import android.app.ActivityManager; import android.content.Context; import android.os.Debug; import android.os.Build; + +import androidx.annotation.NonNull; import com.mapswithme.maps.MwmApplication; - +@SuppressWarnings("unused") public class MemLogging { - public static String getMemoryInfo() + public static String getMemoryInfo(@NonNull Context context) { final Debug.MemoryInfo debugMI = new Debug.MemoryInfo(); Debug.getMemoryInfo(debugMI); final ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); final ActivityManager activityManager = - (ActivityManager) MwmApplication.get().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); + (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); StringBuilder log = new StringBuilder("Memory info: ");