forked from organicmaps/organicmaps-tmp
[android] Refactoring.
This commit is contained in:
parent
746039fd95
commit
1144dfcd46
4 changed files with 54 additions and 102 deletions
|
@ -77,7 +77,7 @@ public class MwmApplication extends android.app.Application implements ActiveCou
|
|||
if (newStatus == MapStorage.DOWNLOAD_FAILED)
|
||||
{
|
||||
CountryItem item = ActiveCountryTree.getCountryItem(group, position);
|
||||
Notifier.placeDownloadFailed(ActiveCountryTree.getCoreIndex(group, position), item.getName());
|
||||
Notifier.notifyDownloadFailed(ActiveCountryTree.getCoreIndex(group, position), item.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,15 +98,7 @@ public class MwmApplication extends android.app.Application implements ActiveCou
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onCountryOptionsChanged(int group, int position, int newOptions, int requestOptions)
|
||||
{
|
||||
CountryItem item = ActiveCountryTree.getCountryItem(group, position);
|
||||
if (item.getStatus() != MapStorage.ON_DISK)
|
||||
return;
|
||||
|
||||
if (newOptions == requestOptions)
|
||||
Notifier.placeDownloadCompleted(ActiveCountryTree.getCoreIndex(group, position), item.getName());
|
||||
}
|
||||
public void onCountryOptionsChanged(int group, int position, int newOptions, int requestOptions) {}
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
|
|
|
@ -5,113 +5,81 @@ import android.app.NotificationManager;
|
|||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.mapswithme.maps.MapStorage.Index;
|
||||
import com.mapswithme.maps.MwmActivity;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.MapStorage.Index;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
public class Notifier
|
||||
public final class Notifier
|
||||
{
|
||||
private final static int ID_UPDATE_AVAIL = 0x1;
|
||||
private final static int ID_DOWNLOAD_STATUS = 0x3;
|
||||
private final static int ID_DOWNLOAD_FAILED = 0x3;
|
||||
private final static int ID_DOWNLOAD_NEW_COUNTRY = 0x4;
|
||||
private final static int ID_PRO_IS_FREE = 0x5;
|
||||
|
||||
private static final String FREE_PROMO_SHOWN = "ProIsFreePromo";
|
||||
private static final String EXTRA_CONTENT = "ExtraContent";
|
||||
private static final String EXTRA_TITLE = "ExtraTitle";
|
||||
private static final String EXTRA_INTENT = "ExtraIntent";
|
||||
|
||||
public static final String ACTION_NAME = "com.mapswithme.MYACTION";
|
||||
private static IntentFilter mIntentFilter = new IntentFilter(ACTION_NAME);
|
||||
private static final MwmApplication APP = MwmApplication.get();
|
||||
|
||||
private Notifier() { }
|
||||
|
||||
public static NotificationCompat.Builder getBuilder()
|
||||
{
|
||||
return new NotificationCompat.Builder(MwmApplication.get())
|
||||
return new NotificationCompat.Builder(APP)
|
||||
.setAutoCancel(true)
|
||||
.setSmallIcon(R.drawable.ic_notification);
|
||||
}
|
||||
|
||||
private static NotificationManager getNotificationManager()
|
||||
{
|
||||
return (NotificationManager) MwmApplication.get().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
return (NotificationManager) APP.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
public static void placeUpdateAvailable(String forWhat)
|
||||
public static void notifyUpdateAvailable(String countryName)
|
||||
{
|
||||
final String title = MwmApplication.get().getString(R.string.advise_update_maps);
|
||||
final String title = APP.getString(R.string.advise_update_maps);
|
||||
|
||||
final PendingIntent pi = PendingIntent.getActivity(MwmApplication.get(), 0, MwmActivity.createUpdateMapsIntent(),
|
||||
final PendingIntent pi = PendingIntent.getActivity(APP, 0, MwmActivity.createUpdateMapsIntent(),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
final Notification notification = Notifier.getBuilder()
|
||||
.setContentTitle(title)
|
||||
.setContentText(forWhat)
|
||||
.setTicker(title + forWhat)
|
||||
.setContentIntent(pi)
|
||||
.build();
|
||||
|
||||
getNotificationManager().cancel(ID_UPDATE_AVAIL);
|
||||
getNotificationManager().notify(ID_UPDATE_AVAIL, notification);
|
||||
placeNotification(title, countryName, pi, ID_UPDATE_AVAIL);
|
||||
}
|
||||
|
||||
public static void placeDownloadCompleted(Index idx, String name)
|
||||
public static void notifyDownloadFailed(Index idx, String countryName)
|
||||
{
|
||||
final String title = MwmApplication.get().getString(R.string.app_name);
|
||||
final String content = MwmApplication.get().getString(R.string.download_country_success, name);
|
||||
final String title = APP.getString(R.string.app_name);
|
||||
final String content = APP.getString(R.string.download_country_failed, countryName);
|
||||
|
||||
// TODO add complex stacked notification with progress, number of countries and other info.
|
||||
// placeDownloadNotification(title, content, idx);
|
||||
}
|
||||
|
||||
public static void placeDownloadFailed(Index idx, String name)
|
||||
{
|
||||
final String title = MwmApplication.get().getString(R.string.app_name);
|
||||
final String content = MwmApplication.get().getString(R.string.download_country_failed, name);
|
||||
|
||||
placeDownloadNotification(title, content, idx);
|
||||
}
|
||||
|
||||
private static void placeDownloadNotification(String title, String content, Index idx)
|
||||
{
|
||||
final PendingIntent pi = PendingIntent.getActivity(MwmApplication.get(), 0,
|
||||
MwmActivity.createShowMapIntent(MwmApplication.get(), idx, false).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
final PendingIntent pi = PendingIntent.getActivity(APP, 0,
|
||||
MwmActivity.createShowMapIntent(APP, idx, false).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
final Notification notification = getBuilder()
|
||||
.setContentTitle(title)
|
||||
.setContentText(content)
|
||||
.setTicker(title + ": " + content)
|
||||
.setContentIntent(pi)
|
||||
.build();
|
||||
|
||||
getNotificationManager().notify(ID_DOWNLOAD_STATUS, notification);
|
||||
placeNotification(title, content, pi, ID_DOWNLOAD_FAILED);
|
||||
}
|
||||
|
||||
public static void placeDownloadSuggest(String title, String content, Index countryIndex)
|
||||
public static void notifyDownloadSuggest(String title, String content, Index countryIndex)
|
||||
{
|
||||
final PendingIntent pi = PendingIntent.getActivity(MwmApplication.get(), 0,
|
||||
MwmActivity.createShowMapIntent(MwmApplication.get(), countryIndex, true).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
final PendingIntent pi = PendingIntent.getActivity(APP, 0,
|
||||
MwmActivity.createShowMapIntent(APP, countryIndex, true).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
final Notification notification = getBuilder()
|
||||
.setContentTitle(title)
|
||||
.setContentText(content)
|
||||
.setTicker(title + ": " + content)
|
||||
.setContentIntent(pi)
|
||||
.build();
|
||||
|
||||
getNotificationManager().notify(ID_DOWNLOAD_NEW_COUNTRY, notification);
|
||||
placeNotification(title, content, pi, ID_DOWNLOAD_NEW_COUNTRY);
|
||||
|
||||
Statistics.INSTANCE.trackDownloadCountryNotificationShown();
|
||||
}
|
||||
|
||||
private static void placeNotification(String title, String content, PendingIntent pendingIntent, int notificationId)
|
||||
{
|
||||
final Notification notification = getBuilder()
|
||||
.setContentTitle(title)
|
||||
.setContentText(content)
|
||||
.setTicker(title + ": " + content)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build();
|
||||
|
||||
getNotificationManager().notify(notificationId, notification);
|
||||
}
|
||||
|
||||
public static void cancelDownloadSuggest()
|
||||
{
|
||||
getNotificationManager().cancel(ID_DOWNLOAD_NEW_COUNTRY);
|
||||
|
|
|
@ -14,11 +14,6 @@ import com.mapswithme.maps.R;
|
|||
import com.mapswithme.util.LocationUtils;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
||||
/**
|
||||
* An {@link IntentService} subclass for handling asynchronous task requests in
|
||||
* a service on a separate handler thread.
|
||||
* <p/>
|
||||
*/
|
||||
public class WorkerService extends IntentService
|
||||
{
|
||||
public static final String ACTION_CHECK_UPDATE = "com.mapswithme.maps.action.update";
|
||||
|
@ -76,11 +71,12 @@ public class WorkerService extends IntentService
|
|||
|
||||
private void handleActionCheckUpdate()
|
||||
{
|
||||
if (!Framework.nativeIsDataVersionChanged()) return;
|
||||
if (!Framework.nativeIsDataVersionChanged())
|
||||
return;
|
||||
|
||||
final String countriesToUpdate = Framework.nativeGetOutdatedCountriesString();
|
||||
if (!TextUtils.isEmpty(countriesToUpdate))
|
||||
Notifier.placeUpdateAvailable(countriesToUpdate);
|
||||
Notifier.notifyUpdateAvailable(countriesToUpdate);
|
||||
// We are done with current version
|
||||
Framework.nativeUpdateSavedDataVersion();
|
||||
}
|
||||
|
@ -124,8 +120,6 @@ public class WorkerService extends IntentService
|
|||
|
||||
/**
|
||||
* Adds notification with download country suggest.
|
||||
*
|
||||
* @param l
|
||||
*/
|
||||
private void placeDownloadNotification(Location l)
|
||||
{
|
||||
|
@ -144,9 +138,9 @@ public class WorkerService extends IntentService
|
|||
return;
|
||||
}
|
||||
|
||||
Notifier.placeDownloadSuggest(country, String.format(getApplicationContext().getString(R.string.download_location_country), country),
|
||||
Notifier.notifyDownloadSuggest(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();
|
||||
prefs.edit().putString(country, String.valueOf(System.currentTimeMillis())).apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -131,6 +131,10 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
|
|||
//Log.i(TAG, "Start downloading chunk " + getChunkID());
|
||||
|
||||
HttpURLConnection urlConnection = null;
|
||||
/**
|
||||
* TODO improve reliability of connections & handle EOF errors.
|
||||
<a href="http://stackoverflow.com/questions/19258518/android-httpurlconnection-eofexception">asd</a>
|
||||
*/
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -214,20 +218,16 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
|
|||
{
|
||||
Log.d(TAG, "Invalid url: " + mUrl);
|
||||
|
||||
// Notify the client about error
|
||||
mHttpErrorCode = INVALID_URL;
|
||||
return false;
|
||||
} catch (final IOException ex)
|
||||
{
|
||||
Log.d(TAG, "IOException in doInBackground for URL: " + mUrl, ex);
|
||||
|
||||
// Notify the client about error
|
||||
mHttpErrorCode = IO_ERROR;
|
||||
return false;
|
||||
} finally
|
||||
{
|
||||
//Log.i(FRAGMENT_TAG, "End downloading chunk " + getChunkID());
|
||||
|
||||
if (urlConnection != null)
|
||||
urlConnection.disconnect();
|
||||
else
|
||||
|
@ -235,41 +235,39 @@ class DownloadChunkTask extends AsyncTask<Void, byte[], Boolean>
|
|||
}
|
||||
}
|
||||
|
||||
/// Because of timeouts in InpetStream.read (for bad connection),
|
||||
/// try to introduce dynamic buffer size to read in one query.
|
||||
private boolean downloadFromStream(InputStream stream)
|
||||
{
|
||||
// Because of timeouts in InputStream.read (for bad connection),
|
||||
// try to introduce dynamic buffer size to read in one query.
|
||||
final int arrSize[] = {64, 32, 1};
|
||||
int ret = -1;
|
||||
|
||||
for (int i = 0; i < arrSize.length; ++i)
|
||||
for (int size : arrSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
// download chunk from stream
|
||||
ret = downloadFromStreamImpl(stream, arrSize[i] * Constants.KB);
|
||||
ret = downloadFromStreamImpl(stream, size * Constants.KB);
|
||||
break;
|
||||
} catch (final IOException ex)
|
||||
{
|
||||
Log.d(TAG, "IOException in downloadFromStream for chunk size: " + arrSize[i], ex);
|
||||
Log.d(TAG, "IOException in downloadFromStream for chunk size: " + size, ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
// notify the client about error
|
||||
mHttpErrorCode = IO_ERROR;
|
||||
}
|
||||
|
||||
Utils.closeStream(stream);
|
||||
|
||||
return (ret == 0);
|
||||
}
|
||||
|
||||
/// @return
|
||||
/// 0 - download successful;
|
||||
/// 1 - download canceled;
|
||||
/// -1 - some error occurred;
|
||||
/**
|
||||
* @return 0 - download successful;
|
||||
* 1 - download canceled;
|
||||
* -1 - some error occurred;
|
||||
* @throws IOException
|
||||
*/
|
||||
private int downloadFromStreamImpl(InputStream stream, int bufferSize) throws IOException
|
||||
{
|
||||
final byte[] tempBuf = new byte[bufferSize];
|
||||
|
|
Loading…
Add table
Reference in a new issue