Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit e5c1b7b

Browse files
author
Langston Smith
authored
Code cleanup refactoring, null checks, and other lint adjustments (#1180)
1 parent 1e941aa commit e5c1b7b

File tree

66 files changed

+758
-665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+758
-665
lines changed

MapboxAndroidDemo/src/androidTest/java/com/mapbox/mapboxsdk/testapp/BaseActivityTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
1111
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
1212

13-
import junit.framework.Assert;
14-
1513
import org.junit.After;
1614
import org.junit.Before;
1715
import org.junit.Rule;
1816

19-
import androidx.test.espresso.Espresso;
17+
import androidx.test.espresso.IdlingRegistry;
2018
import androidx.test.espresso.IdlingResourceTimeoutException;
2119
import androidx.test.rule.ActivityTestRule;
2220
import timber.log.Timber;
@@ -25,21 +23,23 @@
2523
import static androidx.test.espresso.assertion.ViewAssertions.matches;
2624
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
2725
import static androidx.test.espresso.matcher.ViewMatchers.withId;
26+
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertTrue;
2828

2929

3030
public abstract class BaseActivityTest {
3131

3232
@Rule
3333
public ActivityTestRule<Activity> rule = new ActivityTestRule<>(getActivityClass());
34-
protected MapboxMap mapboxMap;
35-
protected OnMapReadyIdlingResource idlingResource;
34+
private MapboxMap mapboxMap;
35+
private OnMapReadyIdlingResource idlingResource;
3636

3737
@Before
3838
public void beforeTest() {
3939
try {
4040
Timber.e("@Before test: register idle resource");
4141
idlingResource = new OnMapReadyIdlingResource(rule.getActivity());
42-
Espresso.registerIdlingResources(idlingResource);
42+
IdlingRegistry.getInstance().register(idlingResource);
4343
checkViewIsDisplayed(R.id.mapView);
4444
mapboxMap = idlingResource.getMapboxMap();
4545
} catch (IdlingResourceTimeoutException idlingResourceTimeoutException) {
@@ -54,9 +54,9 @@ public void beforeTest() {
5454
}
5555

5656
protected void validateTestSetup() {
57-
Assert.assertTrue("Device is not connected to the Internet.", isConnected(rule.getActivity()));
57+
assertTrue("Device is not connected to the Internet.", isConnected(rule.getActivity()));
5858
checkViewIsDisplayed(R.id.mapView);
59-
Assert.assertNotNull(mapboxMap);
59+
assertNotNull(mapboxMap);
6060
}
6161

6262
protected MapboxMap getMapboxMap() {
@@ -87,7 +87,7 @@ static boolean isConnected(Context context) {
8787
@After
8888
public void afterTest() {
8989
Timber.e("@After test: unregister idle resource");
90-
Espresso.unregisterIdlingResources(idlingResource);
90+
IdlingRegistry.getInstance().unregister(idlingResource);
9191
}
9292
}
9393

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
193193
private ExampleAdapter adapter;
194194
private RecyclerView recyclerView;
195195
private TextView noExamplesTv;
196-
private NavigationView navigationView;
197196

198197
private boolean loggedIn;
199198
private int currentCategory = R.id.nav_basics;
@@ -267,7 +266,7 @@ public void onItemClicked(RecyclerView recyclerView, int position, View view) {
267266
}
268267
toggle.syncState();
269268

270-
navigationView = findViewById(R.id.nav_view);
269+
NavigationView navigationView = findViewById(R.id.nav_view);
271270
if (navigationView != null) {
272271
navigationView.setNavigationItemSelectedListener(this);
273272
navigationView.setCheckedItem(R.id.nav_basics);

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MapboxApplication.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ public void onCreate() {
2323
setUpCrashlytics();
2424
}
2525
Mapbox.getInstance(this, getString(R.string.access_token));
26-
Mapbox.getTelemetry().setDebugLoggingEnabled(true);
26+
if (BuildConfig.DEBUG) {
27+
if (Mapbox.getTelemetry() == null) {
28+
throw new RuntimeException("Mapbox.getTelemetry() == null in debug config");
29+
} else {
30+
Mapbox.getTelemetry().setDebugLoggingEnabled(true);
31+
}
32+
}
2733
setUpTileLoadingMeasurement();
2834
}
2935

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/account/LandingActivity.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import android.net.Uri;
55
import android.os.Bundle;
66
import android.preference.PreferenceManager;
7-
import androidx.browser.customtabs.CustomTabsIntent;
8-
import androidx.core.content.ContextCompat;
9-
import androidx.appcompat.app.AppCompatActivity;
107
import android.widget.CheckBox;
118
import android.widget.Toast;
129

@@ -16,6 +13,10 @@
1613
import com.mapbox.mapboxandroiddemo.R;
1714
import com.mapbox.mapboxandroiddemo.commons.AnalyticsTracker;
1815

16+
import androidx.appcompat.app.AppCompatActivity;
17+
import androidx.browser.customtabs.CustomTabsIntent;
18+
import androidx.core.content.ContextCompat;
19+
1920
import static com.mapbox.mapboxandroiddemo.commons.AnalyticsTracker.CLICKED_ON_CREATE_ACCOUNT_BUTTON;
2021
import static com.mapbox.mapboxandroiddemo.commons.AnalyticsTracker.CLICKED_ON_SIGN_IN_BUTTON;
2122
import static com.mapbox.mapboxandroiddemo.commons.StringConstants.AUTHCODE_KEY;
@@ -35,7 +36,6 @@ public class LandingActivity extends AppCompatActivity {
3536
private static final String REDIRECT_URI = "mapbox-android-dev-preview://authorize";
3637
private static String CREATE_ACCOUNT_AUTH_URL;
3738
private boolean loggedIn;
38-
private boolean alreadyIgnoredLandingAsk;
3939
private boolean ignoreCheckboxIsChecked;
4040
private AnalyticsTracker analytics;
4141

@@ -48,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {
4848
getApplicationContext())
4949
.getBoolean(TOKEN_SAVED_KEY, false);
5050

51-
alreadyIgnoredLandingAsk = PreferenceManager.getDefaultSharedPreferences(
51+
boolean alreadyIgnoredLandingAsk = PreferenceManager.getDefaultSharedPreferences(
5252
getApplicationContext())
5353
.getBoolean(LOGIN_SIGNIN_IGNORE_KEY, false);
5454

@@ -60,7 +60,9 @@ protected void onCreate(Bundle savedInstanceState) {
6060
if (!loggedIn && !alreadyIgnoredLandingAsk || getIntent().getBooleanExtra(
6161
FROM_LOGIN_SCREEN_MENU_ITEM_KEY, false)) {
6262
setContentView(R.layout.activity_landing);
63-
getSupportActionBar().hide();
63+
if (getSupportActionBar() != null) {
64+
getSupportActionBar().hide();
65+
}
6466
buildAccountAuthUrl();
6567
setUpButtons();
6668
} else {
@@ -81,18 +83,20 @@ protected void onResume() {
8183
Toast.makeText(getApplicationContext(), R.string.log_out_toast_confirm, Toast.LENGTH_LONG).show();
8284
} else if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
8385
Uri uri = getIntent().getData();
84-
String error = uri.getQueryParameter("error");
85-
if (error != null) {
86-
showErrorDialog();
87-
} else {
88-
String authCode = uri.getQueryParameter("code");
89-
Intent intent = new Intent(this, AccountRetrievalService.class);
90-
intent.putExtra(AUTHCODE_KEY, authCode);
91-
intent.putExtra(REDIRECT_URI_KEY, REDIRECT_URI);
92-
intent.putExtra(CLIENT_ID_KEY, CLIENT_ID);
93-
startService(intent);
94-
Intent loadingActivityIntent = new Intent(this, LoadingActivity.class);
95-
startActivity(loadingActivityIntent);
86+
if (uri != null) {
87+
String error = uri.getQueryParameter("error");
88+
if (error != null) {
89+
showErrorDialog();
90+
} else {
91+
String authCode = uri.getQueryParameter("code");
92+
Intent intent = new Intent(this, AccountRetrievalService.class);
93+
intent.putExtra(AUTHCODE_KEY, authCode);
94+
intent.putExtra(REDIRECT_URI_KEY, REDIRECT_URI);
95+
intent.putExtra(CLIENT_ID_KEY, CLIENT_ID);
96+
startService(intent);
97+
Intent loadingActivityIntent = new Intent(this, LoadingActivity.class);
98+
startActivity(loadingActivityIntent);
99+
}
96100
}
97101
}
98102
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/account/LoadingActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class LoadingActivity extends AppCompatActivity {
1313
@Override
1414
protected void onCreate(Bundle savedInstanceState) {
1515
super.onCreate(savedInstanceState);
16-
getSupportActionBar().setDisplayShowTitleEnabled(false);
16+
if (getSupportActionBar() != null) {
17+
getSupportActionBar().setDisplayShowTitleEnabled(false);
18+
}
1719
setContentView(R.layout.activity_loading);
1820
}
1921
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/basics/SupportMapFragmentActivity.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ protected void onCreate(Bundle savedInstanceState) {
5353
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
5454
}
5555

56-
mapFragment.getMapAsync(new OnMapReadyCallback() {
57-
@Override
58-
public void onMapReady(@NonNull MapboxMap mapboxMap) {
56+
if (mapFragment != null) {
57+
mapFragment.getMapAsync(new OnMapReadyCallback() {
58+
@Override
59+
public void onMapReady(@NonNull MapboxMap mapboxMap) {
60+
mapboxMap.setStyle(Style.SATELLITE, new Style.OnStyleLoaded() {
61+
@Override
62+
public void onStyleLoaded(@NonNull Style style) {
5963

60-
mapboxMap.setStyle(Style.SATELLITE, new Style.OnStyleLoaded() {
61-
@Override
62-
public void onStyleLoaded(@NonNull Style style) {
64+
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
6365

64-
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
6566

66-
67-
}
68-
});
69-
}
70-
});
67+
}
68+
});
69+
}
70+
});
71+
}
7172
}
7273
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/DrawGeojsonLineActivity.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ public void onStyleLoaded(@NonNull Style style) {
6363
private void drawLines(@NonNull FeatureCollection featureCollection) {
6464
if (mapboxMap != null) {
6565
mapboxMap.getStyle(style -> {
66-
if (featureCollection.features().size() > 0) {
67-
style.addSource(new GeoJsonSource("line-source", featureCollection));
68-
69-
// The layer properties for our line. This is where we make the line dotted, set the
70-
// color, etc.
71-
style.addLayer(new LineLayer("linelayer", "line-source")
72-
.withProperties(PropertyFactory.lineCap(Property.LINE_CAP_SQUARE),
73-
PropertyFactory.lineJoin(Property.LINE_JOIN_MITER),
74-
PropertyFactory.lineOpacity(.7f),
75-
PropertyFactory.lineWidth(7f),
76-
PropertyFactory.lineColor(Color.parseColor("#3bb2d0"))));
66+
if (featureCollection.features() != null) {
67+
if (featureCollection.features().size() > 0) {
68+
style.addSource(new GeoJsonSource("line-source", featureCollection));
69+
70+
// The layer properties for our line. This is where we make the line dotted, set the
71+
// color, etc.
72+
style.addLayer(new LineLayer("linelayer", "line-source")
73+
.withProperties(PropertyFactory.lineCap(Property.LINE_CAP_SQUARE),
74+
PropertyFactory.lineJoin(Property.LINE_JOIN_MITER),
75+
PropertyFactory.lineOpacity(.7f),
76+
PropertyFactory.lineWidth(7f),
77+
PropertyFactory.lineColor(Color.parseColor("#3bb2d0"))));
78+
}
7779
}
7880
});
7981
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/ExpressionIntegrationActivity.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,28 @@ protected FeatureCollection doInBackground(Void... voids) {
183183

184184
// Find out the states represented in the FeatureCollection
185185
// and bounds of the extreme conditions
186-
for (Feature feature : featureCollection.features()) {
187-
String stateName = feature.getStringProperty("state");
188-
String lat = feature.getStringProperty("latitude");
189-
String lon = feature.getStringProperty("longitude");
190-
191-
LatLng latLng = new LatLng(
192-
Double.parseDouble(lat),
193-
Double.parseDouble(lon));
194-
195-
State state = null;
196-
for (State curState : activity.states) {
197-
if (curState.name.equals(stateName)) {
198-
state = curState;
199-
break;
186+
if (featureCollection.features() != null) {
187+
for (Feature feature : featureCollection.features()) {
188+
String stateName = feature.getStringProperty("state");
189+
String lat = feature.getStringProperty("latitude");
190+
String lon = feature.getStringProperty("longitude");
191+
192+
LatLng latLng = new LatLng(
193+
Double.parseDouble(lat),
194+
Double.parseDouble(lon));
195+
196+
State state = null;
197+
for (State curState : activity.states) {
198+
if (curState.name.equals(stateName)) {
199+
state = curState;
200+
break;
201+
}
202+
}
203+
if (state == null) {
204+
activity.states.add(activity.createState(stateName, latLng));
205+
} else {
206+
state.add(latLng);
200207
}
201-
}
202-
if (state == null) {
203-
activity.states.add(activity.createState(stateName, latLng));
204-
} else {
205-
state.add(latLng);
206208
}
207209
}
208210
return featureCollection;
@@ -292,12 +294,16 @@ private void changeTemperatureUnits(boolean isImperial, @NonNull Style loadedMap
292294

293295
// Apply new units to the data displayed in text fields of SymbolLayers
294296
SymbolLayer maxTempLayer = (SymbolLayer) loadedMapStyle.getLayer(MAX_TEMP_LAYER_ID);
295-
maxTempLayer.withProperties(textField(getTemperatureValue()));
297+
if (maxTempLayer != null) {
298+
maxTempLayer.withProperties(textField(getTemperatureValue()));
299+
}
296300

297301
SymbolLayer minTempLayer = (SymbolLayer) loadedMapStyle.getLayer(MIN_TEMP_LAYER_ID);
298-
minTempLayer.withProperties(textField(getTemperatureValue()));
299-
302+
if (minTempLayer != null) {
303+
minTempLayer.withProperties(textField(getTemperatureValue()));
304+
}
300305
unitsText.setText(isImperial ? DEGREES_C : DEGREES_F);
306+
301307
}
302308
}
303309

@@ -403,31 +409,33 @@ private void selectState(CharSequence stateName, int stateIndex, @NonNull Style
403409
// Adds a SymbolLayer to display maximum temperature in state
404410
SymbolLayer maxTempLayer = (SymbolLayer) loadedMapStyle.getLayer(MAX_TEMP_LAYER_ID);
405411
// Only display Maximum Temperature in this layer for SELECTED State
406-
maxTempLayer.setFilter(all(
407-
eq(get("element"), literal("All-Time Maximum Temperature")),
408-
eq(get("state"), literal(stateName))));
409-
412+
if (maxTempLayer != null) {
413+
maxTempLayer.setFilter(all(
414+
eq(get("element"), literal("All-Time Maximum Temperature")),
415+
eq(get("state"), literal(stateName))));
416+
}
410417

411418
// Adds a SymbolLayer to display minimum temperature in state
412419
SymbolLayer minTempLayer = (SymbolLayer) loadedMapStyle.getLayer(MIN_TEMP_LAYER_ID);
413420
// Only display Maximum Temperature in this layer for SELECTED State
414-
minTempLayer.setFilter(all(
415-
eq(get("element"), literal("All-Time Minimum Temperature")),
416-
eq(get("state"), literal(stateName))));
421+
if (minTempLayer != null) {
422+
minTempLayer.setFilter(all(
423+
eq(get("element"), literal("All-Time Minimum Temperature")),
424+
eq(get("state"), literal(stateName))));
425+
}
417426

418427
CameraUpdate cameraUpdate =
419428
CameraUpdateFactory.newLatLngBounds(states.get(stateIndex).bounds, 100);
420429
mapboxMap.animateCamera(cameraUpdate);
421430

422431
Toast.makeText(this,
423432
String.format(getString(R.string.temp_change_feedback), stateName),
424-
Toast.LENGTH_LONG)
425-
.show();
433+
Toast.LENGTH_LONG).show();
426434
}
427435
}
428436

429437
private int indexOfState(CharSequence name) {
430-
if (states != null && name != null) {
438+
if (name != null) {
431439
for (int i = 0; i < states.size(); i++) {
432440
if (name.equals(states.get(i).name)) {
433441
return i;

0 commit comments

Comments
 (0)