Added download country notification.

This commit is contained in:
Dmitry Yunitsky 2014-08-01 11:58:56 +03:00 committed by Alex Zolotarev
parent 7a704babd8
commit 35a34ec213
8 changed files with 178 additions and 25 deletions

View file

@ -778,7 +778,10 @@ public class DownloadResourcesActivity extends MapsWithMeBaseActivity
public boolean processIntent(Intent intent)
{
final Index index = (Index) intent.getSerializableExtra(EXTRA_COUNTRY_INDEX);
mMapTaskToForward = new MWMActivity.ShowCountryTask(index, intent.getBooleanExtra(EXTRA_AUTODOWNLOAD_CONTRY, false));
final boolean autoDownload = intent.getBooleanExtra(EXTRA_AUTODOWNLOAD_CONTRY, false);
if (autoDownload)
Statistics.INSTANCE.trackDownloadCountryNotificationClicked();
mMapTaskToForward = new MWMActivity.ShowCountryTask(index, autoDownload);
return true;
}
}

View file

@ -11,22 +11,17 @@ public class ConnectivityChangedReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
{
if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
throw new IllegalStateException("This class must listen only to CONNECTIVITY_ACTION");
@SuppressWarnings("deprecation")
final NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
final NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (networkInfo != null)
{
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
onWiFiConnectionChanged(networkInfo.isConnected(), context);
}
}
public void onWiFiConnectionChanged(boolean isConnected, Context context)
{
if (isConnected)
{
WorkerService.startActionDownload(context);
WorkerService.startActionCheckUpdate(context);
WorkerService.startActionPushStat(context);
}

View file

@ -12,12 +12,14 @@ import com.mapswithme.maps.MWMActivity;
import com.mapswithme.maps.MapStorage.Index;
import com.mapswithme.maps.R;
import com.mapswithme.maps.guides.GuidesUtils;
import com.mapswithme.util.statistics.Statistics;
public class Notifier
{
private final static int ID_UPDATE_AVAIL = 0x1;
private final static int ID_GUIDE_AVAIL = 0x2;
private final static int ID_DOWNLOAD_STATUS = 0x3;
private final static int ID_DOWNLOAD_NEW_COUNTRY = 0x4;
private final NotificationManager mNotificationManager;
private final Context mContext;
@ -66,7 +68,7 @@ public class Notifier
private void placeDownloadNoti(String title, String content, Index idx)
{
final PendingIntent pi = PendingIntent
.getActivity(mContext, 0, MWMActivity.createShowMapIntent(mContext, idx), Intent.FLAG_ACTIVITY_NEW_TASK);
.getActivity(mContext, 0, MWMActivity.createShowMapIntent(mContext, idx, false), Intent.FLAG_ACTIVITY_NEW_TASK);
final Notification notification = getBuilder()
.setContentTitle(title)
@ -99,4 +101,20 @@ public class Notifier
mNotificationManager.notify(ID_GUIDE_AVAIL, guideNoti);
}
public void placeDownloadSuggest(String title, String content, Index countryIndex)
{
final PendingIntent pi = PendingIntent
.getActivity(mContext, 0, MWMActivity.createShowMapIntent(mContext, countryIndex, true), Intent.FLAG_ACTIVITY_NEW_TASK);
final Notification notification = getBuilder()
.setContentTitle(title)
.setContentText(content)
.setTicker(title + ": " + content)
.setContentIntent(pi)
.build();
mNotificationManager.notify(ID_DOWNLOAD_STATUS, notification);
Statistics.INSTANCE.trackDownloadCountryNotificationShown();
}
}

View file

@ -3,11 +3,20 @@ package com.mapswithme.maps.background;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.text.TextUtils;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.util.LocationUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.StubLogger;
import com.mapswithme.util.statistics.Statistics;
import java.util.Timer;
import java.util.TimerTask;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
@ -18,8 +27,10 @@ public class WorkerService extends IntentService
{
private static final String ACTION_PUSH_STATISTICS = "com.mapswithme.maps.action.stat";
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 Logger mLogger = StubLogger.get();//SimpleLogger.get("MWMWorkerService");
private Logger mLogger = StubLogger.get();
// = SimpleLogger.get("MWMWorkerService");
private Notifier mNotifier;
@ -49,6 +60,19 @@ public class WorkerService extends IntentService
context.startService(intent);
}
/**
* Starts this service to perform check update action with the given parameters. If the
* service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionDownload(Context context)
{
final Intent intent = new Intent(context, WorkerService.class);
intent.setAction(WorkerService.ACTION_DOWNLOAD_COUNTRY);
context.startService(intent);
}
public WorkerService()
{
super("WorkerService");
@ -68,10 +92,18 @@ public class WorkerService extends IntentService
{
final String action = intent.getAction();
if (ACTION_CHECK_UPDATE.equals(action))
switch (action)
{
case ACTION_CHECK_UPDATE:
handleActionCheckUpdate();
else if (ACTION_PUSH_STATISTICS.equals(action))
break;
case ACTION_PUSH_STATISTICS:
handleActionPushStat();
break;
case ACTION_DOWNLOAD_COUNTRY:
handleActionCheckLocation();
break;
}
}
}
@ -95,4 +127,74 @@ public class WorkerService extends IntentService
{
// TODO: add server call here
}
private void handleActionCheckLocation()
{
final long delayMillis = 60000; // 60 seconds
boolean isLocationValid = processLocation();
Statistics.INSTANCE.trackWifiConnected(isLocationValid);
if (!isLocationValid)
{
final Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
Statistics.INSTANCE.trackWifiConnectedAfterDelay(processLocation(), delayMillis);
}
}, delayMillis);
}
}
/**
* Adds notification if current location isnt expired.
*
* @return whether notification was added
*/
private boolean processLocation()
{
final LocationManager manager = (LocationManager) getApplication().getSystemService(Context.LOCATION_SERVICE);
final Location l = manager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (!LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG))
{
placeDownloadNotification(l);
return true;
}
return false;
}
/**
* Adds notification with download country suggest.
*
* @param l
*/
private void placeDownloadNotification(Location l)
{
final Notifier notifier = new Notifier(this);
final String country = Framework.nativeGetCountryNameIfAbsent(l.getLatitude(), l.getLongitude());
if (!TextUtils.isEmpty(country))
{
final SharedPreferences prefs = getApplicationContext().
getSharedPreferences(getApplicationContext().getString(R.string.pref_file_name), Context.MODE_PRIVATE);
final String lastNotification = prefs.getString(country, "");
boolean shouldPlaceNotification = false; // should place notification only if it wasnt displayed for 180 days at least
if (lastNotification.equals(""))
shouldPlaceNotification = true;
else
{
final long timeStamp = Long.valueOf(lastNotification);
final long outdatedMillis = 180L * 24 * 60 * 60 * 1000; // half of year
if (System.currentTimeMillis() - timeStamp > outdatedMillis)
shouldPlaceNotification = true;
}
if (shouldPlaceNotification)
{
notifier.placeDownloadSuggest(country, String.format(getApplicationContext().getString(R.string.download_location_country), country),
Framework.nativeGetCountryIndex(l.getLatitude(), l.getLongitude()));
prefs.edit().putString(country, String.valueOf(System.currentTimeMillis())).commit();
}
}
}
}

View file

@ -316,9 +316,9 @@ public class LocationService implements
}
@Override
public Location getLastGPSLocation()
public Location getLastGpsLocation()
{
return mLocationProvider.getLastGPSLocation();
return mLocationProvider.getLastGpsLocation();
}
private abstract class LocationProvider
@ -344,7 +344,7 @@ public class LocationService implements
mIsActive = false;
}
protected abstract Location getLastGPSLocation();
protected abstract Location getLastGpsLocation();
protected boolean isLocationBetterThanCurrent(Location l)
{
@ -423,7 +423,7 @@ public class LocationService implements
final Location l = findBestLocation(providers);
if (isLocationBetterThanCurrent(l))
emitLocation(l);
else if (mLastLocation != null && !LocationUtils.isExpired(mLastLocation, mLastLocationTime))
else if (mLastLocation != null && !LocationUtils.isExpired(mLastLocation, mLastLocationTime, LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
notifyLocationUpdated(mLastLocation); // notify UI about last valid location
else
mLastLocation = null; // forget about old location
@ -453,7 +453,7 @@ public class LocationService implements
for (final String pr : providers)
{
final Location l = mLocationManager.getLastKnownLocation(pr);
if (l != null && !LocationUtils.isExpired(l, l.getTime()))
if (l != null && !LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
{
if (res == null || res.getAccuracy() > l.getAccuracy())
res = l;
@ -491,10 +491,11 @@ public class LocationService implements
}
@Override
protected Location getLastGPSLocation()
protected Location getLastGpsLocation()
{
return mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
private class GoogleFusedLocationProvider extends LocationProvider
@ -569,7 +570,7 @@ public class LocationService implements
}
@Override
protected Location getLastGPSLocation()
protected Location getLastGpsLocation()
{
return mLocationClient.getLastLocation();
}

View file

@ -40,7 +40,7 @@ public class WifiLocationScanner extends BroadcastReceiver
{
public void onWifiLocationUpdated(Location l);
public Location getLastGPSLocation();
public Location getLastGpsLocation();
}
private Listener mObserver = null;
@ -153,7 +153,7 @@ public class WifiLocationScanner extends BroadcastReceiver
if (statsEnabled)
{
final Location l = mObserver.getLastGPSLocation();
final Location l = mObserver.getLastGpsLocation();
if (l != null)
{
if (wifiHeaderAdded)

View file

@ -9,7 +9,8 @@ public class LocationUtils
{
private LocationUtils() {}
private static final long LOCATION_EXPIRATION_TIME_MILLIS = 5 * 60 * 1000; // 5 minutes
public static final long LOCATION_EXPIRATION_TIME_MILLIS_SHORT = 60 * 1000; // 1 minute
public static final long LOCATION_EXPIRATION_TIME_MILLIS_LONG = 6 * 60 * 60 * 1000; // 6 hours
/**
* Correct compass angles due to display orientation.
@ -59,13 +60,13 @@ public class LocationUtils
return correctAngle(0.0, Math.toRadians(bearing));
}
public static boolean isExpired(Location l, long t)
public static boolean isExpired(Location l, long millis, long expirationMillis)
{
long timeDiff;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
timeDiff = (SystemClock.elapsedRealtimeNanos() - l.getElapsedRealtimeNanos()) / 1000000;
else
timeDiff = System.currentTimeMillis() - t;
return (timeDiff > LOCATION_EXPIRATION_TIME_MILLIS);
timeDiff = System.currentTimeMillis() - millis;
return (timeDiff > expirationMillis);
}
}

View file

@ -179,6 +179,39 @@ public enum Statistics
}
}
public void trackWifiConnected(boolean hasValidLocation)
{
ensureConfigured(MWMApplication.get());
final Event event = getEventBuilder().reset().
setName("Wifi connected").
addParam("Had valid location", String.valueOf(hasValidLocation)).
getEvent();
trackIfEnabled(MWMApplication.get(), event);
}
public void trackWifiConnectedAfterDelay(boolean isLocationExpired, long delayMillis)
{
ensureConfigured(MWMApplication.get());
final Event event = getEventBuilder().reset().
setName("Wifi connected").
addParam("Had valid location", String.valueOf(isLocationExpired)).
addParam("Delay in milliseconds", String.valueOf(delayMillis)).
getEvent();
trackIfEnabled(MWMApplication.get(), event);
}
public void trackDownloadCountryNotificationShown()
{
ensureConfigured(MWMApplication.get());
getEventBuilder().getSimpleNamedEvent("Download country notification shown").post();
}
public void trackDownloadCountryNotificationClicked()
{
ensureConfigured(MWMApplication.get());
getEventBuilder().getSimpleNamedEvent("Download country notification clicked").post();
}
public void startActivity(Activity activity)
{
ensureConfigured(activity);