Skip to content

Commit

Permalink
Refactor Gradle Properties (#292)
Browse files Browse the repository at this point in the history
* Refactor gradle props

* Fix NPE when reverse geocode returns empty body

* Set sample app API key via gradle props

* Update deploy scripts to inject API key via build props

* Centralize API key management in new MapzenManager class

* Checkstyle

* Tangram 0.4.9 release

* Update javadoc
  • Loading branch information
ecgreb authored Feb 7, 2017
1 parent 6e52222 commit 2db52f9
Show file tree
Hide file tree
Showing 29 changed files with 294 additions and 141 deletions.
2 changes: 2 additions & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
<suppress checks="ConstantName" files="src/main/java/com/mapzen/places/api/Places.java"/>
<suppress checks="ParameterNumberCheck" files="src/main/java/com/mapzen/places/api/internal/PlaceImpl.java"/>
<suppress checks="LineLength" files="OkHttp3TestUtils.java"/>
<suppress checks="TypeName" files="R.java" />
<suppress checks="ConstantName" files="R.java" />
</suppressions>
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ group = GROUP
version = VERSION_NAME
project.archivesBaseName = POM_ARTIFACT_ID

ext.tangram_version = "0.4.9-SNAPSHOT"
ext.tangram_version = "0.4.9"

release {
tagTemplate = 'mapzen-core${version}'
Expand Down
16 changes: 0 additions & 16 deletions core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
version=0.0.1-SNAPSHOT

POM_ARTIFACT_ID=mapzen-core
POM_NAME=Mapzen Android Core
POM_PACKAGING=aar

GROUP=com.mapzen
VERSION_NAME=0.0.1-SNAPSHOT

POM_DESCRIPTION=Core classes used by the Mapzen Android SDK and Mapzen Places API.

POM_URL=https://github.com/mapzen/android
POM_SCM_URL=http://github.com/mapzen/android
POM_SCM_CONNECTION=scm:git:git://github.com/mapzen/android.git
POM_SCM_DEV_CONNECTION=scm:git:[email protected]:mapzen/android.git

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=Mapzen
POM_DEVELOPER_NAME=Mapzen
10 changes: 0 additions & 10 deletions core/src/main/java/com/mapzen/android/core/ApiKeyConstants.java

This file was deleted.

70 changes: 70 additions & 0 deletions core/src/main/java/com/mapzen/android/core/MapzenManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.mapzen.android.core;

import android.content.Context;
import android.content.res.Resources;

/**
* {@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.
*
* <pre>
* &lt;resources&gt;
* &lt;string name="mapzen_api_key"&gt;[YOUR_MAPZEN_API_KEY]&lt;/string&gt;
* &lt;/resources&gt;
* </pre>
*
* If an API key is set in code via {@link #setApiKey(String)} it will override the declared as a
* string resource.
*
* Finally, if an API key is explicitly passed into a specific component when created it will
* supersede any key stored by this class.
*
* <pre>
* MapzenSearch mapzenSearch = new MapzenSearch(context, "mapzen-XXXXXXX")
* </pre>
*/
public class MapzenManager {
public static final String API_KEY_RES_NAME = "mapzen_api_key";
public static final String API_KEY_RES_TYPE = "string";
public static final String API_KEY_PARAM_NAME = "api_key";

static MapzenManager instance;

/**
* Get singleton instance.
*/
public static MapzenManager instance(Context context) {
if (instance == null) {
instance = new MapzenManager(context);
}

return instance;
}

private String apiKey;

/**
* Creates a new instance of the manager.
*/
private MapzenManager(Context context) {
final Resources resources = context.getResources();
int id = resources.getIdentifier(API_KEY_RES_NAME, API_KEY_RES_TYPE, context.getPackageName());
if (id > 0) {
apiKey = resources.getString(id);
}
}

/**
* Returns the currently active API key stored by this class.
*/
public String getApiKey() {
return apiKey;
}

/**
* Sets a new API key value. This will override any previous key including those declared in xml.
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mapzen.android.graphics;

import com.mapzen.android.core.ApiKeyConstants;
import com.mapzen.android.core.MapzenManager;
import com.mapzen.android.graphics.model.BubbleWrapStyle;
import com.mapzen.android.graphics.model.MapStyle;
import com.mapzen.tangram.MapController;
Expand Down Expand Up @@ -85,9 +85,7 @@ private void loadMap(final MapView mapView, MapStyle mapStyle, boolean styleExpl
private void loadMap(final MapView mapView, String sceneFile, final OnMapReadyCallback callback,
String apiKey) {
if (apiKey == null) {
final int resId = context.getResources().getIdentifier(ApiKeyConstants.API_KEY_RES_NAME,
ApiKeyConstants.API_KEY_RES_TYPE, context.getPackageName());
apiKey = context.getString(resId);
apiKey = MapzenManager.instance(context).getApiKey();
}

final ArrayList<SceneUpdate> sceneUpdates = new ArrayList<>(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.mapzen.android.search;


import com.mapzen.android.core.ApiKeyConstants;
import com.mapzen.android.core.MapzenManager;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

/**
* Handles setting the api key and a request handler for given {@link MapzenSearch} objects.
*/
public class SearchInitializer {

private static final String TAG = SearchInitializer.class.getSimpleName();

private SearchRequestHandler requestHandler = new SearchRequestHandler();

/**
Expand All @@ -23,16 +17,7 @@ public class SearchInitializer {
* @param context
*/
public void initSearch(MapzenSearch search, Context context) {
Resources res = context.getResources();
final String packageName = context.getPackageName();
try {
final int apiKeyId = res.getIdentifier(ApiKeyConstants.API_KEY_RES_NAME,
ApiKeyConstants.API_KEY_RES_TYPE, packageName);
final String apiKey = res.getString(apiKeyId);
initSearch(search, apiKey);
} catch (Resources.NotFoundException e) {
Log.e(TAG, e.getLocalizedMessage());
}
initSearch(search, MapzenManager.instance(context).getApiKey());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mapzen.android.search;

import com.mapzen.android.core.ApiKeyConstants;
import com.mapzen.android.core.MapzenManager;
import com.mapzen.pelias.PeliasRequestHandler;

import java.util.HashMap;
Expand Down Expand Up @@ -34,7 +34,7 @@ public String getApiKey() {

@Override public Map<String, String> queryParamsForRequest() {
HashMap<String, String> params = new HashMap<>();
params.put(ApiKeyConstants.API_KEY, apiKey);
params.put(MapzenManager.API_KEY_PARAM_NAME, apiKey);
return params;
}
}
109 changes: 109 additions & 0 deletions core/src/test/java/com/mapzen/android/core/MapzenManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.mapzen.android.core;

import com.mapzen.BuildConfig;
import com.mapzen.android.core.test.R;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;

import static com.mapzen.android.core.MapzenManager.API_KEY_RES_NAME;
import static com.mapzen.android.core.MapzenManager.API_KEY_RES_TYPE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(BuildConfig.class)
public class MapzenManagerTest {
@After public void tearDown() throws Exception {
MapzenManager.instance = null;
}

@Test public void shouldNotBeNull() throws Exception {
Context context = mock(Context.class);
Resources resources = new TestResources();
when(context.getResources()).thenReturn(resources);
MapzenManager mapzenManager = MapzenManager.instance(context);
assertThat(mapzenManager).isNotNull();
}

@Test public void getApiKey_shouldReturnNullIfNotSet() throws Exception {
Context context = mock(Context.class);
Resources resources = new TestResources();
when(context.getResources()).thenReturn(resources);
MapzenManager mapzenManager = MapzenManager.instance(context);
assertThat(mapzenManager.getApiKey()).isNull();
}

@Test public void getApiKey_shouldReturnNullIfResourceNotFound() throws Exception {
Context context = mock(Context.class);
Resources resources = new TestResourcesNotFound();
when(context.getResources()).thenReturn(resources);
MapzenManager mapzenManager = MapzenManager.instance(context);
assertThat(mapzenManager.getApiKey()).isNull();
}

@Test public void getApiKey_shouldReturnStringResourceValue() throws Exception {
Context context = mock(Context.class);
TestResources resources = new TestResources();
resources.testApiKey = "mapzen-fake-api-key";
when(context.getResources()).thenReturn(resources);
MapzenManager mapzenManager = MapzenManager.instance(context);
assertThat(mapzenManager.getApiKey()).isEqualTo("mapzen-fake-api-key");
}

@Test public void setApiKey_shouldOverrideStringResourcesValue() throws Exception {
Context context = mock(Context.class);
TestResources resources = new TestResources();
resources.testApiKey = "mapzen-fake-api-key";
when(context.getResources()).thenReturn(resources);
MapzenManager mapzenManager = MapzenManager.instance(context);
mapzenManager.setApiKey("mapzen-fake-api-key-2");
assertThat(mapzenManager.getApiKey()).isEqualTo("mapzen-fake-api-key-2");
}

private class TestResources extends Resources {
private String testApiKey;

TestResources() {
super(null, null, null);
}

@Override public int getIdentifier(String name, String defType, String defPackage) {
if (API_KEY_RES_NAME.equals(name) && API_KEY_RES_TYPE.equals(defType)) {
return R.id.mapzen_api_key;
}

return 0;
}

@NonNull @Override public String getString(int id) throws NotFoundException {
if (id == R.id.mapzen_api_key) {
return testApiKey;
}

throw new NotFoundException();
}
}

private class TestResourcesNotFound extends Resources {
TestResourcesNotFound() {
super(null, null, null);
}

@Override public int getIdentifier(String name, String defType, String defPackage) {
return 0;
}

@NonNull @Override public String getString(int id) throws NotFoundException {
throw new NotFoundException();
}
}
}
7 changes: 7 additions & 0 deletions core/src/test/java/com/mapzen/android/core/test/R.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mapzen.android.core.test;

public final class R {
public static final class id {
public static final int mapzen_api_key = 0x00000001;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public class MapzenSearchTest {
MapzenSearch search = new MapzenSearch(context, pelias);

@Test public void shouldCreatePelias() {
MapzenSearch mzSearch = new MapzenSearch(context, "API_KEY");
MapzenSearch mzSearch = new MapzenSearch(context, "API_KEY_PARAM_NAME");
assertThat(mzSearch.getPelias()).isNotNull();
assertThat(mzSearch.searchInitializer.getRequestHandler().getApiKey()).isEqualTo("API_KEY");
assertThat(mzSearch.searchInitializer.getRequestHandler().getApiKey())
.isEqualTo("API_KEY_PARAM_NAME");
}

@Test public void suggest_shouldCallPelias() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mapzen.android.search;

import com.mapzen.android.core.ApiKeyConstants;
import com.mapzen.android.core.MapzenManager;

import org.junit.Test;

Expand All @@ -17,6 +17,7 @@ public class SearchRequestHandlerTest {

@Test public void queryParamsForRequest_shouldReturnApiKey() {
adapter.setApiKey("TEST_KEY");
assertThat(adapter.queryParamsForRequest().get(ApiKeyConstants.API_KEY)).isEqualTo("TEST_KEY");
assertThat(adapter.queryParamsForRequest().get(MapzenManager.API_KEY_PARAM_NAME))
.isEqualTo("TEST_KEY");
}
}
15 changes: 15 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version=1.3.0-SNAPSHOT

VERSION_NAME=1.3.0-SNAPSHOT

POM_URL=https://github.com/mapzen/android
POM_SCM_URL=http://github.com/mapzen/android
POM_SCM_CONNECTION=scm:git:git://github.com/mapzen/android.git
POM_SCM_DEV_CONNECTION=scm:git:[email protected]:mapzen/android.git

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=Mapzen
POM_DEVELOPER_NAME=Mapzen
2 changes: 1 addition & 1 deletion mapzen-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ task verify(dependsOn: ['compileDebugSources',
'lint'])

dependencies {
compile 'com.mapzen:mapzen-core:0.0.1-SNAPSHOT'
compile 'com.mapzen:mapzen-core:1.3.0-SNAPSHOT'
compile ('com.mapzen:on-the-road:1.2.0-SNAPSHOT') {
exclude group: 'com.squareup.retrofit2', module: 'converter-gson'
}
Expand Down
16 changes: 0 additions & 16 deletions mapzen-android-sdk/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
version=1.3.0-SNAPSHOT

POM_ARTIFACT_ID=mapzen-android-sdk
POM_NAME=Mapzen Android SDK
POM_PACKAGING=aar

GROUP=com.mapzen
VERSION_NAME=1.3.0-SNAPSHOT

POM_DESCRIPTION=Unified SDK for Mapzen tools and services on Android.

POM_URL=https://github.com/mapzen/android
POM_SCM_URL=http://github.com/mapzen/android
POM_SCM_CONNECTION=scm:git:git://github.com/mapzen/android.git
POM_SCM_DEV_CONNECTION=scm:git:[email protected]:mapzen/android.git

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=Mapzen
POM_DEVELOPER_NAME=Mapzen
Loading

0 comments on commit 2db52f9

Please sign in to comment.