summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/IconFactory.java
blob: 57aa512401bb13bf82216601126d8d71287e4997 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.mapbox.mapboxsdk.annotations;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.exceptions.TooManyIconsException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Factory for creating Icons from bitmap images.
 * <p>
 * {@link Icon} is used to display bitmaps on top of the map using {@link Marker} and {@link MarkerView}.
 * </p>
 *
 * @see Icon
 */
public final class IconFactory {

  private static final String ICON_ID_PREFIX = "com.mapbox.icons.icon_";
  public static final Bitmap ICON_MARKERVIEW_BITMAP = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
  public static final String ICON_MARKERVIEW_ID = ICON_ID_PREFIX + "marker_view";

  private Context context;
  private static IconFactory instance;
  private Icon defaultMarker;
  private Icon defaultMarkerView;
  private BitmapFactory.Options options;

  private int nextId = 0;

  public static synchronized IconFactory getInstance(@NonNull Context context) {
    if (instance == null) {
      instance = new IconFactory(context.getApplicationContext());
    }
    return instance;
  }

  private IconFactory(@NonNull Context context) {
    this.context = context;
    DisplayMetrics realMetrics = null;
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      realMetrics = new DisplayMetrics();
      wm.getDefaultDisplay().getRealMetrics(realMetrics);
    }
    wm.getDefaultDisplay().getMetrics(metrics);

    options = new BitmapFactory.Options();
    options.inScaled = true;
    options.inDensity = DisplayMetrics.DENSITY_DEFAULT;
    options.inTargetDensity = metrics.densityDpi;
    if (realMetrics != null) {
      options.inScreenDensity = realMetrics.densityDpi;
    }
  }

  /**
   * Creates an {@link Icon} from a given Bitmap image.
   *
   * @param bitmap image used for creating the Icon.
   * @return The {@link Icon} using the given Bitmap image.
   */
  public Icon fromBitmap(@NonNull Bitmap bitmap) {
    if (nextId < 0) {
      throw new TooManyIconsException();
    }
    String id = ICON_ID_PREFIX + ++nextId;
    return new Icon(id, bitmap);
  }

  /**
   * Create an {@link Icon} using the resource ID of a Bitmap image.
   *
   * @param resourceId The resource ID of a Bitmap image.
   * @return The {@link Icon} that was loaded from the asset or {@code null} if failed to load.
   */
  public Icon fromResource(@DrawableRes int resourceId) {
    Drawable drawable = ContextCompat.getDrawable(context, resourceId);
    if (drawable instanceof BitmapDrawable) {
      BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
      return fromBitmap(bitmapDrawable.getBitmap());
    } else {
      throw new IllegalArgumentException("Failed to decode image. The resource provided must be a Bitmap.");
    }
  }

  /**
   * Provides an {@link Icon} using the default marker icon used for {@link Marker}.
   *
   * @return An {@link Icon} with the default {@link Marker} icon.
   */
  public Icon defaultMarker() {
    if (defaultMarker == null) {
      defaultMarker = fromResource(R.drawable.mapbox_marker_icon_default);
    }
    return defaultMarker;
  }

  /**
   * Provides an {@link Icon} using the default marker icon used for {@link MarkerView}.
   *
   * @return An {@link Icon} with the default {@link MarkerView} icon.
   */
  public Icon defaultMarkerView() {
    if (defaultMarkerView == null) {
      defaultMarkerView = fromResource(R.drawable.mapbox_markerview_icon_default);
    }
    return defaultMarkerView;
  }

  private Icon fromInputStream(@NonNull InputStream is) {
    Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
    return fromBitmap(bitmap);
  }

  /**
   * Creates an {@link Icon} using the name of a Bitmap image in the assets directory.
   *
   * @param assetName The name of a Bitmap image in the assets directory.
   * @return The {@link Icon} that was loaded from the asset or {@code null} if failed to load.
   */
  public Icon fromAsset(@NonNull String assetName) {
    InputStream is;
    try {
      is = context.getAssets().open(assetName);
    } catch (IOException ioException) {
      return null;
    }
    return fromInputStream(is);
  }

  /**
   * Creates an {@link Icon} using the absolute file path of a Bitmap image.
   *
   * @param absolutePath The absolute path of the Bitmap image.
   * @return The {@link Icon} that was loaded from the absolute path or {@code null} if failed to
   * load.
   */
  public Icon fromPath(@NonNull String absolutePath) {
    Bitmap bitmap = BitmapFactory.decodeFile(absolutePath, options);
    return fromBitmap(bitmap);
  }

  /**
   * Create an {@link Icon} using the name of a Bitmap image file located in the internal storage.
   * In particular, this calls {@link Context#openFileInput(String)}.
   *
   * @param fileName The name of the Bitmap image file.
   * @return The {@link Icon} that was loaded from the asset or {@code null} if failed to load.
   * @see <a href="https://developer.android.com/guide/topics/data/data-storage.html#filesInternal">
   * Using the Internal Storage</a>
   */
  public Icon fromFile(@NonNull String fileName) {
    FileInputStream is;
    try {
      is = context.openFileInput(fileName);
    } catch (FileNotFoundException fileNotFoundException) {
      return null;
    }
    return fromInputStream(is);
  }

  /**
   * Create an {@link Icon} using a previously created icon identifier along with a provided
   * Bitmap.
   *
   * @param iconId The {@link Icon} identifier you'd like to recreate.
   * @param bitmap a Bitmap used to replace the current one.
   * @return The {@link Icon} using the new Bitmap.
   */
  public static Icon recreate(@NonNull String iconId, @NonNull Bitmap bitmap) {
    return new Icon(iconId, bitmap);
  }

}