[android] Replace country name with country index in downloading routine.

This commit is contained in:
vng 2012-12-17 13:50:17 +03:00 committed by Alex Zolotarev
parent 350802ddc9
commit 362a283c93
10 changed files with 92 additions and 84 deletions

View file

@ -23,6 +23,7 @@ LOCAL_HEADER_FILES := \
com/mapswithme/core/logging.hpp \
com/mapswithme/core/render_context.hpp \
com/mapswithme/maps/Framework.hpp \
com/mapswithme/maps/MapStorage.hpp \
com/mapswithme/platform/Platform.hpp \
com/mapswithme/platform/http_thread_android.hpp \
nv_thread/nv_thread.hpp \

View file

@ -1,4 +1,5 @@
#include <jni.h>
#include "Framework.hpp"
#include "MapStorage.hpp"
#include "../../../../../defines.hpp"
@ -18,10 +19,6 @@
#include "../../../../../std/bind.hpp"
#include "../../../../../std/shared_ptr.hpp"
#include "../core/jni_helper.hpp"
#include "Framework.hpp"
using namespace downloader;
@ -309,14 +306,16 @@ extern "C"
my::DeleteFileX((from + files[i]).c_str());
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_findCountryByPos(JNIEnv * env, jobject thiz,
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_findIndexByPos(JNIEnv * env, jobject thiz,
jdouble lat, jdouble lon)
{
string const name = g_framework->GetCountryName(MercatorBounds::LonToX(lon),
MercatorBounds::LatToY(lat));
storage::TIndex const idx = g_framework->GetCountryIndex(lat, lon);
// Important thing. Return 0 if no any country.
return (name.empty() ? 0 : jni::ToJavaString(env, name));
if (idx.IsValid())
return storage::toJava(idx);
else
return 0;
}
}

View file

@ -477,7 +477,7 @@ namespace android
if (!cont.IsReaderExist(SEARCH_INDEX_FILE_TAG))
{
pl::GetNameWithoutExt(v[i]);
out.push_back(m_work.GetCountryName(v[i]));
out.push_back(v[i]);
}
}
catch (RootException const & ex)
@ -489,14 +489,14 @@ namespace android
}
}
string const Framework::GetCountryName(double x, double y) const
storage::TIndex Framework::GetCountryIndex(double lat, double lon) const
{
return m_work.GetCountryName(m2::PointD(x, y));
return m_work.GetCountryIndex(m2::PointD(MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat)));
}
string const Framework::GetCountryCode(double lat, double lon) const
string Framework::GetCountryCode(double lat, double lon) const
{
return m_work.GetCountryCodeByPosition(lat, lon);
return m_work.GetCountryCode(m2::PointD(MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat)));
}
void Framework::AddString(string const & name, string const & value)

View file

@ -102,8 +102,8 @@ namespace android
void GetMapsWithoutSearch(vector<string> & out) const;
string const GetCountryName(double x, double y) const;
string const GetCountryCode(double lat, double lon) const;
storage::TIndex GetCountryIndex(double lat, double lon) const;
string GetCountryCode(double lat, double lon) const;
void AddString(string const & name, string const & value);

View file

@ -1,11 +1,25 @@
///////////////////////////////////////////////////////////////////////////////////
// DownloadUI
///////////////////////////////////////////////////////////////////////////////////
#include "MapStorage.hpp"
#include "Framework.hpp"
#include "../core/jni_helper.hpp"
namespace storage
{
jobject toJava(storage::TIndex const & idx)
{
JNIEnv * env = jni::GetEnv();
jclass klass = env->FindClass("com/mapswithme/maps/MapStorage$Index");
ASSERT(klass, ());
jmethodID 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));
}
}
extern "C"
{
@ -49,20 +63,6 @@ extern "C"
{
return storage::TIndex(group(), country(), region());
}
static jobject toJava(storage::TIndex const & idx)
{
JNIEnv * env = jni::GetEnv();
jclass klass = env->FindClass("com/mapswithme/maps/MapStorage$Index");
ASSERT(klass, ());
jmethodID methodID = env->GetMethodID(klass, "<init>", "(III)V");
ASSERT(methodID, ());
return env->NewObject(klass, methodID,
(jint)idx.m_group, (jint)idx.m_country, (jint)idx.m_region);
}
};
JNIEXPORT jint JNICALL
@ -116,18 +116,17 @@ extern "C"
}
JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_MapStorage_findIndexByName(JNIEnv * env, jobject thiz, jstring name)
Java_com_mapswithme_maps_MapStorage_findIndexByFile(JNIEnv * env, jobject thiz, jstring name)
{
char const * strCountry = env->GetStringUTFChars(name, 0);
char const * s = env->GetStringUTFChars(name, 0);
if (s == 0)
return 0;
if (!strCountry)
return IndexBinding::toJava(storage::TIndex());
// In case of countries, splitted on regions.
string group, map;
storage::CountryInfo::FullName2GroupAndMap(strCountry, group, map);
return IndexBinding::toJava(g_framework->Storage().FindIndexByName(map));
storage::TIndex const idx = g_framework->Storage().FindIndexByFile(s);
if (idx.IsValid())
return storage::toJava(idx);
else
return 0;
}
JNIEXPORT void JNICALL
@ -141,7 +140,7 @@ extern "C"
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "onCountryStatusChanged", "(Lcom/mapswithme/maps/MapStorage$Index;)V");
env->CallVoidMethod(*obj.get(), methodID, IndexBinding::toJava(idx));
env->CallVoidMethod(*obj.get(), methodID, storage::toJava(idx));
}
void ReportCountryProgress(shared_ptr<jobject> const & obj, storage::TIndex const & idx, pair<int64_t, int64_t> const & p)
@ -152,7 +151,7 @@ extern "C"
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "onCountryProgress", "(Lcom/mapswithme/maps/MapStorage$Index;JJ)V");
env->CallVoidMethod(*obj.get(), methodID, IndexBinding::toJava(idx), current, total);
env->CallVoidMethod(*obj.get(), methodID, storage::toJava(idx), current, total);
}
JNIEXPORT jint JNICALL

View file

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

View file

@ -15,6 +15,7 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.mapswithme.maps.MapStorage.Index;
import com.mapswithme.maps.location.LocationService;
import com.mapswithme.util.ConnectionState;
@ -41,7 +42,7 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
private Button mButton = null;
private CheckBox mDownloadCountryCheckBox = null;
private LocationService mLocationService = null;
private String mCountryName = null;
private Index mCountryIndex = null;
private void setDownloadMessage(int bytesToDownload)
{
@ -238,26 +239,19 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
{
if (result == ERR_NO_MORE_FILES)
{
if (mCountryName != null && mDownloadCountryCheckBox.isChecked())
if (mCountryIndex != null && mDownloadCountryCheckBox.isChecked())
{
mDownloadCountryCheckBox.setVisibility(View.GONE);
mLocationMsgView.setVisibility(View.GONE);
mMsgView.setText(String.format(getString(R.string.downloading_country_can_proceed),
mCountryName));
mMapStorage.countryName(mCountryIndex)));
MapStorage.Index idx = mMapStorage.findIndexByName(mCountryName);
mProgress.setMax((int)mMapStorage.countryRemoteSizeInBytes(mCountryIndex));
mProgress.setProgress(0);
if (idx.isValid())
{
mProgress.setMax((int)mMapStorage.countryRemoteSizeInBytes(idx));
mProgress.setProgress(0);
mMapStorage.downloadCountry(mCountryIndex);
mMapStorage.downloadCountry(idx);
setAction(PROCEED_TO_MAP);
}
else
showMapView();
setAction(PROCEED_TO_MAP);
}
else
showMapView();
@ -411,18 +405,20 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
@Override
public void onLocationUpdated(long time, double lat, double lon, float accuracy)
{
if (mCountryName == null)
if (mCountryIndex == null)
{
Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon);
mCountryName = findCountryByPos(lat, lon);
if (mCountryName != null)
mCountryIndex = findIndexByPos(lat, lon);
if (mCountryIndex != null)
{
mLocationMsgView.setVisibility(View.VISIBLE);
int countryStatus = mMapStorage.countryStatus(mMapStorage.findIndexByName(mCountryName));
final int countryStatus = mMapStorage.countryStatus(mCountryIndex);
final String name = mMapStorage.countryName(mCountryIndex);
if (countryStatus == MapStorage.ON_DISK)
mLocationMsgView.setText(String.format(getString(R.string.download_location_map_up_to_date), mCountryName));
mLocationMsgView.setText(String.format(getString(R.string.download_location_map_up_to_date), name));
else
{
CheckBox checkBox = (CheckBox)findViewById(R.id.download_country_checkbox);
@ -434,12 +430,12 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
if (countryStatus == MapStorage.ON_DISK_OUT_OF_DATE)
{
msgViewText = getString(R.string.download_location_update_map_proposal);
checkBoxText = String.format(getString(R.string.update_country_ask), mCountryName);
checkBoxText = String.format(getString(R.string.update_country_ask), name);
}
else
{
msgViewText = getString(R.string.download_location_map_proposal);
checkBoxText = String.format(getString(R.string.download_country_ask), mCountryName);
checkBoxText = String.format(getString(R.string.download_country_ask), name);
}
mLocationMsgView.setText(msgViewText);
@ -466,6 +462,6 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
private native int getBytesToDownload();
private native boolean isWorldExists(String path);
private native int startNextFileDownload(Object observer);
private native String findCountryByPos(double lat, double lon);
private native Index findIndexByPos(double lat, double lon);
private native void cancelCurrentFile();
}

View file

@ -118,7 +118,7 @@ public class MapStorage
public native void downloadCountry(Index idx);
public native void deleteCountry(Index idx);
public native Index findIndexByName(String name);
public native Index findIndexByFile(String name);
public native void showCountry(Index idx);
@ -160,15 +160,17 @@ public class MapStorage
final Index[] indexes = new Index[maps.length];
for (int i = 0; i < maps.length; ++i)
{
final Index idx = findIndexByName(maps[i]);
final int st = countryStatus(idx);
indexes[i] = null;
if (st == DOWNLOADING || st == IN_QUEUE)
indexes[i] = null;
else
final Index idx = findIndexByFile(maps[i]);
if (idx != null)
{
indexes[i] = idx;
++count;
final int st = countryStatus(idx);
if (st != DOWNLOADING && st != IN_QUEUE)
{
indexes[i] = idx;
++count;
}
}
}

View file

@ -1346,10 +1346,9 @@ void Framework::DeleteOldMaps()
}
*/
string Framework::GetCountryCodeByPosition(double lat, double lon) const
string Framework::GetCountryCode(m2::PointD const & pt) const
{
return GetSearchEngine()->GetCountryCode(m2::PointD(
MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat)));
return GetSearchEngine()->GetCountryCode(pt);
}
gui::Controller * Framework::GetGuiController() const

View file

@ -271,15 +271,16 @@ public:
double lat, double lon, double north,
string & distance, double & azimut);
private:
//@{
storage::TIndex GetCountryIndex(m2::PointD const & pt) const;
public:
string GetCountryName(m2::PointD const & pt) const;
/// @param[in] id Country file name without an extension.
string GetCountryName(string const & id) const;
/// @return country code in ISO 3166-1 alpha-2 format (two small letters) or empty string
string GetCountryCodeByPosition(double lat, double lon) const;
string GetCountryCode(m2::PointD const & pt) const;
//@}
void SetMaxWorldRect();