Merge pull request #2512 from trashkalmar/no-notifications-before-migration

[new downloader][android] fix: Do not show notifications about suggested/outdated countries until migrated.
This commit is contained in:
Alex Zolotarev 2016-03-25 23:25:52 +07:00
commit 626cc4cc51
5 changed files with 94 additions and 107 deletions

View file

@ -8,6 +8,7 @@
#include "base/thread_checker.hpp"
#include "platform/local_country_file_utils.hpp"
#include "platform/mwm_version.hpp"
#include "std/bind.hpp"
@ -94,6 +95,13 @@ Java_com_mapswithme_maps_downloader_MapManager_nativeIsLegacyMode(JNIEnv * env,
return !version::IsSingleMwm(GetStorage().GetCurrentDataVersion());
}
// static native boolean nativeNeedMigrate();
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeNeedMigrate(JNIEnv * env, jclass clazz)
{
return platform::migrate::NeedMigrate();
}
static void FinishMigration(JNIEnv * env)
{
env->DeleteGlobalRef(g_migrationListener);
@ -334,21 +342,6 @@ Java_com_mapswithme_maps_downloader_MapManager_nativeGetAttributes(JNIEnv * env,
UpdateItem(env, item, attrs);
}
// static void nativeGetShortAttributes(CountryItem item);
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeGetShortAttributes(JNIEnv * env, jclass clazz, jobject item)
{
PrepareClassRefs(env);
static jfieldID countryItemFieldId = env->GetFieldID(g_countryItemClass, "id", "Ljava/lang/String;");
jstring id = static_cast<jstring>(env->GetObjectField(item, countryItemFieldId));
NodeStatuses ns;
GetStorage().GetNodeStatuses(jni::ToNativeString(env, id), ns);
UpdateItemShort(env, item, ns.m_status, ns.m_error);
}
// static @Nullable String nativeFindCountry(double lat, double lon);
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeFindCountry(JNIEnv * env, jclass clazz, jdouble lat, jdouble lon)

View file

@ -3,6 +3,7 @@ package com.mapswithme.maps;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.support.annotation.UiThread;
import com.mapswithme.maps.bookmarks.data.DistanceAndAzimut;
import com.mapswithme.maps.bookmarks.data.MapObject;
@ -84,6 +85,7 @@ public class Framework
public static native void nativeRemoveMapObjectListener();
@UiThread
public static native String nativeGetOutdatedCountriesString();
public static native boolean nativeIsDataVersionChanged();

View file

@ -3,11 +3,13 @@ package com.mapswithme.maps.background;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.downloader.MapManager;
import com.mapswithme.util.ConnectionState;
import static com.mapswithme.maps.MwmApplication.prefs;
public class ConnectivityChangedReceiver extends BroadcastReceiver
{
private static final String DOWNLOAD_UPDATE_TIMESTAMP = "DownloadOrUpdateTimestamp";
@ -16,20 +18,21 @@ public class ConnectivityChangedReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
{
if (ConnectionState.isWifiConnected())
onWiFiConnected(context);
}
if (!ConnectionState.isWifiConnected() || MapManager.nativeNeedMigrate())
return;
public void onWiFiConnected(Context context)
{
final SharedPreferences prefs = MwmApplication.prefs();
final long lastEventTimestamp = prefs.getLong(DOWNLOAD_UPDATE_TIMESTAMP, 0);
final long lastEventTimestamp = prefs().getLong(DOWNLOAD_UPDATE_TIMESTAMP, 0);
if (System.currentTimeMillis() - lastEventTimestamp > MIN_EVENT_DELTA_MILLIS)
{
prefs.edit().putLong(DOWNLOAD_UPDATE_TIMESTAMP, System.currentTimeMillis()).apply();
WorkerService.startActionDownload(context);
WorkerService.startActionCheckUpdate(context);
prefs().edit()
.putLong(DOWNLOAD_UPDATE_TIMESTAMP, System.currentTimeMillis())
.apply();
MwmApplication.get().initNativeCore();
MapManager.checkUpdates();
WorkerService.startActionCheckLocation(context);
}
}
}

View file

@ -6,43 +6,32 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.text.TextUtils;
import com.mapswithme.maps.Framework;
import java.util.concurrent.CountDownLatch;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.downloader.MapManager;
import com.mapswithme.maps.editor.Editor;
import com.mapswithme.util.LocationUtils;
import com.mapswithme.util.concurrency.UiThread;
public class WorkerService extends IntentService
{
private static final String ACTION_CHECK_UPDATE = "com.mapswithme.maps.action.update";
private static final String ACTION_DOWNLOAD_COUNTRY = "com.mapswithme.maps.action.download_country";
private static final String ACTION_CHECK_LOCATIION = "com.mapswithme.maps.action.check_location";
private static final String ACTION_UPLOAD_OSM_CHANGES = "com.mapswithme.maps.action.upload_osm_changes";
private static final SharedPreferences PREFS = MwmApplication.prefs();
/**
* Starts this service to check map updates available with the given parameters. If the
* service is already performing a task this action will be queued.
*/
static void startActionCheckUpdate(Context context)
{
Intent intent = new Intent(context, WorkerService.class);
intent.setAction(ACTION_CHECK_UPDATE);
context.startService(intent);
}
/**
* Starts this service to check if map download for current location is available. If the
* service is already performing a task this action will be queued.
*/
static void startActionDownload(Context context)
static void startActionCheckLocation(Context context)
{
final Intent intent = new Intent(context, WorkerService.class);
intent.setAction(WorkerService.ACTION_DOWNLOAD_COUNTRY);
intent.setAction(WorkerService.ACTION_CHECK_LOCATIION);
context.startService(intent);
}
@ -77,82 +66,71 @@ public class WorkerService extends IntentService
switch (action)
{
case ACTION_CHECK_UPDATE:
handleActionCheckUpdate();
break;
case ACTION_DOWNLOAD_COUNTRY:
case ACTION_CHECK_LOCATIION:
handleActionCheckLocation();
break;
case ACTION_UPLOAD_OSM_CHANGES:
handleActionUploadOsmChanges();
break;
}
}
}
private static void handleActionCheckUpdate()
{
if (!Framework.nativeIsDataVersionChanged() || MapManager.nativeIsLegacyMode())
return;
final String countriesToUpdate = Framework.nativeGetOutdatedCountriesString();
if (!TextUtils.isEmpty(countriesToUpdate))
Notifier.notifyUpdateAvailable(countriesToUpdate);
// We are done with current version
Framework.nativeUpdateSavedDataVersion();
private static void handleActionCheckLocation()
{
if (!checkLocationDelayed(0))
checkLocationDelayed(60000); // 1 minute delay
}
private void handleActionCheckLocation()
private static boolean checkLocationDelayed(long delay)
{
if (MapManager.nativeIsLegacyMode())
return;
final long delayMillis = 60000; // 60 seconds
boolean isLocationValid = processLocation();
if (!isLocationValid)
class Holder
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
processLocation();
}
}, delayMillis);
final CountDownLatch hook = new CountDownLatch(1);
boolean result;
}
final Holder holder = new Holder();
UiThread.runLater(new Runnable()
{
@Override
public void run()
{
holder.result = processLocation();
// Release awaiting caller
holder.hook.countDown();
}
}, delay);
try
{
holder.hook.await();
}
catch (InterruptedException ignored) {}
return holder.result;
}
private void handleActionUploadOsmChanges()
private static void handleActionUploadOsmChanges()
{
Editor.uploadChanges();
}
/**
* Adds notification if current location isnt expired.
*
* @return whether notification was added
*/
private boolean processLocation()
@android.support.annotation.UiThread
private static boolean processLocation()
{
final LocationManager manager = (LocationManager) getApplication().getSystemService(Context.LOCATION_SERVICE);
final LocationManager manager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE);
final Location l = manager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (l != null && !LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG))
{
placeDownloadNotification(l);
return true;
}
if (l == null || LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG))
return false;
return false;
}
/**
* Adds notification with download country suggest.
*/
private static void placeDownloadNotification(Location l)
{
final String country = MapManager.nativeFindCountry(l.getLatitude(), l.getLongitude());
String country = MapManager.nativeFindCountry(l.getLatitude(), l.getLongitude());
if (TextUtils.isEmpty(country))
return;
return false;
final String lastNotification = PREFS.getString(country, null);
if (lastNotification != null)
@ -161,10 +139,11 @@ public class WorkerService extends IntentService
final long timeStamp = Long.valueOf(lastNotification);
final long outdatedMillis = 180L * 24 * 60 * 60 * 1000;
if (System.currentTimeMillis() - timeStamp < outdatedMillis)
return;
return true;
}
Notifier.notifyDownloadSuggest(country, MwmApplication.get().getString(R.string.download_location_country, country), country);
PREFS.edit().putString(country, String.valueOf(System.currentTimeMillis())).apply();
return true;
}
}

View file

@ -4,15 +4,19 @@ import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.UiThread;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import java.lang.ref.WeakReference;
import java.util.List;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.util.statistics.Statistics;
@UiThread
public final class MapManager
{
@SuppressWarnings("unused")
@ -120,6 +124,18 @@ public final class MapManager
sCurrentErrorDialog = new WeakReference<>(dlg);
}
public static void checkUpdates()
{
if (!Framework.nativeIsDataVersionChanged())
return;
String countriesToUpdate = Framework.nativeGetOutdatedCountriesString();
if (!TextUtils.isEmpty(countriesToUpdate))
Notifier.notifyUpdateAvailable(countriesToUpdate);
Framework.nativeUpdateSavedDataVersion();
}
/**
* Moves a file from one place to another.
*/
@ -135,6 +151,11 @@ public final class MapManager
*/
public static native boolean nativeIsLegacyMode();
/**
* Quickly determines if the migration is needed. In the most cases you should use {@link #nativeIsLegacyMode()} instead.
*/
public static native boolean nativeNeedMigrate();
/**
* Performs migration from old (large MWMs) mode.
* @return Name of the country to be loaded during the prefetch.
@ -183,17 +204,6 @@ public final class MapManager
*/
public static native void nativeGetAttributes(CountryItem item);
/**
* Sets following attributes of the given {@code item}:
* <pre>
* <ul>
* <li>status;</li>
* <li>errorCode</li>
* </ul>
* </pre>
*/
public static native void nativeGetShortAttributes(CountryItem item);
/**
* Returns country ID corresponding to given coordinates or {@code null} on error.
*/