-
Notifications
You must be signed in to change notification settings - Fork 3k
/
UiSettingsDemoActivity.kt
121 lines (109 loc) · 4.86 KB
/
UiSettingsDemoActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* 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.os.Bundle
import android.view.View
import android.widget.CheckBox
import androidx.appcompat.app.AppCompatActivity
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<CheckBox>(id)?.isChecked ?: false
/** Override the onRequestPermissionResult to use EasyPermissions */
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
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)
}
}