[new downloader][android] add: Downloading error dialog.

This commit is contained in:
Alexander Marchuk 2016-03-09 14:53:28 +03:00 committed by Sergey Yershov
parent 80690dfa07
commit 0c2cacdb20
8 changed files with 107 additions and 11 deletions

View file

@ -27,11 +27,13 @@ struct TBatchedData
{
TCountryId const m_countryId;
NodeStatus const m_newStatus;
NodeErrorCode const m_errorCode;
bool const m_isLeaf;
TBatchedData(TCountryId const & countryId, NodeStatus const newStatus, bool isLeaf)
TBatchedData(TCountryId const & countryId, NodeStatus const newStatus, NodeErrorCode const errorCode, bool isLeaf)
: m_countryId(countryId)
, m_newStatus(newStatus)
, m_errorCode(errorCode)
, m_isLeaf(isLeaf)
{}
};
@ -349,10 +351,11 @@ static void EndBatchingCallbacks(JNIEnv * env)
{
// Create StorageCallbackData instance…
static jclass batchDataClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/downloader/MapManager$StorageCallbackData");
static jmethodID batchDataCtor = jni::GetConstructorID(env, batchDataClass, "(Ljava/lang/String;IZ)V");
static jmethodID batchDataCtor = jni::GetConstructorID(env, batchDataClass, "(Ljava/lang/String;IIZ)V");
jni::TScopedLocalRef const item(env, env->NewObject(batchDataClass, batchDataCtor, jni::ToJavaString(env, dataItem.m_countryId),
static_cast<jint>(dataItem.m_newStatus),
static_cast<jint>(dataItem.m_errorCode),
dataItem.m_isLeaf));
// …and put it into the resulting list
env->CallBooleanMethod(list.get(), arrayListAdd, item.get());
@ -425,7 +428,7 @@ static void StatusChangedCallback(shared_ptr<jobject> const & listenerRef, TCoun
NodeAttrs attrs;
GetStorage().GetNodeAttrs(countryId, attrs);
TBatchedData const data(countryId, attrs.m_status, (attrs.m_mwmCounter == 1));
TBatchedData const data(countryId, attrs.m_status, attrs.m_error, (attrs.m_mwmCounter == 1));
g_batchedCallbackData[*listenerRef].push_back(move(data));
if (!g_isBatched)

View file

@ -47,6 +47,7 @@
android:layout_marginBottom="@dimen/margin_base"
android:textAppearance="@style/MwmTextAppearance.Body3"
android:textColor="@color/base_red"
android:gravity="center_horizontal"
tools:text="Some error occured! Please, be patient, man."/>
<TextView

View file

@ -182,13 +182,22 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity
public void onStatusChanged(List<MapManager.StorageCallbackData> data)
{
for (MapManager.StorageCallbackData item : data)
if (item.isLeafNode && item.newStatus == CountryItem.STATUS_DONE)
{
if (!item.isLeafNode)
continue;
switch (item.newStatus)
{
case CountryItem.STATUS_DONE:
mAreResourcesDownloaded = true;
showMap();
return;
case CountryItem.STATUS_FAILED:
MapManager.showError(DownloadResourcesActivity.this, item);
return;
}
}
}
@Override

View file

@ -57,6 +57,11 @@ public final class Notifier
Statistics.INSTANCE.trackEvent(Statistics.EventName.DOWNLOAD_COUNTRY_NOTIFICATION_SHOWN);
}
public static void cancelDownloadFailed()
{
getNotificationManager().cancel(ID_DOWNLOAD_FAILED);
}
public static void cancelDownloadSuggest()
{
getNotificationManager().cancel(ID_DOWNLOAD_NEW_COUNTRY);

View file

@ -16,14 +16,13 @@ public final class CountryItem implements Comparable<CountryItem>
// Must correspond to NodeStatus in storage_defines.hpp
public static final int STATUS_UNKNOWN = 0;
public static final int STATUS_PROGRESS = 1; // Downloading a new mwm or updating an old one.
public static final int STATUS_ENQUEUED = 2; // An mwm is waiting for downloading in the queue.
public static final int STATUS_FAILED = 3; // An error happened while downloading
public static final int STATUS_UPDATABLE = 4; // An update for a downloaded mwm is ready according to counties.txt.
public static final int STATUS_DONE = 5; // Downloaded mwm(s) is up to date. No need to update it.
public static final int STATUS_DOWNLOADABLE = 6; // An mwm can be downloaded but not downloaded yet.
public static final int STATUS_PARTLY = 7; // Leafs of group node has a mix of NotDownloaded and OnDisk status.
public static final int STATUS_PARTLY = 7; // Leafs of group node has a mix of STATUS_DONE and STATUS_DOWNLOADABLE.
// Must correspond to NodeErrorCode in storage_defines.hpp
public static final int ERROR_NONE = 0;
@ -64,6 +63,7 @@ public final class CountryItem implements Comparable<CountryItem>
return id.hashCode();
}
@SuppressWarnings("SimplifiableIfStatement")
@Override
public boolean equals(Object other)
{

View file

@ -25,6 +25,7 @@ import java.util.Stack;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.maps.widget.WheelProgressView;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.Graphics;
@ -178,6 +179,13 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
@Override
public void onStatusChanged(List<MapManager.StorageCallbackData> data)
{
for (MapManager.StorageCallbackData item : data)
if (item.isLeafNode && item.newStatus == CountryItem.STATUS_FAILED)
{
MapManager.showError(mActivity, item);
break;
}
if (mSearchResultsMode)
{
for (MapManager.StorageCallbackData item : data)
@ -224,6 +232,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
break;
case CountryItem.STATUS_FAILED:
Notifier.cancelDownloadFailed();
MapManager.nativeRetry(mItem.id);
break;
@ -373,6 +382,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
break;
case CountryItem.STATUS_DOWNLOADABLE:
case CountryItem.STATUS_PARTLY:
iconRes = R.drawable.ic_downloader_download;
break;
@ -511,7 +521,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
private void processData()
{
Collections.<CountryItem>sort(mItems);
Collections.sort(mItems);
collectHeaders();
mCountryIndex.clear();

View file

@ -1,9 +1,16 @@
package com.mapswithme.maps.downloader;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import java.lang.ref.WeakReference;
import java.util.List;
import com.mapswithme.maps.R;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.util.statistics.Statistics;
public final class MapManager
@ -13,12 +20,14 @@ public final class MapManager
{
public final String countryId;
public final int newStatus;
public final int errorCode;
public final boolean isLeafNode;
public StorageCallbackData(String countryId, int newStatus, boolean isLeafNode)
public StorageCallbackData(String countryId, int newStatus, int errorCode, boolean isLeafNode)
{
this.countryId = countryId;
this.newStatus = newStatus;
this.errorCode = errorCode;
this.isLeafNode = isLeafNode;
}
}
@ -44,6 +53,8 @@ public final class MapManager
void onError(int code);
}
private static WeakReference<AlertDialog> sCurrentErrorDialog;
private MapManager() {}
public static void sendErrorStat(String event, int code)
@ -66,6 +77,49 @@ public final class MapManager
Statistics.INSTANCE.trackEvent(event, Statistics.params().add(Statistics.EventParam.TYPE, text));
}
public static void showError(Activity activity, final StorageCallbackData errorData)
{
if (sCurrentErrorDialog != null)
{
AlertDialog dlg = sCurrentErrorDialog.get();
if (dlg != null && dlg.isShowing())
return;
sCurrentErrorDialog = null;
}
@StringRes int text;
switch (errorData.errorCode)
{
case CountryItem.ERROR_NO_INTERNET:
text = R.string.no_internet_connection_detected;
break;
case CountryItem.ERROR_OOM:
text = R.string.not_enough_disk_space;
break;
default:
throw new IllegalArgumentException("Give error can not be displayed: " + errorData.errorCode);
}
AlertDialog dlg = new AlertDialog.Builder(activity)
.setTitle(R.string.country_status_download_failed)
.setMessage(text)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.downloader_retry, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Notifier.cancelDownloadFailed();
MapManager.nativeRetry(errorData.countryId);
}
}).create();
dlg.show();
sCurrentErrorDialog = new WeakReference<>(dlg);
}
/**
* Moves a file from one place to another.
*/

View file

@ -10,6 +10,7 @@ import java.util.List;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.maps.widget.WheelProgressView;
import com.mapswithme.util.Config;
@ -20,6 +21,7 @@ import com.mapswithme.util.statistics.Statistics;
public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
{
private final MwmActivity mActivity;
private final View mFrame;
private final TextView mParent;
private final TextView mTitle;
@ -40,13 +42,21 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
return;
for (MapManager.StorageCallbackData item : data)
if (item.isLeafNode && mCurrentCountry.id.equals(item.countryId))
{
if (!item.isLeafNode)
continue;
if (item.newStatus == CountryItem.STATUS_FAILED)
MapManager.showError(mActivity, item);
if (mCurrentCountry.id.equals(item.countryId))
{
mCurrentCountry.update();
updateState();
return;
}
}
}
@Override
@ -134,8 +144,9 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
UiUtils.showIf(showFrame, mFrame);
}
public OnmapDownloader(final MwmActivity activity)
public OnmapDownloader(MwmActivity activity)
{
mActivity = activity;
mFrame = activity.findViewById(R.id.onmap_downloader);
mParent = (TextView)mFrame.findViewById(R.id.downloader_parent);
mTitle = (TextView)mFrame.findViewById(R.id.downloader_title);
@ -163,13 +174,16 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
{
if (MapManager.nativeIsLegacyMode())
{
activity.showDownloader(false);
mActivity.showDownloader(false);
return;
}
boolean retry = (mCurrentCountry.status == CountryItem.STATUS_FAILED);
if (retry)
{
Notifier.cancelDownloadFailed();
MapManager.nativeRetry(mCurrentCountry.id);
}
else
MapManager.nativeDownload(mCurrentCountry.id);