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