[new downloader][android] tmp: Transfer new downloader JNI code.

This commit is contained in:
Alexander Marchuk 2016-02-02 12:42:26 +03:00 committed by Sergey Yershov
parent e2cda5d723
commit 9e8ee5759f
23 changed files with 436 additions and 294 deletions

View file

@ -64,7 +64,6 @@ LOCAL_HEADER_FILES := \
com/mapswithme/core/ScopedLocalRef.hpp \
com/mapswithme/core/logging.hpp \
com/mapswithme/maps/Framework.hpp \
com/mapswithme/maps/MapStorage.hpp \
com/mapswithme/platform/Platform.hpp \
com/mapswithme/platform/MethodRef.hpp \
com/mapswithme/platform/http_thread_android.hpp \
@ -75,9 +74,6 @@ LOCAL_HEADER_FILES := \
LOCAL_SRC_FILES := \
com/mapswithme/core/jni_helper.cpp \
com/mapswithme/core/logging.cpp \
com/mapswithme/country/country_helper.cpp \
com/mapswithme/country/CountryTree.cpp \
com/mapswithme/country/ActiveCountryTree.cpp \
com/mapswithme/maps/Framework.cpp \
com/mapswithme/maps/bookmarks/data/Bookmark.cpp \
com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp \
@ -88,7 +84,7 @@ LOCAL_SRC_FILES := \
com/mapswithme/maps/LocationState.cpp \
com/mapswithme/maps/LocationHelper.cpp \
com/mapswithme/maps/TrackRecorder.cpp \
com/mapswithme/maps/MapStorage.cpp \
com/mapswithme/maps/MapManager.cpp \
com/mapswithme/maps/DownloadResourcesActivity.cpp \
com/mapswithme/maps/PrivateVariables.cpp \
com/mapswithme/maps/SearchEngine.cpp \

View file

@ -14,7 +14,6 @@ extern JavaVM * GetJVM()
}
// caching is necessary to create class from native threads
jclass g_indexClazz;
jclass g_mapObjectClazz;
jclass g_bookmarkClazz;
@ -28,7 +27,6 @@ extern "C"
jni::InitAssertLog();
JNIEnv * env = jni::GetEnv();
g_indexClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/MapStorage$Index");
g_mapObjectClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/bookmarks/data/MapObject");
g_bookmarkClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/bookmarks/data/Bookmark");
@ -40,7 +38,6 @@ extern "C"
{
g_jvm = 0;
JNIEnv * env = jni::GetEnv();
env->DeleteGlobalRef(g_indexClazz);
env->DeleteGlobalRef(g_mapObjectClazz);
env->DeleteGlobalRef(g_bookmarkClazz);
}

View file

@ -9,7 +9,6 @@
#include "std/string.hpp"
#include "std/shared_ptr.hpp"
extern jclass g_indexClazz;
extern jclass g_mapObjectClazz;
extern jclass g_bookmarkClazz;

View file

@ -1,8 +1,6 @@
#include "Framework.hpp"
#include "MapStorage.hpp"
#include "UserMarkHelper.hpp"
#include "com/mapswithme/core/jni_helper.hpp"
#include "com/mapswithme/country/country_helper.hpp"
#include "com/mapswithme/opengl/androidoglcontextfactory.hpp"
#include "com/mapswithme/platform/Platform.hpp"
@ -67,12 +65,12 @@ Framework::Framework()
{
ASSERT_EQUAL ( g_framework, 0, () );
g_framework = this;
m_activeMapsConnectionID = m_work.GetCountryTree().GetActiveMapLayout().AddListener(this);
//m_activeMapsConnectionID = m_work.GetCountryTree().GetActiveMapLayout().AddListener(this);
}
Framework::~Framework()
{
m_work.GetCountryTree().GetActiveMapLayout().RemoveListener(m_activeMapsConnectionID);
//m_work.GetCountryTree().GetActiveMapLayout().RemoveListener(m_activeMapsConnectionID);
}
void Framework::OnLocationError(int errorCode)
@ -220,7 +218,7 @@ Storage & Framework::Storage()
return m_work.Storage();
}
void Framework::ShowCountry(TIndex const & idx, bool zoomToDownloadButton)
void Framework::ShowCountry(TCountryId const & idx, bool zoomToDownloadButton)
{
if (zoomToDownloadButton)
{
@ -233,10 +231,12 @@ void Framework::ShowCountry(TIndex const & idx, bool zoomToDownloadButton)
m_work.ShowCountry(idx);
}
/* TODO (trashkalmar): remove old downloader's stuff
TStatus Framework::GetCountryStatus(TIndex const & idx) const
{
return m_work.GetCountryStatus(idx);
}
*/
void Framework::Touch(int action, Finger const & f1, Finger const & f2, uint8_t maskedPointer)
{
@ -270,6 +270,7 @@ void Framework::Touch(int action, Finger const & f1, Finger const & f2, uint8_t
m_work.TouchEvent(event);
}
/* TODO (trashkalmar): remove old downloader's stuff
TIndex Framework::GetCountryIndex(double lat, double lon) const
{
return m_work.GetCountryIndex(MercatorBounds::FromLatLon(lat, lon));
@ -279,14 +280,15 @@ string Framework::GetCountryCode(double lat, double lon) const
{
return m_work.GetCountryCode(MercatorBounds::FromLatLon(lat, lon));
}
*/
string Framework::GetCountryNameIfAbsent(m2::PointD const & pt) const
{
/* TODO (trashkalmar): remove old downloader's stuff
TIndex const idx = m_work.GetCountryIndex(pt);
TStatus const status = m_work.GetCountryStatus(idx);
if (status != TStatus::EOnDisk && status != TStatus::EOnDiskOutOfDate)
return m_work.GetCountryName(idx);
else
else*/
return string();
}
@ -375,12 +377,12 @@ void Framework::ShowTrack(int category, int track)
void Framework::SetCountryTreeListener(shared_ptr<jobject> objPtr)
{
m_javaCountryListener = objPtr;
m_work.GetCountryTree().SetListener(this);
//m_work.GetCountryTree().SetListener(this);
}
void Framework::ResetCountryTreeListener()
{
m_work.GetCountryTree().ResetListener();
//m_work.GetCountryTree().ResetListener();
m_javaCountryListener.reset();
}
@ -441,6 +443,7 @@ void Framework::SetupMeasurementSystem()
//////////////////////////////////////////////////////////////////////////////////////////
void Framework::ItemStatusChanged(int childPosition)
{
/* TODO (trashkalmar): remove old downloader's stuff
if (m_javaCountryListener == NULL)
return;
@ -449,9 +452,10 @@ void Framework::ItemStatusChanged(int childPosition)
"onItemStatusChanged", "(I)V");
ASSERT ( methodID, () );
env->CallVoidMethod(*m_javaCountryListener, methodID, childPosition);
env->CallVoidMethod(*m_javaCountryListener, methodID, childPosition);*/
}
/* TODO (trashkalmar): remove old downloader's stuff
void Framework::ItemProgressChanged(int childPosition, LocalAndRemoteSizeT const & sizes)
{
if (m_javaCountryListener == NULL)
@ -518,6 +522,7 @@ void Framework::DownloadingProgressUpdate(ActiveMapsLayout::TGroup const & group
env->CallVoidMethod(*(it->second), methodID, group, position, storage_utils::ToArray(env, progress));
}
}
*/
void Framework::PostDrapeTask(TDrapeTask && task)
{
@ -601,9 +606,9 @@ extern "C"
CallOnMapObjectActivatedListener(obj, mapObject.get());
}
void CallRoutingListener(shared_ptr<jobject> obj, int errorCode, vector<storage::TIndex> const & absentCountries, vector<storage::TIndex> const & absentRoutes)
void CallRoutingListener(shared_ptr<jobject> obj, int errorCode, vector<storage::TCountryId> const & absentCountries, vector<storage::TCountryId> const & absentRoutes)
{
JNIEnv * env = jni::GetEnv();
/*JNIEnv * env = jni::GetEnv();
// cache methodID - it cannot change after class is loaded.
// http://developer.android.com/training/articles/perf-jni.html#jclass_jmethodID_and_jfieldID more details here
static jmethodID const methodId = jni::GetMethodID(env, *obj.get(), "onRoutingEvent",
@ -628,7 +633,7 @@ extern "C"
env->CallVoidMethod(*obj.get(), methodId, errorCode, countriesJava, routesJava);
env->DeleteLocalRef(countriesJava);
env->DeleteLocalRef(countriesJava);*/
}
void CallRouteProgressListener(shared_ptr<jobject> sharedListener, float progress)
@ -1001,19 +1006,20 @@ extern "C"
Java_com_mapswithme_maps_Framework_nativeGetCountryIndex(JNIEnv * env, jobject thiz,
jdouble lat, jdouble lon)
{
/* TODO (trashkalmar): remove old downloader's stuff
TIndex const idx = g_framework->GetCountryIndex(lat, lon);
// Return 0 if no any country.
if (idx.IsValid())
return ToJava(idx);
else
else*/
return 0;
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeShowCountry(JNIEnv * env, jobject thiz, jobject idx, jboolean zoomToDownloadButton)
Java_com_mapswithme_maps_Framework_nativeShowCountry(JNIEnv * env, jobject thiz, jobject countryId, jboolean zoomToDownloadButton)
{
g_framework->ShowCountry(ToNative(idx), (bool) zoomToDownloadButton);
g_framework->ShowCountry(ToNative(countryId), (bool) zoomToDownloadButton);
}
JNIEXPORT void JNICALL
@ -1031,7 +1037,7 @@ extern "C"
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeDownloadCountry(JNIEnv * env, jobject thiz, jobject idx)
{
storage_utils::GetMapLayout().DownloadMap(storage::ToNative(idx), MapOptions::Map);
//storage_utils::GetMapLayout().DownloadMap(storage::ToNative(idx), MapOptions::Map);
}
JNIEXPORT void JNICALL

View file

@ -26,10 +26,11 @@
#include "std/unique_ptr.hpp"
#include "std/cstdint.hpp"
// TODO (trashkalmar): remove old downloader's stuff
namespace android
{
class Framework : public storage::CountryTree::CountryTreeListener,
public storage::ActiveMapsLayout::ActiveMapsListener
class Framework //: public storage::CountryTree::CountryTreeListener,
// public storage::ActiveMapsLayout::ActiveMapsListener
{
private:
drape_ptr<dp::ThreadSafeFactory> m_contextFactory;
@ -41,7 +42,7 @@ namespace android
TListenerMap m_javaActiveMapListeners;
int m_currentSlotID;
int m_activeMapsConnectionID;
//int m_activeMapsConnectionID;
math::LowPassVector<float, 3> m_sensors[2];
double m_lastCompass;
@ -66,8 +67,9 @@ namespace android
storage::Storage & Storage();
void ShowCountry(storage::TIndex const & idx, bool zoomToDownloadButton);
storage::TStatus GetCountryStatus(storage::TIndex const & idx) const;
void ShowCountry(storage::TCountryId const & idx, bool zoomToDownloadButton);
// TODO (trashkalmar): remove old downloader's stuff
//storage::TStatus GetCountryStatus(storage::TIndex const & idx) const;
void OnLocationError(int/* == location::TLocationStatus*/ newStatus);
void OnLocationUpdated(location::GpsInfo const & info);
@ -117,8 +119,9 @@ namespace android
void AddLocalMaps();
void RemoveLocalMaps();
storage::TIndex GetCountryIndex(double lat, double lon) const;
string GetCountryCode(double lat, double lon) const;
// TODO (trashkalmar): remove old downloader's stuff
// storage::TIndex GetCountryIndex(double lat, double lon) const;
// string GetCountryCode(double lat, double lon) const;
string GetCountryNameIfAbsent(m2::PointD const & pt) const;
m2::PointD GetViewportCenter() const;
@ -177,6 +180,7 @@ namespace android
virtual void ItemStatusChanged(int childPosition);
virtual void ItemProgressChanged(int childPosition, storage::LocalAndRemoteSizeT const & sizes);
/* TODO (trashkalmar): remove old downloader's stuff
virtual void CountryGroupChanged(storage::ActiveMapsLayout::TGroup const & oldGroup, int oldPosition,
storage::ActiveMapsLayout::TGroup const & newGroup, int newPosition);
virtual void CountryStatusChanged(storage::ActiveMapsLayout::TGroup const & group, int position,
@ -186,7 +190,7 @@ namespace android
MapOptions const & newOpt);
virtual void DownloadingProgressUpdate(storage::ActiveMapsLayout::TGroup const & group, int position,
storage::LocalAndRemoteSizeT const & progress);
*/
private:
vector<TDrapeTask> m_drapeTasksQueue;
mutex m_drapeQueueMutex;

View file

@ -1,8 +1,6 @@
#include "Framework.hpp"
#include "MapStorage.hpp"
#include "../country/country_helper.hpp"
#include "../core/jni_helper.hpp"
#include "../platform/Platform.hpp"
@ -34,60 +32,61 @@ extern "C"
#pragma clang pop_options
static jobject g_this = nullptr;
// TODO (trashkalmar): Connect to new downloader
// static jobject g_this = nullptr;
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MapFragment_nativeConnectDownloaderListeners(JNIEnv * env, jobject thiz)
{
g_this = env->NewGlobalRef(thiz);
g_framework->NativeFramework()->SetDownloadCountryListener([](TIndex const & idx, int options)
{
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetMethodID(env, g_this, "onDownloadClicked", "(IIII)V");
env->CallVoidMethod(g_this, methodID, idx.m_group, idx.m_country, idx.m_region, options);
});
g_framework->NativeFramework()->SetDownloadCancelListener([](TIndex const & idx)
{
GetMapLayout().CancelDownloading(idx);
});
g_framework->NativeFramework()->SetAutoDownloadListener([](TIndex const & idx)
{
if (g_framework->NeedMigrate())
return;
bool autoDownload = true;
Settings::Get("AutoDownloadEnabled", autoDownload);
if (autoDownload && Platform::ConnectionStatus() == Platform::EConnectionType::CONNECTION_WIFI)
GetMapLayout().DownloadMap(idx, MapOptions::Map);
});
// g_this = env->NewGlobalRef(thiz);
// g_framework->NativeFramework()->SetDownloadCountryListener([](TIndex const & idx, int options)
// {
// JNIEnv * env = jni::GetEnv();
// jmethodID methodID = jni::GetMethodID(env, g_this, "onDownloadClicked", "(IIII)V");
// env->CallVoidMethod(g_this, methodID, idx.m_group, idx.m_country, idx.m_region, options);
// });
//
// g_framework->NativeFramework()->SetDownloadCancelListener([](TIndex const & idx)
// {
// GetMapLayout().CancelDownloading(idx);
// });
//
// g_framework->NativeFramework()->SetAutoDownloadListener([](TIndex const & idx)
// {
// if (g_framework->NeedMigrate())
// return;
//
// bool autoDownload = true;
// Settings::Get("AutoDownloadEnabled", autoDownload);
//
// if (autoDownload && Platform::ConnectionStatus() == Platform::EConnectionType::CONNECTION_WIFI)
// GetMapLayout().DownloadMap(idx, MapOptions::Map);
// });
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MapFragment_nativeDisconnectListeners(JNIEnv * env, jclass clazz)
{
g_framework->NativeFramework()->SetDownloadCountryListener(nullptr);
g_framework->NativeFramework()->SetDownloadCancelListener(nullptr);
g_framework->NativeFramework()->SetAutoDownloadListener(nullptr);
if (g_this)
{
env->DeleteGlobalRef(g_this);
g_this = nullptr;
}
// g_framework->NativeFramework()->SetDownloadCountryListener(nullptr);
// g_framework->NativeFramework()->SetDownloadCancelListener(nullptr);
// g_framework->NativeFramework()->SetAutoDownloadListener(nullptr);
//
// if (g_this)
// {
// env->DeleteGlobalRef(g_this);
// g_this = nullptr;
// }
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MapFragment_nativeDownloadCountry(JNIEnv * env, jclass clazz, jobject idx, jint options)
{
TIndex index = ToNative(idx);
ActiveMapsLayout & layout = storage_utils::GetMapLayout();
if (options == -1)
layout.RetryDownloading(index);
else
layout.DownloadMap(index, storage_utils::ToOptions(options));
// TIndex index = ToNative(idx);
// ActiveMapsLayout & layout = storage_utils::GetMapLayout();
// if (options == -1)
// layout.RetryDownloading(index);
// else
// layout.DownloadMap(index, storage_utils::ToOptions(options));
}
JNIEXPORT void JNICALL

View file

@ -0,0 +1,213 @@
#include "Framework.hpp"
#include "../core/jni_helper.hpp"
#include "platform/mwm_version.hpp"
#include "storage/storage.hpp"
#include "std/bind.hpp"
#include "std/shared_ptr.hpp"
namespace data
{
using namespace storage;
enum ItemCategory : uint32_t
{
NEAR_ME,
DOWNLOADED,
OTHER,
};
enum ItemStatus : uint32_t
{
UPDATABLE,
DOWNLOADABLE,
ENQUEUED,
DONE,
PROGRESS,
FAILED,
};
jclass g_listClass;
jmethodID g_listAddMethod;
jclass g_countryItemClass;
Storage & GetStorage()
{
return g_framework->Storage();
}
void PrepareClassRefs(JNIEnv * env)
{
if (g_listClass)
return;
g_listClass = jni::GetGlobalClassRef(env, "java/util/List");
g_listAddMethod = env->GetMethodID(g_listClass, "add", "(Ljava/lang/Object;)Z");
g_countryItemClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/downloader/CountryItem");
}
string GetLocalizedName(string const & id)
{
// TODO
return id;
}
} // namespace data
extern "C"
{
using namespace storage;
using namespace data;
// static native boolean nativeIsLegacyMode();
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeIsLegacyMode(JNIEnv * env, jclass clazz)
{
// TODO (trashkalmar): use appropriate method
return version::IsSingleMwm(g_framework->Storage().GetCurrentDataVersion());
}
// static @Nullable UpdateInfo nativeGetUpdateInfo();
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeGetUpdateInfo(JNIEnv * env, jclass clazz)
{
// FIXME (trashkalmar): Uncomment after Storage::GetUpdateInfo() is implemented
static Storage::UpdateInfo info = { 0 };
//if (!GetStorage().GetUpdateInfo(info))
// return nullptr;
static jclass const infoClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/downloader/UpdateInfo");
ASSERT(infoClass, (jni::DescribeException()));
static jmethodID const ctor = env->GetMethodID(infoClass, "<init>", "(II)V");
ASSERT(ctor, (jni::DescribeException()));
return env->NewObject(infoClass, ctor, info.m_numberOfMwmFilesToUpdate, info.m_totalUpdateSizeInBytes);
}
static void PutItemsToList(JNIEnv * env, jobject const list, vector<TCountryId> const & children, TCountryId const & parent, function<void (jobject const)> const & callback)
{
static jmethodID const countryItemCtor = env->GetMethodID(g_countryItemClass, "<init>", "()V");
static jfieldID const countryItemFieldId = env->GetFieldID(g_countryItemClass, "id", "Ljava/lang/String;");
static jfieldID const countryItemFieldParentId = env->GetFieldID(g_countryItemClass, "parentId", "Ljava/lang/String;");
static jfieldID const countryItemFieldName = env->GetFieldID(g_countryItemClass, "name", "Ljava/lang/String;");
static jfieldID const countryItemFieldParentName = env->GetFieldID(g_countryItemClass, "parentName", "Ljava/lang/String;");
jstring parentId = jni::ToJavaString(env, parent);
jstring parentName = jni::ToJavaString(env, GetLocalizedName(parent));
for (TCountryId const & child : children)
{
jobject item = env->NewObject(g_countryItemClass, countryItemCtor);
// ID and parent`s ID
jstring id = jni::ToJavaString(env, child);
env->SetObjectField(item, countryItemFieldId, id);
env->SetObjectField(item, countryItemFieldParentId, parentId);
// Localized name and parent`s name
jstring name = jni::ToJavaString(env, GetLocalizedName(child));
env->SetObjectField(item, countryItemFieldName, name);
env->SetObjectField(item, countryItemFieldParentName, parentName);
// Let the caller do special processing
callback(item);
// Put to resulting list
env->CallBooleanMethod(list, g_listAddMethod, item);
// Drop local refs
env->DeleteLocalRef(item);
env->DeleteLocalRef(id);
env->DeleteLocalRef(name);
}
}
// static void nativeListItems(@Nullable String parent, List<CountryItem> result);
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeListItems(JNIEnv * env, jclass clazz, jstring parent, jobject result)
{
PrepareClassRefs(env);
Storage const & storage = GetStorage();
TCountryId const parentId = (parent ? jni::ToNativeString(env, parent) : storage.GetRootId());
if (parent)
{
vector<TCountryId> children;
storage.GetChildren(parentId, children);
PutItemsToList(env, result, children, parentId, [](jobject const item)
{
});
}
else
{
// TODO (trashkalmar): Countries near me
// Downloaded
vector<TCountryId> children;
storage.GetDownloadedChildren(parentId, children);
PutItemsToList(env, result, children, parentId, [](jobject const item)
{
});
//
}
//vector<TCountryId> const children = storage::GetChildren(parentId);
}
// static boolean nativeStartDownload(String countryId);
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeStartDownload(JNIEnv * env, jclass clazz, jstring countryId)
{
// FIXME (trashkalmar): Uncomment after Storage::DownloadNode() is implemented
return true;//GetStorage().DownloadNode(jni::ToNativeString(env, countryId));
}
// static boolean nativeCancelDownload(String countryId);
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeCancelDownload(JNIEnv * env, jclass clazz, jstring countryId)
{
// FIXME (trashkalmar): Uncomment after Storage::DeleteNode() is implemented
return true;//GetStorage().DeleteNode(jni::ToNativeString(env, countryId));
}
static void StatusChangedCallback(shared_ptr<jobject> const & listenerRef, TCountryId const & countryId)
{
JNIEnv * env = jni::GetEnv();
jmethodID const methodID = jni::GetJavaMethodID(env, *listenerRef.get(), "onStatusChanged", "(Ljava/lang/String;)V");
env->CallVoidMethod(*listenerRef.get(), methodID, jni::ToJavaString(env, countryId));
}
static void ProgressChangedCallback(shared_ptr<jobject> const & listenerRef, TCountryId const & countryId, LocalAndRemoteSizeT const & sizes)
{
JNIEnv * env = jni::GetEnv();
jmethodID const methodID = jni::GetJavaMethodID(env, *listenerRef.get(), "onProgress", "(Ljava/lang/String;JJ)V");
env->CallVoidMethod(*listenerRef.get(), methodID, jni::ToJavaString(env, countryId), sizes.first, sizes.second);
}
// static int nativeSubscribe(StorageCallback listener);
JNIEXPORT jint JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeSubscribe(JNIEnv * env, jclass clazz, jobject listener)
{
return GetStorage().Subscribe(bind(&StatusChangedCallback, jni::make_global_ref(listener), _1),
bind(&ProgressChangedCallback, jni::make_global_ref(listener), _1, _2));
}
// static void nativeUnsubscribe(int slot);
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeUnsubscribe(JNIEnv * env, jclass clazz, jint slot)
{
GetStorage().Unsubscribe(slot);
}
} // extern "C"

View file

@ -1,178 +0,0 @@
#include "MapStorage.hpp"
#include "Framework.hpp"
#include "com/mapswithme/country/country_helper.hpp"
#include "com/mapswithme/platform/Platform.hpp"
#include "coding/internal/file_data.hpp"
using namespace storage;
namespace
{
::Framework * frm()
{
return g_framework->NativeFramework();
}
Storage & GetStorage()
{
return frm()->Storage();
}
}
extern "C"
{
class IndexBinding
{
private:
shared_ptr<jobject> m_self;
jfieldID m_groupID;
jfieldID m_countryID;
jfieldID m_regionID;
jobject object() const { return *m_self.get(); }
public:
IndexBinding(jobject self) : m_self(jni::make_global_ref(self))
{
jclass klass = jni::GetEnv()->GetObjectClass(object());
m_groupID = jni::GetEnv()->GetFieldID(klass, "mGroup", "I");
m_countryID = jni::GetEnv()->GetFieldID(klass, "mCountry", "I");
m_regionID = jni::GetEnv()->GetFieldID(klass, "mRegion", "I");
}
int group() const
{
return jni::GetEnv()->GetIntField(object(), m_groupID);
}
int country() const
{
return jni::GetEnv()->GetIntField(object(), m_countryID);
}
int region() const
{
return jni::GetEnv()->GetIntField(object(), m_regionID);
}
TIndex const toNative() const
{
return TIndex(group(), country(), region());
}
};
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_MapStorage_countryName(JNIEnv * env, jobject thiz, jobject idx)
{
string const name = GetStorage().CountryName(IndexBinding(idx).toNative());
return env->NewStringUTF(name.c_str());
}
JNIEXPORT jlong JNICALL
Java_com_mapswithme_maps_MapStorage_countryRemoteSizeInBytes(JNIEnv * env, jobject thiz, jobject idx, jint options)
{
ActiveMapsLayout & layout = storage_utils::GetMapLayout();
LocalAndRemoteSizeT const sizes = layout.GetRemoteCountrySizes(ToNative(idx));
switch (storage_utils::ToOptions(options))
{
case MapOptions::Map:
return sizes.first;
case MapOptions::CarRouting:
return sizes.second;
case MapOptions::MapWithCarRouting:
return sizes.first + sizes.second;
case MapOptions::Nothing:
return 0;
}
}
JNIEXPORT jint JNICALL
Java_com_mapswithme_maps_MapStorage_countryStatus(JNIEnv * env, jobject thiz, jobject idx)
{
return static_cast<jint>(g_framework->GetCountryStatus(IndexBinding(idx).toNative()));
}
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_MapStorage_findIndexByFile(JNIEnv * env, jobject thiz, jstring name)
{
char const * s = env->GetStringUTFChars(name, 0);
if (s == 0)
return 0;
TIndex const idx = GetStorage().FindIndexByFile(s);
if (idx.IsValid())
return ToJava(idx);
else
return 0;
}
void ReportChangeCountryStatus(shared_ptr<jobject> const & obj, TIndex const & idx)
{
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetMethodID(env, *obj.get(), "onCountryStatusChanged", "(Lcom/mapswithme/maps/MapStorage$Index;)V");
env->CallVoidMethod(*obj.get(), methodID, ToJava(idx));
}
void ReportCountryProgress(shared_ptr<jobject> const & obj, TIndex const & idx, pair<int64_t, int64_t> const & p)
{
jlong const current = p.first;
jlong const total = p.second;
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetMethodID(env, *obj.get(), "onCountryProgress", "(Lcom/mapswithme/maps/MapStorage$Index;JJ)V");
env->CallVoidMethod(*obj.get(), methodID, ToJava(idx), current, total);
}
JNIEXPORT jint JNICALL
Java_com_mapswithme_maps_MapStorage_subscribe(JNIEnv * env, jobject thiz, jobject obj)
{
LOG(LDEBUG, ("Subscribe on storage"));
return GetStorage().Subscribe(bind(&ReportChangeCountryStatus, jni::make_global_ref(obj), _1),
bind(&ReportCountryProgress, jni::make_global_ref(obj), _1, _2));
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_MapStorage_unsubscribe(JNIEnv * env, jobject thiz, jint slotID)
{
LOG(LDEBUG, ("UnSubscribe from storage"));
GetStorage().Unsubscribe(slotID);
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_MapStorage_nativeMoveFile(JNIEnv * env, jobject thiz, jstring oldFile, jstring newFile)
{
return my::RenameFileX(jni::ToNativeString(env, oldFile), jni::ToNativeString(env, newFile));
}
}
namespace storage
{
jobject ToJava(TIndex const & idx)
{
JNIEnv * env = jni::GetEnv();
jclass const klass = g_indexClazz;
ASSERT(klass, ());
jmethodID static const methodID = env->GetMethodID(klass, "<init>", "(III)V");
ASSERT(methodID, ());
return env->NewObject(klass, methodID,
static_cast<jint>(idx.m_group),
static_cast<jint>(idx.m_country),
static_cast<jint>(idx.m_region));
}
TIndex ToNative(jobject idx)
{
return IndexBinding(idx).toNative();
}
}

View file

@ -1,12 +0,0 @@
#pragma once
#include "storage/index.hpp"
#include "../core/jni_helper.hpp"
namespace storage
{
jobject ToJava(TIndex const & idx);
TIndex ToNative(jobject idx);
}

View file

@ -2,6 +2,7 @@ package com.mapswithme.country;
import com.mapswithme.maps.MapStorage.Index;
@Deprecated
public class ActiveCountryTree
{
private ActiveCountryTree() {}

View file

@ -31,6 +31,7 @@ import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.Statistics;
@Deprecated
abstract class BaseDownloadAdapter extends BaseAdapter
{
static final int TYPE_GROUP = 0;

View file

@ -4,6 +4,7 @@ import android.graphics.Typeface;
import com.mapswithme.maps.MapStorage;
@Deprecated
public class CountryItem
{
public static final CountryItem EMPTY = new CountryItem("", MapStorage.NOT_DOWNLOADED, StorageOptions.MAP_OPTION_MAP_ONLY, false);

View file

@ -1,5 +1,6 @@
package com.mapswithme.country;
@Deprecated
public class CountryTree
{
// interface for listening callbacks from native

View file

@ -3,10 +3,10 @@ package com.mapswithme.country;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
@Deprecated
public class DownloadActivity extends BaseMwmFragmentActivity
{
public static final String EXTRA_OPEN_DOWNLOADED_LIST = "open_downloaded";

View file

@ -8,6 +8,7 @@ import android.widget.TextView;
import com.mapswithme.maps.R;
import com.mapswithme.util.Utils;
@Deprecated
class DownloadAdapter extends BaseDownloadAdapter implements CountryTree.CountryTreeListener
{
private static final int EXTENDED_VIEWS_COUNT = 2; // 3 more views at the top of baseadapter

View file

@ -18,6 +18,7 @@ import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.statistics.Statistics;
@Deprecated
public class DownloadFragment extends BaseMwmListFragment implements View.OnClickListener, ActiveCountryTree.ActiveCountryListener, OnBackPressListener
{
private DownloadAdapter mDownloadAdapter;

View file

@ -11,6 +11,7 @@ import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
@Deprecated
class DownloadedAdapter extends BaseDownloadAdapter implements ActiveCountryTree.ActiveCountryListener
{
private static final String TAG = DownloadedAdapter.class.getSimpleName();

View file

@ -1,5 +1,6 @@
package com.mapswithme.country;
@Deprecated
public class StorageOptions
{
private StorageOptions() {}

View file

@ -0,0 +1,64 @@
package com.mapswithme.maps.downloader;
import java.util.Comparator;
/**
* Class representing a single item in countries hierarchy.
* Fields are filled by native code.
*/
@SuppressWarnings("unused")
public final class CountryItem implements Comparator<CountryItem>
{
// Must correspond to ItemCategory in MapManager.cpp
public static final int CATEGORY_NEAR_ME = 0;
public static final int CATEGORY_DOWNLOADED = 1;
public static final int CATEGORY_OTHER = 2;
// Must correspond to ItemStatus in MapManager.cpp
public static final int STATUS_UPDATABLE = 0;
public static final int STATUS_DOWNLOADABLE = 1;
public static final int STATUS_ENQUEUED = 2;
public static final int STATUS_DONE = 3;
public static final int STATUS_PROGRESS = 4;
public static final int STATUS_FAILED = 5;
public String id;
public String parentId;
public String name;
public String parentName;
public long size;
public int childCount;
public int totalChildCount;
public int category;
public int status;
@Override
public int hashCode()
{
return id.hashCode();
}
@Override
public boolean equals(Object other)
{
if (this == other)
return true;
if (other == null || getClass() != other.getClass())
return false;
return id.equals(((CountryItem)other).id);
}
@Override
public int compare(CountryItem lhs, CountryItem rhs)
{
return lhs.name.compareTo(rhs.name);
}
}

View file

@ -1,13 +1,8 @@
package com.mapswithme.maps.downloader;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import com.mapswithme.util.Constants;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.Utils;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@ -19,6 +14,11 @@ import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import com.mapswithme.util.Constants;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.Utils;
@SuppressWarnings("unused")
class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
{
private static final String TAG = "DownloadChunkTask";
@ -33,16 +33,16 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
private byte[] mPostBody;
private final String mUserAgent;
private final int NOT_SET = -1;
private final int IO_ERROR = -2;
private final int INVALID_URL = -3;
private final int WRITE_ERROR = -4;
private final int FILE_SIZE_CHECK_FAILED = -5;
private static final int NOT_SET = -1;
private static final int IO_ERROR = -2;
private static final int INVALID_URL = -3;
private static final int WRITE_ERROR = -4;
private static final int FILE_SIZE_CHECK_FAILED = -5;
private int mHttpErrorCode = NOT_SET;
private long mDownloadedBytes = 0;
private long mDownloadedBytes;
private static Executor sExecutors = Executors.newFixedThreadPool(4);
private static final Executor sExecutors = Executors.newFixedThreadPool(4);
native boolean onWrite(long httpCallbackID, long beg, byte[] data, long size);
@ -103,7 +103,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
executeOnExecutor(sExecutors, (Void[]) null);
}
static long parseContentRange(String contentRangeValue)
private static long parseContentRange(String contentRangeValue)
{
if (contentRangeValue != null)
{
@ -185,8 +185,8 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
// we've set error code so client should be notified about the error
mHttpErrorCode = FILE_SIZE_CHECK_FAILED;
Log.w(TAG, "Error for " + urlConnection.getURL() +
": Server replied with code " + err +
", aborting download. " + Utils.mapPrettyPrint(requestParams));
": Server replied with code " + err +
", aborting download. " + Utils.mapPrettyPrint(requestParams));
return false;
}
@ -203,8 +203,8 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
// we've set error code so client should be notified about the error
mHttpErrorCode = FILE_SIZE_CHECK_FAILED;
Log.w(TAG, "Error for " + urlConnection.getURL() +
": Invalid file size received (" + contentLength + ") while expecting " + mExpectedFileSize +
". Aborting download.");
": Invalid file size received (" + contentLength + ") while expecting " + mExpectedFileSize +
". Aborting download.");
return false;
}
// @TODO Else display received web page to user - router is redirecting us to some page

View file

@ -8,6 +8,7 @@ import com.mapswithme.maps.R;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.Constants;
@Deprecated
public class DownloadHelper
{
private DownloadHelper() {}
@ -28,7 +29,6 @@ public class DownloadHelper
listener.onDownload();
else
new AlertDialog.Builder(activity)
.setCancelable(true)
.setMessage(String.format(activity.getString(R.string.no_wifi_ask_cellular_download), name))
.setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener()
{
@ -39,15 +39,7 @@ public class DownloadHelper
dlg.dismiss();
}
})
.setNegativeButton(activity.getString(R.string.close), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dlg, int which)
{
dlg.dismiss();
}
})
.create()
.setNegativeButton(activity.getString(R.string.close), null)
.show();
}
}

View file

@ -0,0 +1,38 @@
package com.mapswithme.maps.downloader;
import android.support.annotation.Nullable;
import java.util.List;
public final class MapManager
{
public interface StorageCallback
{
void onStatusChanged(String countryId);
void onProgress(String countryId, long localSize, long remoteSize);
}
private MapManager() {}
// Determines whether the legacy (large MWMs) mode is used.
public static native boolean nativeIsLegacyMode();
// Returns info about updatable data. Returns null on error.
public static native @Nullable UpdateInfo nativeGetUpdateInfo();
// Retrieves list of country items with its status info. Use root as parent if parent is null.
public static native void nativeListItems(@Nullable String root, List<CountryItem> result);
// Enqueue country in downloader.
public static native boolean nativeStartDownload(String countryId);
// Remove downloading country from downloader.
public static native boolean nativeCancelDownload(String countryId);
// Registers callback about storage status changed. Returns slot ID which is should be used to unsubscribe.
public static native int nativeSubscribe(StorageCallback listener);
// Unregisters storage status changed callback.
public static native void nativeUnsubscribe(int slot);
}

View file

@ -0,0 +1,16 @@
package com.mapswithme.maps.downloader;
/**
* Info about data to be updated. Created by native code.
*/
public final class UpdateInfo
{
public final int filesCount;
public final int totalSize;
public UpdateInfo(int filesCount, int totalSize)
{
this.filesCount = filesCount;
this.totalSize = totalSize;
}
}