1 /* 2 * Copyright (C) 2020 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.injector; 18 19 import static com.android.server.location.LocationManagerService.TAG; 20 21 import android.location.Geofence; 22 import android.location.LocationManager; 23 import android.location.LocationRequest; 24 import android.stats.location.LocationStatsEnums; 25 import android.util.Log; 26 27 import com.android.internal.annotations.GuardedBy; 28 import com.android.internal.util.FrameworkStatsLog; 29 30 import java.time.Instant; 31 32 /** 33 * Logger for Location API usage logging. 34 */ 35 public class LocationUsageLogger { 36 37 private static final int ONE_SEC_IN_MILLIS = 1000; 38 private static final int ONE_MINUTE_IN_MILLIS = 60000; 39 private static final int ONE_HOUR_IN_MILLIS = 3600000; 40 41 private static final int API_USAGE_LOG_HOURLY_CAP = 60; 42 43 @GuardedBy("this") 44 private long mLastApiUsageLogHour = 0; 45 @GuardedBy("this") 46 private int mApiUsageLogHourlyCount = 0; 47 48 /** 49 * Log a location API usage event. 50 */ logLocationApiUsage(int usageType, int apiInUse, String packageName, String attributionTag, String provider, LocationRequest locationRequest, boolean hasListener, boolean hasIntent, Geofence geofence, boolean foreground)51 public void logLocationApiUsage(int usageType, int apiInUse, 52 String packageName, String attributionTag, String provider, 53 LocationRequest locationRequest, boolean hasListener, 54 boolean hasIntent, Geofence geofence, boolean foreground) { 55 try { 56 if (hitApiUsageLogCap()) { 57 return; 58 } 59 60 boolean isLocationRequestNull = locationRequest == null; 61 boolean isGeofenceNull = geofence == null; 62 63 FrameworkStatsLog.write(FrameworkStatsLog.LOCATION_MANAGER_API_USAGE_REPORTED, 64 usageType, apiInUse, packageName, 65 isLocationRequestNull 66 ? LocationStatsEnums.PROVIDER_UNKNOWN 67 : bucketizeProvider(provider), 68 isLocationRequestNull 69 ? LocationStatsEnums.QUALITY_UNKNOWN 70 : locationRequest.getQuality(), 71 isLocationRequestNull 72 ? LocationStatsEnums.INTERVAL_UNKNOWN 73 : bucketizeInterval(locationRequest.getIntervalMillis()), 74 isLocationRequestNull 75 ? LocationStatsEnums.DISTANCE_UNKNOWN 76 : bucketizeDistance( 77 locationRequest.getMinUpdateDistanceMeters()), 78 isLocationRequestNull ? 0 : locationRequest.getMaxUpdates(), 79 // only log expireIn for USAGE_STARTED 80 isLocationRequestNull || usageType == LocationStatsEnums.USAGE_ENDED 81 ? LocationStatsEnums.EXPIRATION_UNKNOWN 82 : bucketizeExpireIn(locationRequest.getDurationMillis()), 83 getCallbackType(apiInUse, hasListener, hasIntent), 84 isGeofenceNull 85 ? LocationStatsEnums.RADIUS_UNKNOWN 86 : bucketizeRadius(geofence.getRadius()), 87 categorizeActivityImportance(foreground), 88 attributionTag); 89 } catch (Exception e) { 90 // Swallow exceptions to avoid crashing LMS. 91 Log.w(TAG, "Failed to log API usage to statsd.", e); 92 } 93 } 94 95 /** 96 * Log a location API usage event. 97 */ logLocationApiUsage(int usageType, int apiInUse, String providerName)98 public void logLocationApiUsage(int usageType, int apiInUse, String providerName) { 99 try { 100 if (hitApiUsageLogCap()) { 101 return; 102 } 103 104 FrameworkStatsLog.write(FrameworkStatsLog.LOCATION_MANAGER_API_USAGE_REPORTED, 105 usageType, apiInUse, 106 /* package_name= */ null, 107 bucketizeProvider(providerName), 108 LocationStatsEnums.QUALITY_UNKNOWN, 109 LocationStatsEnums.INTERVAL_UNKNOWN, 110 LocationStatsEnums.DISTANCE_UNKNOWN, 111 /* numUpdates= */ 0, 112 LocationStatsEnums.EXPIRATION_UNKNOWN, 113 getCallbackType( 114 apiInUse, 115 /* isListenerNull= */ true, 116 /* isIntentNull= */ true), 117 /* bucketizedRadius= */ 0, 118 LocationStatsEnums.IMPORTANCE_UNKNOWN, 119 /* attribution_tag */ null); 120 } catch (Exception e) { 121 Log.w(TAG, "Failed to log API usage to statsd.", e); 122 } 123 } 124 125 /** 126 * Log a location enabled state change event. 127 */ logLocationEnabledStateChanged(boolean enabled)128 public synchronized void logLocationEnabledStateChanged(boolean enabled) { 129 FrameworkStatsLog.write(FrameworkStatsLog.LOCATION_ENABLED_STATE_CHANGED, enabled); 130 } 131 bucketizeProvider(String provider)132 private static int bucketizeProvider(String provider) { 133 if (LocationManager.NETWORK_PROVIDER.equals(provider)) { 134 return LocationStatsEnums.PROVIDER_NETWORK; 135 } else if (LocationManager.GPS_PROVIDER.equals(provider)) { 136 return LocationStatsEnums.PROVIDER_GPS; 137 } else if (LocationManager.PASSIVE_PROVIDER.equals(provider)) { 138 return LocationStatsEnums.PROVIDER_PASSIVE; 139 } else if (LocationManager.FUSED_PROVIDER.equals(provider)) { 140 return LocationStatsEnums.PROVIDER_FUSED; 141 } else { 142 return LocationStatsEnums.PROVIDER_UNKNOWN; 143 } 144 } 145 bucketizeInterval(long interval)146 private static int bucketizeInterval(long interval) { 147 if (interval < ONE_SEC_IN_MILLIS) { 148 return LocationStatsEnums.INTERVAL_BETWEEN_0_SEC_AND_1_SEC; 149 } else if (interval < ONE_SEC_IN_MILLIS * 5) { 150 return LocationStatsEnums.INTERVAL_BETWEEN_1_SEC_AND_5_SEC; 151 } else if (interval < ONE_MINUTE_IN_MILLIS) { 152 return LocationStatsEnums.INTERVAL_BETWEEN_5_SEC_AND_1_MIN; 153 } else if (interval < ONE_MINUTE_IN_MILLIS * 10) { 154 return LocationStatsEnums.INTERVAL_BETWEEN_1_MIN_AND_10_MIN; 155 } else if (interval < ONE_HOUR_IN_MILLIS) { 156 return LocationStatsEnums.INTERVAL_BETWEEN_10_MIN_AND_1_HOUR; 157 } else { 158 return LocationStatsEnums.INTERVAL_LARGER_THAN_1_HOUR; 159 } 160 } 161 bucketizeDistance(float smallestDisplacement)162 private static int bucketizeDistance(float smallestDisplacement) { 163 if (smallestDisplacement <= 0) { 164 return LocationStatsEnums.DISTANCE_ZERO; 165 } else if (smallestDisplacement > 0 && smallestDisplacement <= 100) { 166 return LocationStatsEnums.DISTANCE_BETWEEN_0_AND_100; 167 } else { 168 return LocationStatsEnums.DISTANCE_LARGER_THAN_100; 169 } 170 } 171 bucketizeRadius(float radius)172 private static int bucketizeRadius(float radius) { 173 if (radius < 0) { 174 return LocationStatsEnums.RADIUS_NEGATIVE; 175 } else if (radius < 100) { 176 return LocationStatsEnums.RADIUS_BETWEEN_0_AND_100; 177 } else if (radius < 200) { 178 return LocationStatsEnums.RADIUS_BETWEEN_100_AND_200; 179 } else if (radius < 300) { 180 return LocationStatsEnums.RADIUS_BETWEEN_200_AND_300; 181 } else if (radius < 1000) { 182 return LocationStatsEnums.RADIUS_BETWEEN_300_AND_1000; 183 } else if (radius < 10000) { 184 return LocationStatsEnums.RADIUS_BETWEEN_1000_AND_10000; 185 } else { 186 return LocationStatsEnums.RADIUS_LARGER_THAN_100000; 187 } 188 } 189 bucketizeExpireIn(long expireIn)190 private static int bucketizeExpireIn(long expireIn) { 191 if (expireIn == Long.MAX_VALUE) { 192 return LocationStatsEnums.EXPIRATION_NO_EXPIRY; 193 } 194 195 if (expireIn < 20 * ONE_SEC_IN_MILLIS) { 196 return LocationStatsEnums.EXPIRATION_BETWEEN_0_AND_20_SEC; 197 } else if (expireIn < ONE_MINUTE_IN_MILLIS) { 198 return LocationStatsEnums.EXPIRATION_BETWEEN_20_SEC_AND_1_MIN; 199 } else if (expireIn < ONE_MINUTE_IN_MILLIS * 10) { 200 return LocationStatsEnums.EXPIRATION_BETWEEN_1_MIN_AND_10_MIN; 201 } else if (expireIn < ONE_HOUR_IN_MILLIS) { 202 return LocationStatsEnums.EXPIRATION_BETWEEN_10_MIN_AND_1_HOUR; 203 } else { 204 return LocationStatsEnums.EXPIRATION_LARGER_THAN_1_HOUR; 205 } 206 } 207 categorizeActivityImportance(boolean foreground)208 private static int categorizeActivityImportance(boolean foreground) { 209 if (foreground) { 210 return LocationStatsEnums.IMPORTANCE_TOP; 211 } else { 212 return LocationStatsEnums.IMPORTANCE_BACKGROUND; 213 } 214 } 215 getCallbackType( int apiType, boolean hasListener, boolean hasIntent)216 private static int getCallbackType( 217 int apiType, boolean hasListener, boolean hasIntent) { 218 if (apiType == LocationStatsEnums.API_SEND_EXTRA_COMMAND) { 219 return LocationStatsEnums.CALLBACK_NOT_APPLICABLE; 220 } 221 222 // Listener and PendingIntent will not be set at 223 // the same time. 224 if (hasIntent) { 225 return LocationStatsEnums.CALLBACK_PENDING_INTENT; 226 } else if (hasListener) { 227 return LocationStatsEnums.CALLBACK_LISTENER; 228 } else { 229 return LocationStatsEnums.CALLBACK_UNKNOWN; 230 } 231 } 232 hitApiUsageLogCap()233 private synchronized boolean hitApiUsageLogCap() { 234 long currentHour = Instant.now().toEpochMilli() / ONE_HOUR_IN_MILLIS; 235 if (currentHour > mLastApiUsageLogHour) { 236 mLastApiUsageLogHour = currentHour; 237 mApiUsageLogHourlyCount = 0; 238 return false; 239 } else { 240 mApiUsageLogHourlyCount = Math.min( 241 mApiUsageLogHourlyCount + 1, API_USAGE_LOG_HOURLY_CAP); 242 return mApiUsageLogHourlyCount >= API_USAGE_LOG_HOURLY_CAP; 243 } 244 } 245 } 246