// Created by plusminus on 18:23:13 - 03.10.2008 package org.osmdroid.google.sample; import android.Manifest; import android.app.ListActivity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import org.osmdroid.google.wrapper.v2.MapFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends ListActivity { boolean isV1Supported = false; boolean isV2Supported = false; boolean isGooglePlayServicesInstalled = false; /** * Called when the activity is first created. */ @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Request permissions to support Android Marshmallow and above devices if (Build.VERSION.SDK_INT >= 23) { checkPermissions(); } GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { // Do Your Stuff Here isGooglePlayServicesInstalled=true; } isV1Supported = MapFactory.isGoogleMapsV1Supported(); isV2Supported = MapFactory.isGoogleMapsV2Supported(this); // Generate a ListView with Sample Maps final ArrayList<String> list = new ArrayList<String>(); list.add("Google Maps v2 (out of the box)"); //list.add("OSMDroid using Bing for tiles"); //MOVE INTO osmdroid-android and the Open Map sample application list.add("Google Maps v1 via the osmdroid APIs"); list.add("Google Maps v2 via the osmdroid APIs"); this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); } @Override protected void onListItemClick(final ListView l, final View v, final int position, final long id) { switch (position) { case 0: if (isGooglePlayServicesInstalled && isV2Supported) { this.startActivity(new Intent(this, MapsActivity.class)); } else Toast.makeText(this, "Not available on your device", Toast.LENGTH_LONG).show(); break; //case 1: MOVED // this.startActivity(new Intent(this, OsmMapActivity.class)); //break; case 1: if (isGooglePlayServicesInstalled && isV1Supported) { this.startActivity(new Intent(this, Googlev1WrapperSample.class)); } else Toast.makeText(this, "Not available on your device", Toast.LENGTH_LONG).show(); break; case 2: if (isGooglePlayServicesInstalled && isV2Supported) { this.startActivity(new Intent(this, Googlev2WrapperSample.class)); } else Toast.makeText(this, "Not available on your device", Toast.LENGTH_LONG).show(); break; } } // START PERMISSION CHECK final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; private void checkPermissions() { List<String> permissions = new ArrayList<String>(); String message = "OSMDroid permissions:"; if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); message += "\nStorage access to store map tiles."; } if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); message += "\nLocation to show user location."; } if (!permissions.isEmpty()) { Toast.makeText(this, message, Toast.LENGTH_LONG).show(); String[] params = permissions.toArray(new String[permissions.size()]); requestPermissions(params, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); } // else: We already have permissions, so handle as normal } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: { Map<String, Integer> perms = new HashMap<String, Integer>(); // Initial perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); // Fill with results for (int i = 0; i < permissions.length; i++) perms.put(permissions[i], grantResults[i]); // Check for ACCESS_FINE_LOCATION and WRITE_EXTERNAL_STORAGE Boolean location = perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; Boolean storage = perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; if (location && storage) { // All Permissions Granted Toast.makeText(MainActivity.this, "All permissions granted", Toast.LENGTH_SHORT).show(); } else if (location) { Toast.makeText(this, "Storage permission is required to store map tiles to reduce data usage and for offline usage.", Toast.LENGTH_LONG).show(); } else if (storage) { Toast.makeText(this, "Location permission is required to show the user's location on map.", Toast.LENGTH_LONG).show(); } else { // !location && !storage case // Permission Denied Toast.makeText(MainActivity.this, "Storage permission is required to store map tiles to reduce data usage and for offline usage." + "\nLocation permission is required to show the user's location on map.", Toast.LENGTH_SHORT).show(); } } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } // END PERMISSION CHECK }