Better permission management

Add copyright for the map
Fix old maps parameters to be at the same place after come back to map
This commit is contained in:
Schoumi 2017-08-27 15:13:15 +02:00
parent 83e7ac84fc
commit 8359aa27fb
4 changed files with 83 additions and 15 deletions

View File

@ -39,11 +39,18 @@ import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@ -58,7 +65,6 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.PopupMenu; import android.widget.PopupMenu;
import android.widget.Toast;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -73,6 +79,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private int postCode; private int postCode;
private List<Long> notifications; private List<Long> notifications;
private Menu optionMenu; private Menu optionMenu;
private Snackbar barWrite;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -132,7 +139,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1 && grantResults.length == 2 && (grantResults[0] == PackageManager.PERMISSION_GRANTED || grantResults[1] == PackageManager.PERMISSION_GRANTED)) { if(requestCode == 1 && grantResults.length == 2 && (grantResults[0] == PackageManager.PERMISSION_GRANTED || grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
FragmentManager manager = getFragmentManager(); FragmentManager manager = getFragmentManager();
@ -140,9 +147,38 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if(frag instanceof MapsFragment) { if(frag instanceof MapsFragment) {
((MapsFragment)frag).startUpdateLocations(); ((MapsFragment)frag).startUpdateLocations();
} }
} else if (requestCode == 1 && grantResults.length == 2) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("deactivate_localisation", true);
editor.apply();
} }
if(requestCode == 2 && grantResults.length == 1 && grantResults[0] != PackageManager.PERMISSION_GRANTED) { if(requestCode == 2 && grantResults.length == 1 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,R.string.need_write,Toast.LENGTH_LONG).show(); barWrite = Snackbar.make(findViewById(R.id.fragment_container),R.string.need_write,Snackbar.LENGTH_INDEFINITE);
barWrite.setAction(R.string.retry, new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragment_container);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!shouldShowRequestPermissionRationale(permissions[0])) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 5);
dismissSnackBar();
} else {
if(fragment instanceof MapsFragment)
((MapsFragment)fragment).askWritePermission();
}
} else {
if (fragment instanceof MapsFragment)
((MapsFragment) fragment).askWritePermission();
}
dismissSnackBar();
}
});
barWrite.show();
} }
} }
@ -345,25 +381,31 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
switch (id) { switch (id) {
case R.id.nav_my_donation: case R.id.nav_my_donation:
showDonation(); showDonation();
dismissSnackBar();
break; break;
case R.id.nav_user_information: case R.id.nav_user_information:
showDonationInfo(); showDonationInfo();
dismissSnackBar();
break; break;
case R.id.nav_legend: case R.id.nav_legend:
openLegendDialog(); openLegendDialog();
break; break;
case R.id.nav_preferences: case R.id.nav_preferences:
showPrefs(); showPrefs();
dismissSnackBar();
break; break;
case R.id.nav_post_don: case R.id.nav_post_don:
openPostDonationDialog(); openPostDonationDialog();
break; break;
case R.id.nav_about: case R.id.nav_about:
showAbout(); showAbout();
dismissSnackBar();
break; break;
} }
@ -373,6 +415,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
return true; return true;
} }
private void dismissSnackBar() {
if(barWrite != null) {
barWrite.dismiss();
barWrite = null;
}
}
@Override @Override
public boolean onCreateOptionsMenu(final Menu menu) { public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();

View File

@ -29,7 +29,6 @@ import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.util.LongSparseArray; import android.support.v4.util.LongSparseArray;
@ -39,7 +38,6 @@ import android.view.ViewGroup;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.car2go.maps.AnyMap; import com.car2go.maps.AnyMap;
import com.car2go.maps.OnMapReadyCallback; import com.car2go.maps.OnMapReadyCallback;
@ -150,6 +148,7 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
postCode = savedInstanceState.getInt("posteCode"); postCode = savedInstanceState.getInt("posteCode");
previousLocation = new LatLng(latitude,longitude); previousLocation = new LatLng(latitude,longitude);
previousZoom = zoom; previousZoom = zoom;
myLocation = savedInstanceState.getParcelable("myLocation");
} else { } else {
isFirstLocation = true; isFirstLocation = true;
postCode = -1; postCode = -1;
@ -161,6 +160,8 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
locationListener = new LocationListener() { locationListener = new LocationListener() {
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
if(location == null)
return;
myLocation = location; myLocation = location;
myLocationBt.setVisibility(View.VISIBLE); myLocationBt.setVisibility(View.VISIBLE);
if(map != null) { if(map != null) {
@ -283,6 +284,7 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
stopUpdateLocations(); stopUpdateLocations();
return; return;
} }
locationListener.onLocationChanged(myLocation);
LocationManager manager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); LocationManager manager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
@ -331,22 +333,31 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(mapView != null) { if(mapView != null) {
mapView.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState);
outState.putBoolean("isFirstLocation",isFirstLocation);
outState.putDouble("locationLatitude",previousLocation.latitude);
outState.putDouble("locationLongitude",previousLocation.longitude);
outState.putFloat("locationZoom",previousZoom);
outState.putInt("posteCode",postCode);
outState.putParcelable("myLocation",myLocation);
}
super.onSaveInstanceState(outState);
}
public void askWritePermission() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(getActivity(),permissions,2);
} }
} }
@Override @Override
public void onMapReady(AnyMap anyMap) { public void onMapReady(AnyMap anyMap) {
map = anyMap; map = anyMap;
askWritePermission();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || map.animateCamera(CameraUpdateFactory.getInstance().newLatLngZoom(previousLocation,previousZoom));
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(getActivity(),permissions,2);
}
startUpdateLocations(); startUpdateLocations();
country = Country.FRANCE; country = Country.FRANCE;
@ -413,7 +424,6 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
selectedSite = -1; selectedSite = -1;
} }
}); });
startUpdateLocations();
initPosition(); initPosition();
BootReceiver.startNextChecking(getActivity(), false); BootReceiver.startNextChecking(getActivity(), false);

View File

@ -9,6 +9,14 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
/> />
<TextView
android:text="@string/osm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<ImageButton <ImageButton
android:id="@+id/move_to_user_location" android:id="@+id/move_to_user_location"
android:contentDescription="@string/my_location" android:contentDescription="@string/my_location"

View File

@ -89,8 +89,9 @@
<string name="anymaps_licence">Copyright 2017 car2go group GmbH\nReleased under the MIT license.\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string> <string name="anymaps_licence">Copyright 2017 car2go group GmbH\nReleased under the MIT license.\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
<string name="blood_donation_licence">Copyright © 2017 Anthony Chomienne, anthony@mobdev.fr\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301, USA.</string> <string name="blood_donation_licence">Copyright © 2017 Anthony Chomienne, anthony@mobdev.fr\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301, USA.</string>
<string name="my_location">Centrer sur ma position</string> <string name="my_location">Centrer sur ma position</string>
<string name="osm">Données © OpenStreetMap/ODbL\nRendu OSM France</string> <string name="osm">Données © OpenStreetMap</string>
<string name="need_write">La Carte à besoin des droits en écriture/lecture sur le stockage externe</string> <string name="need_write">La Carte à besoin des droits en écriture/lecture sur le stockage externe</string>
<string name="retry">Réessayer</string>
<string-array name="gender_array"> <string-array name="gender_array">