From 09dcb13876d1a7e5ac7f3745cb58dcbbdd5ea643 Mon Sep 17 00:00:00 2001 From: Sean Barbeau Date: Mon, 1 Jun 2020 12:31:02 -0400 Subject: [PATCH] chore: Add IndoorDemoActivity in Kotlin (#232) Also apply following changes to Java version: * Refactor variables to remove Hungarian notation * Remove redundant cast for TextView * Fix spelling in string --- .../example/mapdemo/IndoorDemoActivity.java | 20 +-- .../example/kotlindemos/DemoDetailsList.kt | 2 + .../example/kotlindemos/IndoorDemoActivity.kt | 129 ++++++++++++++++++ .../app/src/gms/res/layout/indoor_demo.xml | 84 ++++++++++++ .../kotlin/app/src/main/AndroidManifest.xml | 1 + .../app/src/main/res/values/strings.xml | 8 ++ 6 files changed, 234 insertions(+), 10 deletions(-) create mode 100644 ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/IndoorDemoActivity.kt create mode 100644 ApiDemos/kotlin/app/src/gms/res/layout/indoor_demo.xml diff --git a/ApiDemos/java/app/src/gms/java/com/example/mapdemo/IndoorDemoActivity.java b/ApiDemos/java/app/src/gms/java/com/example/mapdemo/IndoorDemoActivity.java index feb50b77..62b078ab 100755 --- a/ApiDemos/java/app/src/gms/java/com/example/mapdemo/IndoorDemoActivity.java +++ b/ApiDemos/java/app/src/gms/java/com/example/mapdemo/IndoorDemoActivity.java @@ -36,7 +36,7 @@ import java.util.List; */ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyCallback { - private GoogleMap mMap; + private GoogleMap map; private boolean showLevelPicker = true; @@ -51,9 +51,9 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC } @Override - public void onMapReady(GoogleMap map) { - mMap = map; - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.614631, -122.385153), 18)); + public void onMapReady(GoogleMap googleMap) { + map = googleMap; + map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.614631, -122.385153), 18)); } /** @@ -61,14 +61,14 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC */ public void onToggleLevelPicker(View view) { showLevelPicker = !showLevelPicker; - mMap.getUiSettings().setIndoorLevelPickerEnabled(showLevelPicker); + map.getUiSettings().setIndoorLevelPickerEnabled(showLevelPicker); } /** * Called when the focused building info is clicked. */ public void onFocusedBuildingInfo(View view) { - IndoorBuilding building = mMap.getFocusedBuilding(); + IndoorBuilding building = map.getFocusedBuilding(); if (building != null) { StringBuilder s = new StringBuilder(); for (IndoorLevel level : building.getLevels()) { @@ -87,7 +87,7 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC * Called when the focused level info is clicked. */ public void onVisibleLevelInfo(View view) { - IndoorBuilding building = mMap.getFocusedBuilding(); + IndoorBuilding building = map.getFocusedBuilding(); if (building != null) { IndoorLevel level = building.getLevels().get(building.getActiveLevelIndex()); @@ -105,7 +105,7 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC * Called when the activate higher level is clicked. */ public void onHigherLevel(View view) { - IndoorBuilding building = mMap.getFocusedBuilding(); + IndoorBuilding building = map.getFocusedBuilding(); if (building != null) { List levels = building.getLevels(); if (!levels.isEmpty()) { @@ -117,7 +117,7 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC newLevel = levels.size() - 1; } IndoorLevel level = levels.get(newLevel); - setText("Activiating level " + level.getName()); + setText("Activating level " + level.getName()); level.activate(); } else { setText("No levels in building"); @@ -128,7 +128,7 @@ public class IndoorDemoActivity extends AppCompatActivity implements OnMapReadyC } private void setText(String message) { - TextView text = (TextView) findViewById(R.id.top_text); + TextView text = findViewById(R.id.top_text); text.setText(message); } } diff --git a/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt index a7c0a75b..20b0da5e 100644 --- a/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt +++ b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt @@ -39,6 +39,8 @@ class DemoDetailsList { R.string.ground_overlay_demo_label, R.string.ground_overlay_demo_details, GroundOverlayDemoActivity::class.java), + DemoDetails(R.string.indoor_demo_label, R.string.indoor_demo_details, + IndoorDemoActivity::class.java), DemoDetails(R.string.layers_demo_label, R.string.layers_demo_description, LayersDemoActivity::class.java), DemoDetails(R.string.lite_demo_label, R.string.lite_demo_details, diff --git a/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/IndoorDemoActivity.kt b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/IndoorDemoActivity.kt new file mode 100644 index 00000000..0ab752a4 --- /dev/null +++ b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/IndoorDemoActivity.kt @@ -0,0 +1,129 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.example.kotlindemos + +import android.os.Bundle +import android.view.View +import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import com.google.android.gms.maps.CameraUpdateFactory +import com.google.android.gms.maps.GoogleMap +import com.google.android.gms.maps.OnMapReadyCallback +import com.google.android.gms.maps.SupportMapFragment +import com.google.android.gms.maps.model.LatLng + +/** + * A demo activity showing how to use indoor. + */ +class IndoorDemoActivity : AppCompatActivity(), OnMapReadyCallback { + + private lateinit var map: GoogleMap + private var showLevelPicker = true + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.indoor_demo) + val mapFragment = + supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? + mapFragment?.getMapAsync(this) + } + + override fun onMapReady(googleMap: GoogleMap?) { + map = googleMap ?: return + googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.614631, -122.385153), 18f)) + } + + /** + * Called when the toggle level picker button is clicked. + */ + fun onToggleLevelPicker(view: View?) { + if (!::map.isInitialized) return + + showLevelPicker = !showLevelPicker + map.uiSettings.isIndoorLevelPickerEnabled = showLevelPicker + } + + /** + * Called when the focused building info is clicked. + */ + fun onFocusedBuildingInfo(view: View?) { + if (!::map.isInitialized) return + + val building = map.focusedBuilding + if (building != null) { + val s = StringBuilder() + for (level in building.levels) { + s.append(level.name).append(" ") + } + if (building.isUnderground) { + s.append("is underground") + } + setText(s.toString()) + } else { + setText("No visible building") + } + } + + /** + * Called when the focused level info is clicked. + */ + fun onVisibleLevelInfo(view: View?) { + if (!::map.isInitialized) return + + val building = map.focusedBuilding + if (building != null) { + val level = building.levels[building.activeLevelIndex] + if (level != null) { + setText(level.name) + } else { + setText("No visible level") + } + } else { + setText("No visible building") + } + } + + /** + * Called when the activate higher level is clicked. + */ + fun onHigherLevel(view: View?) { + if (!::map.isInitialized) return + + val building = map.focusedBuilding + if (building != null) { + val levels = building.levels + if (!levels.isEmpty()) { + val currentLevel = building.activeLevelIndex + // The levels are in 'display order' from top to bottom, + // i.e. higher levels in the building are lower numbered in the array. + var newLevel = currentLevel - 1 + if (newLevel == -1) { + newLevel = levels.size - 1 + } + val level = levels[newLevel] + setText("Activating level " + level.name) + level.activate() + } else { + setText("No levels in building") + } + } else { + setText("No visible building") + } + } + + private fun setText(message: String) { + val text = findViewById(R.id.top_text) + text.text = message + } +} \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/gms/res/layout/indoor_demo.xml b/ApiDemos/kotlin/app/src/gms/res/layout/indoor_demo.xml new file mode 100644 index 00000000..00a4ae99 --- /dev/null +++ b/ApiDemos/kotlin/app/src/gms/res/layout/indoor_demo.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + +