From 9e1acfd5935a5bf84f6a42e87e4d7257f34252a7 Mon Sep 17 00:00:00 2001 From: ExMix Date: Wed, 10 Sep 2014 17:38:45 +0300 Subject: [PATCH] review fixes --- .../jni/com/mapswithme/maps/LocationState.cpp | 13 ++--- .../maps/LocationButtonImageSetter.java | 20 +++---- .../com/mapswithme/maps/LocationState.java | 9 +-- .../src/com/mapswithme/maps/MWMActivity.java | 37 ++---------- iphone/Maps/Classes/MapViewController.mm | 13 +---- map/location_state.cpp | 58 +++++++++---------- map/location_state.hpp | 8 +-- qt/mainwindow.cpp | 7 ++- 8 files changed, 58 insertions(+), 107 deletions(-) diff --git a/android/jni/com/mapswithme/maps/LocationState.cpp b/android/jni/com/mapswithme/maps/LocationState.cpp index 91cf986a69..a6e3bd9e3f 100644 --- a/android/jni/com/mapswithme/maps/LocationState.cpp +++ b/android/jni/com/mapswithme/maps/LocationState.cpp @@ -17,27 +17,22 @@ extern "C" return g_framework->NativeFramework()->GetLocationState()->GetMode(); } - void CompassStatusChanged(location::State::Mode mode, shared_ptr const & obj) + void LocationStateModeChanged(location::State::Mode mode, shared_ptr const & obj) { JNIEnv * env = jni::GetEnv(); - jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "OnLocationStateModeChanged", "(I)V"); - jint val = static_cast(mode); - env->CallVoidMethod(*obj.get(), methodID, val); + env->CallVoidMethod(*obj.get(), jni::GetJavaMethodID(env, *obj.get(), "onLocationStateModeChangedCallback", "(I)V"), static_cast(mode)); } JNIEXPORT jint JNICALL Java_com_mapswithme_maps_LocationState_addLocationStateModeListener(JNIEnv * env, jobject thiz, jobject obj) { - location::State::TStateModeListener fn = bind(&CompassStatusChanged, _1, jni::make_global_ref(obj)); - shared_ptr ls = g_framework->NativeFramework()->GetLocationState(); - return ls->AddStateModeListener(fn); + g_framework->NativeFramework()->GetLocationState()->AddStateModeListener(bind(&LocationStateModeChanged, _1, jni::make_global_ref(obj))); } JNIEXPORT void JNICALL Java_com_mapswithme_maps_LocationState_removeLocationStateModeListener(JNIEnv * env, jobject thiz, jint slotID) { - shared_ptr ls = g_framework->NativeFramework()->GetLocationState(); - ls->RemoveStateModeListener(slotID); + g_framework->NativeFramework()->GetLocationState()->RemoveStateModeListener(slotID); } JNIEXPORT void JNICALL diff --git a/android/src/com/mapswithme/maps/LocationButtonImageSetter.java b/android/src/com/mapswithme/maps/LocationButtonImageSetter.java index 374725dcd4..36c65eeba4 100644 --- a/android/src/com/mapswithme/maps/LocationButtonImageSetter.java +++ b/android/src/com/mapswithme/maps/LocationButtonImageSetter.java @@ -9,25 +9,19 @@ import java.util.Map; public class LocationButtonImageSetter { - public enum ButtonState - { - NO_LOCATION, - WAITING_LOCATION, - HAS_LOCATION, - FOLLOW_MODE - } - private final static Map STATE_TO_RES = new HashMap(); + private final static Map STATE_TO_RES = new HashMap(); static { - STATE_TO_RES.put(ButtonState.NO_LOCATION, R.drawable.ic_my_position); - STATE_TO_RES.put(ButtonState.HAS_LOCATION, R.drawable.ic_my_position_pressed); - STATE_TO_RES.put(ButtonState.FOLLOW_MODE, R.drawable.ic_my_position_auto_follow); - STATE_TO_RES.put(ButtonState.WAITING_LOCATION, R.drawable.ic_my_position_search); + STATE_TO_RES.put(LocationState.UNKNOWN_POSITION, R.drawable.ic_my_position); + STATE_TO_RES.put(LocationState.NOT_FOLLOW, R.drawable.ic_my_position_pressed); + STATE_TO_RES.put(LocationState.FOLLOW, R.drawable.ic_my_position_pressed); + STATE_TO_RES.put(LocationState.ROTATE_AND_FOLLOW, R.drawable.ic_my_position_auto_follow); + STATE_TO_RES.put(LocationState.PENDING_POSITION, R.drawable.ic_my_position_search); } - public static void setButtonViewFromState(ButtonState state, ImageButton button) + public static void setButtonViewFromState(int state, ImageButton button) { final int id = STATE_TO_RES.get(state); final Drawable draw = button.getResources().getDrawable(id); diff --git a/android/src/com/mapswithme/maps/LocationState.java b/android/src/com/mapswithme/maps/LocationState.java index 7c2f791566..9645457886 100644 --- a/android/src/com/mapswithme/maps/LocationState.java +++ b/android/src/com/mapswithme/maps/LocationState.java @@ -2,12 +2,13 @@ package com.mapswithme.maps; public class LocationState { - // location::State::Mode enum - public static final int UNKNOW_POSITION = 0; + /// These values should correspond to values of + /// location::State::Mode defined in map/location_state.hpp + public static final int UNKNOWN_POSITION = 0; public static final int PENDING_POSITION = 0x1; public static final int NOT_FOLLOW = 0x2; - public static final int FOLLOW = 0x4; - public static final int ROTATE_AND_FOLLOW = 0x8; + public static final int FOLLOW = 0x3; + public static final int ROTATE_AND_FOLLOW = 0x4; public native void switchToNextMode(); public native int getLocationStateMode(); diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index 33b7d8b9b1..c181b347a9 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -36,7 +36,6 @@ import com.mapswithme.country.DownloadActivity; import com.mapswithme.maps.Ads.AdsManager; import com.mapswithme.maps.Ads.MenuAd; import com.mapswithme.maps.Framework.OnBalloonListener; -import com.mapswithme.maps.LocationButtonImageSetter.ButtonState; import com.mapswithme.maps.MapStorage.Index; import com.mapswithme.maps.api.ParsedMmwRequest; import com.mapswithme.maps.background.WorkerService; @@ -156,33 +155,11 @@ public class MWMActivity extends NvEventQueueActivity Utils.automaticIdleScreen(false, getWindow()); } - private void updateMyPositionButton(int locationStateMode) - { - ButtonState buttonState = ButtonState.NO_LOCATION; - switch (locationStateMode) - { - case LocationState.UNKNOW_POSITION: - break; - case LocationState.PENDING_POSITION: - buttonState = ButtonState.WAITING_LOCATION; - break; - case LocationState.NOT_FOLLOW: - case LocationState.FOLLOW: - buttonState = ButtonState.HAS_LOCATION; - break; - case LocationState.ROTATE_AND_FOLLOW: - buttonState = ButtonState.FOLLOW_MODE; - break; - } - - LocationButtonImageSetter.setButtonViewFromState(buttonState, mLocationButton); - } - public void checkShouldResumeLocationService() { final LocationState state = MWMApplication.get().getLocationState(); final int currentLocationMode = state.getLocationStateMode(); - updateMyPositionButton(currentLocationMode); + LocationButtonImageSetter.setButtonViewFromState(currentLocationMode, mLocationButton); if (currentLocationMode > LocationState.NOT_FOLLOW) resumeLocation(); @@ -990,26 +967,22 @@ public class MWMActivity extends NvEventQueueActivity public void onLocationStateModeChanged(int newMode) { - updateMyPositionButton(newMode); + LocationButtonImageSetter.setButtonViewFromState(newMode, mLocationButton); switch (newMode) { - case LocationState.UNKNOW_POSITION: + case LocationState.UNKNOWN_POSITION: pauseLocation(); break; case LocationState.PENDING_POSITION: resumeLocation(); break; - case LocationState.NOT_FOLLOW: - break; - case LocationState.FOLLOW: - break; - case LocationState.ROTATE_AND_FOLLOW: + default: break; } } /// Callback from native compass GUI element processing. - public void OnLocationStateModeChanged(int newStatus) + public void onLocationStateModeChangedCallback(int newStatus) { final int val = newStatus; runOnUiThread(new Runnable() diff --git a/iphone/Maps/Classes/MapViewController.mm b/iphone/Maps/Classes/MapViewController.mm index 15b63d5824..4f0099beb6 100644 --- a/iphone/Maps/Classes/MapViewController.mm +++ b/iphone/Maps/Classes/MapViewController.mm @@ -115,12 +115,9 @@ - (void)onLocationStateModeChanged:(location::State::Mode)newMode { - Framework & f = GetFramework(); - shared_ptr ls = f.GetLocationState(); - switch (newMode) { - case location::State::UnknowPosition: + case location::State::UnknownPosition: { [[MapsAppDelegate theApp] enableStandby]; [[MapsAppDelegate theApp].m_locationManager stop:self]; @@ -149,11 +146,6 @@ break; } case location::State::NotFollow: - { - [self.toolbarView.locationButton setImage:[UIImage imageNamed:@"LocationSelected"] forState:UIControlStateSelected]; - self.toolbarView.locationButton.selected = YES; - break; - } case location::State::Follow: { [self.toolbarView.locationButton setImage:[UIImage imageNamed:@"LocationSelected"] forState:UIControlStateSelected]; @@ -573,8 +565,7 @@ SEL locationStateModeSelector = @selector(onLocationStateModeChanged:); LocationStateModeFnT locationStateModeFn = (LocationStateModeFnT)[self methodForSelector:locationStateModeSelector]; - shared_ptr ls = f.GetLocationState(); - ls->AddStateModeListener(bind(locationStateModeFn, self, locationStateModeSelector, _1)); + f.GetLocationState()->AddStateModeListener(bind(locationStateModeFn, self, locationStateModeSelector, _1)); m_StickyThreshold = 10; diff --git a/map/location_state.cpp b/map/location_state.cpp index adb3b85277..e3b65325a4 100644 --- a/map/location_state.cpp +++ b/map/location_state.cpp @@ -55,7 +55,7 @@ State::Params::Params() State::State(Params const & p) : TBase(p), - m_modeInfo(UnknowPosition), + m_modeInfo(UnknownPosition), m_errorRadius(0), m_position(0, 0), m_drawDirection(0.0), @@ -79,12 +79,12 @@ State::Mode State::GetMode() const bool State::IsModeChangeViewport() const { - return !(GetMode() < Follow); + return GetMode() >= Follow; } bool State::IsModeHasPosition() const { - return !(GetMode() < NotFollow); + return GetMode() >= NotFollow; } void State::SwitchToNextMode() @@ -93,11 +93,11 @@ void State::SwitchToNextMode() Mode newMode = currentMode; switch (currentMode) { - case UnknowPosition: + case UnknownPosition: newMode = PendingPosition; break; case PendingPosition: - newMode = UnknowPosition; + newMode = UnknownPosition; break; case NotFollow: newMode = Follow; @@ -106,10 +106,10 @@ void State::SwitchToNextMode() if (TestModeBit(m_modeInfo, KnownDirectionBit)) newMode = RotateAndFollow; else - newMode = UnknowPosition; + newMode = UnknownPosition; break; case RotateAndFollow: - newMode = UnknowPosition; + newMode = UnknownPosition; break; } @@ -123,20 +123,18 @@ void State::RestoreMode() void State::TurnOff() { - SetModeInfo(UnknowPosition); + SetModeInfo(UnknownPosition); setIsVisible(false); invalidate(); } void State::OnLocationUpdate(location::GpsInfo const & info) { - m2::RectD rect = MercatorBounds::MetresToXY(info.m_longitude, info.m_latitude, info.m_horizontalAccuracy); - m2::PointD const center = rect.Center(); - m_position = center; + m_position = rect.Center(); m_errorRadius = rect.SizeX() / 2; setIsVisible(true); @@ -165,14 +163,14 @@ void State::OnCompassUpdate(location::CompassInfo const & info) void State::CallStateModeListeners() { - Mode currentMode = GetMode(); + Mode const currentMode = GetMode(); for (auto it : m_modeListeners) it.second(currentMode); } int State::AddStateModeListener(TStateModeListener const & l) { - int slotID = m_currentSlotID++; + int const slotID = m_currentSlotID++; m_modeListeners[slotID] = l; return slotID; } @@ -190,9 +188,9 @@ void State::CallPositionChangedListeners(m2::PointD const & pt) int State::AddPositionChangedListener(State::TPositionListener const & func) { - int result = m_currentSlotID++; - m_positionListeners[result] = func; - return result; + int const slotID = m_currentSlotID++; + m_positionListeners[slotID] = func; + return slotID; } void State::RemovePositionChangedListener(int slotID) @@ -227,7 +225,7 @@ void State::update() void State::draw(graphics::OverlayRenderer * r, math::Matrix const & m) const { - Mode currentMode = GetMode(); + Mode const currentMode = GetMode(); if (currentMode < NotFollow || !isVisible()) return; @@ -273,8 +271,8 @@ void State::CachePositionArrow() graphics::Icon::Info info("current-position-compas"); graphics::Resource const * res = cacheScreen->fromID(cacheScreen->findInfo(info)); - m2::RectU rect = res->m_texRect; - m2::PointD halfArrowSize(rect.SizeX() / 2.0, rect.SizeY() / 2.0); + m2::RectU const rect = res->m_texRect; + m2::PointD const halfArrowSize(rect.SizeX() / 2.0, rect.SizeY() / 2.0); m_positionArrow.reset(); m_positionArrow.reset(cacheScreen->createDisplayList()); @@ -290,7 +288,7 @@ void State::CachePositionArrow() m2::PointD( halfArrowSize.x, halfArrowSize.y) }; - m2::PointF normal(0.0, 0.0); + m2::PointF const normal(0.0, 0.0); shared_ptr texture = cacheScreen->pipeline(res->m_pipelineID).texture(); m2::PointF texCoords[4] = @@ -356,8 +354,8 @@ void State::FollowCompass() void State::SetModeInfo(uint16_t modeInfo) { - Mode newMode = ExcludeAllBits(modeInfo); - Mode oldMode = GetMode(); + Mode const newMode = ExcludeAllBits(modeInfo); + Mode const oldMode = GetMode(); m_modeInfo = modeInfo; if (newMode != oldMode) { @@ -438,18 +436,18 @@ namespace bool ValidateTransition(State::Mode oldMode, State::Mode newMode) { - if (oldMode == State::UnknowPosition) + if (oldMode == State::UnknownPosition) return newMode == State::PendingPosition; if (oldMode == State::PendingPosition) { - return newMode == State::UnknowPosition || + return newMode == State::UnknownPosition || newMode == State::Follow; } if (oldMode == State::Follow) { - return newMode == State::UnknowPosition || + return newMode == State::UnknownPosition || newMode == State::NotFollow || newMode == State::RotateAndFollow; } @@ -460,7 +458,7 @@ bool ValidateTransition(State::Mode oldMode, State::Mode newMode) if (oldMode == State::RotateAndFollow) { return newMode == State::NotFollow || - newMode == State::UnknowPosition; + newMode == State::UnknownPosition; } return false; @@ -474,30 +472,26 @@ void State::AnimateStateTransition(Mode oldMode, Mode newMode) if (oldMode == PendingPosition && newMode == Follow) { - //TODO animate to position and scale - m2::PointD size(m_errorRadius, m_errorRadius); + m2::PointD const size(m_errorRadius, m_errorRadius); m_framework->ShowRectExVisibleScale(m2::RectD(m_position - size, m_position + size), scales::GetUpperComfortScale()); } else if (oldMode == NotFollow && newMode == Follow) { - // TODO animate to position m_framework->SetViewportCenterAnimated(Position()); } else if (newMode == RotateAndFollow) { FollowCompass(); } - else if (oldMode == RotateAndFollow && newMode == UnknowPosition) + else if (oldMode == RotateAndFollow && newMode == UnknownPosition) { - // TODO rotate viewport on north m_framework->GetAnimator().RotateScreen(m_framework->GetNavigator().Screen().GetAngle(), 0.0); } } void State::AnimateFollow() { - // TODO m_framework->SetViewportCenterAnimated(Position()); FollowCompass(); } diff --git a/map/location_state.hpp b/map/location_state.hpp index 9b912f22e6..3724cbc8eb 100644 --- a/map/location_state.hpp +++ b/map/location_state.hpp @@ -35,14 +35,14 @@ namespace location }; public: - // Do not change the order + // Do not change the order and values enum Mode { - UnknowPosition = 0x0, + UnknownPosition = 0x0, PendingPosition = 0x1, NotFollow = 0x2, - Follow = 0x4, - RotateAndFollow = 0x8, + Follow = 0x3, + RotateAndFollow = 0x4, }; typedef function TStateModeListener; diff --git a/qt/mainwindow.cpp b/qt/mainwindow.cpp index 5a5d693e0c..4ede1f348d 100644 --- a/qt/mainwindow.cpp +++ b/qt/mainwindow.cpp @@ -50,7 +50,10 @@ MainWindow::MainWindow() : m_locationService(CreateDesktopLocationService(*this) { m_pDrawWidget = new DrawWidget(this); shared_ptr locState = m_pDrawWidget->GetFramework().GetLocationState(); - locState->AddStateModeListener([this](location::State::Mode mode) { LocationStateModeChanged(mode);}); + locState->AddStateModeListener([this](location::State::Mode mode) + { + LocationStateModeChanged(mode); + }); CreateNavigationBar(); CreateSearchBarAndPanel(); @@ -179,7 +182,7 @@ void MainWindow::LocationStateModeChanged(location::State::Mode mode) return; } - if (mode == location::State::UnknowPosition) + if (mode == location::State::UnknownPosition) m_locationService->Stop(); m_pMyPositionAction->setIcon(QIcon(":/navig64/location.png"));