1 /*
2  * Copyright (C) 2019 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 
17 package com.android.server.location;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 
22 /**
23  * Encapsulates utility functions and classes related to location permission checking.
24  */
25 public final class LocationPermissionUtil {
26     /**
27      * Returns true if the calling process identified by {@code callerIdentity} is enabled to
28      * report location to AppOps service before providing device location identifiable information
29      * to its clients. Packages with these permissions must report any reporting of location
30      * information to apps, via AppOps.
31      *
32      * <p>The calling package represented by {@code callerIdentity} is considered a part of the
33      * extended Location Manager Service if it has all of the permissions below.
34      * <ul>
35      *     <li>{@link android.Manifest.permission#LOCATION_HARDWARE}
36      *     <li>{@link android.Manifest.permission#UPDATE_APP_OPS_STATS}
37      * </ul>
38      *
39      * <p>Any package with these permissions, that passes along location information from Android
40      * framework to apps, must report to AppOps, similarly to Location Manager Service - i.e.
41      * whenever it reports device location or location identifiable information such as
42      * GNSS status, GNSS measurements, etc. to its clients.
43      */
doesCallerReportToAppOps(Context context, CallerIdentity callerIdentity)44     public static boolean doesCallerReportToAppOps(Context context, CallerIdentity callerIdentity) {
45         return hasPermissionLocationHardware(context, callerIdentity)
46                 && hasPermissionUpdateAppOpsStats(context, callerIdentity);
47     }
48 
hasPermissionLocationHardware(Context context, CallerIdentity callerIdentity)49     private static boolean hasPermissionLocationHardware(Context context,
50             CallerIdentity callerIdentity) {
51         return context.checkPermission(android.Manifest.permission.LOCATION_HARDWARE,
52                 callerIdentity.mPid, callerIdentity.mUid) == PackageManager.PERMISSION_GRANTED;
53     }
54 
hasPermissionUpdateAppOpsStats(Context context, CallerIdentity callerIdentity)55     private static boolean hasPermissionUpdateAppOpsStats(Context context,
56             CallerIdentity callerIdentity) {
57         return context.checkPermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
58                 callerIdentity.mPid, callerIdentity.mUid) == PackageManager.PERMISSION_GRANTED;
59     }
60 
LocationPermissionUtil()61     private LocationPermissionUtil() {}
62 }
63