[android] Refactored to PendingIntent.

This is more flexible and reliable approach.
This commit is contained in:
Dmitry Kunin 2013-05-29 19:59:33 +03:00 committed by Alex Zolotarev
parent c50066b02e
commit 50393615dd
5 changed files with 19 additions and 79 deletions

View file

@ -3,26 +3,19 @@ package com.mapswithme.maps.api;
public class Const
{
/* Codes */
// status
public static final int STATUS_OK = 0;
public static final int STATUS_CANCEL = 1;
/* Request extras */
static final String AUTHORITY = "com.mapswithme.maps.api";
public static final String EXTRA_URL = AUTHORITY + ".url";
public static final String EXTRA_TITLE = AUTHORITY + ".title";
public static final String EXTRA_CALLMEBACK_MODE = AUTHORITY + ".callmeback_mode";
public static final String EXTRA_CALLBACK_ACTION = AUTHORITY + ".callback";
public static final String EXTRA_API_VERSION = AUTHORITY + ".version";
public static final String EXTRA_CALLER_APP_INFO = AUTHORITY + ".caller_app_info";
public static final String EXTRA_HAS_PENDING_INTENT = AUTHORITY + ".has_pen_intent";
public static final String EXTRA_CALLER_PENDING_INTENT = AUTHORITY + ".pending_intent";
/* Response extras */
public static final String EXTRA_MWM_RESPONSE_STATUS = AUTHORITY + ".status";
/* Point part-by-part*/
public static final String EXTRA_MWM_RESPONSE_HAS_POINT = AUTHORITY + ".has_point";
public static final String EXTRA_MWM_RESPONSE_POINT_NAME = AUTHORITY + ".point_name";
public static final String EXTRA_MWM_RESPONSE_POINT_LAT = AUTHORITY + ".point_lat";
public static final String EXTRA_MWM_RESPONSE_POINT_LON = AUTHORITY + ".point_lon";
@ -31,7 +24,7 @@ public class Const
static final int API_VERSION = 1;
static final String CALLBACK_PREFIX = "mapswithme.client.";
static final String ACTION_MWM_REQUEST = AUTHORITY + ".request";
public static final String ACTION_MWM_REQUEST = AUTHORITY + ".request";
private Const() {}
}

View file

@ -6,35 +6,28 @@ import android.content.Intent;
// TODO add javadoc for public interface
public class MWMResponse
{
private int mStatus;
private MWMPoint mPoint;
public boolean isCanceled() { return Const.STATUS_CANCEL == mStatus; }
public boolean isSuccessful() { return Const.STATUS_OK == mStatus; }
public MWMPoint getPoint() { return mPoint; }
public boolean hasPoint() { return mPoint != null; }
@Override
public String toString()
{
return "MWMResponse [mStatus=" + mStatus + ", mSelectedPoint=" + mPoint + "]";
return "MWMResponse [mSelectedPoint=" + mPoint + "]";
}
static MWMResponse extractFromIntent(Context context, Intent intent)
{
final MWMResponse response = new MWMResponse();
// parse status
response.mStatus = intent.getIntExtra(Const.EXTRA_MWM_RESPONSE_STATUS, Const.STATUS_OK);
// parse point
if (intent.getBooleanExtra(Const.EXTRA_MWM_RESPONSE_HAS_POINT, false))
{
final double lat = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LAT, 0);
final double lon = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LON, 0);
final String name = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_NAME);
final String id = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_ID);
response.mPoint = new MWMPoint(lat, lon, name, id);
}
final double lat = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LAT, 0);
final double lon = intent.getDoubleExtra(Const.EXTRA_MWM_RESPONSE_POINT_LON, 0);
final String name = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_NAME);
final String id = intent.getStringExtra(Const.EXTRA_MWM_RESPONSE_POINT_ID);
response.mPoint = new MWMPoint(lat, lon, name, id);
return response;
}

View file

@ -1,9 +0,0 @@
package com.mapswithme.maps.api;
import android.content.Context;
// TODO add javadoc
public interface MWMResponseHandler
{
public void onResponse(Context context, MWMResponse response);
}

View file

@ -1,28 +0,0 @@
package com.mapswithme.maps.api;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
// TODO add javadoc with example
public final class MWMResponseReciever extends BroadcastReceiver
{
// I believe this it not the best approach
// But we leave it to keep things simple
static MWMResponseHandler sResponseHandler;
@Override
final public void onReceive(Context context, Intent intent)
{
if (sResponseHandler != null
&& MapsWithMeApi.getCallbackAction(context).equals(intent.getAction()))
{
sResponseHandler.onResponse(context, MWMResponse.extractFromIntent(context, intent));
// clean up handler to avoid context-leak
sResponseHandler = null;
}
}
}

View file

@ -2,6 +2,7 @@
package com.mapswithme.maps.api;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@ -21,7 +22,7 @@ public final class MapsWithMeApi
public static void showPointOnMap(Activity caller, double lat, double lon, String name, String id)
{
showPointsOnMap(caller, (String)null, (MWMResponseHandler)null, new MWMPoint(lat, lon, name));
showPointsOnMap(caller, (String)null, (PendingIntent)null, new MWMPoint(lat, lon, name));
}
public static void showPointsOnMap(Activity caller, String title, MWMPoint... points)
@ -29,29 +30,20 @@ public final class MapsWithMeApi
showPointsOnMap(caller, title, null, points);
}
public static void showPointsOnMap(Activity caller, String title, MWMResponseHandler responseHandler, MWMPoint... points)
public static void showPointsOnMap(Activity caller, String title, PendingIntent pendingIntent, MWMPoint... points)
{
final Intent mwmIntent = new Intent(Const.ACTION_MWM_REQUEST);
mwmIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mwmIntent.putExtra(Const.EXTRA_URL, createMwmUrl(caller, title, points).toString());
mwmIntent.putExtra(Const.EXTRA_TITLE, title);
mwmIntent.putExtra(Const.EXTRA_CALLMEBACK_MODE, responseHandler != null);
final boolean hasIntent = pendingIntent != null;
mwmIntent.putExtra(Const.EXTRA_HAS_PENDING_INTENT, hasIntent);
if (hasIntent)
mwmIntent.putExtra(Const.EXTRA_CALLER_PENDING_INTENT, pendingIntent);
addCommonExtras(caller, mwmIntent);
MWMResponseReciever.sResponseHandler = responseHandler;
if (responseHandler != null)
{
// detect if it is registered
// throw if not
final Intent callbackIntentStub = new Intent(getCallbackAction(caller));
final boolean recieverEnabled = caller.getPackageManager().queryBroadcastReceivers(callbackIntentStub, 0).size() > 0;
if (!recieverEnabled)
throw new IllegalStateException(String.format(
"BroadcastReciever with intent-filter for action \"%s\" must be added to manifest.", getCallbackAction(caller)));
}
if (isMapsWithMeInstalled(caller))
{
// Match activity for intent
@ -111,7 +103,6 @@ public final class MapsWithMeApi
{
intent.putExtra(Const.EXTRA_CALLER_APP_INFO, context.getApplicationInfo());
intent.putExtra(Const.EXTRA_API_VERSION, Const.API_VERSION);
intent.putExtra(Const.EXTRA_CALLBACK_ACTION, getCallbackAction(context));
return intent;
}