1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.packageinstaller.permission.utils; 17 18 import android.Manifest; 19 import android.app.AlertDialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.content.DialogInterface.OnClickListener; 23 import android.content.Intent; 24 import android.content.res.Resources; 25 import android.location.ILocationManager; 26 import android.location.LocationManager; 27 import android.os.RemoteException; 28 import android.os.ServiceManager; 29 import android.provider.Settings; 30 31 import com.android.packageinstaller.R; 32 33 import java.util.ArrayList; 34 35 public class LocationUtils { 36 37 public static final String LOCATION_PERMISSION = Manifest.permission_group.LOCATION; 38 showLocationDialog(final Context context, CharSequence label)39 public static void showLocationDialog(final Context context, CharSequence label) { 40 new AlertDialog.Builder(context) 41 .setIcon(R.drawable.ic_dialog_alert_material) 42 .setTitle(android.R.string.dialog_alert_title) 43 .setMessage(context.getString(R.string.location_warning, label)) 44 .setNegativeButton(R.string.ok, null) 45 .setPositiveButton(R.string.location_settings, new OnClickListener() { 46 @Override 47 public void onClick(DialogInterface dialog, int which) { 48 context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 49 } 50 }) 51 .show(); 52 } 53 isLocationEnabled(Context context)54 public static boolean isLocationEnabled(Context context) { 55 return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, 56 Settings.Secure.LOCATION_MODE_OFF) != Settings.Secure.LOCATION_MODE_OFF; 57 } 58 isLocationGroupAndProvider(String groupName, String packageName)59 public static boolean isLocationGroupAndProvider(String groupName, String packageName) { 60 return LOCATION_PERMISSION.equals(groupName) && isNetworkLocationProvider(packageName); 61 } 62 isNetworkLocationProvider(String packageName)63 private static boolean isNetworkLocationProvider(String packageName) { 64 ILocationManager locationService = ILocationManager.Stub.asInterface( 65 ServiceManager.getService(Context.LOCATION_SERVICE)); 66 try { 67 return packageName.equals(locationService.getNetworkProviderPackage()); 68 } catch (RemoteException e) { 69 return false; 70 } 71 } 72 } 73