Fix bug that don't load maps due to permission error

This commit is contained in:
Schoumi 2017-08-25 10:13:29 +02:00
parent 9f1d1cc6f1
commit 52033acc22
2 changed files with 26 additions and 9 deletions

View file

@ -39,6 +39,7 @@ import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
@ -57,6 +58,7 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
@ -129,6 +131,20 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1 && (grantResults[0] == PackageManager.PERMISSION_GRANTED || grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
FragmentManager manager = getFragmentManager();
Fragment frag = manager.findFragmentById(R.id.fragment_container);
if(frag instanceof MapsFragment) {
((MapsFragment)frag).startUpdateLocations();
}
}
if(requestCode == 2 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,R.string.need_write,Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {

View file

@ -39,6 +39,7 @@ import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.car2go.maps.AnyMap;
import com.car2go.maps.OnMapReadyCallback;
@ -271,14 +272,6 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
map = null;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED || grantResults[1] == PackageManager.PERMISSION_GRANTED) {
startUpdateLocations();
}
}
public void startUpdateLocations() {
if(getActivity() == null)
return;
@ -292,7 +285,8 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
}
LocationManager manager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(getActivity(),permissions,1);
} else {
@ -346,6 +340,13 @@ public class MapsFragment extends Fragment implements OnMapReadyCallback {
@Override
public void onMapReady(AnyMap anyMap) {
map = anyMap;
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);
}
startUpdateLocations();
country = Country.FRANCE;