Skip to content

Commit

Permalink
Pull API Key (#448)
Browse files Browse the repository at this point in the history
* Add ApiKeyChangeListener interface

* migrate RouterHttpHandler to pull api key

* Migrate SearchHttpHandler to pull api key

* Rm TmpHttpHandler class

* Update map api key when updates in MapzenManager

* better listener api
  • Loading branch information
sarahsnow1 authored Sep 11, 2017
1 parent bf93f44 commit 93b8385
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 287 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mapzen.android.core;

/**
* Listener for handling changes to API key.
*/
public interface ApiKeyChangeListener {
/**
* Called when the {@link MapzenManager}'s API key is changed.
* @param apiKey the current API key
*/
void onApiKeyChanged(String apiKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* Provide initializer for configuring {@link com.mapzen.android.search.MapzenSearch} objects.
* @return
*/
@Provides @Singleton public SearchInitializer provideSearchInitializer() {
return new SearchInitializer();
@Provides @Singleton public SearchInitializer provideSearchInitializer(Context context) {
return new SearchInitializer(context);
}
}
27 changes: 27 additions & 0 deletions core/src/main/java/com/mapzen/android/core/MapzenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import android.content.Context;
import android.content.res.Resources;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* {@code MapzenManager} assists with Mapzen API key management. When created it reads the API key
* set in string resources if one has been declared by the application.
Expand Down Expand Up @@ -33,6 +38,8 @@ public class MapzenManager {

static MapzenManager instance;

private List<WeakReference<ApiKeyChangeListener>> listeners = new ArrayList<>();

/**
* Get singleton instance.
*/
Expand Down Expand Up @@ -80,6 +87,7 @@ public String getApiKey() {
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
notifyListeners();
}

/**
Expand All @@ -89,4 +97,23 @@ public void setApiKey(String apiKey) {
public static String getSdkVersion() {
return BuildConfig.SDK_VERSION;
}

/**
* Adds listener to list of managed callbacks so that it can be notified when API key changes
* occur.
* @param listenerReference
*/
public void addApiKeyChangeListener(WeakReference<ApiKeyChangeListener> listenerReference) {
Collections.synchronizedList(listeners).add(listenerReference);
}

private void notifyListeners() {
for (WeakReference<ApiKeyChangeListener> weakReference : Collections.synchronizedList(
listeners)) {
ApiKeyChangeListener listener = weakReference.get();
if (listener != null) {
listener.onApiKeyChanged(apiKey);
}
}
}
}
12 changes: 12 additions & 0 deletions core/src/main/java/com/mapzen/android/graphics/MapzenMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapzen.android.graphics;

import com.mapzen.android.core.ApiKeyChangeListener;
import com.mapzen.android.core.MapzenManager;
import com.mapzen.android.graphics.internal.StyleStringGenerator;
import com.mapzen.android.graphics.model.BitmapMarker;
Expand Down Expand Up @@ -27,6 +28,7 @@
import android.support.annotation.Nullable;
import android.view.View;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -147,6 +149,15 @@ public boolean onRotate(float x, float y, float rotation) {
}
};

WeakReference<ApiKeyChangeListener> apiKeyChangeListener = new WeakReference(
new ApiKeyChangeListener() {
@Override public void onApiKeyChanged(String apiKey) {
List<SceneUpdate> updates = new ArrayList<>();
updates.add(sceneUpdateManager.getApiKeyUpdate(apiKey));
mapController.updateSceneAsync(updates);
}
});

/**
* Creates a new map based on the given {@link MapView} and {@link MapController}.
*/
Expand All @@ -164,6 +175,7 @@ public boolean onRotate(float x, float y, float rotation) {
this.sceneUpdateManager = sceneUpdateManager;
this.locale = locale;
this.mapzenManager = mapzenManager;
this.mapzenManager.addApiKeyChangeListener(apiKeyChangeListener);
mapView.setMapzenMap(this);
mapController.setPanResponder(internalPanResponder);
mapController.setRotateResponder(internalRotateResponder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ HttpHandler httpHandler() {
return httpHandler;
}

private class InternalHttpHandler extends TmpHttpHandler {
private class InternalHttpHandler extends HttpHandler {

public InternalHttpHandler(File directory, long maxSize) {
super(directory, maxSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SceneUpdateManager {
List<SceneUpdate> getUpdatesFor(String apiKey, Locale locale, boolean transitOverlayEnabled,
boolean bikeOverlayEnabled, boolean pathOverlayEnabled) {
final ArrayList<SceneUpdate> sceneUpdates = new ArrayList<>(2);
sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, apiKey));
sceneUpdates.add(getApiKeyUpdate(apiKey));
sceneUpdates.add(new SceneUpdate(STYLE_GLOBAL_VAR_LANGUAGE, locale.getLanguage()));
sceneUpdates.add(getTransitOverlayUpdate(transitOverlayEnabled));
sceneUpdates.add(getBikeOverlayUpdate(bikeOverlayEnabled));
Expand Down Expand Up @@ -63,4 +63,13 @@ SceneUpdate getPathOverlayUpdate(boolean pathOverlayEnabled) {
return new SceneUpdate(STYLE_GLOBAL_VAR_PATH_OVERLAY, String.valueOf(
pathOverlayEnabled));
}

/**
* Creates a {@link SceneUpdate} for the API key.
* @param apiKey
* @return
*/
SceneUpdate getApiKeyUpdate(String apiKey) {
return new SceneUpdate(STYLE_GLOBAL_VAR_API_KEY, apiKey);
}
}
184 changes: 0 additions & 184 deletions core/src/main/java/com/mapzen/android/graphics/TmpHttpHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MapzenSearch {
public MapzenSearch(Context context) {
initDI(context);
internalSearch = new Pelias();
searchInitializer.initSearch(this, context);
searchInitializer.initSearch(this);
}

/**
Expand All @@ -37,7 +37,7 @@ public MapzenSearch(Context context) {
public MapzenSearch(Context context, Pelias pelias) {
initDI(context);
internalSearch = pelias;
searchInitializer.initSearch(this, context);
searchInitializer.initSearch(this);
}

private void initDI(Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.mapzen.android.search;

import com.mapzen.android.core.ApiKeyChangeListener;
import com.mapzen.android.core.GenericHttpHandler;
import com.mapzen.android.core.MapzenManager;
import com.mapzen.pelias.PeliasRequestHandler;

import android.content.Context;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -13,12 +17,21 @@
public abstract class MapzenSearchHttpHandler implements GenericHttpHandler {

private SearchRequestHandler searchHandler;
WeakReference<ApiKeyChangeListener> apiKeyChangeListener = new WeakReference(
new ApiKeyChangeListener() {
@Override public void onApiKeyChanged(String apiKey) {
searchHandler.setApiKey(apiKey);
}
});

/**
* Public constructor.
*/
public MapzenSearchHttpHandler() {
public MapzenSearchHttpHandler(Context context) {
searchHandler = new SearchRequestHandler();
MapzenManager mapzenManager = MapzenManager.instance(context);
mapzenManager.addApiKeyChangeListener(apiKeyChangeListener);
searchHandler.setApiKey(mapzenManager.getApiKey());
}

/**
Expand Down
Loading

0 comments on commit 93b8385

Please sign in to comment.