aboutsummaryrefslogtreecommitdiffstats
path: root/mapswithme-api/src/com/mapswithme/maps/api/MwmRequest.java
blob: 7a2335dc13fc08c9c3785946a4c187428ee4c30d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.mapswithme.maps.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class MwmRequest
{

  // **
  private List<MWMPoint> mPoints    = new ArrayList<MWMPoint>();
  private PendingIntent  mPendingIntent;
  private String         mTitle;
  private double         mZoomLevel = 1;
  private boolean        mReturnOnBalloonClick;
  private boolean        mPickPoint = false;
  private String         mCustomButtonName = "";
  // **

  public MwmRequest setCustomButtonName(String buttonName)
  {
    mCustomButtonName = buttonName != null ? buttonName : "";
    return this;
  }

  public MwmRequest setTitle(String title)
  {
    mTitle = title;
    return this;
  }

  public MwmRequest setPickPointMode(boolean pickPoint)
  {
    mPickPoint = pickPoint;
    return this;
  }

  public MwmRequest addPoint(MWMPoint point)
  {
    mPoints.add(point);
    return this;
  }

  public MwmRequest addPoint(double lat, double lon, String name, String id)
  {
    return addPoint(new MWMPoint(lat, lon, name, id));
  }

  public MwmRequest setPoints(Collection<MWMPoint> points)
  {
    mPoints = new ArrayList<MWMPoint>(points);
    return this;
  }

  public MwmRequest setReturnOnBalloonClick(boolean doReturn)
  {
    mReturnOnBalloonClick = doReturn;
    return this;
  }

  public MwmRequest setZoomLevel(double zoomLevel)
  {
    mZoomLevel = zoomLevel;
    return this;
  }

  public MwmRequest setPendingIntent(PendingIntent pi)
  {
    mPendingIntent = pi;
    return this;
  }

  public Intent toIntent(Context context)
  {
    final Intent mwmIntent = new Intent(Const.ACTION_MWM_REQUEST);

    // url
    final String mwmUrl = createMwmUrl(context, mTitle, mZoomLevel, mPoints).toString();
    mwmIntent.putExtra(Const.EXTRA_URL, mwmUrl);
    // title
    mwmIntent.putExtra(Const.EXTRA_TITLE, mTitle);
    // more
    mwmIntent.putExtra(Const.EXTRA_RETURN_ON_BALLOON_CLICK, mReturnOnBalloonClick);
    // pick point
    mwmIntent.putExtra(Const.EXTRA_PICK_POINT, mPickPoint);
    // custom button name
    mwmIntent.putExtra(Const.EXTRA_CUSTOM_BUTTON_NAME, mCustomButtonName);

    final boolean hasIntent = mPendingIntent != null;
    mwmIntent.putExtra(Const.EXTRA_HAS_PENDING_INTENT, hasIntent);
    if (hasIntent)
      mwmIntent.putExtra(Const.EXTRA_CALLER_PENDING_INTENT, mPendingIntent);

    addCommonExtras(context, mwmIntent);

    return mwmIntent;
  }

  /**
   * @Hidden
   * This method is internal only.
   * Used for compatibility.
   */
  MwmRequest setPoints(MWMPoint[] points)
  {
    return setPoints(Arrays.asList(points));
  }

  // Below are utilities from MapsWithMeApi because we are not "Feature Envy"

  private static StringBuilder createMwmUrl(Context context, String title, double zoomLevel, List<MWMPoint> points)
  {
    final StringBuilder urlBuilder = new StringBuilder("mapswithme://map?");
    // version
    urlBuilder.append("v=").append(Const.API_VERSION).append("&");
    // back url, always not null
    urlBuilder.append("backurl=").append(getCallbackAction(context)).append("&");
    // title
    appendIfNotNull(urlBuilder, "appname", title);
    // zoom
    appendIfNotNull(urlBuilder, "z", isValidZoomLevel(zoomLevel) ? String.valueOf(zoomLevel) : null);

    // points
    for (final MWMPoint point : points)
    {
      if (point != null)
      {
        urlBuilder.append("ll=").append(String.format(Locale.US, "%f,%f&", point.getLat(), point.getLon()));

        appendIfNotNull(urlBuilder, "n", point.getName());
        appendIfNotNull(urlBuilder, "id", point.getId());
      }
    }

    return urlBuilder;
  }

  private static String getCallbackAction(Context context)
  {
    return Const.CALLBACK_PREFIX + context.getPackageName();
  }

  @SuppressLint("NewApi")
  private static Intent addCommonExtras(Context context, Intent intent)
  {
    intent.putExtra(Const.EXTRA_CALLER_APP_INFO, context.getApplicationInfo());
    intent.putExtra(Const.EXTRA_API_VERSION, Const.API_VERSION);

    return intent;
  }

  private static StringBuilder appendIfNotNull(StringBuilder builder, String key, String value)
  {
    if (value != null)
      builder.append(key).append("=").append(Uri.encode(value)).append("&");

    return builder;
  }

  private static boolean isValidZoomLevel(double zoom)
  {
    return zoom >= MapsWithMeApi.ZOOM_MIN && zoom <= MapsWithMeApi.ZOOM_MAX;
  }

}