forked from organicmaps/organicmaps
[android] Implemented bookmark download result transfore from service to activity
This commit is contained in:
parent
f004ad5757
commit
67c0747343
4 changed files with 111 additions and 27 deletions
|
@ -0,0 +1,7 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
public interface BookmarkDownloadHandler
|
||||
{
|
||||
void onAuthorizationRequired();
|
||||
void onPaymentRequired();
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mapswithme.maps.background.AbstractLogBroadcastReceiver;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
|
||||
import com.mapswithme.maps.bookmarks.data.Error;
|
||||
import com.mapswithme.maps.bookmarks.data.Result;
|
||||
import com.mapswithme.maps.dialog.Detachable;
|
||||
|
||||
public class BookmarkDownloadReceiver extends AbstractLogBroadcastReceiver implements Detachable<BookmarkDownloadHandler>
|
||||
{
|
||||
@Nullable
|
||||
private BookmarkDownloadHandler mHandler;
|
||||
|
||||
@Override
|
||||
public void attach(@NonNull BookmarkDownloadHandler handler)
|
||||
{
|
||||
mHandler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detach()
|
||||
{
|
||||
mHandler = null;
|
||||
}
|
||||
|
||||
public void register(@NonNull Application application)
|
||||
{
|
||||
IntentFilter filter = new IntentFilter(SystemDownloadCompletedService.ACTION_DOWNLOAD_COMPLETED);
|
||||
LocalBroadcastManager.getInstance(application).registerReceiver(this, filter);
|
||||
}
|
||||
|
||||
public void unregister(@NonNull Application application)
|
||||
{
|
||||
LocalBroadcastManager.getInstance(application).unregisterReceiver(this);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected String getAssertAction()
|
||||
{
|
||||
return SystemDownloadCompletedService.ACTION_DOWNLOAD_COMPLETED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceiveInternal(@NonNull Context context, @NonNull Intent intent)
|
||||
{
|
||||
OperationStatus status
|
||||
= intent.getParcelableExtra(SystemDownloadCompletedService.EXTRA_DOWNLOAD_STATUS);
|
||||
Result result = status.getResult();
|
||||
if (status.isOk() && result != null && !TextUtils.isEmpty(result.getArchiveId())
|
||||
&& !TextUtils.isEmpty(result.getFilePath()))
|
||||
{
|
||||
|
||||
BookmarkManager.INSTANCE.importFromCatalog(result.getArchiveId(), result.getFilePath());
|
||||
return;
|
||||
}
|
||||
|
||||
Error error = status.getError();
|
||||
if (error == null || mHandler == null)
|
||||
return;
|
||||
|
||||
if (error.isForbidden())
|
||||
mHandler.onAuthorizationRequired();
|
||||
else if (error.isPaymentRequired())
|
||||
mHandler.onPaymentRequired();
|
||||
}
|
||||
}
|
|
@ -32,7 +32,7 @@ import com.mapswithme.util.statistics.Statistics;
|
|||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
||||
public class BookmarksCatalogFragment extends BaseWebViewMwmFragment implements BookmarkDownloadHandler
|
||||
{
|
||||
public static final String EXTRA_BOOKMARKS_CATALOG_URL = "bookmarks_catalog_url";
|
||||
|
||||
|
@ -59,6 +59,8 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
@SuppressWarnings("NullableProblems")
|
||||
@NonNull
|
||||
private BookmarkManager.BookmarksCatalogListener mCatalogListener;
|
||||
@NonNull
|
||||
private final BookmarkDownloadReceiver mDownloadCompleteReceiver = new BookmarkDownloadReceiver();
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState)
|
||||
|
@ -72,6 +74,8 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
public void onStart()
|
||||
{
|
||||
super.onStart();
|
||||
mDownloadCompleteReceiver.attach(this);
|
||||
mDownloadCompleteReceiver.register(getActivity().getApplication());
|
||||
BookmarkManager.INSTANCE.addCatalogListener(mCatalogListener);
|
||||
}
|
||||
|
||||
|
@ -79,6 +83,8 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
public void onStop()
|
||||
{
|
||||
super.onStop();
|
||||
mDownloadCompleteReceiver.detach();
|
||||
mDownloadCompleteReceiver.unregister(getActivity().getApplication());
|
||||
BookmarkManager.INSTANCE.removeCatalogListener(mCatalogListener);
|
||||
}
|
||||
|
||||
|
@ -140,6 +146,20 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationRequired()
|
||||
{
|
||||
Toast.makeText(getActivity(), "Authorization required. Ui coming soon!",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaymentRequired()
|
||||
{
|
||||
Toast.makeText(getActivity(), "Payment required. Ui coming soon!",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private static class WebViewBookmarksCatalogClient extends WebViewClient
|
||||
{
|
||||
private final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
|
||||
|
|
|
@ -8,12 +8,9 @@ import android.net.Uri;
|
|||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.JobIntentService;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
|
||||
import com.mapswithme.maps.bookmarks.data.Error;
|
||||
import com.mapswithme.maps.bookmarks.data.Result;
|
||||
import com.mapswithme.util.Utils;
|
||||
|
@ -23,6 +20,9 @@ import java.io.IOException;
|
|||
|
||||
public class SystemDownloadCompletedService extends JobIntentService
|
||||
{
|
||||
public final static String ACTION_DOWNLOAD_COMPLETED = "action_download_completed";
|
||||
public final static String EXTRA_DOWNLOAD_STATUS = "extra_download_status";
|
||||
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
|
@ -142,28 +142,9 @@ public class SystemDownloadCompletedService extends JobIntentService
|
|||
@Override
|
||||
public void run()
|
||||
{
|
||||
Result result = mStatus.getResult();
|
||||
if (mStatus.isOk() && result != null && !TextUtils.isEmpty(result.getArchiveId())
|
||||
&& !TextUtils.isEmpty(result.getFilePath()))
|
||||
{
|
||||
|
||||
BookmarkManager.INSTANCE.importFromCatalog(result.getArchiveId(), result.getFilePath());
|
||||
return;
|
||||
}
|
||||
|
||||
Error error = mStatus.getError();
|
||||
if (error != null)
|
||||
{
|
||||
if (error.isForbidden())
|
||||
{
|
||||
Toast.makeText(mAppContext, "Authorization needed. Ui coming soon!",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
else
|
||||
{
|
||||
Toast.makeText(mAppContext, R.string.download_failed, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
Intent intent = new Intent(ACTION_DOWNLOAD_COMPLETED);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_STATUS, mStatus);
|
||||
LocalBroadcastManager.getInstance(mAppContext).sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue