[android] Remove weird static enum pattern from LocationHelper

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
This commit is contained in:
Roman Tsisyk 2023-09-10 11:45:38 +03:00 committed by Viktor Govako
parent adb93c6f49
commit 06050fd135
25 changed files with 124 additions and 142 deletions

View file

@ -132,7 +132,7 @@ public class DownloadResourcesLegacyActivity extends BaseMwmFragmentActivity
mChbDownloadCountry.setText(checkBoxText);
}
LocationHelper.INSTANCE.removeListener(this);
LocationHelper.from(DownloadResourcesLegacyActivity.this).removeListener(this);
}
};
@ -239,14 +239,14 @@ public class DownloadResourcesLegacyActivity extends BaseMwmFragmentActivity
{
super.onResume();
if (!isFinishing())
LocationHelper.INSTANCE.addListener(mLocationListener);
LocationHelper.from(this).addListener(mLocationListener);
}
@Override
protected void onPause()
{
super.onPause();
LocationHelper.INSTANCE.removeListener(mLocationListener);
LocationHelper.from(this).removeListener(mLocationListener);
if (mAlertDialog != null && mAlertDialog.isShowing())
mAlertDialog.dismiss();
mAlertDialog = null;

View file

@ -155,7 +155,9 @@ public final class Map
mRequireResize = false;
setupWidgets(context, surfaceFrame.width(), surfaceFrame.height());
final boolean firstStart = LocationHelper.INSTANCE.isInFirstRun();
final LocationHelper locationHelper = LocationHelper.from(context);
final boolean firstStart = locationHelper.isInFirstRun();
if (!nativeCreateEngine(surface, surfaceDpi, firstStart, mLaunchByDeepLink, BuildConfig.VERSION_CODE))
{
if (mCallbackUnsupported != null)
@ -164,7 +166,7 @@ public final class Map
}
if (firstStart)
UiThread.runLater(LocationHelper.INSTANCE::onExitFromFirstRun);
UiThread.runLater(locationHelper::onExitFromFirstRun);
mSurfaceCreated = true;
mSurfaceAttached = true;

View file

@ -333,7 +333,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
private void shareMyLocation()
{
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
final Location loc = LocationHelper.from(this).getSavedLocation();
if (loc != null)
{
SharingUtils.shareLocation(this, loc);
@ -819,7 +819,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
closeFloatingPanels();
startLocation();
MapObject startPoint = LocationHelper.INSTANCE.getMyPosition();
MapObject startPoint = LocationHelper.from(this).getMyPosition();
RoutingController.get().prepare(startPoint, endPoint);
// TODO: check for tablet.
@ -1052,7 +1052,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
RoutingController.get().attach(this);
IsolinesManager.from(getApplicationContext()).attach(this::onIsolinesStateChanged);
LocationState.nativeSetListener(this);
LocationHelper.INSTANCE.addListener(this);
LocationHelper.from(this).addListener(this);
onMyPositionModeChanged(LocationState.nativeGetMode());
mSearchController.attach(this);
if (!Config.isScreenSleepEnabled())
@ -1065,7 +1065,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
super.onStop();
Framework.nativeRemovePlacePageActivationListener();
BookmarkManager.INSTANCE.removeLoadingListener(this);
LocationHelper.INSTANCE.removeListener(this);
LocationHelper.from(this).removeListener(this);
LocationState.nativeRemoveListener();
RoutingController.get().detach();
IsolinesManager.from(getApplicationContext()).detach();
@ -1332,7 +1332,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
// +S+F-L -> Hide
// +S+F+L -> Hide
MapObject myPosition = LocationHelper.INSTANCE.getMyPosition();
MapObject myPosition = LocationHelper.from(this).getMyPosition();
if (myPosition != null && !controller.hasEndPoint())
{
@ -1648,29 +1648,27 @@ public class MwmActivity extends BaseMwmFragmentActivity
.show();
}
@Override
public void onSuggestRebuildRoute()
private void onSuggestRebuildRoute()
{
final RoutingController controller = RoutingController.get();
// Starting and ending points must be non-null, see {@link #showAddStartOrFinishFrame() }.
final MapObject endPoint = Objects.requireNonNull(controller.getEndPoint());
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this, R.style.MwmTheme_AlertDialog)
.setTitle(R.string.p2p_only_from_current)
.setMessage(R.string.p2p_reroute_from_current)
.setCancelable(false)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.ok, MapObject.isOfType(MapObject.MY_POSITION, endPoint) ?
(dialog, which) -> controller.swapPoints() :
(dialog, which) -> {
// The current location may change while this dialog is still shown on the screen.
final MapObject myPosition = LocationHelper.from(this).getMyPosition();
controller.setStartPoint(myPosition);
}
)
.setOnDismissListener(dialog -> mAlertDialog = null);
final TextView titleView = (TextView)View.inflate(this, R.layout.dialog_suggest_reroute_title, null);
titleView.setText(R.string.p2p_only_from_current);
builder.setCustomTitle(titleView);
if (MapObject.isOfType(MapObject.MY_POSITION, RoutingController.get().getEndPoint()))
builder.setPositiveButton(R.string.ok, (dialog, which) -> RoutingController.get().swapPoints());
else
{
if (LocationHelper.INSTANCE.getMyPosition() == null)
builder.setMessage(null).setNegativeButton(null, null);
builder.setPositiveButton(R.string.ok, (dialog, which) -> RoutingController.get().setStartFromMyPosition());
}
dismissAlertDialog();
mAlertDialog = builder.show();
}
@ -1760,7 +1758,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED)
{
Logger.i(LOCATION_TAG, "Permission ACCESS_FINE_LOCATION is granted");
LocationHelper.INSTANCE.start();
LocationHelper.from(this).start();
return;
}
@ -1799,23 +1797,23 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
Logger.i(LOCATION_TAG, "Location updates are stopped by the user manually.");
LocationState.nativeOnLocationError(LocationState.ERROR_GPS_OFF);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
}
else if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED)
{
Logger.i(LOCATION_TAG, "Permission ACCESS_FINE_LOCATION is granted");
LocationHelper.INSTANCE.start();
LocationHelper.from(this).start();
}
else if (ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED)
{
Logger.i(LOCATION_TAG, "Permission ACCESS_COARSE_LOCATION is granted");
LocationHelper.INSTANCE.start();
LocationHelper.from(this).start();
}
else
{
Logger.w(LOCATION_TAG, "Permissions ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION are not granted");
LocationState.nativeOnLocationError(LocationState.ERROR_DENIED);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
Logger.i(LOCATION_TAG, "Requesting ACCESS_FINE_LOCATION + ACCESS_FINE_LOCATION permissions");
dismissLocationErrorDialog();
@ -1847,13 +1845,13 @@ public class MwmActivity extends BaseMwmFragmentActivity
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED)
{
LocationHelper.INSTANCE.start();
LocationHelper.from(this).start();
return;
}
Logger.w(LOCATION_TAG, "Permissions ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION have been refused");
LocationState.nativeOnLocationError(LocationState.ERROR_DENIED);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
if (mLocationErrorDialog != null && mLocationErrorDialog.isShowing())
{
@ -1915,12 +1913,12 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
Logger.w(LOCATION_TAG, "Location resolution has been refused");
LocationState.nativeOnLocationError(LocationState.ERROR_GPS_OFF);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
return;
}
Logger.i(LOCATION_TAG, "Location resolution has been granted, restarting location");
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
startLocation();
}
@ -1934,7 +1932,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
Logger.d(LOCATION_TAG, "settings = " + LocationUtils.areLocationServicesTurnedOn(this));
LocationState.nativeOnLocationError(LocationState.ERROR_GPS_OFF);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
if (mLocationErrorDialog != null && mLocationErrorDialog.isShowing())
{
@ -1991,7 +1989,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
Logger.w(LOCATION_TAG, "Disabled by user");
LocationState.nativeOnLocationError(LocationState.ERROR_GPS_OFF);
LocationHelper.INSTANCE.stop();
LocationHelper.from(this).stop();
})
.setPositiveButton(R.string.current_location_unknown_continue_button, (dialog, which) ->
{
@ -2003,7 +2001,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
@Override
public void onUseMyPositionAsStart()
{
RoutingController.get().setStartPoint(LocationHelper.INSTANCE.getMyPosition());
RoutingController.get().setStartPoint(LocationHelper.from(this).getMyPosition());
}
@Override
@ -2017,6 +2015,14 @@ public class MwmActivity extends BaseMwmFragmentActivity
@Override
public void onRoutingStart()
{
final RoutingController routing = RoutingController.get();
final MapObject my = LocationHelper.from(this).getMyPosition();
if (my == null || !MapObject.isOfType(MapObject.MY_POSITION, routing.getStartPoint()))
{
onSuggestRebuildRoute();
return;
}
closeFloatingPanels();
RoutingController.get().start();
}

View file

@ -62,6 +62,10 @@ public class MwmApplication extends Application implements Application.ActivityL
@NonNull
private IsolinesManager mIsolinesManager;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private LocationHelper mLocationHelper;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private SensorHelper mSensorHelper;
@ -97,6 +101,12 @@ public class MwmApplication extends Application implements Application.ActivityL
return mIsolinesManager;
}
@NonNull
public LocationHelper getLocationHelper()
{
return mLocationHelper;
}
@NonNull
public SensorHelper getSensorHelper()
{
@ -144,6 +154,7 @@ public class MwmApplication extends Application implements Application.ActivityL
registerActivityLifecycleCallbacks(this);
mSubwayManager = new SubwayManager(this);
mIsolinesManager = new IsolinesManager(this);
mLocationHelper = new LocationHelper(this);
mSensorHelper = new SensorHelper(this);
}
@ -218,8 +229,7 @@ public class MwmApplication extends Application implements Application.ActivityL
BookmarkManager.loadBookmarks();
TtsPlayer.INSTANCE.initialize(this);
ThemeSwitcher.INSTANCE.restart(false);
LocationHelper.INSTANCE.initialize(this);
RoutingController.get().initialize(null);
RoutingController.get().initialize(this);
TrafficManager.INSTANCE.initialize(null);
SubwayManager.from(this).initialize(null);
IsolinesManager.from(this).initialize(null);
@ -340,7 +350,7 @@ public class MwmApplication extends Application implements Application.ActivityL
nativeOnTransit(true);
LocationHelper.INSTANCE.resumeLocationInForeground();
mLocationHelper.resumeLocationInForeground();
}
private void onBackground()
@ -356,7 +366,7 @@ public class MwmApplication extends Application implements Application.ActivityL
Logger.i(LOCATION_TAG, "Navigation is in progress, keeping location in the background");
return;
}
LocationHelper.INSTANCE.stop();
mLocationHelper.stop();
}
private class StorageCallbackImpl implements MapManager.StorageCallback

View file

@ -156,9 +156,10 @@ public class SplashActivity extends AppCompatActivity
(ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED))
{
LocationHelper.INSTANCE.onEnteredIntoFirstRun();
if (!LocationHelper.INSTANCE.isActive())
LocationHelper.INSTANCE.start();
final LocationHelper locationHelper = app.getLocationHelper();
locationHelper.onEnteredIntoFirstRun();
if (!locationHelper.isActive())
locationHelper.start();
}
if (!asyncContinue)

View file

@ -409,7 +409,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<ConcatAdapter
{
mLastSortTimestamp = System.nanoTime();
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
final Location loc = LocationHelper.from(requireContext()).getSavedLocation();
final boolean hasMyPosition = loc != null;
if (!hasMyPosition && sortingType == BookmarkManager.SORT_BY_DISTANCE)
return;
@ -497,7 +497,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<ConcatAdapter
private int[] getAvailableSortingTypes()
{
final long catId = mCategoryDataSource.getData().getId();
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
final Location loc = LocationHelper.from(requireContext()).getSavedLocation();
final boolean hasMyPosition = loc != null;
return BookmarkManager.INSTANCE.getAvailableSortingTypes(catId, hasMyPosition);
}

View file

@ -364,7 +364,7 @@ public class Holders
BookmarkInfo bookmark = new BookmarkInfo(sectionsDataSource.getCategory().getId(),
bookmarkId);
mName.setText(bookmark.getName());
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
final Location loc = LocationHelper.from(mIcon.getContext()).getSavedLocation();
String distanceValue = loc == null ? "" : bookmark.getDistance(loc.getLatitude(),
loc.getLongitude(), 0.0).toString(mDistance.getContext());

View file

@ -20,7 +20,6 @@ import app.organicmaps.location.LocationHelper;
import app.organicmaps.widget.WheelProgressView;
import app.organicmaps.util.StringUtils;
import app.organicmaps.util.UiUtils;
import app.organicmaps.util.Utils;
import java.util.List;
@ -115,7 +114,7 @@ public class CountrySuggestFragment extends BaseMwmFragment implements View.OnCl
{
super.onResume();
Location loc = LocationHelper.INSTANCE.getSavedLocation();
Location loc = LocationHelper.from(requireContext()).getSavedLocation();
if (loc != null)
{
String id = MapManager.nativeFindCountry(loc.getLatitude(), loc.getLongitude());

View file

@ -628,7 +628,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
if (!mMyMapsMode && CountryItem.isRoot(parent))
{
Location loc = LocationHelper.INSTANCE.getSavedLocation();
Location loc = LocationHelper.from(mActivity).getSavedLocation();
hasLocation = (loc != null);
if (hasLocation)
{

View file

@ -160,7 +160,7 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
!failed &&
ConnectionState.INSTANCE.isWifiConnected())
{
Location loc = LocationHelper.INSTANCE.getSavedLocation();
Location loc = LocationHelper.from(mActivity).getSavedLocation();
if (loc != null)
{
String country = MapManager.nativeFindCountry(loc.getLatitude(), loc.getLongitude());

View file

@ -16,6 +16,7 @@ import androidx.annotation.UiThread;
import androidx.core.content.ContextCompat;
import app.organicmaps.Framework;
import app.organicmaps.MwmApplication;
import app.organicmaps.base.Initializable;
import app.organicmaps.bookmarks.data.FeatureId;
import app.organicmaps.bookmarks.data.MapObject;
@ -28,10 +29,8 @@ import app.organicmaps.util.log.Logger;
import java.util.LinkedHashSet;
import java.util.Set;
public enum LocationHelper implements Initializable<Context>, BaseLocationProvider.Listener
public class LocationHelper implements BaseLocationProvider.Listener
{
INSTANCE;
private static final long INTERVAL_FOLLOW_AND_ROTATE_MS = 3000;
private static final long INTERVAL_FOLLOW_MS = 1000;
private static final long INTERVAL_NOT_FOLLOW_MS = 3000;
@ -43,7 +42,6 @@ public enum LocationHelper implements Initializable<Context>, BaseLocationProvid
private static final long AGPS_EXPIRATION_TIME_MS = 16 * 60 * 60 * 1000; // 16 hours
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private Context mContext;
@ -53,26 +51,24 @@ public enum LocationHelper implements Initializable<Context>, BaseLocationProvid
@Nullable
private Location mSavedLocation;
private MapObject mMyPosition;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private BaseLocationProvider mLocationProvider;
private long mInterval;
private boolean mInFirstRun;
private boolean mActive;
@Override
public void initialize(@NonNull Context context)
@NonNull
public static LocationHelper from(@NonNull Context context)
{
return MwmApplication.from(context).getLocationHelper();
}
public LocationHelper(@NonNull Context context)
{
mContext = context;
mLocationProvider = LocationProviderFactory.getProvider(mContext, this);
}
@Override
public void destroy()
{
// No op.
}
/**
* @return MapObject.MY_POSITION, null if location is not yet determined or "My position" button is switched off.
*/

View file

@ -136,7 +136,7 @@ public class NavigationService extends Service implements LocationListener
/*
* Subscribe to location updates.
*/
LocationHelper.INSTANCE.addListener(this);
LocationHelper.from(this).addListener(this);
}
@RequiresPermission(value = ACCESS_FINE_LOCATION)
@ -146,7 +146,7 @@ public class NavigationService extends Service implements LocationListener
Logger.i(TAG);
super.onDestroy();
LocationHelper.INSTANCE.removeListener(this);
LocationHelper.from(this).removeListener(this);
TtsPlayer.INSTANCE.stop();
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
@ -155,7 +155,7 @@ public class NavigationService extends Service implements LocationListener
mPlayer.release();
// Restart the location to resubscribe with a less frequent refresh interval (see {@link onStartCommand() }).
LocationHelper.INSTANCE.restart();
LocationHelper.from(this).restart();
}
@Override
@ -189,7 +189,7 @@ public class NavigationService extends Service implements LocationListener
// Tests on different devices demonstrated that background location works significantly longer when
// requested AFTER starting the foreground service. Restarting the location is also necessary to
// re-subscribe for more frequent GPS updates for navigation.
LocationHelper.INSTANCE.restart();
LocationHelper.from(this).restart();
return START_STICKY;
}

View file

@ -47,7 +47,7 @@ class ResultCodesHelper
switch (errorCode)
{
case NO_POSITION:
if (!LocationHelper.INSTANCE.isActive())
if (!LocationHelper.from(context).isActive())
{
titleRes = R.string.dialog_routing_location_turn_on;
messages.add(resources.getString(R.string.dialog_routing_location_unknown_turn_on));

View file

@ -251,7 +251,7 @@ final class RoutingBottomMenuController implements View.OnClickListener
UiUtils.show(mActionFrame);
mActionMessage.setText(R.string.routing_add_start_point);
mActionMessage.setTag(RoutePointInfo.ROUTE_MARK_START);
if (LocationHelper.INSTANCE.getMyPosition() != null)
if (LocationHelper.from(mContext).getMyPosition() != null)
{
UiUtils.show(mActionButton);
Drawable icon = ContextCompat.getDrawable(mContext, R.drawable.ic_my_location);

View file

@ -29,7 +29,7 @@ import java.util.concurrent.TimeUnit;
@androidx.annotation.UiThread
public class RoutingController implements Initializable<Void>
public class RoutingController implements Initializable<Context>
{
private static final String TAG = RoutingController.class.getSimpleName();
@ -68,7 +68,6 @@ public class RoutingController implements Initializable<Void>
default void onCommonBuildError(int lastResultCode, @NonNull String[] lastMissingMaps) {}
default void onDrivingOptionsBuildError() {}
default void onShowDisclaimer(@Nullable MapObject startPoint, @Nullable MapObject endPoint) {}
default void onSuggestRebuildRoute() {}
/**
* @param progress progress to be displayed.
@ -141,7 +140,6 @@ public class RoutingController implements Initializable<Void>
mContainer.onBuiltRoute();
}
@SuppressWarnings("FieldCanBeLocal")
private final Framework.RoutingProgressListener mRoutingProgressListener = new Framework.RoutingProgressListener()
{
@MainThread
@ -153,13 +151,6 @@ public class RoutingController implements Initializable<Void>
}
};
@SuppressWarnings("FieldCanBeLocal")
private final Framework.RoutingRecommendationListener mRoutingRecommendationListener =
recommendation -> UiThread.run(() -> {
if (recommendation == Framework.ROUTE_REBUILD_AFTER_POINTS_LOADING)
setStartPoint(LocationHelper.INSTANCE.getMyPosition());
});
@SuppressWarnings("FieldCanBeLocal")
private final Framework.RoutingLoadPointsListener mRoutingLoadPointsListener =
success -> {
@ -266,7 +257,7 @@ public class RoutingController implements Initializable<Void>
}
@Override
public void initialize(@Nullable Void aVoid)
public void initialize(@NonNull Context context)
{
mLastRouterType = Framework.nativeGetLastUsedRouter();
mInvalidRoutePointsTransactionId = Framework.nativeInvalidRoutePointsTransactionId();
@ -274,7 +265,10 @@ public class RoutingController implements Initializable<Void>
Framework.nativeSetRoutingListener(mRoutingListener);
Framework.nativeSetRouteProgressListener(mRoutingProgressListener);
Framework.nativeSetRoutingRecommendationListener(mRoutingRecommendationListener);
Framework.nativeSetRoutingRecommendationListener(recommendation -> UiThread.run(() -> {
if (recommendation == Framework.ROUTE_REBUILD_AFTER_POINTS_LOADING)
setStartPoint(LocationHelper.from(context).getMyPosition());
}));
Framework.nativeSetRoutingLoadPointsListener(mRoutingLoadPointsListener);
}
@ -415,15 +409,6 @@ public class RoutingController implements Initializable<Void>
// and then app crashes. So, the previous route will be restored on the next app launch.
saveRoute();
MapObject my = LocationHelper.INSTANCE.getMyPosition();
if (my == null || !MapObject.isOfType(MapObject.MY_POSITION, getStartPoint()))
{
if (mContainer != null)
mContainer.onSuggestRebuildRoute();
return;
}
setState(State.NAVIGATION);
cancelPlanning(false);
@ -731,17 +716,10 @@ public class RoutingController implements Initializable<Void>
build();
}
public boolean setStartFromMyPosition()
public boolean setStartFromMyPosition(@NonNull MapObject my)
{
Logger.d(TAG, "setStartFromMyPosition");
MapObject my = LocationHelper.INSTANCE.getMyPosition();
if (my == null)
{
Logger.d(TAG, "setStartFromMyPosition: no my position - skip");
return false;
}
return setStartPoint(my);
}

View file

@ -48,7 +48,6 @@ import java.util.List;
public class SearchFragment extends BaseMwmFragment
implements OnBackPressListener,
NativeSearchListener,
SearchToolbarController.Container,
CategoriesAdapter.CategoriesUiListener
{
private long mLastQueryTimestamp;
@ -144,6 +143,7 @@ public class SearchFragment extends BaseMwmFragment
private PlaceholderView mResultsPlaceholder;
private FloatingActionButton mShowOnMapFab;
@NonNull
private SearchToolbarController mToolbarController;
@SuppressWarnings("NullableProblems")
@ -309,7 +309,7 @@ public class SearchFragment extends BaseMwmFragment
public void onResume()
{
super.onResume();
LocationHelper.INSTANCE.addListener(mLocationListener);
LocationHelper.from(requireContext()).addListener(mLocationListener);
if (mInitialQuery != null) {
setQuery(mInitialQuery, false);
mInitialQuery = null;
@ -319,7 +319,7 @@ public class SearchFragment extends BaseMwmFragment
@Override
public void onPause()
{
LocationHelper.INSTANCE.removeListener(mLocationListener);
LocationHelper.from(requireContext()).removeListener(mLocationListener);
super.onPause();
}
@ -561,8 +561,8 @@ public class SearchFragment extends BaseMwmFragment
mAttachedRecyclers.add(recycler);
}
@Override
public SearchToolbarController getController()
@NonNull
public SearchToolbarController requireController()
{
return mToolbarController;
}

View file

@ -5,6 +5,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import app.organicmaps.R;
@ -19,6 +20,7 @@ class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.Vie
private static final int TYPE_CLEAR = 1;
private static final int TYPE_MY_POSITION = 2;
@NonNull
private final SearchToolbarController mSearchToolbarController;
private final boolean mShowMyPosition;
@ -34,12 +36,11 @@ class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.Vie
}
}
public SearchHistoryAdapter(SearchToolbarController searchToolbarController)
public SearchHistoryAdapter(@NonNull SearchToolbarController searchToolbarController, boolean showMyPosition)
{
SearchRecents.refresh();
mSearchToolbarController = searchToolbarController;
mShowMyPosition = (RoutingController.get().isWaitingPoiPick() &&
LocationHelper.INSTANCE.getMyPosition() != null);
mShowMyPosition = showMyPosition;
}
@Override
@ -81,7 +82,7 @@ class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.Vie
@Override
public void onClick(View v)
{
RoutingController.get().onPoiSelected(LocationHelper.INSTANCE.getMyPosition());
RoutingController.get().onPoiSelected(LocationHelper.from(viewGroup.getContext()).getMyPosition());
mSearchToolbarController.onUpClick();
}
});

View file

@ -3,7 +3,6 @@ package app.organicmaps.search;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.CallSuper;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -12,10 +11,14 @@ import androidx.recyclerview.widget.RecyclerView;
import app.organicmaps.R;
import app.organicmaps.base.BaseMwmRecyclerFragment;
import app.organicmaps.location.LocationHelper;
import app.organicmaps.routing.RoutingController;
import app.organicmaps.widget.PlaceholderView;
import app.organicmaps.widget.SearchToolbarController;
import app.organicmaps.util.UiUtils;
import java.util.Objects;
public class SearchHistoryFragment extends BaseMwmRecyclerFragment<SearchHistoryAdapter>
{
private PlaceholderView mPlaceHolder;
@ -29,7 +32,10 @@ public class SearchHistoryFragment extends BaseMwmRecyclerFragment<SearchHistory
@Override
protected SearchHistoryAdapter createAdapter()
{
return new SearchHistoryAdapter(((SearchToolbarController.Container) getParentFragment()).getController());
final SearchToolbarController controller = ((SearchFragment) requireParentFragment()).requireController();
final boolean showMyPosition = (RoutingController.get().isWaitingPoiPick() &&
LocationHelper.from(requireContext()).getMyPosition() != null);
return new SearchHistoryAdapter(controller, showMyPosition);
}
@Override

View file

@ -505,15 +505,16 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
final LocationHelper locationHelper = LocationHelper.from(requireContext());
boolean oldVal = Config.useGoogleServices();
boolean newVal = (Boolean) newValue;
if (oldVal != newVal)
{
Config.setUseGoogleService(newVal);
if (LocationHelper.INSTANCE.isActive())
if (locationHelper.isActive())
{
LocationHelper.INSTANCE.stop();
LocationHelper.INSTANCE.start();
locationHelper.stop();
locationHelper.start();
}
}
return true;

View file

@ -34,7 +34,7 @@ public enum ThemeSwitcher implements Initializable<Context>
if (RoutingController.get().isNavigating())
{
Location last = LocationHelper.INSTANCE.getSavedLocation();
Location last = LocationHelper.from(mContext).getSavedLocation();
if (last == null)
{
theme = Config.getCurrentUiTheme(mContext);

View file

@ -50,11 +50,6 @@ public class SearchToolbarController extends ToolbarController implements View.O
}
};
public interface Container
{
SearchToolbarController getController();
}
public SearchToolbarController(@NonNull View root,
@NonNull Activity activity)
{

View file

@ -14,7 +14,6 @@ import app.organicmaps.R;
import app.organicmaps.location.LocationHelper;
import app.organicmaps.routing.RoutingInfo;
import app.organicmaps.sound.TtsPlayer;
import app.organicmaps.util.log.Logger;
import app.organicmaps.widget.FlatProgressView;
import app.organicmaps.util.Graphics;
import app.organicmaps.util.StringUtils;
@ -213,7 +212,7 @@ public class NavMenu
private void updateSpeedView(@NonNull RoutingInfo info)
{
final Location last = LocationHelper.INSTANCE.getSavedLocation();
final Location last = LocationHelper.from(mActivity).getSavedLocation();
if (last == null)
return;

View file

@ -102,7 +102,7 @@ public class DirectionFragment extends BaseMwmDialogFragment
public void onResume()
{
super.onResume();
LocationHelper.INSTANCE.addListener(this);
LocationHelper.from(requireContext()).addListener(this);
SensorHelper.from(requireContext()).addListener(this);
refreshViews();
}
@ -111,7 +111,7 @@ public class DirectionFragment extends BaseMwmDialogFragment
public void onPause()
{
super.onPause();
LocationHelper.INSTANCE.removeListener(this);
LocationHelper.from(requireContext()).removeListener(this);
SensorHelper.from(requireContext()).removeListener(this);
}
@ -130,7 +130,7 @@ public class DirectionFragment extends BaseMwmDialogFragment
@Override
public void onCompassUpdated(double north)
{
final Location last = LocationHelper.INSTANCE.getSavedLocation();
final Location last = LocationHelper.from(requireContext()).getSavedLocation();
if (last == null || mMapObject == null)
return;

View file

@ -258,7 +258,7 @@ public class PlacePageView extends Fragment implements View.OnClickListener,
{
super.onStart();
mViewModel.getMapObject().observe(requireActivity(), this);
LocationHelper.INSTANCE.addListener(this);
LocationHelper.from(requireContext()).addListener(this);
SensorHelper.from(requireContext()).addListener(this);
}
@ -267,7 +267,7 @@ public class PlacePageView extends Fragment implements View.OnClickListener,
{
super.onStop();
mViewModel.getMapObject().removeObserver(this);
LocationHelper.INSTANCE.removeListener(this);
LocationHelper.from(requireContext()).removeListener(this);
SensorHelper.from(requireContext()).removeListener(this);
detachCountry();
}
@ -285,7 +285,7 @@ public class PlacePageView extends Fragment implements View.OnClickListener,
{
refreshPreview();
refreshDetails();
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
final Location loc = LocationHelper.from(requireContext()).getSavedLocation();
if (mMapObject.getMapObjectType() == MapObject.MY_POSITION)
refreshMyPosition(loc);
else
@ -657,7 +657,7 @@ public class PlacePageView extends Fragment implements View.OnClickListener,
if (mMapObject == null || MapObject.isOfType(MapObject.MY_POSITION, mMapObject))
return;
final Location location = LocationHelper.INSTANCE.getSavedLocation();
final Location location = LocationHelper.from(requireContext()).getSavedLocation();
if (location == null)
{
UiUtils.hide(mAvDirection);

View file

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/margin_base_plus"
android:paddingTop="@dimen/margin_base_plus"
android:paddingEnd="@dimen/margin_base_plus"
android:textAppearance="@android:style/TextAppearance.DialogWindowTitle"
android:fontFamily="@string/robotoMedium"
tools:text="Title"/>