mirror of
https://github.com/googlemaps/android-samples.git
synced 2025-12-08 18:02:20 +00:00
chore: Add MyLocationDemoActivity in Kotlin (#239)
Includes region tags for: * maps_check_location_permission_result * maps_check_location_permission Also adds PermissionUtils in Kotlin Also apply following changes to Java version: * Refactor variables to remove Hungarian notation Also remove redundant schema from layout
This commit is contained in:
parent
b27512a8f5
commit
a6d24c6395
@ -56,9 +56,9 @@ public class MyLocationDemoActivity extends AppCompatActivity
|
||||
* Flag indicating whether a requested permission has been denied after returning in
|
||||
* {@link #onRequestPermissionsResult(int, String[], int[])}.
|
||||
*/
|
||||
private boolean mPermissionDenied = false;
|
||||
private boolean permissionDenied = false;
|
||||
|
||||
private GoogleMap mMap;
|
||||
private GoogleMap map;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -71,11 +71,10 @@ public class MyLocationDemoActivity extends AppCompatActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapReady(GoogleMap map) {
|
||||
mMap = map;
|
||||
|
||||
mMap.setOnMyLocationButtonClickListener(this);
|
||||
mMap.setOnMyLocationClickListener(this);
|
||||
public void onMapReady(GoogleMap googleMap) {
|
||||
map = googleMap;
|
||||
map.setOnMyLocationButtonClickListener(this);
|
||||
map.setOnMyLocationClickListener(this);
|
||||
enableMyLocation();
|
||||
}
|
||||
|
||||
@ -86,8 +85,8 @@ public class MyLocationDemoActivity extends AppCompatActivity
|
||||
// [START maps_check_location_permission]
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
if (mMap != null) {
|
||||
mMap.setMyLocationEnabled(true);
|
||||
if (map != null) {
|
||||
map.setMyLocationEnabled(true);
|
||||
}
|
||||
} else {
|
||||
// Permission to access the location is missing. Show rationale and request permission
|
||||
@ -124,7 +123,7 @@ public class MyLocationDemoActivity extends AppCompatActivity
|
||||
// Permission was denied. Display an error message
|
||||
// [START_EXCLUDE]
|
||||
// Display the missing permission error dialog when the fragments resume.
|
||||
mPermissionDenied = true;
|
||||
permissionDenied = true;
|
||||
// [END_EXCLUDE]
|
||||
}
|
||||
}
|
||||
@ -133,10 +132,10 @@ public class MyLocationDemoActivity extends AppCompatActivity
|
||||
@Override
|
||||
protected void onResumeFragments() {
|
||||
super.onResumeFragments();
|
||||
if (mPermissionDenied) {
|
||||
if (permissionDenied) {
|
||||
// Permission was not granted, display error dialog.
|
||||
showMissingPermissionError();
|
||||
mPermissionDenied = false;
|
||||
permissionDenied = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -72,7 +72,7 @@ public abstract class PermissionUtils {
|
||||
|
||||
private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
|
||||
|
||||
private boolean mFinishActivity = false;
|
||||
private boolean finishActivity = false;
|
||||
|
||||
/**
|
||||
* Creates a new instance of this dialog and optionally finishes the calling Activity
|
||||
@ -89,7 +89,7 @@ public abstract class PermissionUtils {
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
mFinishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);
|
||||
finishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);
|
||||
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setMessage(R.string.location_permission_denied)
|
||||
@ -100,7 +100,7 @@ public abstract class PermissionUtils {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
if (mFinishActivity) {
|
||||
if (finishActivity) {
|
||||
Toast.makeText(getActivity(), R.string.permission_required_toast,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
getActivity().finish();
|
||||
@ -122,7 +122,7 @@ public abstract class PermissionUtils {
|
||||
|
||||
private static final String ARGUMENT_FINISH_ACTIVITY = "finish";
|
||||
|
||||
private boolean mFinishActivity = false;
|
||||
private boolean finishActivity = false;
|
||||
|
||||
/**
|
||||
* Creates a new instance of a dialog displaying the rationale for the use of the location
|
||||
@ -149,7 +149,7 @@ public abstract class PermissionUtils {
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Bundle arguments = getArguments();
|
||||
final int requestCode = arguments.getInt(ARGUMENT_PERMISSION_REQUEST_CODE);
|
||||
mFinishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);
|
||||
finishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);
|
||||
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setMessage(R.string.permission_rationale_location)
|
||||
@ -161,7 +161,7 @@ public abstract class PermissionUtils {
|
||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
|
||||
requestCode);
|
||||
// Do not finish the Activity while requesting permission.
|
||||
mFinishActivity = false;
|
||||
finishActivity = false;
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
@ -171,7 +171,7 @@ public abstract class PermissionUtils {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
if (mFinishActivity) {
|
||||
if (finishActivity) {
|
||||
Toast.makeText(getActivity(),
|
||||
R.string.permission_required_toast,
|
||||
Toast.LENGTH_SHORT)
|
||||
|
||||
@ -22,7 +22,6 @@
|
||||
<fragment
|
||||
android:id="@+id/map"
|
||||
class="com.google.android.gms.maps.SupportMapFragment"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
</FrameLayout>
|
||||
|
||||
@ -49,6 +49,8 @@ class DemoDetailsList {
|
||||
LiteListDemoActivity::class.java),
|
||||
DemoDetails(R.string.markers_demo_label, R.string.markers_demo_description,
|
||||
MarkerDemoActivity::class.java),
|
||||
DemoDetails(R.string.my_location_demo_label, R.string.my_location_demo_details,
|
||||
MyLocationDemoActivity::class.java),
|
||||
DemoDetails(R.string.polygon_demo_label, R.string.polygon_demo_details,
|
||||
PolygonDemoActivity::class.java),
|
||||
DemoDetails(R.string.polyline_demo_label, R.string.polyline_demo_description,
|
||||
|
||||
@ -0,0 +1,132 @@
|
||||
// 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.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.location.Location
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.example.kotlindemos.PermissionUtils.PermissionDeniedDialog.Companion.newInstance
|
||||
import com.example.kotlindemos.PermissionUtils.isPermissionGranted
|
||||
import com.example.kotlindemos.PermissionUtils.requestPermission
|
||||
import com.google.android.gms.maps.GoogleMap
|
||||
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener
|
||||
import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener
|
||||
import com.google.android.gms.maps.OnMapReadyCallback
|
||||
import com.google.android.gms.maps.SupportMapFragment
|
||||
|
||||
/**
|
||||
* This demo shows how GMS Location can be used to check for changes to the users location. The
|
||||
* "My Location" button uses GMS Location to set the blue dot representing the users location.
|
||||
* Permission for [Manifest.permission.ACCESS_FINE_LOCATION] is requested at run
|
||||
* time. If the permission has not been granted, the Activity is finished with an error message.
|
||||
*/
|
||||
class MyLocationDemoActivity : AppCompatActivity(), OnMyLocationButtonClickListener,
|
||||
OnMyLocationClickListener, OnMapReadyCallback, OnRequestPermissionsResultCallback {
|
||||
/**
|
||||
* Flag indicating whether a requested permission has been denied after returning in
|
||||
* [.onRequestPermissionsResult].
|
||||
*/
|
||||
private var permissionDenied = false
|
||||
private lateinit var map: GoogleMap
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.my_location_demo)
|
||||
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
|
||||
mapFragment?.getMapAsync(this)
|
||||
}
|
||||
|
||||
override fun onMapReady(googleMap: GoogleMap?) {
|
||||
map = googleMap ?: return
|
||||
googleMap.setOnMyLocationButtonClickListener(this)
|
||||
googleMap.setOnMyLocationClickListener(this)
|
||||
enableMyLocation()
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the My Location layer if the fine location permission has been granted.
|
||||
*/
|
||||
private fun enableMyLocation() {
|
||||
if (!::map.isInitialized) return
|
||||
// [START maps_check_location_permission]
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
map.isMyLocationEnabled = true
|
||||
} else {
|
||||
// Permission to access the location is missing. Show rationale and request permission
|
||||
requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION, true
|
||||
)
|
||||
}
|
||||
// [END maps_check_location_permission]
|
||||
}
|
||||
|
||||
override fun onMyLocationButtonClick(): Boolean {
|
||||
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show()
|
||||
// Return false so that we don't consume the event and the default behavior still occurs
|
||||
// (the camera animates to the user's current position).
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onMyLocationClick(location: Location) {
|
||||
Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
// [START maps_check_location_permission_result]
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
|
||||
return
|
||||
}
|
||||
if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
|
||||
// Enable the my location layer if the permission has been granted.
|
||||
enableMyLocation()
|
||||
} else {
|
||||
// Permission was denied. Display an error message
|
||||
// [START_EXCLUDE]
|
||||
// Display the missing permission error dialog when the fragments resume.
|
||||
permissionDenied = true
|
||||
// [END_EXCLUDE]
|
||||
}
|
||||
}
|
||||
|
||||
// [END maps_check_location_permission_result]
|
||||
override fun onResumeFragments() {
|
||||
super.onResumeFragments()
|
||||
if (permissionDenied) {
|
||||
// Permission was not granted, display error dialog.
|
||||
showMissingPermissionError()
|
||||
permissionDenied = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a dialog with error message explaining that the location permission is missing.
|
||||
*/
|
||||
private fun showMissingPermissionError() {
|
||||
newInstance(true).show(supportFragmentManager, "dialog")
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Request code for location permission request.
|
||||
*
|
||||
* @see .onRequestPermissionsResult
|
||||
*/
|
||||
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,188 @@
|
||||
// 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.Manifest
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.content.DialogInterface
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.fragment.app.DialogFragment
|
||||
|
||||
/**
|
||||
* Utility class for access to runtime permissions.
|
||||
*/
|
||||
object PermissionUtils {
|
||||
/**
|
||||
* Requests the fine location permission. If a rationale with an additional explanation should
|
||||
* be shown to the user, displays a dialog that triggers the request.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun requestPermission(
|
||||
activity: AppCompatActivity, requestId: Int,
|
||||
permission: String, finishActivity: Boolean
|
||||
) {
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
|
||||
// Display a dialog with rationale.
|
||||
RationaleDialog.newInstance(requestId, finishActivity)
|
||||
.show(activity.supportFragmentManager, "dialog")
|
||||
} else {
|
||||
// Location permission has not been granted yet, request it.
|
||||
ActivityCompat.requestPermissions(
|
||||
activity,
|
||||
arrayOf(permission),
|
||||
requestId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the result contains a [PackageManager.PERMISSION_GRANTED] result for a
|
||||
* permission from a runtime permissions request.
|
||||
*
|
||||
* @see androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback
|
||||
*/
|
||||
@JvmStatic
|
||||
fun isPermissionGranted(
|
||||
grantPermissions: Array<String>, grantResults: IntArray,
|
||||
permission: String
|
||||
): Boolean {
|
||||
for (i in grantPermissions.indices) {
|
||||
if (permission == grantPermissions[i]) {
|
||||
return grantResults[i] == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* A dialog that displays a permission denied message.
|
||||
*/
|
||||
class PermissionDeniedDialog : DialogFragment() {
|
||||
private var finishActivity = false
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
finishActivity =
|
||||
arguments?.getBoolean(ARGUMENT_FINISH_ACTIVITY) ?: false
|
||||
return AlertDialog.Builder(activity)
|
||||
.setMessage(R.string.location_permission_denied)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.create()
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
if (finishActivity) {
|
||||
Toast.makeText(
|
||||
activity, R.string.permission_required_toast,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
activity?.finish()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARGUMENT_FINISH_ACTIVITY = "finish"
|
||||
|
||||
/**
|
||||
* Creates a new instance of this dialog and optionally finishes the calling Activity
|
||||
* when the 'Ok' button is clicked.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun newInstance(finishActivity: Boolean): PermissionDeniedDialog {
|
||||
val arguments = Bundle().apply {
|
||||
putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity)
|
||||
}
|
||||
return PermissionDeniedDialog().apply {
|
||||
this.arguments = arguments
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A dialog that explains the use of the location permission and requests the necessary
|
||||
* permission.
|
||||
*
|
||||
*
|
||||
* The activity should implement
|
||||
* [androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback]
|
||||
* to handle permit or denial of this permission request.
|
||||
*/
|
||||
class RationaleDialog : DialogFragment() {
|
||||
private var finishActivity = false
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val requestCode =
|
||||
arguments?.getInt(ARGUMENT_PERMISSION_REQUEST_CODE) ?: 0
|
||||
finishActivity =
|
||||
arguments?.getBoolean(ARGUMENT_FINISH_ACTIVITY) ?: false
|
||||
return AlertDialog.Builder(activity)
|
||||
.setMessage(R.string.permission_rationale_location)
|
||||
.setPositiveButton(android.R.string.ok) { dialog, which -> // After click on Ok, request the permission.
|
||||
ActivityCompat.requestPermissions(
|
||||
activity!!,
|
||||
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
|
||||
requestCode
|
||||
)
|
||||
// Do not finish the Activity while requesting permission.
|
||||
finishActivity = false
|
||||
}
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.create()
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
if (finishActivity) {
|
||||
Toast.makeText(
|
||||
activity,
|
||||
R.string.permission_required_toast,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
activity?.finish()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARGUMENT_PERMISSION_REQUEST_CODE = "requestCode"
|
||||
private const val ARGUMENT_FINISH_ACTIVITY = "finish"
|
||||
|
||||
/**
|
||||
* Creates a new instance of a dialog displaying the rationale for the use of the location
|
||||
* permission.
|
||||
*
|
||||
*
|
||||
* The permission is requested after clicking 'ok'.
|
||||
*
|
||||
* @param requestCode Id of the request that is used to request the permission. It is
|
||||
* returned to the
|
||||
* [androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback].
|
||||
* @param finishActivity Whether the calling Activity should be finished if the dialog is
|
||||
* cancelled.
|
||||
*/
|
||||
fun newInstance(requestCode: Int, finishActivity: Boolean): RationaleDialog {
|
||||
val arguments = Bundle().apply {
|
||||
putInt(ARGUMENT_PERMISSION_REQUEST_CODE, requestCode)
|
||||
putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity)
|
||||
}
|
||||
return RationaleDialog().apply {
|
||||
this.arguments = arguments
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
ApiDemos/kotlin/app/src/gms/res/layout/my_location_demo.xml
Normal file
27
ApiDemos/kotlin/app/src/gms/res/layout/my_location_demo.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
Copyright (C) 2012 The Android Open Source Project
|
||||
|
||||
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.
|
||||
-->
|
||||
<!-- This can go anywhere in your layout (see other demos for some examples). -->
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/layout">
|
||||
|
||||
<fragment
|
||||
android:id="@+id/map"
|
||||
class="com.google.android.gms.maps.SupportMapFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
</FrameLayout>
|
||||
@ -66,6 +66,7 @@
|
||||
<activity android:name=".EventsDemoActivity" />
|
||||
<activity android:name=".GroundOverlayDemoActivity" />
|
||||
<activity android:name=".IndoorDemoActivity" />
|
||||
<activity android:name=".MyLocationDemoActivity" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@ -121,6 +121,10 @@
|
||||
<string name="rotation">Rotation</string>
|
||||
<string name="state_badge_label">State badge</string>
|
||||
|
||||
<!-- MyLocationDemo-->
|
||||
<string name="my_location_demo_label">My Location Demo</string>
|
||||
<string name="my_location_demo_details">Demonstrates how to use GMS Location.</string>
|
||||
|
||||
<!-- Permissions -->
|
||||
<string name="permission_rationale_location">Access to the location service is required to demonstrate the \'my location\' feature, which shows your current location on the map.</string>
|
||||
<string name="location_permission_denied">This sample requires location permission to enable the \'my location\' layer. Please try again and grant access to use the location.\nIf the permission has been permanently denied, it can be enabled from the System Settings > Apps > \'Google Maps API Demos\'.</string>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user