forked from organicmaps/organicmaps
refactored Location and Compass observers mechanism.
This commit is contained in:
parent
f81ed7bd00
commit
ca29b9e478
23 changed files with 298 additions and 256 deletions
|
@ -32,6 +32,7 @@ LOCAL_SRC_FILES := \
|
|||
com/mapswithme/maps/MWMActivity.cpp \
|
||||
com/mapswithme/maps/MWMApplication.cpp \
|
||||
com/mapswithme/maps/Lifecycle.cpp \
|
||||
com/mapswithme/maps/LocationState.cpp \
|
||||
com/mapswithme/maps/MapStorage.cpp \
|
||||
com/mapswithme/maps/DownloadResourcesActivity.cpp \
|
||||
com/mapswithme/maps/SearchActivity.cpp \
|
||||
|
|
|
@ -60,9 +60,9 @@ namespace android
|
|||
delete m_videoTimer;
|
||||
}
|
||||
|
||||
void Framework::OnLocationStatusChanged(int newStatus)
|
||||
void Framework::OnLocationError(int errorCode)
|
||||
{
|
||||
m_work.OnLocationStatusChanged(static_cast<location::TLocationStatus>(newStatus));
|
||||
m_work.OnLocationError(static_cast<location::TLocationError>(errorCode));
|
||||
}
|
||||
|
||||
void Framework::OnLocationUpdated(uint64_t time, double lat, double lon, float accuracy)
|
||||
|
@ -72,7 +72,7 @@ namespace android
|
|||
info.m_latitude = lat;
|
||||
info.m_longitude = lon;
|
||||
info.m_horizontalAccuracy = accuracy;
|
||||
m_work.OnGpsUpdate(info);
|
||||
m_work.OnLocationUpdate(info);
|
||||
}
|
||||
|
||||
void Framework::OnCompassUpdated(uint64_t timestamp, double magneticNorth, double trueNorth, double accuracy)
|
||||
|
@ -180,7 +180,6 @@ namespace android
|
|||
|
||||
m_doLoadState = false;
|
||||
|
||||
m_work.SkipLocationCentering();
|
||||
m_work.ShowRect(r);
|
||||
}
|
||||
|
||||
|
@ -408,7 +407,11 @@ namespace android
|
|||
{
|
||||
m_doLoadState = false;
|
||||
|
||||
m_work.SkipLocationCentering();
|
||||
shared_ptr<location::State> ls = m_work.GetInformationDisplay().locationState();
|
||||
|
||||
ls->StopCompassFollowing();
|
||||
ls->SetLocationProcessMode(location::ELocationDoNothing);
|
||||
|
||||
m_work.ShowSearchResult(r);
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ namespace android
|
|||
storage::TStatus GetCountryStatus(storage::TIndex const & idx) const;
|
||||
void DeleteCountry(storage::TIndex const & idx);
|
||||
|
||||
void OnLocationStatusChanged(int/* == location::TLocationStatus*/ newStatus);
|
||||
void OnLocationError(int/* == location::TLocationStatus*/ newStatus);
|
||||
void OnLocationUpdated(uint64_t time, double lat, double lon, float accuracy);
|
||||
void OnCompassUpdated(uint64_t time, double magneticNorth, double trueNorth, double accuracy);
|
||||
void UpdateCompassSensor(int ind, float * arr);
|
||||
|
|
104
android/jni/com/mapswithme/maps/LocationState.cpp
Normal file
104
android/jni/com/mapswithme/maps/LocationState.cpp
Normal file
|
@ -0,0 +1,104 @@
|
|||
#include "Framework.hpp"
|
||||
|
||||
#include "../core/jni_helper.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_getCompassProcessMode(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->CompassProcessMode();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_setCompassProcessMode(JNIEnv * env, jobject thiz, jint mode)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
ls->SetCompassProcessMode((location::ECompassProcessMode)mode);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_getLocationProcessMode(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->LocationProcessMode();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_setLocationProcessMode(JNIEnv * env, jobject thiz, jint mode)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->SetLocationProcessMode((location::ELocationProcessMode)mode);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_startCompassFollowing(JNIEnv * env,
|
||||
jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
if (!ls->IsCentered())
|
||||
ls->AnimateToPositionAndEnqueueFollowing();
|
||||
else
|
||||
ls->StartCompassFollowing();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_stopCompassFollowing(JNIEnv * env,
|
||||
jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
ls->StopCompassFollowing();
|
||||
}
|
||||
|
||||
void CompassStatusChanged(int mode, shared_ptr<jobject> const & obj)
|
||||
{
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "OnCompassStatusChanged", "(I)V");
|
||||
jint val = static_cast<jint>(mode);
|
||||
env->CallVoidMethod(*obj.get(), methodID, val);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_addCompassStatusListener(JNIEnv * env, jobject thiz, jobject obj)
|
||||
{
|
||||
location::State::TCompassStatusListener fn = bind(&CompassStatusChanged, _1, jni::make_global_ref(obj));
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->AddCompassStatusListener(fn);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_removeCompassStatusListener(JNIEnv * env, jobject thiz, jint slotID)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
ls->RemoveCompassStatusListener(slotID);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_hasPosition(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->HasPosition();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_hasCompass(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->HasCompass();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_turnOff(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->TurnOff();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_mapswithme_maps_LocationState_isVisible(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->isVisible();
|
||||
}
|
||||
}
|
|
@ -15,10 +15,10 @@
|
|||
extern "C"
|
||||
{
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_MWMActivity_nativeLocationStatusChanged(JNIEnv * env, jobject thiz,
|
||||
int status)
|
||||
Java_com_mapswithme_maps_MWMActivity_nativeOnLocationError(JNIEnv * env, jobject thiz,
|
||||
int errorCode)
|
||||
{
|
||||
g_framework->OnLocationStatusChanged(status);
|
||||
g_framework->OnLocationError(errorCode);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
|
@ -218,11 +218,4 @@ extern "C"
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_MWMActivity_nativeSkipLocationCentering(JNIEnv * env, jobject thiz)
|
||||
{
|
||||
g_framework->NativeFramework()->SkipLocationCentering();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
|
|
@ -78,53 +78,4 @@ extern "C"
|
|||
bool flag = val;
|
||||
(void)Settings::Set(jni::ToNativeString(env, name), flag);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_mapswithme_maps_MWMApplication_nativeIsFollowingCompass(JNIEnv * env,
|
||||
jobject thiz)
|
||||
{
|
||||
location::ECompassProcessMode compassMode = g_framework->NativeFramework()->GetInformationDisplay().locationState()->CompassProcessMode();
|
||||
return compassMode == location::ECompassFollow;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_MWMApplication_nativeStartCompassFollowing(JNIEnv * env,
|
||||
jobject thiz)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
if (!ls->IsCentered())
|
||||
ls->AnimateToPositionAndEnqueueFollowing();
|
||||
else
|
||||
ls->StartCompassFollowing();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_MWMApplication_nativeStopCompassFollowing(JNIEnv * env,
|
||||
jobject thiz)
|
||||
{
|
||||
g_framework->NativeFramework()->GetInformationDisplay().locationState()->StopCompassFollowing();
|
||||
}
|
||||
|
||||
void CompassStatusChanged(int mode, shared_ptr<jobject> const & obj)
|
||||
{
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "OnCompassStatusChanged", "(I)V");
|
||||
jint val = static_cast<jint>(mode);
|
||||
env->CallVoidMethod(*obj.get(), methodID, val);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_mapswithme_maps_MWMApplication_nativeAddCompassStatusListener(JNIEnv * env, jobject thiz, jobject obj)
|
||||
{
|
||||
location::State::TCompassStatusListener fn = bind(&CompassStatusChanged, _1, jni::make_global_ref(obj));
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
return ls->AddCompassStatusListener(fn);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_MWMApplication_nativeRemoveCompassStatusListener(JNIEnv * env, jobject thiz, jint slotID)
|
||||
{
|
||||
shared_ptr<location::State> ls = g_framework->NativeFramework()->GetInformationDisplay().locationState();
|
||||
ls->RemoveCompassStatusListener(slotID);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -401,7 +401,7 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onLocationStatusChanged(int status)
|
||||
public void onLocationError(int errorCode)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
33
android/src/com/mapswithme/maps/LocationState.java
Normal file
33
android/src/com/mapswithme/maps/LocationState.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.mapswithme.maps;
|
||||
|
||||
public class LocationState
|
||||
{
|
||||
/// These values should correspond to values of
|
||||
/// location::ELocationProcessMode defined in map/location_state.hpp
|
||||
public static final int LOCATION_DO_NOTHING = 0;
|
||||
public static final int LOCATION_CENTER_AND_SCALE = 1;
|
||||
public static final int LOCATION_CENTER_ONLY = 2;
|
||||
|
||||
/// These values should correspond to values of
|
||||
/// location::ECompassProcessMode defined in map/location_state.hpp
|
||||
public static final int COMPASS_DO_NOTHING = 0;
|
||||
public static final int COMPASS_FOLLOW = 1;
|
||||
|
||||
public native int getCompassProcessMode();
|
||||
public native void setCompassProcessMode(int mode);
|
||||
|
||||
public native int getLocationProcessMode();
|
||||
public native void setLocationProcessMode(int mode);
|
||||
|
||||
public native void startCompassFollowing();
|
||||
public native void stopCompassFollowing();
|
||||
|
||||
public native int addCompassStatusListener(Object l);
|
||||
public native void removeCompassStatusListener(int slotID);
|
||||
|
||||
public native boolean hasPosition();
|
||||
public native boolean hasCompass();
|
||||
|
||||
public native void turnOff();
|
||||
public native boolean isVisible();
|
||||
}
|
|
@ -37,12 +37,8 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
private MWMApplication mApplication = null;
|
||||
private BroadcastReceiver m_externalStorageReceiver = null;
|
||||
private AlertDialog m_storageDisconnectedDialog = null;
|
||||
private boolean m_shouldStartLocationService = false;
|
||||
private boolean m_hasLocation = false;
|
||||
private boolean m_hasCompass = false;
|
||||
private boolean m_isLocationActive = false;
|
||||
private boolean m_locationWasActiveBeforePause = false;
|
||||
private boolean m_suggestAutoFollowMode = false;
|
||||
private boolean m_locationWasActive = false;
|
||||
private boolean m_isFirstLocation = false;
|
||||
|
||||
private LocationService getLocationService()
|
||||
{
|
||||
|
@ -54,30 +50,69 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
return mApplication.getMapStorage();
|
||||
}
|
||||
|
||||
private LocationState getLocationState()
|
||||
{
|
||||
return mApplication.getLocationState();
|
||||
}
|
||||
|
||||
private void startLocation()
|
||||
{
|
||||
m_isLocationActive = true;
|
||||
getLocationService().startUpdate(this);
|
||||
// Do not turn off the screen while displaying position
|
||||
Utils.automaticIdleScreen(false, getWindow());
|
||||
getLocationState().setLocationProcessMode(LocationState.LOCATION_CENTER_AND_SCALE);
|
||||
getLocationState().setCompassProcessMode(LocationState.COMPASS_DO_NOTHING);
|
||||
m_isFirstLocation = true;
|
||||
resumeLocation();
|
||||
}
|
||||
|
||||
private void stopLocation()
|
||||
{
|
||||
m_hasLocation = false;
|
||||
m_hasCompass = false;
|
||||
m_isLocationActive = false;
|
||||
getLocationState().setLocationProcessMode(LocationState.LOCATION_DO_NOTHING);
|
||||
getLocationState().setCompassProcessMode(LocationState.COMPASS_DO_NOTHING);
|
||||
getLocationState().turnOff();
|
||||
pauseLocation();
|
||||
}
|
||||
|
||||
private void pauseLocation()
|
||||
{
|
||||
getLocationService().stopUpdate(this);
|
||||
// Enable automatic turning screen off while app is idle
|
||||
Utils.automaticIdleScreen(true, getWindow());
|
||||
}
|
||||
|
||||
public void checkShouldStartLocationService()
|
||||
private void resumeLocation()
|
||||
{
|
||||
if (m_shouldStartLocationService)
|
||||
getLocationService().startUpdate(this);
|
||||
// Do not turn off the screen while displaying position
|
||||
Utils.automaticIdleScreen(false, getWindow());
|
||||
}
|
||||
|
||||
public void checkShouldResumeLocationService()
|
||||
{
|
||||
final View v = findViewById(R.id.map_button_myposition);
|
||||
|
||||
LocationState locationState = getLocationState();
|
||||
|
||||
if (v != null)
|
||||
{
|
||||
startLocation();
|
||||
m_shouldStartLocationService = false;
|
||||
if (m_locationWasActive)
|
||||
{
|
||||
resumeLocation();
|
||||
|
||||
if (locationState.getCompassProcessMode() == LocationState.COMPASS_FOLLOW)
|
||||
{
|
||||
locationState.startCompassFollowing();
|
||||
v.setBackgroundResource(R.drawable.myposition_button_follow);
|
||||
v.setSelected(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (locationState.hasPosition())
|
||||
v.setBackgroundResource(R.drawable.myposition_button_found);
|
||||
else
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
|
||||
v.setSelected(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,7 +137,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
public void run()
|
||||
{
|
||||
// Run all checks in main thread after rendering is initialized.
|
||||
checkShouldStartLocationService();
|
||||
checkShouldResumeLocationService();
|
||||
checkMeasurementSystem();
|
||||
checkProVersionAvailable();
|
||||
checkUpdateMaps();
|
||||
|
@ -212,7 +247,9 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
|
||||
public void onMyPositionClicked(View v)
|
||||
{
|
||||
if (!m_isLocationActive)
|
||||
LocationState locationState = mApplication.getLocationState();
|
||||
|
||||
if (!locationState.hasPosition())
|
||||
{
|
||||
/// first set the button state to "searching"
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
|
@ -223,7 +260,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!m_hasCompass)
|
||||
if (!locationState.hasCompass())
|
||||
{
|
||||
stopLocation();
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
|
@ -231,15 +268,15 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
}
|
||||
else
|
||||
{
|
||||
if(!mApplication.nativeIsFollowingCompass())
|
||||
if(locationState.getCompassProcessMode() != LocationState.COMPASS_FOLLOW)
|
||||
{
|
||||
mApplication.nativeStartCompassFollowing();
|
||||
locationState.startCompassFollowing();
|
||||
v.setBackgroundResource(R.drawable.myposition_button_follow);
|
||||
v.setSelected(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
mApplication.nativeStopCompassFollowing();
|
||||
locationState.stopCompassFollowing();
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
v.setSelected(false);
|
||||
stopLocation();
|
||||
|
@ -249,7 +286,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
|
||||
// Store active state of My Position
|
||||
SharedPreferences.Editor prefsEdit = getSharedPreferences(mApplication.getPackageName(), MODE_PRIVATE).edit();
|
||||
prefsEdit.putBoolean(PREFERENCES_MYPOSITION, !m_isLocationActive);
|
||||
prefsEdit.putBoolean(PREFERENCES_MYPOSITION, getLocationState().hasPosition());
|
||||
prefsEdit.commit();
|
||||
}
|
||||
|
||||
|
@ -540,23 +577,15 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
/// @name From Location interface
|
||||
//@{
|
||||
@Override
|
||||
public void onLocationStatusChanged(int newStatus)
|
||||
public void onLocationError(int errorCode)
|
||||
{
|
||||
if (newStatus == LocationService.FIRST_EVENT)
|
||||
{
|
||||
final View v = findViewById(R.id.map_button_myposition);
|
||||
|
||||
v.setBackgroundResource(R.drawable.myposition_button_found);
|
||||
v.setSelected(true);
|
||||
|
||||
m_hasLocation = true;
|
||||
}
|
||||
|
||||
nativeLocationStatusChanged(newStatus);
|
||||
nativeOnLocationError(errorCode);
|
||||
|
||||
// Notify user about turned off location services
|
||||
if (newStatus == LocationService.DISABLED_BY_USER)
|
||||
if (errorCode == LocationService.ERROR_DENIED)
|
||||
{
|
||||
getLocationState().turnOff();
|
||||
|
||||
// Do not show this dialog on Kindle Fire - it doesn't have location services
|
||||
// and even wifi settings can't be opened programmatically
|
||||
if (!Utils.isKindleFire())
|
||||
|
@ -608,13 +637,16 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
}
|
||||
else
|
||||
{
|
||||
if (m_hasLocation)
|
||||
if (getLocationState().hasPosition())
|
||||
{
|
||||
v.setBackgroundResource(R.drawable.myposition_button_found);
|
||||
v.setSelected(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
v.setSelected(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -634,6 +666,16 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
@Override
|
||||
public void onLocationUpdated(long time, double lat, double lon, float accuracy)
|
||||
{
|
||||
if (m_isFirstLocation)
|
||||
{
|
||||
final View v = findViewById(R.id.map_button_myposition);
|
||||
|
||||
v.setBackgroundResource(R.drawable.myposition_button_found);
|
||||
v.setSelected(true);
|
||||
|
||||
m_isFirstLocation = false;
|
||||
}
|
||||
|
||||
nativeLocationUpdated(time, lat, lon, accuracy);
|
||||
}
|
||||
|
||||
|
@ -647,8 +689,6 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
magneticNorth = LocationService.correctAngle(magneticNorth, correction);
|
||||
trueNorth = LocationService.correctAngle(trueNorth, correction);
|
||||
|
||||
m_hasCompass = true;
|
||||
|
||||
nativeCompassUpdated(time, magneticNorth, trueNorth, accuracy);
|
||||
}
|
||||
//@}
|
||||
|
@ -668,20 +708,20 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
|
||||
private void startWatchingCompassStatusUpdate()
|
||||
{
|
||||
m_compassStatusListenerID = mApplication.nativeAddCompassStatusListener(this);
|
||||
m_compassStatusListenerID = mApplication.getLocationState().addCompassStatusListener(this);
|
||||
}
|
||||
|
||||
private void stopWatchingCompassStatusUpdate()
|
||||
{
|
||||
mApplication.nativeRemoveCompassStatusListener(m_compassStatusListenerID);
|
||||
mApplication.getLocationState().removeCompassStatusListener(m_compassStatusListenerID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause()
|
||||
{
|
||||
m_locationWasActiveBeforePause = m_isLocationActive;
|
||||
m_locationWasActive = getLocationState().isVisible();
|
||||
|
||||
stopLocation();
|
||||
pauseLocation();
|
||||
|
||||
stopWatchingExternalStorage();
|
||||
|
||||
|
@ -693,22 +733,6 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
@Override
|
||||
protected void onResume()
|
||||
{
|
||||
final View v = findViewById(R.id.map_button_myposition);
|
||||
|
||||
if (v != null && m_locationWasActiveBeforePause)
|
||||
{
|
||||
m_locationWasActiveBeforePause = false;
|
||||
|
||||
// change button appearance to "looking for position"
|
||||
v.setBackgroundResource(R.drawable.myposition_button_normal);
|
||||
|
||||
// do not move map's viewport to a location when this activity is resumed
|
||||
nativeSkipLocationCentering();
|
||||
|
||||
// and remember to start locationService updates in OnRenderingInitialized
|
||||
m_shouldStartLocationService = true;
|
||||
}
|
||||
|
||||
startWatchingCompassStatusUpdate();
|
||||
|
||||
startWatchingExternalStorage();
|
||||
|
@ -852,7 +876,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
|
||||
private native void nativeDestroy();
|
||||
|
||||
private native void nativeLocationStatusChanged(int newStatus);
|
||||
private native void nativeOnLocationError(int errorCode);
|
||||
private native void nativeLocationUpdated(long time, double lat, double lon, float accuracy);
|
||||
private native void nativeCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy);
|
||||
|
||||
|
@ -860,5 +884,4 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
|
|||
private native void nativeCheckForProVersion(String serverURL);
|
||||
|
||||
private native boolean nativeIsInChina(double lat, double lon);
|
||||
private native void nativeSkipLocationCentering();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ public class MWMApplication extends android.app.Application implements MapStorag
|
|||
private final static String TAG = "MWMApplication";
|
||||
|
||||
private LocationService m_location = null;
|
||||
private LocationState m_locationState = null;
|
||||
private MapStorage m_storage = null;
|
||||
private int m_slotID = 0;
|
||||
|
||||
|
@ -106,6 +107,14 @@ public class MWMApplication extends android.app.Application implements MapStorag
|
|||
return m_location;
|
||||
}
|
||||
|
||||
public LocationState getLocationState()
|
||||
{
|
||||
if (m_locationState == null)
|
||||
m_locationState = new LocationState();
|
||||
|
||||
return m_locationState;
|
||||
}
|
||||
|
||||
public MapStorage getMapStorage()
|
||||
{
|
||||
if (m_storage == null)
|
||||
|
@ -183,11 +192,4 @@ public class MWMApplication extends android.app.Application implements MapStorag
|
|||
/// Dealing with Settings
|
||||
public native boolean nativeGetBoolean(String name, boolean defaultVal);
|
||||
public native void nativeSetBoolean(String name, boolean val);
|
||||
|
||||
public native boolean nativeIsFollowingCompass();
|
||||
public native void nativeStartCompassFollowing();
|
||||
public native void nativeStopCompassFollowing();
|
||||
|
||||
public native int nativeAddCompassStatusListener(Object l);
|
||||
public native void nativeRemoveCompassStatusListener(int slotID);
|
||||
}
|
||||
|
|
|
@ -373,7 +373,7 @@ public class SearchActivity extends ListActivity implements LocationService.List
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onLocationStatusChanged(int status)
|
||||
public void onLocationError(int errorCode)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -27,17 +27,13 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
private static final String TAG = "LocationService";
|
||||
|
||||
/// These constants should correspond to values defined in platform/location.hpp
|
||||
public static final int STOPPED = 0;
|
||||
public static final int STARTED = 1;
|
||||
public static final int FIRST_EVENT = 2;
|
||||
public static final int NOT_SUPPORTED = 3;
|
||||
public static final int DISABLED_BY_USER = 4;
|
||||
public static final int ERROR_DENIED = 0;
|
||||
|
||||
public interface Listener
|
||||
{
|
||||
public void onLocationUpdated(long time, double lat, double lon, float accuracy);
|
||||
public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy);
|
||||
public void onLocationStatusChanged(int status);
|
||||
public void onLocationError(int errorCode);
|
||||
};
|
||||
|
||||
private HashSet<Listener> m_observers = new HashSet<Listener>(10);
|
||||
|
@ -78,11 +74,11 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
|
||||
public Location getLastKnown() { return m_lastLocation; }
|
||||
|
||||
private void notifyStatusChanged(int newStatus)
|
||||
private void notifyOnError(int errorCode)
|
||||
{
|
||||
Iterator<Listener> it = m_observers.iterator();
|
||||
while (it.hasNext())
|
||||
it.next().onLocationStatusChanged(newStatus);
|
||||
it.next().onLocationError(errorCode);
|
||||
}
|
||||
|
||||
private void notifyLocationUpdated(long time, double lat, double lon, float accuracy)
|
||||
|
@ -136,21 +132,17 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
if (ConnectionState.isConnected(m_application)
|
||||
&& ((WifiManager) m_application.getSystemService(Context.WIFI_SERVICE)).isWifiEnabled())
|
||||
{
|
||||
observer.onLocationStatusChanged(STARTED);
|
||||
|
||||
if (m_wifiScanner == null)
|
||||
m_wifiScanner = new WifiLocation();
|
||||
m_wifiScanner.StartScan(m_application, this);
|
||||
}
|
||||
else
|
||||
observer.onLocationStatusChanged(DISABLED_BY_USER);
|
||||
observer.onLocationError(ERROR_DENIED);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_isActive = true;
|
||||
|
||||
observer.onLocationStatusChanged(STARTED);
|
||||
|
||||
Location lastKnown = null;
|
||||
|
||||
for (String provider : enabledProviders)
|
||||
|
@ -186,8 +178,6 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
observer.onLocationStatusChanged(STARTED);
|
||||
}
|
||||
|
||||
public void stopUpdate(Listener observer)
|
||||
|
@ -206,8 +196,6 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
|
||||
m_isActive = false;
|
||||
}
|
||||
|
||||
observer.onLocationStatusChanged(STOPPED);
|
||||
}
|
||||
|
||||
private static final int ONE_MINUTE = 1000 * 60 * 1;
|
||||
|
@ -295,9 +283,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
final long currTime = getBetterLocationTime(l);
|
||||
if (currTime != 0)
|
||||
{
|
||||
if (m_lastLocation == null)
|
||||
notifyStatusChanged(FIRST_EVENT);
|
||||
else
|
||||
if (m_lastLocation != null)
|
||||
calcDirection(l, currTime);
|
||||
|
||||
// Used for more precise compass updates
|
||||
|
|
|
@ -78,17 +78,10 @@ void Framework::RemoveMap(string const & datFile)
|
|||
m_model.RemoveMap(datFile);
|
||||
}
|
||||
|
||||
void Framework::SkipLocationCentering()
|
||||
{
|
||||
m_informationDisplay.locationState()->SkipLocationCentering();
|
||||
}
|
||||
void Framework::OnLocationError(location::TLocationError error)
|
||||
{}
|
||||
|
||||
void Framework::OnLocationStatusChanged(location::TLocationStatus newStatus)
|
||||
{
|
||||
m_informationDisplay.locationState()->OnLocationStatusChanged(newStatus);
|
||||
}
|
||||
|
||||
void Framework::OnGpsUpdate(location::GpsInfo const & info)
|
||||
void Framework::OnLocationUpdate(location::GpsInfo const & info)
|
||||
{
|
||||
#ifdef FIXED_LOCATION
|
||||
location::GpsInfo rInfo(info);
|
||||
|
@ -98,7 +91,7 @@ void Framework::OnGpsUpdate(location::GpsInfo const & info)
|
|||
location::GpsInfo const & rInfo = info;
|
||||
#endif
|
||||
|
||||
m_informationDisplay.locationState()->OnGpsUpdate(rInfo);
|
||||
m_informationDisplay.locationState()->OnLocationUpdate(rInfo);
|
||||
}
|
||||
|
||||
void Framework::OnCompassUpdate(location::CompassInfo const & info)
|
||||
|
|
|
@ -206,9 +206,8 @@ public:
|
|||
|
||||
/// @name GPS location updates routine.
|
||||
//@{
|
||||
void SkipLocationCentering();
|
||||
void OnLocationStatusChanged(location::TLocationStatus newStatus);
|
||||
void OnGpsUpdate(location::GpsInfo const & info);
|
||||
void OnLocationError(location::TLocationError error);
|
||||
void OnLocationUpdate(location::GpsInfo const & info);
|
||||
void OnCompassUpdate(location::CompassInfo const & info);
|
||||
//@}
|
||||
|
||||
|
|
|
@ -85,11 +85,6 @@ namespace location
|
|||
setIsVisible(false);
|
||||
}
|
||||
|
||||
void State::SkipLocationCentering()
|
||||
{
|
||||
m_locationProcessMode = ELocationSkipCentering;
|
||||
}
|
||||
|
||||
ELocationProcessMode State::LocationProcessMode() const
|
||||
{
|
||||
return m_locationProcessMode;
|
||||
|
@ -115,38 +110,11 @@ namespace location
|
|||
CallCompassStatusListeners(mode);
|
||||
}
|
||||
|
||||
void State::OnLocationStatusChanged(location::TLocationStatus newStatus)
|
||||
void State::OnLocationUpdate(location::GpsInfo const & info)
|
||||
{
|
||||
switch (newStatus)
|
||||
{
|
||||
case location::EStarted:
|
||||
|
||||
if (m_locationProcessMode != ELocationSkipCentering)
|
||||
m_locationProcessMode = ELocationCenterAndScale;
|
||||
break;
|
||||
|
||||
case location::EFirstEvent:
|
||||
|
||||
if (m_locationProcessMode != ELocationSkipCentering)
|
||||
{
|
||||
// set centering mode for the first location
|
||||
m_locationProcessMode = ELocationCenterAndScale;
|
||||
SetCompassProcessMode(ECompassDoNothing);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
m_locationProcessMode = ELocationDoNothing;
|
||||
TurnOff();
|
||||
}
|
||||
|
||||
m_framework->Invalidate();
|
||||
}
|
||||
|
||||
void State::OnGpsUpdate(location::GpsInfo const & info)
|
||||
{
|
||||
m2::RectD rect = MercatorBounds::MetresToXY(
|
||||
info.m_longitude, info.m_latitude, info.m_horizontalAccuracy);
|
||||
m2::RectD rect = MercatorBounds::MetresToXY(info.m_longitude,
|
||||
info.m_latitude,
|
||||
info.m_horizontalAccuracy);
|
||||
m2::PointD const center = rect.Center();
|
||||
|
||||
m_hasPosition = true;
|
||||
|
@ -187,6 +155,7 @@ namespace location
|
|||
}
|
||||
|
||||
case ELocationCenterOnly:
|
||||
|
||||
m_framework->SetViewportCenter(center);
|
||||
|
||||
SetIsCentered(true);
|
||||
|
@ -195,11 +164,6 @@ namespace location
|
|||
|
||||
break;
|
||||
|
||||
case ELocationSkipCentering:
|
||||
SetIsCentered(false);
|
||||
m_locationProcessMode = ELocationDoNothing;
|
||||
break;
|
||||
|
||||
case ELocationDoNothing:
|
||||
break;
|
||||
}
|
||||
|
@ -319,7 +283,7 @@ namespace location
|
|||
|
||||
void State::update()
|
||||
{
|
||||
if (isVisible())
|
||||
if (isVisible() && m_hasPosition)
|
||||
{
|
||||
m2::PointD const pxPosition = m_framework->GetNavigator().GtoP(Position());
|
||||
|
||||
|
@ -561,6 +525,7 @@ namespace location
|
|||
void State::StartCompassFollowing()
|
||||
{
|
||||
SetCompassProcessMode(ECompassFollow);
|
||||
SetLocationProcessMode(ELocationCenterOnly);
|
||||
CheckCompassRotation();
|
||||
CheckCompassFollowing();
|
||||
setState(EPressed);
|
||||
|
|
|
@ -33,15 +33,14 @@ namespace location
|
|||
|
||||
enum ELocationProcessMode
|
||||
{
|
||||
ELocationDoNothing,
|
||||
ELocationDoNothing = 0,
|
||||
ELocationCenterAndScale,
|
||||
ELocationCenterOnly,
|
||||
ELocationSkipCentering
|
||||
ELocationCenterOnly
|
||||
};
|
||||
|
||||
enum ECompassProcessMode
|
||||
{
|
||||
ECompassDoNothing,
|
||||
ECompassDoNothing = 0,
|
||||
ECompassFollow
|
||||
};
|
||||
|
||||
|
@ -159,9 +158,7 @@ namespace location
|
|||
|
||||
/// @name GPS location updates routine.
|
||||
//@{
|
||||
void SkipLocationCentering();
|
||||
void OnLocationStatusChanged(location::TLocationStatus newStatus);
|
||||
void OnGpsUpdate(location::GpsInfo const & info);
|
||||
void OnLocationUpdate(location::GpsInfo const & info);
|
||||
void OnCompassUpdate(location::CompassInfo const & info);
|
||||
//@}
|
||||
|
||||
|
|
|
@ -41,12 +41,12 @@ public:
|
|||
|
||||
void OnLocationUpdate(GpsInfo const & info)
|
||||
{
|
||||
m_observer.OnGpsUpdated(info);
|
||||
m_observer.OnLocationUpdated(info);
|
||||
}
|
||||
|
||||
void OnDeniedError()
|
||||
{
|
||||
m_observer.OnLocationStatusChanged(location::EDisabledByUser);
|
||||
m_observer.OnLocationError(location::EDenied);
|
||||
}
|
||||
|
||||
virtual void Start()
|
||||
|
@ -59,14 +59,12 @@ public:
|
|||
else
|
||||
{
|
||||
[m_locationManager startUpdatingLocation];
|
||||
m_observer.OnLocationStatusChanged(location::EStarted);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Stop()
|
||||
{
|
||||
[m_locationManager stopUpdatingLocation];
|
||||
m_observer.OnLocationStatusChanged(location::EStopped);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -9,13 +9,9 @@ namespace location
|
|||
/// after this period we cont position as "too old"
|
||||
static double const POSITION_TIMEOUT_SECONDS = 300.0;
|
||||
|
||||
enum TLocationStatus
|
||||
enum TLocationError
|
||||
{
|
||||
EStopped = 0,
|
||||
EStarted,
|
||||
EFirstEvent, //!< Sent when first valid coorinate is received
|
||||
ENotSupported,
|
||||
EDisabledByUser
|
||||
EDenied
|
||||
};
|
||||
|
||||
enum TLocationSource
|
||||
|
|
|
@ -60,20 +60,15 @@ namespace location
|
|||
PositionFilter m_filter;
|
||||
bool m_reportFirstEvent;
|
||||
|
||||
virtual void OnLocationStatusChanged(location::TLocationStatus newStatus)
|
||||
virtual void OnLocationError(location::TLocationError errorCode)
|
||||
{
|
||||
m_observer.OnLocationStatusChanged(newStatus);
|
||||
m_observer.OnLocationError(errorCode);
|
||||
}
|
||||
|
||||
virtual void OnGpsUpdated(GpsInfo const & info)
|
||||
virtual void OnLocationUpdated(GpsInfo const & info)
|
||||
{
|
||||
if (m_reportFirstEvent)
|
||||
{
|
||||
m_observer.OnLocationStatusChanged(location::EFirstEvent);
|
||||
m_reportFirstEvent = false;
|
||||
}
|
||||
if (m_filter.Passes(info))
|
||||
m_observer.OnGpsUpdated(info);
|
||||
m_observer.OnLocationUpdated(info);
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
|
@ -8,8 +8,8 @@ namespace location
|
|||
class LocationObserver
|
||||
{
|
||||
public:
|
||||
virtual void OnLocationStatusChanged(TLocationStatus newStatus) = 0;
|
||||
virtual void OnGpsUpdated(GpsInfo const & info) = 0;
|
||||
virtual void OnLocationError(TLocationError errorCode) = 0;
|
||||
virtual void OnLocationUpdated(GpsInfo const & info) = 0;
|
||||
};
|
||||
|
||||
class LocationService
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace location
|
|||
// @TODO introduce flags to mark valid values
|
||||
info.m_timestamp = static_cast<double>(time(NULL));
|
||||
info.m_source = location::EGoogle;
|
||||
m_observer.OnGpsUpdated(info);
|
||||
m_observer.OnLocationUpdated(info);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ namespace qt
|
|||
MainWindow::MainWindow()
|
||||
{
|
||||
m_pDrawWidget = new DrawWidget(this);
|
||||
m_isFirstLocation = true;
|
||||
m_locationService.reset(CreateDesktopLocationService(*this));
|
||||
|
||||
CreateNavigationBar();
|
||||
|
@ -301,27 +302,27 @@ void MainWindow::OnAbout()
|
|||
dlg.exec();
|
||||
}
|
||||
|
||||
void MainWindow::OnLocationStatusChanged(location::TLocationStatus newStatus)
|
||||
void MainWindow::OnLocationError(location::TLocationError errorCode)
|
||||
{
|
||||
switch (newStatus)
|
||||
switch (errorCode)
|
||||
{
|
||||
case location::EFirstEvent:
|
||||
m_pMyPositionAction->setIcon(QIcon(":/navig64/location.png"));
|
||||
m_pMyPositionAction->setToolTip(tr("My Position"));
|
||||
break;
|
||||
case location::EDisabledByUser:
|
||||
case location::ENotSupported:
|
||||
case location::EDenied:
|
||||
m_pMyPositionAction->setChecked(false);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
m_pDrawWidget->GetFramework().OnLocationStatusChanged(newStatus);
|
||||
m_pDrawWidget->GetFramework().OnLocationError(errorCode);
|
||||
}
|
||||
|
||||
void MainWindow::OnGpsUpdated(location::GpsInfo const & info)
|
||||
void MainWindow::OnLocationUpdated(location::GpsInfo const & info)
|
||||
{
|
||||
m_pDrawWidget->GetFramework().OnGpsUpdate(info);
|
||||
if (m_isFirstLocation)
|
||||
{
|
||||
m_pMyPositionAction->setIcon(QIcon(":/navig64/location.png"));
|
||||
m_pMyPositionAction->setToolTip(tr("My Position"));
|
||||
m_isFirstLocation = false;
|
||||
}
|
||||
|
||||
m_pDrawWidget->GetFramework().OnLocationUpdate(info);
|
||||
}
|
||||
|
||||
void MainWindow::OnMyPosition()
|
||||
|
@ -330,6 +331,7 @@ void MainWindow::OnMyPosition()
|
|||
{
|
||||
m_pMyPositionAction->setIcon(QIcon(":/navig64/location-search.png"));
|
||||
m_pMyPositionAction->setToolTip(tr("Looking for position..."));
|
||||
m_isFirstLocation = true;
|
||||
m_locationService->Start();
|
||||
}
|
||||
else
|
||||
|
|
|
@ -23,6 +23,7 @@ namespace qt
|
|||
|
||||
QDockWidget * m_Docks[3];
|
||||
|
||||
bool m_isFirstLocation;
|
||||
scoped_ptr<location::LocationService> m_locationService;
|
||||
|
||||
Q_OBJECT
|
||||
|
@ -31,8 +32,8 @@ namespace qt
|
|||
MainWindow();
|
||||
virtual ~MainWindow();
|
||||
|
||||
virtual void OnLocationStatusChanged(location::TLocationStatus newStatus);
|
||||
virtual void OnGpsUpdated(location::GpsInfo const & info);
|
||||
virtual void OnLocationError(location::TLocationError errorCode);
|
||||
virtual void OnLocationUpdated(location::GpsInfo const & info);
|
||||
|
||||
protected:
|
||||
string GetIniFile();
|
||||
|
|
Loading…
Add table
Reference in a new issue