LocationUtils.java revision acd09293116133b6a86d348e011d8347a03266ec
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 */ 16package com.android.packageinstaller.permission.utils; 17 18import android.Manifest; 19import android.app.AlertDialog; 20import android.content.Context; 21import android.content.DialogInterface; 22import android.content.DialogInterface.OnClickListener; 23import android.content.Intent; 24import android.content.res.Resources; 25import android.location.ILocationManager; 26import android.location.LocationManager; 27import android.os.RemoteException; 28import android.os.ServiceManager; 29import android.provider.Settings; 30 31import com.android.packageinstaller.R; 32 33import java.util.ArrayList; 34 35public class LocationUtils { 36 37 public static final String LOCATION_PERMISSION = Manifest.permission_group.LOCATION; 38 39 public static ArrayList<String> getLocationProviders() { 40 ArrayList<String> providers = new ArrayList<>(); 41 Resources res = Resources.getSystem(); 42 providers.add(res.getString( 43 com.android.internal.R.string.config_networkLocationProviderPackageName)); 44 45 for (String provider : 46 res.getStringArray(com.android.internal.R.array.config_locationProviderPackageNames)) { 47 providers.add(provider); 48 } 49 50 return providers; 51 } 52 53 public static void showLocationDialog(final Context context, CharSequence label) { 54 new AlertDialog.Builder(context) 55 .setIcon(com.android.internal.R.drawable.ic_dialog_alert_material) 56 .setTitle(android.R.string.dialog_alert_title) 57 .setMessage(context.getString(R.string.location_warning, label)) 58 .setNegativeButton(R.string.ok, null) 59 .setPositiveButton(R.string.location_settings, new OnClickListener() { 60 @Override 61 public void onClick(DialogInterface dialog, int which) { 62 context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 63 } 64 }) 65 .show(); 66 } 67 68 public static boolean isLocationEnabled(Context context) { 69 return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, 70 Settings.Secure.LOCATION_MODE_OFF) != Settings.Secure.LOCATION_MODE_OFF; 71 } 72 73 public static boolean isLocationGroupAndProvider(String groupName, String packageName) { 74 return LOCATION_PERMISSION.equals(groupName) && isNetworkLocationProvider(packageName); 75 } 76 77 private static boolean isNetworkLocationProvider(String packageName) { 78 ILocationManager locationService = ILocationManager.Stub.asInterface( 79 ServiceManager.getService(Context.LOCATION_SERVICE)); 80 try { 81 return packageName.equals(locationService.getNetworkProviderPackage()); 82 } catch (RemoteException e) { 83 return false; 84 } 85 } 86 87} 88