From 462b3d3d0809ebef50a43da6e1b7f43a346b3d9f Mon Sep 17 00:00:00 2001 From: Tobrun Van Nuland Date: Mon, 14 Aug 2017 08:44:19 +0200 Subject: [android] - add style inspecatable in debug mode activity --- .../src/main/AndroidManifest.xml | 8 +- .../activity/maplayout/DebugModeActivity.java | 169 ++++++++++++++++++--- .../src/main/res/layout/activity_debug_mode.xml | 114 +++++++++----- 3 files changed, 231 insertions(+), 60 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 5dc322a530..509580149b 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -305,14 +305,12 @@ - + android:value="@string/category_basic"/> --> - + \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java index 5b394a7895..2c84bf6531 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java @@ -1,10 +1,19 @@ package com.mapbox.mapboxsdk.testapp.activity.maplayout; +import android.content.Context; import android.os.Bundle; -import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; +import android.support.v4.widget.DrawerLayout; +import android.support.v7.app.ActionBar; +import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; +import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.BaseAdapter; +import android.widget.ListView; import android.widget.TextView; import com.mapbox.mapboxsdk.camera.CameraPosition; @@ -12,18 +21,24 @@ import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.style.layers.Layer; +import com.mapbox.mapboxsdk.style.layers.Property; import com.mapbox.mapboxsdk.testapp.R; +import java.util.List; + import timber.log.Timber; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; + /** * Test Activity showcasing the different debug modes and allows to cycle between the default map styles. */ -public class DebugModeActivity extends AppCompatActivity { +public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCallback { private MapView mapView; private MapboxMap mapboxMap; - + private ActionBarDrawerToggle actionBarDrawerToggle; private int currentStyleIndex = 0; private static final String[] STYLES = new String[] { @@ -32,20 +47,46 @@ public class DebugModeActivity extends AppCompatActivity { Style.LIGHT, Style.DARK, Style.SATELLITE, - Style.SATELLITE_STREETS + Style.SATELLITE_STREETS, + Style.TRAFFIC_DAY, + Style.TRAFFIC_NIGHT }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_debug_mode); + setupToolbar(); + setupMapView(savedInstanceState); + setupDebugChangeView(); + setupStyleChangeView(); + } + + private void setupToolbar() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); + DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); + actionBarDrawerToggle = new ActionBarDrawerToggle(this, + drawerLayout, + R.string.navigation_drawer_open, + R.string.navigation_drawer_close + ); + actionBarDrawerToggle.setDrawerIndicatorEnabled(true); + actionBarDrawerToggle.syncState(); + } + } + + private void setupMapView(Bundle savedInstanceState) { mapView = (MapView) findViewById(R.id.mapView); mapView.addOnMapChangedListener(new MapView.OnMapChangedListener() { @Override public void onMapChanged(int change) { if (change == MapView.DID_FINISH_LOADING_STYLE && mapboxMap != null) { - Timber.e("New loaded style = %s", mapboxMap.getStyleJson()); + Timber.v("New style loaded with JSON: %s", mapboxMap.getStyleJson()); + setupNavigationView(mapboxMap.getLayers()); } } }); @@ -53,24 +94,57 @@ public class DebugModeActivity extends AppCompatActivity { mapView.setTag(true); mapView.setStyleUrl(STYLES[currentStyleIndex]); mapView.onCreate(savedInstanceState); - mapView.getMapAsync(new OnMapReadyCallback() { + mapView.getMapAsync(this); + } + + @Override + public void onMapReady(MapboxMap map) { + mapboxMap = map; + mapboxMap.getUiSettings().setZoomControlsEnabled(true); + + setupNavigationView(mapboxMap.getLayers()); + setupZoomView(); + } + + private void setupNavigationView(List layerList) { + final LayerListAdapter adapter = new LayerListAdapter(this, layerList); + ListView listView = (ListView) findViewById(R.id.listView); + listView.setAdapter(adapter); + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override - public void onMapReady(@NonNull MapboxMap map) { - mapboxMap = map; + public void onItemClick(AdapterView parent, View view, int position, long id) { + Layer clickedLayer = adapter.getItem(position); + toggleLayerVisibility(clickedLayer); + closeNavigationView(); + } + }); + } - mapboxMap.getUiSettings().setZoomControlsEnabled(true); + private void toggleLayerVisibility(Layer layer) { + boolean isVisible = layer.getVisibility().getValue().equals(Property.VISIBLE); + layer.setProperties( + visibility( + isVisible ? Property.NONE : Property.VISIBLE + ) + ); + } - // show current zoom level on screen - final TextView textView = (TextView) findViewById(R.id.textZoom); - mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() { - @Override - public void onCameraChange(CameraPosition position) { - textView.setText(String.format(getString(R.string.debug_zoom), position.zoom)); - } - }); + private void closeNavigationView() { + DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); + drawerLayout.closeDrawers(); + } + + private void setupZoomView() { + final TextView textView = (TextView) findViewById(R.id.textZoom); + mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() { + @Override + public void onCameraChange(CameraPosition position) { + textView.setText(String.format(getString(R.string.debug_zoom), position.zoom)); } }); + } + private void setupDebugChangeView() { FloatingActionButton fabDebug = (FloatingActionButton) findViewById(R.id.fabDebug); fabDebug.setOnClickListener(new View.OnClickListener() { @Override @@ -81,7 +155,9 @@ public class DebugModeActivity extends AppCompatActivity { } } }); + } + private void setupStyleChangeView() { FloatingActionButton fabStyles = (FloatingActionButton) findViewById(R.id.fabStyles); fabStyles.setOnClickListener(new View.OnClickListener() { @Override @@ -102,6 +178,11 @@ public class DebugModeActivity extends AppCompatActivity { }); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return actionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); + } + @Override protected void onStart() { super.onStart(); @@ -143,4 +224,58 @@ public class DebugModeActivity extends AppCompatActivity { super.onLowMemory(); mapView.onLowMemory(); } + + private static class LayerListAdapter extends BaseAdapter { + + private LayoutInflater layoutInflater; + private List layers; + + LayerListAdapter(Context context, List layers) { + this.layoutInflater = LayoutInflater.from(context); + this.layers = layers; + } + + @Override + public int getCount() { + return layers.size(); + } + + @Override + public Layer getItem(int position) { + return layers.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + Layer layer = layers.get(position); + View view = convertView; + if (view == null) { + view = layoutInflater.inflate(android.R.layout.simple_list_item_2, parent, false); + ViewHolder holder = new ViewHolder( + (TextView) view.findViewById(android.R.id.text1), + (TextView) view.findViewById(android.R.id.text2) + ); + view.setTag(holder); + } + ViewHolder holder = (ViewHolder) view.getTag(); + holder.text.setText(layer.getClass().getSimpleName()); + holder.subText.setText(layer.getId()); + return view; + } + + private static class ViewHolder { + final TextView text; + final TextView subText; + + ViewHolder(TextView text, TextView subText) { + this.text = text; + this.subText = subText; + } + } + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml index 0a98f7727f..5b0a4a2cdb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml @@ -1,48 +1,86 @@ - + android:clickable="true" + android:focusable="true" + android:focusableInTouchMode="true"> - + android:orientation="vertical"> - - - - - + + + + + + + + + + + + + android:layout_height="match_parent" + android:layout_gravity="start" + android:background="@android:color/white"> + + + + - + \ No newline at end of file -- cgit v1.2.1