Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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.server.camera; |
| 17 | |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 18 | import android.content.BroadcastReceiver; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.content.IntentFilter; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 22 | import android.hardware.ICameraService; |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 23 | import android.hardware.ICameraServiceProxy; |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 24 | import android.metrics.LogMaker; |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 25 | import android.nfc.INfcAdapter; |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 29 | import android.os.Message; |
| 30 | import android.os.Process; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 32 | import android.os.SystemClock; |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 33 | import android.os.SystemProperties; |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 34 | import android.os.UserManager; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 36 | import android.util.ArraySet; |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 37 | import android.util.Slog; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 38 | |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 39 | import com.android.internal.logging.MetricsLogger; |
| 40 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 41 | import com.android.server.LocalServices; |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 42 | import com.android.server.ServiceThread; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 43 | import com.android.server.SystemService; |
| 44 | |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 46 | import java.util.Collection; |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 48 | import java.util.List; |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 49 | import java.util.Set; |
| 50 | |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 51 | /** |
Wei Wang | ed7f548 | 2017-02-21 11:35:10 -0800 | [diff] [blame] | 52 | * CameraServiceProxy is the system_server analog to the camera service running in mediaserver. |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 53 | * |
| 54 | * @hide |
| 55 | */ |
Wei Wang | ed7f548 | 2017-02-21 11:35:10 -0800 | [diff] [blame] | 56 | public class CameraServiceProxy extends SystemService |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 57 | implements Handler.Callback, IBinder.DeathRecipient { |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 58 | private static final String TAG = "CameraService_proxy"; |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 59 | private static final boolean DEBUG = false; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * This must match the ICameraService.aidl definition |
| 63 | */ |
| 64 | private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera"; |
| 65 | |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 66 | public static final String CAMERA_SERVICE_PROXY_BINDER_NAME = "media.camera.proxy"; |
| 67 | |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 68 | // Flags arguments to NFC adapter to enable/disable NFC |
| 69 | public static final int DISABLE_POLLING_FLAGS = 0x1000; |
| 70 | public static final int ENABLE_POLLING_FLAGS = 0x0000; |
| 71 | |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 72 | // Handler message codes |
| 73 | private static final int MSG_SWITCH_USER = 1; |
| 74 | |
| 75 | private static final int RETRY_DELAY_TIME = 20; //ms |
| 76 | |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 77 | // Maximum entries to keep in usage history before dumping out |
| 78 | private static final int MAX_USAGE_HISTORY = 100; |
| 79 | |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 80 | private final Context mContext; |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 81 | private final ServiceThread mHandlerThread; |
| 82 | private final Handler mHandler; |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 83 | private UserManager mUserManager; |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 84 | |
| 85 | private final Object mLock = new Object(); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 86 | private Set<Integer> mEnabledCameraUsers; |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 87 | private int mLastUser; |
| 88 | |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 89 | private ICameraService mCameraServiceRaw; |
| 90 | |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 91 | private final ArrayMap<String, CameraUsageEvent> mActiveCameraUsage = new ArrayMap<>(); |
| 92 | private final List<CameraUsageEvent> mCameraUsageHistory = new ArrayList<>(); |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 93 | private final MetricsLogger mLogger = new MetricsLogger(); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 94 | private static final String NFC_NOTIFICATION_PROP = "ro.camera.notify_nfc"; |
| 95 | private static final String NFC_SERVICE_BINDER_NAME = "nfc"; |
| 96 | private static final IBinder nfcInterfaceToken = new Binder(); |
| 97 | |
| 98 | private final boolean mNotifyNfc; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Structure to track camera usage |
| 102 | */ |
| 103 | private static class CameraUsageEvent { |
| 104 | public final int mCameraFacing; |
| 105 | public final String mClientName; |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 106 | public final int mAPILevel; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 107 | |
| 108 | private boolean mCompleted; |
| 109 | private long mDurationOrStartTimeMs; // Either start time, or duration once completed |
| 110 | |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 111 | public CameraUsageEvent(int facing, String clientName, int apiLevel) { |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 112 | mCameraFacing = facing; |
| 113 | mClientName = clientName; |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 114 | mAPILevel = apiLevel; |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 115 | mDurationOrStartTimeMs = SystemClock.elapsedRealtime(); |
| 116 | mCompleted = false; |
| 117 | } |
| 118 | |
| 119 | public void markCompleted() { |
| 120 | if (mCompleted) { |
| 121 | return; |
| 122 | } |
| 123 | mCompleted = true; |
| 124 | mDurationOrStartTimeMs = SystemClock.elapsedRealtime() - mDurationOrStartTimeMs; |
| 125 | if (CameraServiceProxy.DEBUG) { |
| 126 | Slog.v(TAG, "A camera facing " + cameraFacingToString(mCameraFacing) + |
| 127 | " was in use by " + mClientName + " for " + |
| 128 | mDurationOrStartTimeMs + " ms"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Return duration of camera usage event, or 0 if the event is not done |
| 134 | */ |
| 135 | public long getDuration() { |
| 136 | return mCompleted ? mDurationOrStartTimeMs : 0; |
| 137 | } |
| 138 | } |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 139 | |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 140 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 141 | @Override |
| 142 | public void onReceive(Context context, Intent intent) { |
| 143 | final String action = intent.getAction(); |
| 144 | if (action == null) return; |
| 145 | |
| 146 | switch (action) { |
| 147 | case Intent.ACTION_USER_ADDED: |
| 148 | case Intent.ACTION_USER_REMOVED: |
| 149 | case Intent.ACTION_USER_INFO_CHANGED: |
| 150 | case Intent.ACTION_MANAGED_PROFILE_ADDED: |
| 151 | case Intent.ACTION_MANAGED_PROFILE_REMOVED: |
| 152 | synchronized(mLock) { |
| 153 | // Return immediately if we haven't seen any users start yet |
| 154 | if (mEnabledCameraUsers == null) return; |
| 155 | switchUserLocked(mLastUser); |
| 156 | } |
| 157 | break; |
| 158 | default: |
| 159 | break; // do nothing |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | }; |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 164 | |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 165 | private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() { |
| 166 | @Override |
| 167 | public void pingForUserUpdate() { |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 168 | notifySwitchWithRetries(30); |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 169 | } |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
Eino-Ville Talvala | fb9b64c | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 172 | public void notifyCameraState(String cameraId, int newCameraState, int facing, |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 173 | String clientName, int apiLevel) { |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 174 | String state = cameraStateToString(newCameraState); |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 175 | String facingStr = cameraFacingToString(facing); |
| 176 | if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " + |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 177 | state + " for client " + clientName + " API Level " + apiLevel); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 178 | |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 179 | updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 180 | } |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Wei Wang | ed7f548 | 2017-02-21 11:35:10 -0800 | [diff] [blame] | 183 | public CameraServiceProxy(Context context) { |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 184 | super(context); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 185 | mContext = context; |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 186 | mHandlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_DISPLAY, /*allowTo*/false); |
| 187 | mHandlerThread.start(); |
| 188 | mHandler = new Handler(mHandlerThread.getLooper(), this); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 189 | |
| 190 | mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0; |
| 191 | if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled")); |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean handleMessage(Message msg) { |
| 196 | switch(msg.what) { |
| 197 | case MSG_SWITCH_USER: { |
| 198 | notifySwitchWithRetries(msg.arg1); |
| 199 | } break; |
| 200 | default: { |
Wei Wang | ed7f548 | 2017-02-21 11:35:10 -0800 | [diff] [blame] | 201 | Slog.e(TAG, "CameraServiceProxy error, invalid message: " + msg.what); |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 202 | } break; |
| 203 | } |
| 204 | return true; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 208 | public void onStart() { |
| 209 | mUserManager = UserManager.get(mContext); |
| 210 | if (mUserManager == null) { |
| 211 | // Should never see this unless someone messes up the SystemServer service boot order. |
Wei Wang | ed7f548 | 2017-02-21 11:35:10 -0800 | [diff] [blame] | 212 | throw new IllegalStateException("UserManagerService must start before" + |
| 213 | " CameraServiceProxy!"); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 214 | } |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 215 | |
| 216 | IntentFilter filter = new IntentFilter(); |
| 217 | filter.addAction(Intent.ACTION_USER_ADDED); |
| 218 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 219 | filter.addAction(Intent.ACTION_USER_INFO_CHANGED); |
| 220 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 221 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 222 | mContext.registerReceiver(mIntentReceiver, filter); |
| 223 | |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 224 | publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy); |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 225 | publishLocalService(CameraServiceProxy.class, this); |
| 226 | |
| 227 | CameraStatsJobService.schedule(mContext); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void onStartUser(int userHandle) { |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 232 | synchronized(mLock) { |
| 233 | if (mEnabledCameraUsers == null) { |
| 234 | // Initialize mediaserver, or update mediaserver if we are recovering from a crash. |
| 235 | switchUserLocked(userHandle); |
| 236 | } |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 239 | |
| 240 | @Override |
| 241 | public void onSwitchUser(int userHandle) { |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 242 | synchronized(mLock) { |
| 243 | switchUserLocked(userHandle); |
| 244 | } |
| 245 | } |
| 246 | |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Handle the death of the native camera service |
| 249 | */ |
| 250 | @Override |
| 251 | public void binderDied() { |
| 252 | if (DEBUG) Slog.w(TAG, "Native camera service has died"); |
| 253 | synchronized(mLock) { |
| 254 | mCameraServiceRaw = null; |
| 255 | |
| 256 | // All cameras reset to idle on camera service death |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 257 | boolean wasEmpty = mActiveCameraUsage.isEmpty(); |
| 258 | mActiveCameraUsage.clear(); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 259 | |
| 260 | if ( mNotifyNfc && !wasEmpty ) { |
| 261 | notifyNfcService(/*enablePolling*/ true); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Dump camera usage events to log. |
| 268 | * Package-private |
| 269 | */ |
| 270 | void dumpUsageEvents() { |
| 271 | synchronized(mLock) { |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 272 | // Randomize order of events so that it's not meaningful |
| 273 | Collections.shuffle(mCameraUsageHistory); |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 274 | for (CameraUsageEvent e : mCameraUsageHistory) { |
| 275 | if (DEBUG) { |
| 276 | Slog.v(TAG, "Camera: " + e.mClientName + " used a camera facing " + |
| 277 | cameraFacingToString(e.mCameraFacing) + " for " + |
| 278 | e.getDuration() + " ms"); |
| 279 | } |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 280 | int subtype = 0; |
| 281 | switch(e.mCameraFacing) { |
| 282 | case ICameraServiceProxy.CAMERA_FACING_BACK: |
| 283 | subtype = MetricsEvent.CAMERA_BACK_USED; |
| 284 | break; |
| 285 | case ICameraServiceProxy.CAMERA_FACING_FRONT: |
| 286 | subtype = MetricsEvent.CAMERA_FRONT_USED; |
| 287 | break; |
| 288 | case ICameraServiceProxy.CAMERA_FACING_EXTERNAL: |
| 289 | subtype = MetricsEvent.CAMERA_EXTERNAL_USED; |
| 290 | break; |
| 291 | default: |
| 292 | continue; |
| 293 | } |
| 294 | LogMaker l = new LogMaker(MetricsEvent.ACTION_CAMERA_EVENT) |
| 295 | .setType(MetricsEvent.TYPE_ACTION) |
| 296 | .setSubtype(subtype) |
| 297 | .setLatency(e.getDuration()) |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 298 | .addTaggedData(MetricsEvent.FIELD_CAMERA_API_LEVEL, e.mAPILevel) |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 299 | .setPackageName(e.mClientName); |
| 300 | mLogger.write(l); |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 301 | } |
| 302 | mCameraUsageHistory.clear(); |
| 303 | } |
| 304 | CameraStatsJobService.schedule(mContext); |
| 305 | } |
| 306 | |
Ruben Brunk | 6664aee | 2015-05-19 17:20:24 -0700 | [diff] [blame] | 307 | private void switchUserLocked(int userHandle) { |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 308 | Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle); |
Ruben Brunk | a59a870 | 2015-06-23 11:52:08 -0700 | [diff] [blame] | 309 | mLastUser = userHandle; |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 310 | if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) { |
| 311 | // Some user handles have been added or removed, update mediaserver. |
| 312 | mEnabledCameraUsers = currentUserHandles; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 313 | notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, currentUserHandles); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 316 | |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 317 | private Set<Integer> getEnabledUserHandles(int currentUserHandle) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 318 | int[] userProfiles = mUserManager.getEnabledProfileIds(currentUserHandle); |
| 319 | Set<Integer> handles = new ArraySet<>(userProfiles.length); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 320 | |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 321 | for (int id : userProfiles) { |
| 322 | handles.add(id); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return handles; |
| 326 | } |
| 327 | |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 328 | private void notifySwitchWithRetries(int retries) { |
| 329 | synchronized(mLock) { |
| 330 | if (mEnabledCameraUsers == null) { |
| 331 | return; |
| 332 | } |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 333 | if (notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, mEnabledCameraUsers)) { |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 334 | retries = 0; |
| 335 | } |
| 336 | } |
| 337 | if (retries <= 0) { |
| 338 | return; |
| 339 | } |
| 340 | Slog.i(TAG, "Could not notify camera service of user switch, retrying..."); |
| 341 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SWITCH_USER, retries - 1, 0, null), |
| 342 | RETRY_DELAY_TIME); |
| 343 | } |
| 344 | |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 345 | private boolean notifyMediaserverLocked(int eventType, Set<Integer> updatedUserHandles) { |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 346 | // Forward the user switch event to the native camera service running in the mediaserver |
| 347 | // process. |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 348 | if (mCameraServiceRaw == null) { |
| 349 | IBinder cameraServiceBinder = getBinderService(CAMERA_SERVICE_BINDER_NAME); |
| 350 | if (cameraServiceBinder == null) { |
| 351 | Slog.w(TAG, "Could not notify mediaserver, camera service not available."); |
| 352 | return false; // Camera service not active, cannot evict user clients. |
| 353 | } |
| 354 | try { |
| 355 | cameraServiceBinder.linkToDeath(this, /*flags*/ 0); |
| 356 | } catch (RemoteException e) { |
| 357 | Slog.w(TAG, "Could not link to death of native camera service"); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | mCameraServiceRaw = ICameraService.Stub.asInterface(cameraServiceBinder); |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 362 | } |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 363 | |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 364 | try { |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 365 | mCameraServiceRaw.notifySystemEvent(eventType, toArray(updatedUserHandles)); |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 366 | } catch (RemoteException e) { |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 367 | Slog.w(TAG, "Could not notify mediaserver, remote exception: " + e); |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 368 | // Not much we can do if camera service is dead. |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 369 | return false; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 370 | } |
Ruben Brunk | 28388e8 | 2015-06-01 18:41:28 -0700 | [diff] [blame] | 371 | return true; |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 372 | } |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 373 | |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 374 | private void updateActivityCount(String cameraId, int newCameraState, int facing, |
| 375 | String clientName, int apiLevel) { |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 376 | synchronized(mLock) { |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 377 | // Update active camera list and notify NFC if necessary |
| 378 | boolean wasEmpty = mActiveCameraUsage.isEmpty(); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 379 | switch (newCameraState) { |
Eino-Ville Talvala | fb9b64c | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 380 | case ICameraServiceProxy.CAMERA_STATE_OPEN: |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 381 | break; |
Eino-Ville Talvala | fb9b64c | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 382 | case ICameraServiceProxy.CAMERA_STATE_ACTIVE: |
Emilian Peev | 6bf0a55 | 2018-02-10 02:15:49 +0000 | [diff] [blame] | 383 | CameraUsageEvent newEvent = new CameraUsageEvent(facing, clientName, apiLevel); |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 384 | CameraUsageEvent oldEvent = mActiveCameraUsage.put(cameraId, newEvent); |
| 385 | if (oldEvent != null) { |
| 386 | Slog.w(TAG, "Camera " + cameraId + " was already marked as active"); |
| 387 | oldEvent.markCompleted(); |
| 388 | mCameraUsageHistory.add(oldEvent); |
| 389 | } |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 390 | break; |
Eino-Ville Talvala | fb9b64c | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 391 | case ICameraServiceProxy.CAMERA_STATE_IDLE: |
| 392 | case ICameraServiceProxy.CAMERA_STATE_CLOSED: |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 393 | CameraUsageEvent doneEvent = mActiveCameraUsage.remove(cameraId); |
| 394 | if (doneEvent != null) { |
| 395 | doneEvent.markCompleted(); |
| 396 | mCameraUsageHistory.add(doneEvent); |
Eino-Ville Talvala | 31ad8a3 | 2017-07-10 16:23:50 -0700 | [diff] [blame] | 397 | if (mCameraUsageHistory.size() > MAX_USAGE_HISTORY) { |
| 398 | dumpUsageEvents(); |
| 399 | } |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 400 | } |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 401 | break; |
| 402 | } |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 403 | boolean isEmpty = mActiveCameraUsage.isEmpty(); |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 404 | if ( mNotifyNfc && (wasEmpty != isEmpty) ) { |
| 405 | notifyNfcService(isEmpty); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private void notifyNfcService(boolean enablePolling) { |
| 411 | |
| 412 | IBinder nfcServiceBinder = getBinderService(NFC_SERVICE_BINDER_NAME); |
| 413 | if (nfcServiceBinder == null) { |
| 414 | Slog.w(TAG, "Could not connect to NFC service to notify it of camera state"); |
| 415 | return; |
| 416 | } |
| 417 | INfcAdapter nfcAdapterRaw = INfcAdapter.Stub.asInterface(nfcServiceBinder); |
| 418 | int flags = enablePolling ? ENABLE_POLLING_FLAGS : DISABLE_POLLING_FLAGS; |
| 419 | if (DEBUG) Slog.v(TAG, "Setting NFC reader mode to flags " + flags); |
| 420 | try { |
| 421 | nfcAdapterRaw.setReaderMode(nfcInterfaceToken, null, flags, null); |
| 422 | } catch (RemoteException e) { |
| 423 | Slog.w(TAG, "Could not notify NFC service, remote exception: " + e); |
| 424 | } |
| 425 | } |
| 426 | |
Ruben Brunk | 7f75da2 | 2015-04-30 17:46:30 -0700 | [diff] [blame] | 427 | private static int[] toArray(Collection<Integer> c) { |
| 428 | int len = c.size(); |
| 429 | int[] ret = new int[len]; |
| 430 | int idx = 0; |
| 431 | for (Integer i : c) { |
| 432 | ret[idx++] = i; |
| 433 | } |
| 434 | return ret; |
| 435 | } |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 436 | |
| 437 | private static String cameraStateToString(int newCameraState) { |
| 438 | switch (newCameraState) { |
Eino-Ville Talvala | fb9b64c | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 439 | case ICameraServiceProxy.CAMERA_STATE_OPEN: return "CAMERA_STATE_OPEN"; |
| 440 | case ICameraServiceProxy.CAMERA_STATE_ACTIVE: return "CAMERA_STATE_ACTIVE"; |
| 441 | case ICameraServiceProxy.CAMERA_STATE_IDLE: return "CAMERA_STATE_IDLE"; |
| 442 | case ICameraServiceProxy.CAMERA_STATE_CLOSED: return "CAMERA_STATE_CLOSED"; |
Eino-Ville Talvala | 2e3215c | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 443 | default: break; |
| 444 | } |
| 445 | return "CAMERA_STATE_UNKNOWN"; |
| 446 | } |
Eino-Ville Talvala | e91012b | 2017-07-10 15:27:24 -0700 | [diff] [blame] | 447 | |
| 448 | private static String cameraFacingToString(int cameraFacing) { |
| 449 | switch (cameraFacing) { |
| 450 | case ICameraServiceProxy.CAMERA_FACING_BACK: return "CAMERA_FACING_BACK"; |
| 451 | case ICameraServiceProxy.CAMERA_FACING_FRONT: return "CAMERA_FACING_FRONT"; |
| 452 | case ICameraServiceProxy.CAMERA_FACING_EXTERNAL: return "CAMERA_FACING_EXTERNAL"; |
| 453 | default: break; |
| 454 | } |
| 455 | return "CAMERA_FACING_UNKNOWN"; |
| 456 | } |
| 457 | |
Ruben Brunk | 9d21ee5 | 2015-03-20 22:18:55 -0700 | [diff] [blame] | 458 | } |