Skip to content
Snippets Groups Projects
Commit f56affc7 authored by s22thong's avatar s22thong
Browse files

ft: AlertDialog for Position permission

parent 39e5f941
No related branches found
No related tags found
No related merge requests found
...@@ -3,12 +3,19 @@ package fr.imt_atlantique.myfirstapplication; ...@@ -3,12 +3,19 @@ package fr.imt_atlantique.myfirstapplication;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.Manifest;
import android.app.AlertDialog;
import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap;
...@@ -42,15 +49,34 @@ public class MapsFragment extends Fragment { ...@@ -42,15 +49,34 @@ public class MapsFragment extends Fragment {
*/ */
@Override @Override
public void onMapReady(GoogleMap googleMap) { public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-34, 151); // We check for the application "Call permission"
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); if (ContextCompat.checkSelfPermission(requireContext(),
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
googleMap.getUiSettings().setZoomGesturesEnabled(true); grantPermissionAlertDialog();
googleMap.getUiSettings().setCompassEnabled(true); return;
googleMap.getUiSettings().setMyLocationButtonEnabled(true); }
try {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} catch (SecurityException e) {
Log.e("MapsFragment", "Error while enabling location", e);
}
} }
}; };
private void grantPermissionAlertDialog() {
new AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.position_permission_title))
.setMessage(getString(R.string.position_permission_message))
.setNegativeButton(getString(R.string.cancel), (dialog, which) -> Toast.makeText(requireContext(), getString(R.string.permission_denied), Toast.LENGTH_SHORT)
.show())
.setPositiveButton("Ok", (dialog, which) -> ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.CALL_PHONE}, 3))
.create()
.show();
}
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
<string name="call_attempt_error">Une erreur est survenue lors de la tentative d\'appel : </string> <string name="call_attempt_error">Une erreur est survenue lors de la tentative d\'appel : </string>
<string name="call_permission_title">Permission d\'appel requise</string> <string name="call_permission_title">Permission d\'appel requise</string>
<string name="call_permission_message">Cette application nécessite la permission d\'appel pour pouvoir effectuer un appel téléphonique</string> <string name="call_permission_message">Cette application nécessite la permission d\'appel pour pouvoir effectuer un appel téléphonique</string>
<string name="position_permission_title">Permission de récupération de la position requise</string>
<string name="position_permission_message">Cette application nécessite la permission de récupération de la position requise pour pouvoir afficher votre position</string>
<string name="enter_birth_city">Veuillez entrer une ville de naissance</string> <string name="enter_birth_city">Veuillez entrer une ville de naissance</string>
<string name="no_browser_found">Aucun navigateur trouvé pour ouvrir Wikipédia</string> <string name="no_browser_found">Aucun navigateur trouvé pour ouvrir Wikipédia</string>
<string name="share_city">Partager la ville</string> <string name="share_city">Partager la ville</string>
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
<string name="call_attempt_error">An error occurred during the call attempt: </string> <string name="call_attempt_error">An error occurred during the call attempt: </string>
<string name="call_permission_title">Call Permission Required</string> <string name="call_permission_title">Call Permission Required</string>
<string name="call_permission_message">This App needs Call permission in order to be able to handle calling a phone number</string> <string name="call_permission_message">This App needs Call permission in order to be able to handle calling a phone number</string>
<string name="position_permission_title">Current position Permission Required</string>
<string name="position_permission_message">This App needs Current position permission in order to be able to show your position currently</string>
<string name="enter_birth_city">Please enter a birth city first</string> <string name="enter_birth_city">Please enter a birth city first</string>
<string name="no_browser_found">No browser found to open Wikipedia</string> <string name="no_browser_found">No browser found to open Wikipedia</string>
<string name="share_city">Share city</string> <string name="share_city">Share city</string>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment