diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/CircleDemoActivity.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/CircleDemoActivity.java index ea177eda..9d50a66a 100755 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/CircleDemoActivity.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/CircleDemoActivity.java @@ -21,6 +21,21 @@ package com.example.mapdemo; +import android.graphics.Color; +import android.graphics.Point; +import android.location.Location; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.SeekBar; +import android.widget.SeekBar.OnSeekBarChangeListener; +import android.widget.Spinner; + +import androidx.appcompat.app.AppCompatActivity; + import com.google.android.libraries.maps.CameraUpdateFactory; import com.google.android.libraries.maps.GoogleMap; import com.google.android.libraries.maps.GoogleMap.OnCircleClickListener; @@ -39,21 +54,6 @@ import com.google.android.libraries.maps.model.Marker; import com.google.android.libraries.maps.model.MarkerOptions; import com.google.android.libraries.maps.model.PatternItem; -import android.graphics.Color; -import android.graphics.Point; -import android.location.Location; -import android.os.Bundle; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemSelectedListener; -import android.widget.ArrayAdapter; -import android.widget.CheckBox; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.Spinner; - -import androidx.appcompat.app.AppCompatActivity; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -83,20 +83,20 @@ public class CircleDemoActivity extends AppCompatActivity private static final List PATTERN_DASHED = Arrays.asList(DASH, GAP); private static final List PATTERN_MIXED = Arrays.asList(DOT, GAP, DOT, DASH, GAP); - private GoogleMap mMap; + private GoogleMap map; - private List mCircles = new ArrayList<>(1); + private List circles = new ArrayList<>(1); - private int mFillColorArgb; - private int mStrokeColorArgb; + private int fillColorArgb; + private int strokeColorArgb; - private SeekBar mFillHueBar; - private SeekBar mFillAlphaBar; - private SeekBar mStrokeWidthBar; - private SeekBar mStrokeHueBar; - private SeekBar mStrokeAlphaBar; - private Spinner mStrokePatternSpinner; - private CheckBox mClickabilityCheckbox; + private SeekBar fillHueBar; + private SeekBar fillAlphaBar; + private SeekBar strokeWidthBar; + private SeekBar strokeHueBar; + private SeekBar strokeAlphaBar; + private Spinner strokePatternSpinner; + private CheckBox clickabilityCheckbox; // These are the options for stroke patterns. We use their // string resource IDs as identifiers. @@ -109,57 +109,57 @@ public class CircleDemoActivity extends AppCompatActivity }; private class DraggableCircle { - private final Marker mCenterMarker; - private final Marker mRadiusMarker; - private final Circle mCircle; - private double mRadiusMeters; + private final Marker centerMarker; + private final Marker radiusMarker; + private final Circle circle; + private double radiusMeters; public DraggableCircle(LatLng center, double radiusMeters) { - mRadiusMeters = radiusMeters; - mCenterMarker = mMap.addMarker(new MarkerOptions() + this.radiusMeters = radiusMeters; + centerMarker = map.addMarker(new MarkerOptions() .position(center) .draggable(true)); - mRadiusMarker = mMap.addMarker(new MarkerOptions() + radiusMarker = map.addMarker(new MarkerOptions() .position(toRadiusLatLng(center, radiusMeters)) .draggable(true) .icon(BitmapDescriptorFactory.defaultMarker( BitmapDescriptorFactory.HUE_AZURE))); - mCircle = mMap.addCircle(new CircleOptions() + circle = map.addCircle(new CircleOptions() .center(center) .radius(radiusMeters) - .strokeWidth(mStrokeWidthBar.getProgress()) - .strokeColor(mStrokeColorArgb) - .fillColor(mFillColorArgb) - .clickable(mClickabilityCheckbox.isChecked())); + .strokeWidth(strokeWidthBar.getProgress()) + .strokeColor(strokeColorArgb) + .fillColor(fillColorArgb) + .clickable(clickabilityCheckbox.isChecked())); } public boolean onMarkerMoved(Marker marker) { - if (marker.equals(mCenterMarker)) { - mCircle.setCenter(marker.getPosition()); - mRadiusMarker.setPosition(toRadiusLatLng(marker.getPosition(), mRadiusMeters)); + if (marker.equals(centerMarker)) { + circle.setCenter(marker.getPosition()); + radiusMarker.setPosition(toRadiusLatLng(marker.getPosition(), radiusMeters)); return true; } - if (marker.equals(mRadiusMarker)) { - mRadiusMeters = - toRadiusMeters(mCenterMarker.getPosition(), mRadiusMarker.getPosition()); - mCircle.setRadius(mRadiusMeters); + if (marker.equals(radiusMarker)) { + radiusMeters = + toRadiusMeters(centerMarker.getPosition(), radiusMarker.getPosition()); + circle.setRadius(radiusMeters); return true; } return false; } public void onStyleChange() { - mCircle.setStrokeWidth(mStrokeWidthBar.getProgress()); - mCircle.setStrokeColor(mStrokeColorArgb); - mCircle.setFillColor(mFillColorArgb); + circle.setStrokeWidth(strokeWidthBar.getProgress()); + circle.setStrokeColor(strokeColorArgb); + circle.setFillColor(fillColorArgb); } public void setStrokePattern(List pattern) { - mCircle.setStrokePattern(pattern); + circle.setStrokePattern(pattern); } public void setClickable(boolean clickable) { - mCircle.setClickable(clickable); + circle.setClickable(clickable); } } @@ -182,32 +182,32 @@ public class CircleDemoActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.circle_demo); - mFillHueBar = (SeekBar) findViewById(R.id.fillHueSeekBar); - mFillHueBar.setMax(MAX_HUE_DEGREES); - mFillHueBar.setProgress(MAX_HUE_DEGREES / 2); + fillHueBar = findViewById(R.id.fillHueSeekBar); + fillHueBar.setMax(MAX_HUE_DEGREES); + fillHueBar.setProgress(MAX_HUE_DEGREES / 2); - mFillAlphaBar = (SeekBar) findViewById(R.id.fillAlphaSeekBar); - mFillAlphaBar.setMax(MAX_ALPHA); - mFillAlphaBar.setProgress(MAX_ALPHA / 2); + fillAlphaBar = findViewById(R.id.fillAlphaSeekBar); + fillAlphaBar.setMax(MAX_ALPHA); + fillAlphaBar.setProgress(MAX_ALPHA / 2); - mStrokeWidthBar = (SeekBar) findViewById(R.id.strokeWidthSeekBar); - mStrokeWidthBar.setMax(MAX_WIDTH_PX); - mStrokeWidthBar.setProgress(MAX_WIDTH_PX / 3); + strokeWidthBar = findViewById(R.id.strokeWidthSeekBar); + strokeWidthBar.setMax(MAX_WIDTH_PX); + strokeWidthBar.setProgress(MAX_WIDTH_PX / 3); - mStrokeHueBar = (SeekBar) findViewById(R.id.strokeHueSeekBar); - mStrokeHueBar.setMax(MAX_HUE_DEGREES); - mStrokeHueBar.setProgress(0); + strokeHueBar = findViewById(R.id.strokeHueSeekBar); + strokeHueBar.setMax(MAX_HUE_DEGREES); + strokeHueBar.setProgress(0); - mStrokeAlphaBar = (SeekBar) findViewById(R.id.strokeAlphaSeekBar); - mStrokeAlphaBar.setMax(MAX_ALPHA); - mStrokeAlphaBar.setProgress(MAX_ALPHA); + strokeAlphaBar = findViewById(R.id.strokeAlphaSeekBar); + strokeAlphaBar.setMax(MAX_ALPHA); + strokeAlphaBar.setProgress(MAX_ALPHA); - mStrokePatternSpinner = (Spinner) findViewById(R.id.strokePatternSpinner); - mStrokePatternSpinner.setAdapter(new ArrayAdapter<>( + strokePatternSpinner = findViewById(R.id.strokePatternSpinner); + strokePatternSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(PATTERN_TYPE_NAME_RESOURCE_IDS))); - mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability); + clickabilityCheckbox = findViewById(R.id.toggleClickability); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); @@ -223,36 +223,36 @@ public class CircleDemoActivity extends AppCompatActivity } @Override - public void onMapReady(GoogleMap map) { + public void onMapReady(GoogleMap googleMap) { // Override the default content description on the view, for accessibility mode. - map.setContentDescription(getString(R.string.map_circle_description)); + googleMap.setContentDescription(getString(R.string.map_circle_description)); - mMap = map; - mMap.setOnMarkerDragListener(this); - mMap.setOnMapLongClickListener(this); + map = googleMap; + map.setOnMarkerDragListener(this); + map.setOnMapLongClickListener(this); - mFillColorArgb = Color.HSVToColor( - mFillAlphaBar.getProgress(), new float[]{mFillHueBar.getProgress(), 1, 1}); - mStrokeColorArgb = Color.HSVToColor( - mStrokeAlphaBar.getProgress(), new float[]{mStrokeHueBar.getProgress(), 1, 1}); + fillColorArgb = Color.HSVToColor( + fillAlphaBar.getProgress(), new float[]{fillHueBar.getProgress(), 1, 1}); + strokeColorArgb = Color.HSVToColor( + strokeAlphaBar.getProgress(), new float[]{strokeHueBar.getProgress(), 1, 1}); - mFillHueBar.setOnSeekBarChangeListener(this); - mFillAlphaBar.setOnSeekBarChangeListener(this); + fillHueBar.setOnSeekBarChangeListener(this); + fillAlphaBar.setOnSeekBarChangeListener(this); - mStrokeWidthBar.setOnSeekBarChangeListener(this); - mStrokeHueBar.setOnSeekBarChangeListener(this); - mStrokeAlphaBar.setOnSeekBarChangeListener(this); + strokeWidthBar.setOnSeekBarChangeListener(this); + strokeHueBar.setOnSeekBarChangeListener(this); + strokeAlphaBar.setOnSeekBarChangeListener(this); - mStrokePatternSpinner.setOnItemSelectedListener(this); + strokePatternSpinner.setOnItemSelectedListener(this); DraggableCircle circle = new DraggableCircle(SYDNEY, DEFAULT_RADIUS_METERS); - mCircles.add(circle); + circles.add(circle); // Move the map so that it is centered on the initial circle - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 4.0f)); + map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 4.0f)); // Set up the click listener for the circle. - map.setOnCircleClickListener(new OnCircleClickListener() { + googleMap.setOnCircleClickListener(new OnCircleClickListener() { @Override public void onCircleClick(Circle circle) { // Flip the red, green and blue components of the circle's stroke color. @@ -260,8 +260,8 @@ public class CircleDemoActivity extends AppCompatActivity } }); - List pattern = getSelectedPattern(mStrokePatternSpinner.getSelectedItemPosition()); - for (DraggableCircle draggableCircle : mCircles) { + List pattern = getSelectedPattern(strokePatternSpinner.getSelectedItemPosition()); + for (DraggableCircle draggableCircle : circles) { draggableCircle.setStrokePattern(pattern); } } @@ -284,7 +284,7 @@ public class CircleDemoActivity extends AppCompatActivity @Override public void onItemSelected(AdapterView parent, View view, int pos, long id) { if (parent.getId() == R.id.strokePatternSpinner) { - for (DraggableCircle draggableCircle : mCircles) { + for (DraggableCircle draggableCircle : circles) { draggableCircle.setStrokePattern(getSelectedPattern(pos)); } } @@ -307,21 +307,21 @@ public class CircleDemoActivity extends AppCompatActivity @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (seekBar == mFillHueBar) { - mFillColorArgb = - Color.HSVToColor(Color.alpha(mFillColorArgb), new float[]{progress, 1, 1}); - } else if (seekBar == mFillAlphaBar) { - mFillColorArgb = Color.argb(progress, Color.red(mFillColorArgb), - Color.green(mFillColorArgb), Color.blue(mFillColorArgb)); - } else if (seekBar == mStrokeHueBar) { - mStrokeColorArgb = - Color.HSVToColor(Color.alpha(mStrokeColorArgb), new float[]{progress, 1, 1}); - } else if (seekBar == mStrokeAlphaBar) { - mStrokeColorArgb = Color.argb(progress, Color.red(mStrokeColorArgb), - Color.green(mStrokeColorArgb), Color.blue(mStrokeColorArgb)); + if (seekBar == fillHueBar) { + fillColorArgb = + Color.HSVToColor(Color.alpha(fillColorArgb), new float[]{progress, 1, 1}); + } else if (seekBar == fillAlphaBar) { + fillColorArgb = Color.argb(progress, Color.red(fillColorArgb), + Color.green(fillColorArgb), Color.blue(fillColorArgb)); + } else if (seekBar == strokeHueBar) { + strokeColorArgb = + Color.HSVToColor(Color.alpha(strokeColorArgb), new float[]{progress, 1, 1}); + } else if (seekBar == strokeAlphaBar) { + strokeColorArgb = Color.argb(progress, Color.red(strokeColorArgb), + Color.green(strokeColorArgb), Color.blue(strokeColorArgb)); } - for (DraggableCircle draggableCircle : mCircles) { + for (DraggableCircle draggableCircle : circles) { draggableCircle.onStyleChange(); } } @@ -342,7 +342,7 @@ public class CircleDemoActivity extends AppCompatActivity } private void onMarkerMoved(Marker marker) { - for (DraggableCircle draggableCircle : mCircles) { + for (DraggableCircle draggableCircle : circles) { if (draggableCircle.onMarkerMoved(marker)) { break; } @@ -353,19 +353,19 @@ public class CircleDemoActivity extends AppCompatActivity public void onMapLongClick(LatLng point) { // We know the center, let's place the outline at a point 3/4 along the view. View view = getSupportFragmentManager().findFragmentById(R.id.map).getView(); - LatLng radiusLatLng = mMap.getProjection().fromScreenLocation(new Point( + LatLng radiusLatLng = map.getProjection().fromScreenLocation(new Point( view.getHeight() * 3 / 4, view.getWidth() * 3 / 4)); // Create the circle. DraggableCircle circle = new DraggableCircle(point, toRadiusMeters(point, radiusLatLng)); - mCircles.add(circle); + circles.add(circle); } public void toggleClickability(View view) { boolean clickable = ((CheckBox) view).isChecked(); // Set each of the circles to be clickable or not, based on the // state of the checkbox. - for (DraggableCircle draggableCircle : mCircles) { + for (DraggableCircle draggableCircle : circles) { draggableCircle.setClickable(clickable); } } diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MyLocationDemoActivity.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MyLocationDemoActivity.java index 66227b40..822b8165 100755 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MyLocationDemoActivity.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MyLocationDemoActivity.java @@ -63,9 +63,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) { @@ -78,11 +78,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(); } @@ -93,8 +92,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 @@ -131,7 +130,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] } } @@ -140,10 +139,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; } } diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PermissionUtils.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PermissionUtils.java index 1e56b6bd..21094cb6 100755 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PermissionUtils.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PermissionUtils.java @@ -79,7 +79,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 @@ -96,7 +96,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) @@ -107,7 +107,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(); @@ -129,7 +129,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 @@ -156,7 +156,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) @@ -168,7 +168,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) @@ -178,7 +178,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) diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolygonDemoActivity.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolygonDemoActivity.java index fc3e3f0d..7a5c167d 100644 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolygonDemoActivity.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolygonDemoActivity.java @@ -22,19 +22,6 @@ package com.example.mapdemo; -import com.google.android.libraries.maps.CameraUpdateFactory; -import com.google.android.libraries.maps.GoogleMap; -import com.google.android.libraries.maps.OnMapReadyCallback; -import com.google.android.libraries.maps.SupportMapFragment; -import com.google.android.libraries.maps.model.Dash; -import com.google.android.libraries.maps.model.Dot; -import com.google.android.libraries.maps.model.Gap; -import com.google.android.libraries.maps.model.JointType; -import com.google.android.libraries.maps.model.LatLng; -import com.google.android.libraries.maps.model.PatternItem; -import com.google.android.libraries.maps.model.Polygon; -import com.google.android.libraries.maps.model.PolygonOptions; - import android.graphics.Color; import android.os.Bundle; import android.view.View; @@ -48,6 +35,19 @@ import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; +import com.google.android.libraries.maps.CameraUpdateFactory; +import com.google.android.libraries.maps.GoogleMap; +import com.google.android.libraries.maps.OnMapReadyCallback; +import com.google.android.libraries.maps.SupportMapFragment; +import com.google.android.libraries.maps.model.Dash; +import com.google.android.libraries.maps.model.Dot; +import com.google.android.libraries.maps.model.Gap; +import com.google.android.libraries.maps.model.JointType; +import com.google.android.libraries.maps.model.LatLng; +import com.google.android.libraries.maps.model.PatternItem; +import com.google.android.libraries.maps.model.Polygon; +import com.google.android.libraries.maps.model.PolygonOptions; + import java.util.Arrays; import java.util.List; @@ -71,15 +71,15 @@ public class PolygonDemoActivity extends AppCompatActivity private static final List PATTERN_DASHED = Arrays.asList(DASH, GAP); private static final List PATTERN_MIXED = Arrays.asList(DOT, GAP, DOT, DASH, GAP); - private Polygon mMutablePolygon; - private SeekBar mFillHueBar; - private SeekBar mFillAlphaBar; - private SeekBar mStrokeWidthBar; - private SeekBar mStrokeHueBar; - private SeekBar mStrokeAlphaBar; - private Spinner mStrokeJointTypeSpinner; - private Spinner mStrokePatternSpinner; - private CheckBox mClickabilityCheckbox; + private Polygon mutablePolygon; + private SeekBar fillHueBar; + private SeekBar fillAlphaBar; + private SeekBar strokeWidthBar; + private SeekBar strokeHueBar; + private SeekBar strokeAlphaBar; + private Spinner strokeJointTypeSpinner; + private Spinner strokePatternSpinner; + private CheckBox clickabilityCheckbox; // These are the options for polygon stroke joints and patterns. We use their // string resource IDs as identifiers. @@ -102,37 +102,37 @@ public class PolygonDemoActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.polygon_demo); - mFillHueBar = (SeekBar) findViewById(R.id.fillHueSeekBar); - mFillHueBar.setMax(MAX_HUE_DEGREES); - mFillHueBar.setProgress(MAX_HUE_DEGREES / 2); + fillHueBar = findViewById(R.id.fillHueSeekBar); + fillHueBar.setMax(MAX_HUE_DEGREES); + fillHueBar.setProgress(MAX_HUE_DEGREES / 2); - mFillAlphaBar = (SeekBar) findViewById(R.id.fillAlphaSeekBar); - mFillAlphaBar.setMax(MAX_ALPHA); - mFillAlphaBar.setProgress(MAX_ALPHA / 2); + fillAlphaBar = findViewById(R.id.fillAlphaSeekBar); + fillAlphaBar.setMax(MAX_ALPHA); + fillAlphaBar.setProgress(MAX_ALPHA / 2); - mStrokeWidthBar = (SeekBar) findViewById(R.id.strokeWidthSeekBar); - mStrokeWidthBar.setMax(MAX_WIDTH_PX); - mStrokeWidthBar.setProgress(MAX_WIDTH_PX / 3); + strokeWidthBar = findViewById(R.id.strokeWidthSeekBar); + strokeWidthBar.setMax(MAX_WIDTH_PX); + strokeWidthBar.setProgress(MAX_WIDTH_PX / 3); - mStrokeHueBar = (SeekBar) findViewById(R.id.strokeHueSeekBar); - mStrokeHueBar.setMax(MAX_HUE_DEGREES); - mStrokeHueBar.setProgress(0); + strokeHueBar = findViewById(R.id.strokeHueSeekBar); + strokeHueBar.setMax(MAX_HUE_DEGREES); + strokeHueBar.setProgress(0); - mStrokeAlphaBar = (SeekBar) findViewById(R.id.strokeAlphaSeekBar); - mStrokeAlphaBar.setMax(MAX_ALPHA); - mStrokeAlphaBar.setProgress(MAX_ALPHA); + strokeAlphaBar = findViewById(R.id.strokeAlphaSeekBar); + strokeAlphaBar.setMax(MAX_ALPHA); + strokeAlphaBar.setProgress(MAX_ALPHA); - mStrokeJointTypeSpinner = (Spinner) findViewById(R.id.strokeJointTypeSpinner); - mStrokeJointTypeSpinner.setAdapter(new ArrayAdapter<>( + strokeJointTypeSpinner = findViewById(R.id.strokeJointTypeSpinner); + strokeJointTypeSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(JOINT_TYPE_NAME_RESOURCE_IDS))); - mStrokePatternSpinner = (Spinner) findViewById(R.id.strokePatternSpinner); - mStrokePatternSpinner.setAdapter(new ArrayAdapter<>( + strokePatternSpinner = findViewById(R.id.strokePatternSpinner); + strokePatternSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(PATTERN_TYPE_NAME_RESOURCE_IDS))); - mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability); + clickabilityCheckbox = findViewById(R.id.toggleClickability); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); @@ -153,32 +153,32 @@ public class PolygonDemoActivity extends AppCompatActivity map.setContentDescription(getString(R.string.polygon_demo_description)); int fillColorArgb = Color.HSVToColor( - mFillAlphaBar.getProgress(), new float[]{mFillHueBar.getProgress(), 1, 1}); + fillAlphaBar.getProgress(), new float[]{fillHueBar.getProgress(), 1, 1}); int strokeColorArgb = Color.HSVToColor( - mStrokeAlphaBar.getProgress(), new float[]{mStrokeHueBar.getProgress(), 1, 1}); + strokeAlphaBar.getProgress(), new float[]{strokeHueBar.getProgress(), 1, 1}); // Create a rectangle with two rectangular holes. - mMutablePolygon = map.addPolygon(new PolygonOptions() + mutablePolygon = map.addPolygon(new PolygonOptions() .addAll(createRectangle(CENTER, 5, 5)) .addHole(createRectangle(new LatLng(-22, 128), 1, 1)) .addHole(createRectangle(new LatLng(-18, 133), 0.5, 1.5)) .fillColor(fillColorArgb) .strokeColor(strokeColorArgb) - .strokeWidth(mStrokeWidthBar.getProgress()) - .clickable(mClickabilityCheckbox.isChecked())); + .strokeWidth(strokeWidthBar.getProgress()) + .clickable(clickabilityCheckbox.isChecked())); - mFillHueBar.setOnSeekBarChangeListener(this); - mFillAlphaBar.setOnSeekBarChangeListener(this); + fillHueBar.setOnSeekBarChangeListener(this); + fillAlphaBar.setOnSeekBarChangeListener(this); - mStrokeWidthBar.setOnSeekBarChangeListener(this); - mStrokeHueBar.setOnSeekBarChangeListener(this); - mStrokeAlphaBar.setOnSeekBarChangeListener(this); + strokeWidthBar.setOnSeekBarChangeListener(this); + strokeHueBar.setOnSeekBarChangeListener(this); + strokeAlphaBar.setOnSeekBarChangeListener(this); - mStrokeJointTypeSpinner.setOnItemSelectedListener(this); - mStrokePatternSpinner.setOnItemSelectedListener(this); + strokeJointTypeSpinner.setOnItemSelectedListener(this); + strokePatternSpinner.setOnItemSelectedListener(this); - mMutablePolygon.setStrokeJointType(getSelectedJointType(mStrokeJointTypeSpinner.getSelectedItemPosition())); - mMutablePolygon.setStrokePattern(getSelectedPattern(mStrokePatternSpinner.getSelectedItemPosition())); + mutablePolygon.setStrokeJointType(getSelectedJointType(strokeJointTypeSpinner.getSelectedItemPosition())); + mutablePolygon.setStrokePattern(getSelectedPattern(strokePatternSpinner.getSelectedItemPosition())); // Move the map so that it is centered on the mutable polygon. map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTER, 4)); @@ -235,10 +235,10 @@ public class PolygonDemoActivity extends AppCompatActivity public void onItemSelected(AdapterView parent, View view, int pos, long id) { switch (parent.getId()) { case R.id.strokeJointTypeSpinner: - mMutablePolygon.setStrokeJointType(getSelectedJointType(pos)); + mutablePolygon.setStrokeJointType(getSelectedJointType(pos)); break; case R.id.strokePatternSpinner: - mMutablePolygon.setStrokePattern(getSelectedPattern(pos)); + mutablePolygon.setStrokePattern(getSelectedPattern(pos)); break; } } @@ -260,28 +260,28 @@ public class PolygonDemoActivity extends AppCompatActivity @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (mMutablePolygon == null) { + if (mutablePolygon == null) { return; } - if (seekBar == mFillHueBar) { - mMutablePolygon.setFillColor(Color.HSVToColor( - Color.alpha(mMutablePolygon.getFillColor()), new float[]{progress, 1, 1})); - } else if (seekBar == mFillAlphaBar) { - int prevColor = mMutablePolygon.getFillColor(); - mMutablePolygon.setFillColor(Color.argb( + if (seekBar == fillHueBar) { + mutablePolygon.setFillColor(Color.HSVToColor( + Color.alpha(mutablePolygon.getFillColor()), new float[]{progress, 1, 1})); + } else if (seekBar == fillAlphaBar) { + int prevColor = mutablePolygon.getFillColor(); + mutablePolygon.setFillColor(Color.argb( progress, Color.red(prevColor), Color.green(prevColor), Color.blue(prevColor))); - } else if (seekBar == mStrokeHueBar) { - mMutablePolygon.setStrokeColor(Color.HSVToColor( - Color.alpha(mMutablePolygon.getStrokeColor()), new float[]{progress, 1, 1})); - } else if (seekBar == mStrokeAlphaBar) { - int prevColorArgb = mMutablePolygon.getStrokeColor(); - mMutablePolygon.setStrokeColor(Color.argb( + } else if (seekBar == strokeHueBar) { + mutablePolygon.setStrokeColor(Color.HSVToColor( + Color.alpha(mutablePolygon.getStrokeColor()), new float[]{progress, 1, 1})); + } else if (seekBar == strokeAlphaBar) { + int prevColorArgb = mutablePolygon.getStrokeColor(); + mutablePolygon.setStrokeColor(Color.argb( progress, Color.red(prevColorArgb), Color.green(prevColorArgb), Color.blue(prevColorArgb))); - } else if (seekBar == mStrokeWidthBar) { - mMutablePolygon.setStrokeWidth(progress); + } else if (seekBar == strokeWidthBar) { + mutablePolygon.setStrokeWidth(progress); } } @@ -291,8 +291,8 @@ public class PolygonDemoActivity extends AppCompatActivity * This callback is defined on the CheckBox in the layout for this Activity. */ public void toggleClickability(View view) { - if (mMutablePolygon != null) { - mMutablePolygon.setClickable(((CheckBox) view).isChecked()); + if (mutablePolygon != null) { + mutablePolygon.setClickable(((CheckBox) view).isChecked()); } } } \ No newline at end of file diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolylineDemoActivity.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolylineDemoActivity.java index 2bf95168..ec4bd53a 100644 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolylineDemoActivity.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/PolylineDemoActivity.java @@ -22,6 +22,19 @@ package com.example.mapdemo; +import android.graphics.Color; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.SeekBar; +import android.widget.SeekBar.OnSeekBarChangeListener; +import android.widget.Spinner; + +import androidx.appcompat.app.AppCompatActivity; + import com.google.android.libraries.maps.CameraUpdateFactory; import com.google.android.libraries.maps.GoogleMap; import com.google.android.libraries.maps.OnMapReadyCallback; @@ -41,19 +54,6 @@ import com.google.android.libraries.maps.model.PolylineOptions; import com.google.android.libraries.maps.model.RoundCap; import com.google.android.libraries.maps.model.SquareCap; -import android.graphics.Color; -import android.os.Bundle; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemSelectedListener; -import android.widget.ArrayAdapter; -import android.widget.CheckBox; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.Spinner; - -import androidx.appcompat.app.AppCompatActivity; - import java.util.Arrays; import java.util.List; @@ -90,15 +90,15 @@ public class PolylineDemoActivity extends AppCompatActivity private static final List PATTERN_DASHED = Arrays.asList(DASH, GAP); private static final List PATTERN_MIXED = Arrays.asList(DOT, GAP, DOT, DASH, GAP); - private Polyline mMutablePolyline; - private SeekBar mHueBar; - private SeekBar mAlphaBar; - private SeekBar mWidthBar; - private Spinner mStartCapSpinner; - private Spinner mEndCapSpinner; - private Spinner mJointTypeSpinner; - private Spinner mPatternSpinner; - private CheckBox mClickabilityCheckbox; + private Polyline mutablePolyline; + private SeekBar hueBar; + private SeekBar alphaBar; + private SeekBar widthBar; + private Spinner startCapSpinner; + private Spinner endCapSpinner; + private Spinner jointTypeSpinner; + private Spinner patternSpinner; + private CheckBox clickabilityCheckbox; // These are the options for polyline caps, joints and patterns. We use their // string resource IDs as identifiers. @@ -128,39 +128,39 @@ public class PolylineDemoActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.polyline_demo); - mHueBar = (SeekBar) findViewById(R.id.hueSeekBar); - mHueBar.setMax(MAX_HUE_DEGREES); - mHueBar.setProgress(0); + hueBar = findViewById(R.id.hueSeekBar); + hueBar.setMax(MAX_HUE_DEGREES); + hueBar.setProgress(0); - mAlphaBar = (SeekBar) findViewById(R.id.alphaSeekBar); - mAlphaBar.setMax(MAX_ALPHA); - mAlphaBar.setProgress(MAX_ALPHA); + alphaBar = findViewById(R.id.alphaSeekBar); + alphaBar.setMax(MAX_ALPHA); + alphaBar.setProgress(MAX_ALPHA); - mWidthBar = (SeekBar) findViewById(R.id.widthSeekBar); - mWidthBar.setMax(MAX_WIDTH_PX); - mWidthBar.setProgress(MAX_WIDTH_PX / 2); + widthBar = findViewById(R.id.widthSeekBar); + widthBar.setMax(MAX_WIDTH_PX); + widthBar.setProgress(MAX_WIDTH_PX / 2); - mStartCapSpinner = (Spinner) findViewById(R.id.startCapSpinner); - mStartCapSpinner.setAdapter(new ArrayAdapter<>( + startCapSpinner = findViewById(R.id.startCapSpinner); + startCapSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(CAP_TYPE_NAME_RESOURCE_IDS))); - mEndCapSpinner = (Spinner) findViewById(R.id.endCapSpinner); - mEndCapSpinner.setAdapter(new ArrayAdapter<>( + endCapSpinner = findViewById(R.id.endCapSpinner); + endCapSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(CAP_TYPE_NAME_RESOURCE_IDS))); - mJointTypeSpinner = (Spinner) findViewById(R.id.jointTypeSpinner); - mJointTypeSpinner.setAdapter(new ArrayAdapter<>( + jointTypeSpinner = findViewById(R.id.jointTypeSpinner); + jointTypeSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(JOINT_TYPE_NAME_RESOURCE_IDS))); - mPatternSpinner = (Spinner) findViewById(R.id.patternSpinner); - mPatternSpinner.setAdapter(new ArrayAdapter<>( + patternSpinner = findViewById(R.id.patternSpinner); + patternSpinner.setAdapter(new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, getResourceStrings(PATTERN_TYPE_NAME_RESOURCE_IDS))); - mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability); + clickabilityCheckbox = findViewById(R.id.toggleClickability); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); @@ -187,30 +187,30 @@ public class PolylineDemoActivity extends AppCompatActivity .width(INITIAL_STROKE_WIDTH_PX) .color(Color.BLUE) .geodesic(true) - .clickable(mClickabilityCheckbox.isChecked())); + .clickable(clickabilityCheckbox.isChecked())); // A simple polyline across Australia. This polyline will be mutable. int color = Color.HSVToColor( - mAlphaBar.getProgress(), new float[]{mHueBar.getProgress(), 1, 1}); - mMutablePolyline = map.addPolyline(new PolylineOptions() + alphaBar.getProgress(), new float[]{hueBar.getProgress(), 1, 1}); + mutablePolyline = map.addPolyline(new PolylineOptions() .color(color) - .width(mWidthBar.getProgress()) - .clickable(mClickabilityCheckbox.isChecked()) + .width(widthBar.getProgress()) + .clickable(clickabilityCheckbox.isChecked()) .add(MELBOURNE, ADELAIDE, PERTH, DARWIN)); - mHueBar.setOnSeekBarChangeListener(this); - mAlphaBar.setOnSeekBarChangeListener(this); - mWidthBar.setOnSeekBarChangeListener(this); + hueBar.setOnSeekBarChangeListener(this); + alphaBar.setOnSeekBarChangeListener(this); + widthBar.setOnSeekBarChangeListener(this); - mStartCapSpinner.setOnItemSelectedListener(this); - mEndCapSpinner.setOnItemSelectedListener(this); - mJointTypeSpinner.setOnItemSelectedListener(this); - mPatternSpinner.setOnItemSelectedListener(this); + startCapSpinner.setOnItemSelectedListener(this); + endCapSpinner.setOnItemSelectedListener(this); + jointTypeSpinner.setOnItemSelectedListener(this); + patternSpinner.setOnItemSelectedListener(this); - mMutablePolyline.setStartCap(getSelectedCap(mStartCapSpinner.getSelectedItemPosition())); - mMutablePolyline.setEndCap(getSelectedCap(mEndCapSpinner.getSelectedItemPosition())); - mMutablePolyline.setJointType(getSelectedJointType(mJointTypeSpinner.getSelectedItemPosition())); - mMutablePolyline.setPattern(getSelectedPattern(mPatternSpinner.getSelectedItemPosition())); + mutablePolyline.setStartCap(getSelectedCap(startCapSpinner.getSelectedItemPosition())); + mutablePolyline.setEndCap(getSelectedCap(endCapSpinner.getSelectedItemPosition())); + mutablePolyline.setJointType(getSelectedJointType(jointTypeSpinner.getSelectedItemPosition())); + mutablePolyline.setPattern(getSelectedPattern(patternSpinner.getSelectedItemPosition())); // Move the map so that it is centered on the mutable polyline. map.moveCamera(CameraUpdateFactory.newLatLngZoom(MELBOURNE, 3)); @@ -272,16 +272,16 @@ public class PolylineDemoActivity extends AppCompatActivity public void onItemSelected(AdapterView parent, View view, int pos, long id) { switch (parent.getId()) { case R.id.startCapSpinner: - mMutablePolyline.setStartCap(getSelectedCap(pos)); + mutablePolyline.setStartCap(getSelectedCap(pos)); break; case R.id.endCapSpinner: - mMutablePolyline.setEndCap(getSelectedCap(pos)); + mutablePolyline.setEndCap(getSelectedCap(pos)); break; case R.id.jointTypeSpinner: - mMutablePolyline.setJointType(getSelectedJointType(pos)); + mutablePolyline.setJointType(getSelectedJointType(pos)); break; case R.id.patternSpinner: - mMutablePolyline.setPattern(getSelectedPattern(pos)); + mutablePolyline.setPattern(getSelectedPattern(pos)); break; } } @@ -303,19 +303,19 @@ public class PolylineDemoActivity extends AppCompatActivity @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (mMutablePolyline == null) { + if (mutablePolyline == null) { return; } - if (seekBar == mHueBar) { - mMutablePolyline.setColor(Color.HSVToColor( - Color.alpha(mMutablePolyline.getColor()), new float[]{progress, 1, 1})); - } else if (seekBar == mAlphaBar) { + if (seekBar == hueBar) { + mutablePolyline.setColor(Color.HSVToColor( + Color.alpha(mutablePolyline.getColor()), new float[]{progress, 1, 1})); + } else if (seekBar == alphaBar) { float[] prevHSV = new float[3]; - Color.colorToHSV(mMutablePolyline.getColor(), prevHSV); - mMutablePolyline.setColor(Color.HSVToColor(progress, prevHSV)); - } else if (seekBar == mWidthBar) { - mMutablePolyline.setWidth(progress); + Color.colorToHSV(mutablePolyline.getColor(), prevHSV); + mutablePolyline.setColor(Color.HSVToColor(progress, prevHSV)); + } else if (seekBar == widthBar) { + mutablePolyline.setWidth(progress); } } @@ -325,8 +325,8 @@ public class PolylineDemoActivity extends AppCompatActivity * This callback is defined on the CheckBox in the layout for this Activity. */ public void toggleClickability(View view) { - if (mMutablePolyline != null) { - mMutablePolyline.setClickable(((CheckBox) view).isChecked()); + if (mutablePolyline != null) { + mutablePolyline.setClickable(((CheckBox) view).isChecked()); } } } \ No newline at end of file diff --git a/ApiDemos/java/app/src/v3/res/layout/my_location_demo.xml b/ApiDemos/java/app/src/v3/res/layout/my_location_demo.xml index f365cc3b..06ec085e 100755 --- a/ApiDemos/java/app/src/v3/res/layout/my_location_demo.xml +++ b/ApiDemos/java/app/src/v3/res/layout/my_location_demo.xml @@ -22,7 +22,6 @@ diff --git a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/DemoDetailsList.kt b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/DemoDetailsList.kt index 00076cc8..8cb1e6d8 100644 --- a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/DemoDetailsList.kt +++ b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/DemoDetailsList.kt @@ -56,6 +56,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, diff --git a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MyLocationDemoActivity.kt b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MyLocationDemoActivity.kt new file mode 100644 index 00000000..ca774d3b --- /dev/null +++ b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MyLocationDemoActivity.kt @@ -0,0 +1,139 @@ +/** + * DO NOT EDIT THIS FILE. + * + * This source code was autogenerated from source code within the `app/src/gms` directory + * and is not intended for modifications. If any edits should be made, please do so in the + * corresponding file under the `app/src/gms` directory. + */ +// 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.libraries.maps.GoogleMap +import com.google.android.libraries.maps.GoogleMap.OnMyLocationButtonClickListener +import com.google.android.libraries.maps.GoogleMap.OnMyLocationClickListener +import com.google.android.libraries.maps.OnMapReadyCallback +import com.google.android.libraries.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, 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 + } +} \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/PermissionUtils.kt b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/PermissionUtils.kt new file mode 100644 index 00000000..ab9ef4eb --- /dev/null +++ b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/PermissionUtils.kt @@ -0,0 +1,195 @@ +/** + * DO NOT EDIT THIS FILE. + * + * This source code was autogenerated from source code within the `app/src/gms` directory + * and is not intended for modifications. If any edits should be made, please do so in the + * corresponding file under the `app/src/gms` directory. + */ +// 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, 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 + } + } + } + } +} \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/v3/res/layout/my_location_demo.xml b/ApiDemos/kotlin/app/src/v3/res/layout/my_location_demo.xml new file mode 100644 index 00000000..06ec085e --- /dev/null +++ b/ApiDemos/kotlin/app/src/v3/res/layout/my_location_demo.xml @@ -0,0 +1,27 @@ + + + + + +