• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ActivityNotFoundException;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnClickListener;
24 import android.content.Intent;
25 import android.location.LocationManager;
26 import android.provider.Settings;
27 import android.util.Log;
28 
29 import androidx.annotation.NonNull;
30 
31 import com.android.permissioncontroller.R;
32 
33 public class LocationUtils {
34 
35     public static final String LOCATION_PERMISSION = Manifest.permission_group.LOCATION;
36 
37     private static final String TAG = LocationUtils.class.getSimpleName();
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 
54     /** Start the settings page for the location controller extra package. */
startLocationControllerExtraPackageSettings(@onNull Context context)55     public static void startLocationControllerExtraPackageSettings(@NonNull Context context) {
56         try {
57             context.startActivity(new Intent(
58                         Settings.ACTION_LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS));
59             return;
60         } catch (ActivityNotFoundException e) {
61             // In rare cases where location controller extra package is set, but
62             // no activity exists to handle the location controller extra package settings
63             // intent, log an error instead of crashing permission controller.
64             Log.e(TAG, "No activity to handle "
65                         + "android.settings.LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS");
66         }
67     }
68 
isLocationEnabled(Context context)69     public static boolean isLocationEnabled(Context context) {
70         return context.getSystemService(LocationManager.class).isLocationEnabled();
71     }
72 
isLocationGroupAndProvider(Context context, String groupName, String packageName)73     public static boolean isLocationGroupAndProvider(Context context, String groupName,
74             String packageName) {
75         return LOCATION_PERMISSION.equals(groupName)
76                 && context.getSystemService(LocationManager.class).isProviderPackage(packageName);
77     }
78 
isLocationGroupAndControllerExtraPackage(@onNull Context context, @NonNull String groupName, @NonNull String packageName)79     public static boolean isLocationGroupAndControllerExtraPackage(@NonNull Context context,
80             @NonNull String groupName, @NonNull String packageName) {
81         return LOCATION_PERMISSION.equals(groupName)
82                 && packageName.equals(context.getSystemService(LocationManager.class)
83                         .getExtraLocationControllerPackage());
84     }
85 
86     /** Returns whether the location controller extra package is enabled. */
isExtraLocationControllerPackageEnabled(Context context)87     public static boolean isExtraLocationControllerPackageEnabled(Context context) {
88         try {
89             return context.getSystemService(LocationManager.class)
90                     .isExtraLocationControllerPackageEnabled();
91         } catch (Exception e) {
92             return false;
93         }
94 
95     }
96 }
97