From 304e0b1c035cafad37d082bc42175f9893cca207 Mon Sep 17 00:00:00 2001 From: Bella Mangunsong Date: Thu, 11 Jan 2018 15:05:10 +1100 Subject: [PATCH] add UI settings demo --- ApiDemos/kotlin/app/build.gradle | 2 + .../kotlin/app/src/main/AndroidManifest.xml | 8 ++ .../example/kotlindemos/DemoDetailsList.kt | 4 +- .../kotlindemos/UiSettingsDemoActivity.kt | 124 ++++++++++++++++++ .../res/layout/activity_ui_settings_demo.xml | 112 ++++++++++++++++ .../kotlin/app/src/main/res/values/colors.xml | 1 + .../app/src/main/res/values/strings.xml | 13 ++ 7 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/UiSettingsDemoActivity.kt create mode 100644 ApiDemos/kotlin/app/src/main/res/layout/activity_ui_settings_demo.xml diff --git a/ApiDemos/kotlin/app/build.gradle b/ApiDemos/kotlin/app/build.gradle index 24a82936..63e40c02 100644 --- a/ApiDemos/kotlin/app/build.gradle +++ b/ApiDemos/kotlin/app/build.gradle @@ -33,4 +33,6 @@ dependencies { implementation 'com.google.android.gms:play-services-maps:11.8.0' compile 'com.google.android.gms:play-services-location:11.8.0' + // EasyPermissions is needed to help us request for permission to access location + implementation 'pub.devrel:easypermissions:1.1.1' } \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/main/AndroidManifest.xml b/ApiDemos/kotlin/app/src/main/AndroidManifest.xml index 92848185..19478199 100644 --- a/ApiDemos/kotlin/app/src/main/AndroidManifest.xml +++ b/ApiDemos/kotlin/app/src/main/AndroidManifest.xml @@ -16,6 +16,9 @@ + + + + + + + + diff --git a/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/DemoDetailsList.kt b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/DemoDetailsList.kt index 35d64724..bd277a94 100644 --- a/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/DemoDetailsList.kt +++ b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/DemoDetailsList.kt @@ -29,7 +29,9 @@ class DemoDetailsList { DemoDetails( R.string.street_view_panorama_navigation_demo_label, R.string.street_view_panorama_navigation_demo_details, - StreetViewPanoramaNavigationDemoActivity::class.java) + StreetViewPanoramaNavigationDemoActivity::class.java), + DemoDetails(R.string.ui_settings_demo_label, R.string.ui_settings_demo_details, + UiSettingsDemoActivity::class.java) ) } } \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/UiSettingsDemoActivity.kt b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/UiSettingsDemoActivity.kt new file mode 100644 index 00000000..1f272fea --- /dev/null +++ b/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/UiSettingsDemoActivity.kt @@ -0,0 +1,124 @@ +/* + * Copyright 2018 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 + * + * https://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.Manifest +import android.annotation.SuppressLint +import android.support.v7.app.AppCompatActivity +import android.os.Bundle +import android.view.View +import android.widget.CheckBox +import com.google.android.gms.maps.GoogleMap +import com.google.android.gms.maps.OnMapReadyCallback +import com.google.android.gms.maps.SupportMapFragment +import pub.devrel.easypermissions.AfterPermissionGranted +import pub.devrel.easypermissions.EasyPermissions + +const val REQUEST_CODE_LOCATION = 123 + +class UiSettingsDemoActivity : + AppCompatActivity(), + OnMapReadyCallback { + + private lateinit var map: GoogleMap + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_ui_settings_demo) + val mapFragment: SupportMapFragment = + supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment + mapFragment.getMapAsync(this) + } + + override fun onMapReady(googleMap: GoogleMap?) { + // Return early if map is not initialised properly + map = googleMap ?: return + enableMyLocation() + // Set all the settings of the map to match the current state of the checkboxes + with(map.uiSettings) { + isZoomControlsEnabled = isChecked(R.id.zoom_button) + isCompassEnabled = isChecked(R.id.compass_button) + isMyLocationButtonEnabled = isChecked(R.id.myloc_button) + isIndoorLevelPickerEnabled = isChecked(R.id.level_button) + isMapToolbarEnabled = isChecked(R.id.maptoolbar_button) + isZoomGesturesEnabled = isChecked(R.id.zoomgest_button) + isScrollGesturesEnabled = isChecked(R.id.scrollgest_button) + isTiltGesturesEnabled = isChecked(R.id.tiltgest_button) + isRotateGesturesEnabled = isChecked(R.id.rotategest_button) + } + } + + /** On click listener for checkboxes */ + fun onClick(view: View) { + if (view !is CheckBox) { + return + } + val isChecked: Boolean = view.isChecked + with(map.uiSettings) { + when (view.id) { + R.id.zoom_button -> isZoomControlsEnabled = isChecked + R.id.compass_button -> isCompassEnabled = isChecked + R.id.myloc_button -> isMyLocationButtonEnabled = isChecked + R.id.level_button -> isIndoorLevelPickerEnabled = isChecked + R.id.maptoolbar_button -> isMapToolbarEnabled = isChecked + R.id.zoomgest_button -> isZoomGesturesEnabled = isChecked + R.id.scrollgest_button -> isScrollGesturesEnabled = isChecked + R.id.tiltgest_button -> isTiltGesturesEnabled = isChecked + R.id.rotategest_button -> isRotateGesturesEnabled = isChecked + } + } + } + + /** Returns whether the checkbox with the given id is checked */ + private fun isChecked(id: Int) = findViewById(id)?.isChecked ?: false + + /** Override the onRequestPermissionResult to use EasyPermissions */ + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this) + } + + /** + * enableMyLocation() will enable the location of the map if the user has given permission + * for the app to access their device location. + * Android Studio requires an explicit check before setting map.isMyLocationEnabled to true + * but we are using EasyPermissions to handle it so we can suppress the "MissingPermission" + * check. + */ + @SuppressLint("MissingPermission") + @AfterPermissionGranted(REQUEST_CODE_LOCATION) + private fun enableMyLocation() { + if (hasLocationPermission()) { + map.isMyLocationEnabled = true + } else { + EasyPermissions.requestPermissions( + this, + getString(R.string.location), + REQUEST_CODE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + ) + } + } + + private fun hasLocationPermission(): Boolean { + return EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION) + } +} diff --git a/ApiDemos/kotlin/app/src/main/res/layout/activity_ui_settings_demo.xml b/ApiDemos/kotlin/app/src/main/res/layout/activity_ui_settings_demo.xml new file mode 100644 index 00000000..23bb84a6 --- /dev/null +++ b/ApiDemos/kotlin/app/src/main/res/layout/activity_ui_settings_demo.xml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/main/res/values/colors.xml b/ApiDemos/kotlin/app/src/main/res/values/colors.xml index 3ab3e9cb..5169d545 100644 --- a/ApiDemos/kotlin/app/src/main/res/values/colors.xml +++ b/ApiDemos/kotlin/app/src/main/res/values/colors.xml @@ -3,4 +3,5 @@ #3F51B5 #303F9F #FF4081 + #D0FFFFFF diff --git a/ApiDemos/kotlin/app/src/main/res/values/strings.xml b/ApiDemos/kotlin/app/src/main/res/values/strings.xml index 803033a4..c05dd40b 100644 --- a/ApiDemos/kotlin/app/src/main/res/values/strings.xml +++ b/ApiDemos/kotlin/app/src/main/res/values/strings.xml @@ -58,4 +58,17 @@ Go to San Francisco Go to Santorini + + UI Settings Demo + Demonstrate how to toggle the UI of a map + Location permission is required to run the demo + Zoom + Compass + My Location + Level Picker + Map Toolbar + Zoom Gestures + Scroll Gestures + Tilt Gestures + Rotate Gestures \ No newline at end of file