Skip to content

Commit

Permalink
refactored Location and Compass observers mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
rachytski authored and biodranik committed Sep 22, 2015
1 parent f81ed7b commit ca29b9e
Show file tree
Hide file tree
Showing 23 changed files with 298 additions and 256 deletions.
1 change: 1 addition & 0 deletions android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
13 changes: 8 additions & 5 deletions android/jni/com/mapswithme/maps/Framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -180,7 +180,6 @@ namespace android

m_doLoadState = false;

m_work.SkipLocationCentering();
m_work.ShowRect(r);
}

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion android/jni/com/mapswithme/maps/Framework.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
104 changes: 104 additions & 0 deletions android/jni/com/mapswithme/maps/LocationState.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
}
13 changes: 3 additions & 10 deletions android/jni/com/mapswithme/maps/MWMActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
49 changes: 0 additions & 49 deletions android/jni/com/mapswithme/maps/MWMApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void onCompassUpdated(long time, double magneticNorth, double trueNorth,
}

@Override
public void onLocationStatusChanged(int status)
public void onLocationError(int errorCode)
{
}

Expand Down
33 changes: 33 additions & 0 deletions android/src/com/mapswithme/maps/LocationState.java
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit ca29b9e

Please sign in to comment.