blob: 61c99b88d113cc402a188405537870eaa7ca7995 [file] [log] [blame]
Ruben Brunk9d21ee52015-03-20 22:18:55 -07001/*
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 */
16package com.android.server.camera;
17
Ruben Brunka59a8702015-06-23 11:52:08 -070018import android.content.BroadcastReceiver;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070019import android.content.Context;
Ruben Brunka59a8702015-06-23 11:52:08 -070020import android.content.Intent;
21import android.content.IntentFilter;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070022import android.hardware.ICameraService;
Ruben Brunk6664aee2015-05-19 17:20:24 -070023import android.hardware.ICameraServiceProxy;
Eino-Ville Talvala06075ee2018-10-19 13:09:39 -070024import android.media.AudioManager;
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070025import android.metrics.LogMaker;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070026import android.nfc.INfcAdapter;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070027import android.os.Binder;
Ruben Brunk28388e82015-06-01 18:41:28 -070028import android.os.Handler;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070029import android.os.IBinder;
Ruben Brunk28388e82015-06-01 18:41:28 -070030import android.os.Message;
31import android.os.Process;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070032import android.os.RemoteException;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070033import android.os.SystemClock;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070034import android.os.SystemProperties;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070035import android.os.UserManager;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070036import android.util.ArrayMap;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070037import android.util.ArraySet;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070038import android.util.Slog;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070039
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070040import com.android.internal.logging.MetricsLogger;
41import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Muhammad Qureshie2b24322020-01-28 10:54:17 -080042import com.android.internal.util.FrameworkStatsLog;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070043import com.android.server.LocalServices;
Ruben Brunk28388e82015-06-01 18:41:28 -070044import com.android.server.ServiceThread;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070045import com.android.server.SystemService;
Eino-Ville Talvalab3805162019-05-13 15:38:11 -070046import com.android.server.wm.WindowManagerInternal;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070047
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070048import java.util.ArrayList;
Ruben Brunk7f75da22015-04-30 17:46:30 -070049import java.util.Collection;
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070050import java.util.Collections;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070051import java.util.List;
Ruben Brunk7f75da22015-04-30 17:46:30 -070052import java.util.Set;
Muhammad Qureshie2b24322020-01-28 10:54:17 -080053import java.util.concurrent.ScheduledThreadPoolExecutor;
54import java.util.concurrent.TimeUnit;
Ruben Brunk7f75da22015-04-30 17:46:30 -070055
Ruben Brunk9d21ee52015-03-20 22:18:55 -070056/**
Shuzhen Wang64e64372019-04-08 10:49:42 -070057 * CameraServiceProxy is the system_server analog to the camera service running in cameraserver.
Ruben Brunk9d21ee52015-03-20 22:18:55 -070058 *
59 * @hide
60 */
Wei Wanged7f5482017-02-21 11:35:10 -080061public class CameraServiceProxy extends SystemService
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070062 implements Handler.Callback, IBinder.DeathRecipient {
Ruben Brunk28388e82015-06-01 18:41:28 -070063 private static final String TAG = "CameraService_proxy";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070064 private static final boolean DEBUG = false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070065
66 /**
67 * This must match the ICameraService.aidl definition
68 */
69 private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
70
Ruben Brunk6664aee2015-05-19 17:20:24 -070071 public static final String CAMERA_SERVICE_PROXY_BINDER_NAME = "media.camera.proxy";
72
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070073 // Flags arguments to NFC adapter to enable/disable NFC
74 public static final int DISABLE_POLLING_FLAGS = 0x1000;
75 public static final int ENABLE_POLLING_FLAGS = 0x0000;
76
Ruben Brunk28388e82015-06-01 18:41:28 -070077 // Handler message codes
78 private static final int MSG_SWITCH_USER = 1;
79
80 private static final int RETRY_DELAY_TIME = 20; //ms
Shuzhen Wanga251ea22019-09-09 15:43:49 -070081 private static final int RETRY_TIMES = 60;
Ruben Brunk28388e82015-06-01 18:41:28 -070082
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070083 // Maximum entries to keep in usage history before dumping out
84 private static final int MAX_USAGE_HISTORY = 100;
85
Ruben Brunk7f75da22015-04-30 17:46:30 -070086 private final Context mContext;
Ruben Brunk28388e82015-06-01 18:41:28 -070087 private final ServiceThread mHandlerThread;
88 private final Handler mHandler;
Ruben Brunk7f75da22015-04-30 17:46:30 -070089 private UserManager mUserManager;
Ruben Brunk6664aee2015-05-19 17:20:24 -070090
91 private final Object mLock = new Object();
Ruben Brunk7f75da22015-04-30 17:46:30 -070092 private Set<Integer> mEnabledCameraUsers;
Ruben Brunka59a8702015-06-23 11:52:08 -070093 private int mLastUser;
94
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070095 private ICameraService mCameraServiceRaw;
96
Eino-Ville Talvalab3805162019-05-13 15:38:11 -070097 // Map of currently active camera IDs
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070098 private final ArrayMap<String, CameraUsageEvent> mActiveCameraUsage = new ArrayMap<>();
99 private final List<CameraUsageEvent> mCameraUsageHistory = new ArrayList<>();
Eino-Ville Talvalab3805162019-05-13 15:38:11 -0700100
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700101 private final MetricsLogger mLogger = new MetricsLogger();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700102 private static final String NFC_NOTIFICATION_PROP = "ro.camera.notify_nfc";
103 private static final String NFC_SERVICE_BINDER_NAME = "nfc";
104 private static final IBinder nfcInterfaceToken = new Binder();
105
106 private final boolean mNotifyNfc;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700107
Emilian Peev6729f372019-08-29 09:14:09 -0700108 private ScheduledThreadPoolExecutor mLogWriterService = new ScheduledThreadPoolExecutor(
109 /*corePoolSize*/ 1);
110
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700111 /**
112 * Structure to track camera usage
113 */
114 private static class CameraUsageEvent {
115 public final int mCameraFacing;
116 public final String mClientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000117 public final int mAPILevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700118
119 private boolean mCompleted;
120 private long mDurationOrStartTimeMs; // Either start time, or duration once completed
121
Emilian Peev6bf0a552018-02-10 02:15:49 +0000122 public CameraUsageEvent(int facing, String clientName, int apiLevel) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700123 mCameraFacing = facing;
124 mClientName = clientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000125 mAPILevel = apiLevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700126 mDurationOrStartTimeMs = SystemClock.elapsedRealtime();
127 mCompleted = false;
128 }
129
130 public void markCompleted() {
131 if (mCompleted) {
132 return;
133 }
134 mCompleted = true;
135 mDurationOrStartTimeMs = SystemClock.elapsedRealtime() - mDurationOrStartTimeMs;
136 if (CameraServiceProxy.DEBUG) {
137 Slog.v(TAG, "A camera facing " + cameraFacingToString(mCameraFacing) +
138 " was in use by " + mClientName + " for " +
139 mDurationOrStartTimeMs + " ms");
140 }
141 }
142
143 /**
144 * Return duration of camera usage event, or 0 if the event is not done
145 */
146 public long getDuration() {
147 return mCompleted ? mDurationOrStartTimeMs : 0;
148 }
149 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700150
Ruben Brunka59a8702015-06-23 11:52:08 -0700151 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
152 @Override
153 public void onReceive(Context context, Intent intent) {
154 final String action = intent.getAction();
155 if (action == null) return;
156
157 switch (action) {
158 case Intent.ACTION_USER_ADDED:
159 case Intent.ACTION_USER_REMOVED:
160 case Intent.ACTION_USER_INFO_CHANGED:
161 case Intent.ACTION_MANAGED_PROFILE_ADDED:
162 case Intent.ACTION_MANAGED_PROFILE_REMOVED:
163 synchronized(mLock) {
164 // Return immediately if we haven't seen any users start yet
165 if (mEnabledCameraUsers == null) return;
166 switchUserLocked(mLastUser);
167 }
168 break;
169 default:
170 break; // do nothing
171 }
172
173 }
174 };
Ruben Brunk7f75da22015-04-30 17:46:30 -0700175
Ruben Brunk6664aee2015-05-19 17:20:24 -0700176 private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
177 @Override
178 public void pingForUserUpdate() {
Emilian Peev44e19a02018-04-12 14:21:20 +0100179 if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
180 Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
181 " camera service UID!");
182 return;
183 }
Shuzhen Wang64e64372019-04-08 10:49:42 -0700184 notifySwitchWithRetries(RETRY_TIMES);
Ruben Brunk6664aee2015-05-19 17:20:24 -0700185 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700186
187 @Override
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700188 public void notifyCameraState(String cameraId, int newCameraState, int facing,
Emilian Peev6bf0a552018-02-10 02:15:49 +0000189 String clientName, int apiLevel) {
Emilian Peev44e19a02018-04-12 14:21:20 +0100190 if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
191 Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
192 " camera service UID!");
193 return;
194 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700195 String state = cameraStateToString(newCameraState);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700196 String facingStr = cameraFacingToString(facing);
197 if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
Emilian Peev6bf0a552018-02-10 02:15:49 +0000198 state + " for client " + clientName + " API Level " + apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700199
Emilian Peev6bf0a552018-02-10 02:15:49 +0000200 updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700201 }
Ruben Brunk6664aee2015-05-19 17:20:24 -0700202 };
203
Wei Wanged7f5482017-02-21 11:35:10 -0800204 public CameraServiceProxy(Context context) {
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700205 super(context);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700206 mContext = context;
Ruben Brunk28388e82015-06-01 18:41:28 -0700207 mHandlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_DISPLAY, /*allowTo*/false);
208 mHandlerThread.start();
209 mHandler = new Handler(mHandlerThread.getLooper(), this);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700210
211 mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0;
212 if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled"));
Emilian Peev6729f372019-08-29 09:14:09 -0700213 // Don't keep any extra logging threads if not needed
214 mLogWriterService.setKeepAliveTime(1, TimeUnit.SECONDS);
215 mLogWriterService.allowCoreThreadTimeOut(true);
Ruben Brunk28388e82015-06-01 18:41:28 -0700216 }
217
218 @Override
219 public boolean handleMessage(Message msg) {
220 switch(msg.what) {
221 case MSG_SWITCH_USER: {
222 notifySwitchWithRetries(msg.arg1);
223 } break;
224 default: {
Wei Wanged7f5482017-02-21 11:35:10 -0800225 Slog.e(TAG, "CameraServiceProxy error, invalid message: " + msg.what);
Ruben Brunk28388e82015-06-01 18:41:28 -0700226 } break;
227 }
228 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700229 }
230
231 @Override
Ruben Brunk7f75da22015-04-30 17:46:30 -0700232 public void onStart() {
233 mUserManager = UserManager.get(mContext);
234 if (mUserManager == null) {
235 // Should never see this unless someone messes up the SystemServer service boot order.
Wei Wanged7f5482017-02-21 11:35:10 -0800236 throw new IllegalStateException("UserManagerService must start before" +
237 " CameraServiceProxy!");
Ruben Brunk7f75da22015-04-30 17:46:30 -0700238 }
Ruben Brunka59a8702015-06-23 11:52:08 -0700239
240 IntentFilter filter = new IntentFilter();
241 filter.addAction(Intent.ACTION_USER_ADDED);
242 filter.addAction(Intent.ACTION_USER_REMOVED);
243 filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
244 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
245 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
246 mContext.registerReceiver(mIntentReceiver, filter);
247
Ruben Brunk6664aee2015-05-19 17:20:24 -0700248 publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700249 publishLocalService(CameraServiceProxy.class, this);
250
251 CameraStatsJobService.schedule(mContext);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700252 }
253
254 @Override
255 public void onStartUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700256 synchronized(mLock) {
257 if (mEnabledCameraUsers == null) {
Shuzhen Wang64e64372019-04-08 10:49:42 -0700258 // Initialize cameraserver, or update cameraserver if we are recovering
259 // from a crash.
Ruben Brunk6664aee2015-05-19 17:20:24 -0700260 switchUserLocked(userHandle);
261 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700262 }
263 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700264
265 @Override
266 public void onSwitchUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700267 synchronized(mLock) {
268 switchUserLocked(userHandle);
269 }
270 }
271
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700272 /**
273 * Handle the death of the native camera service
274 */
275 @Override
276 public void binderDied() {
277 if (DEBUG) Slog.w(TAG, "Native camera service has died");
278 synchronized(mLock) {
279 mCameraServiceRaw = null;
280
281 // All cameras reset to idle on camera service death
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700282 boolean wasEmpty = mActiveCameraUsage.isEmpty();
283 mActiveCameraUsage.clear();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700284
285 if ( mNotifyNfc && !wasEmpty ) {
286 notifyNfcService(/*enablePolling*/ true);
287 }
288 }
289 }
290
Emilian Peev6729f372019-08-29 09:14:09 -0700291 private class EventWriterTask implements Runnable {
292 private ArrayList<CameraUsageEvent> mEventList;
293 private static final long WRITER_SLEEP_MS = 100;
294
295 public EventWriterTask(ArrayList<CameraUsageEvent> eventList) {
296 mEventList = eventList;
297 }
298
299 @Override
300 public void run() {
301 if (mEventList != null) {
302 for (CameraUsageEvent event : mEventList) {
303 logCameraUsageEvent(event);
304 try {
305 Thread.sleep(WRITER_SLEEP_MS);
306 } catch (InterruptedException e) {}
307 }
308 mEventList.clear();
309 }
310 }
311
312 /**
313 * Write camera usage events to stats log.
314 * Package-private
315 */
316 private void logCameraUsageEvent(CameraUsageEvent e) {
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800317 int facing = FrameworkStatsLog.CAMERA_ACTION_EVENT__FACING__UNKNOWN;
Emilian Peev6729f372019-08-29 09:14:09 -0700318 switch(e.mCameraFacing) {
319 case ICameraServiceProxy.CAMERA_FACING_BACK:
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800320 facing = FrameworkStatsLog.CAMERA_ACTION_EVENT__FACING__BACK;
Emilian Peev6729f372019-08-29 09:14:09 -0700321 break;
322 case ICameraServiceProxy.CAMERA_FACING_FRONT:
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800323 facing = FrameworkStatsLog.CAMERA_ACTION_EVENT__FACING__FRONT;
Emilian Peev6729f372019-08-29 09:14:09 -0700324 break;
325 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL:
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800326 facing = FrameworkStatsLog.CAMERA_ACTION_EVENT__FACING__EXTERNAL;
Emilian Peev6729f372019-08-29 09:14:09 -0700327 break;
328 default:
329 Slog.w(TAG, "Unknown camera facing: " + e.mCameraFacing);
330 }
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800331 FrameworkStatsLog.write(FrameworkStatsLog.CAMERA_ACTION_EVENT, e.getDuration(),
332 e.mAPILevel, e.mClientName, facing);
Emilian Peev6729f372019-08-29 09:14:09 -0700333 }
334 }
335
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700336 /**
337 * Dump camera usage events to log.
338 * Package-private
339 */
340 void dumpUsageEvents() {
341 synchronized(mLock) {
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700342 // Randomize order of events so that it's not meaningful
343 Collections.shuffle(mCameraUsageHistory);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700344 for (CameraUsageEvent e : mCameraUsageHistory) {
345 if (DEBUG) {
346 Slog.v(TAG, "Camera: " + e.mClientName + " used a camera facing " +
347 cameraFacingToString(e.mCameraFacing) + " for " +
348 e.getDuration() + " ms");
349 }
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700350 int subtype = 0;
351 switch(e.mCameraFacing) {
352 case ICameraServiceProxy.CAMERA_FACING_BACK:
353 subtype = MetricsEvent.CAMERA_BACK_USED;
354 break;
355 case ICameraServiceProxy.CAMERA_FACING_FRONT:
356 subtype = MetricsEvent.CAMERA_FRONT_USED;
357 break;
358 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL:
359 subtype = MetricsEvent.CAMERA_EXTERNAL_USED;
360 break;
361 default:
362 continue;
363 }
364 LogMaker l = new LogMaker(MetricsEvent.ACTION_CAMERA_EVENT)
365 .setType(MetricsEvent.TYPE_ACTION)
366 .setSubtype(subtype)
367 .setLatency(e.getDuration())
Emilian Peev6bf0a552018-02-10 02:15:49 +0000368 .addTaggedData(MetricsEvent.FIELD_CAMERA_API_LEVEL, e.mAPILevel)
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700369 .setPackageName(e.mClientName);
370 mLogger.write(l);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700371 }
Emilian Peev6729f372019-08-29 09:14:09 -0700372
373 mLogWriterService.execute(new EventWriterTask(
374 new ArrayList<CameraUsageEvent>(mCameraUsageHistory)));
375
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700376 mCameraUsageHistory.clear();
377 }
Emilian Peev44e19a02018-04-12 14:21:20 +0100378 final long ident = Binder.clearCallingIdentity();
379 try {
380 CameraStatsJobService.schedule(mContext);
381 } finally {
382 Binder.restoreCallingIdentity(ident);
383 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700384 }
385
Ruben Brunk6664aee2015-05-19 17:20:24 -0700386 private void switchUserLocked(int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700387 Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle);
Ruben Brunka59a8702015-06-23 11:52:08 -0700388 mLastUser = userHandle;
Ruben Brunk7f75da22015-04-30 17:46:30 -0700389 if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) {
Shuzhen Wang64e64372019-04-08 10:49:42 -0700390 // Some user handles have been added or removed, update cameraserver.
Ruben Brunk7f75da22015-04-30 17:46:30 -0700391 mEnabledCameraUsers = currentUserHandles;
Shuzhen Wang64e64372019-04-08 10:49:42 -0700392 notifySwitchWithRetriesLocked(RETRY_TIMES);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700393 }
394 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700395
Ruben Brunk7f75da22015-04-30 17:46:30 -0700396 private Set<Integer> getEnabledUserHandles(int currentUserHandle) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700397 int[] userProfiles = mUserManager.getEnabledProfileIds(currentUserHandle);
398 Set<Integer> handles = new ArraySet<>(userProfiles.length);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700399
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700400 for (int id : userProfiles) {
401 handles.add(id);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700402 }
403
404 return handles;
405 }
406
Ruben Brunk28388e82015-06-01 18:41:28 -0700407 private void notifySwitchWithRetries(int retries) {
408 synchronized(mLock) {
Shuzhen Wang64e64372019-04-08 10:49:42 -0700409 notifySwitchWithRetriesLocked(retries);
410 }
411 }
412
413 private void notifySwitchWithRetriesLocked(int retries) {
414 if (mEnabledCameraUsers == null) {
415 return;
416 }
417 if (notifyCameraserverLocked(ICameraService.EVENT_USER_SWITCHED, mEnabledCameraUsers)) {
418 retries = 0;
Ruben Brunk28388e82015-06-01 18:41:28 -0700419 }
420 if (retries <= 0) {
421 return;
422 }
423 Slog.i(TAG, "Could not notify camera service of user switch, retrying...");
424 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SWITCH_USER, retries - 1, 0, null),
425 RETRY_DELAY_TIME);
426 }
427
Shuzhen Wang64e64372019-04-08 10:49:42 -0700428 private boolean notifyCameraserverLocked(int eventType, Set<Integer> updatedUserHandles) {
429 // Forward the user switch event to the native camera service running in the cameraserver
Ruben Brunk7f75da22015-04-30 17:46:30 -0700430 // process.
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700431 if (mCameraServiceRaw == null) {
432 IBinder cameraServiceBinder = getBinderService(CAMERA_SERVICE_BINDER_NAME);
433 if (cameraServiceBinder == null) {
Shuzhen Wang64e64372019-04-08 10:49:42 -0700434 Slog.w(TAG, "Could not notify cameraserver, camera service not available.");
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700435 return false; // Camera service not active, cannot evict user clients.
436 }
437 try {
438 cameraServiceBinder.linkToDeath(this, /*flags*/ 0);
439 } catch (RemoteException e) {
440 Slog.w(TAG, "Could not link to death of native camera service");
441 return false;
442 }
443
444 mCameraServiceRaw = ICameraService.Stub.asInterface(cameraServiceBinder);
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700445 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700446
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700447 try {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700448 mCameraServiceRaw.notifySystemEvent(eventType, toArray(updatedUserHandles));
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700449 } catch (RemoteException e) {
Shuzhen Wang64e64372019-04-08 10:49:42 -0700450 Slog.w(TAG, "Could not notify cameraserver, remote exception: " + e);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700451 // Not much we can do if camera service is dead.
Ruben Brunk28388e82015-06-01 18:41:28 -0700452 return false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700453 }
Ruben Brunk28388e82015-06-01 18:41:28 -0700454 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700455 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700456
Emilian Peev6bf0a552018-02-10 02:15:49 +0000457 private void updateActivityCount(String cameraId, int newCameraState, int facing,
458 String clientName, int apiLevel) {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700459 synchronized(mLock) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700460 // Update active camera list and notify NFC if necessary
461 boolean wasEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700462 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700463 case ICameraServiceProxy.CAMERA_STATE_OPEN:
Eino-Ville Talvala06075ee2018-10-19 13:09:39 -0700464 // Notify the audio subsystem about the facing of the most-recently opened
465 // camera This can be used to select the best audio tuning in case video
466 // recording with that camera will happen. Since only open events are used, if
467 // multiple cameras are opened at once, the one opened last will be used to
468 // select audio tuning.
469 AudioManager audioManager = getContext().getSystemService(AudioManager.class);
470 if (audioManager != null) {
471 // Map external to front for audio tuning purposes
472 String facingStr = (facing == ICameraServiceProxy.CAMERA_FACING_BACK) ?
473 "back" : "front";
474 String facingParameter = "cameraFacing=" + facingStr;
475 audioManager.setParameters(facingParameter);
476 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700477 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700478 case ICameraServiceProxy.CAMERA_STATE_ACTIVE:
Eino-Ville Talvalab3805162019-05-13 15:38:11 -0700479 // Check current active camera IDs to see if this package is already talking to
480 // some camera
481 boolean alreadyActivePackage = false;
482 for (int i = 0; i < mActiveCameraUsage.size(); i++) {
483 if (mActiveCameraUsage.valueAt(i).mClientName.equals(clientName)) {
484 alreadyActivePackage = true;
485 break;
486 }
487 }
488 // If not already active, notify window manager about this new package using a
489 // camera
490 if (!alreadyActivePackage) {
491 WindowManagerInternal wmi =
492 LocalServices.getService(WindowManagerInternal.class);
493 wmi.addNonHighRefreshRatePackage(clientName);
494 }
495
496 // Update activity events
Emilian Peev6bf0a552018-02-10 02:15:49 +0000497 CameraUsageEvent newEvent = new CameraUsageEvent(facing, clientName, apiLevel);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700498 CameraUsageEvent oldEvent = mActiveCameraUsage.put(cameraId, newEvent);
499 if (oldEvent != null) {
500 Slog.w(TAG, "Camera " + cameraId + " was already marked as active");
501 oldEvent.markCompleted();
502 mCameraUsageHistory.add(oldEvent);
503 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700504 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700505 case ICameraServiceProxy.CAMERA_STATE_IDLE:
506 case ICameraServiceProxy.CAMERA_STATE_CLOSED:
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700507 CameraUsageEvent doneEvent = mActiveCameraUsage.remove(cameraId);
Eino-Ville Talvalab3805162019-05-13 15:38:11 -0700508 if (doneEvent == null) break;
509
510 doneEvent.markCompleted();
511 mCameraUsageHistory.add(doneEvent);
512 if (mCameraUsageHistory.size() > MAX_USAGE_HISTORY) {
513 dumpUsageEvents();
514 }
515
516 // Check current active camera IDs to see if this package is still talking to
517 // some camera
518 boolean stillActivePackage = false;
519 for (int i = 0; i < mActiveCameraUsage.size(); i++) {
520 if (mActiveCameraUsage.valueAt(i).mClientName.equals(clientName)) {
521 stillActivePackage = true;
522 break;
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700523 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700524 }
Eino-Ville Talvalab3805162019-05-13 15:38:11 -0700525 // If not longer active, notify window manager about this package being done
526 // with camera
527 if (!stillActivePackage) {
528 WindowManagerInternal wmi =
529 LocalServices.getService(WindowManagerInternal.class);
530 wmi.removeNonHighRefreshRatePackage(clientName);
531 }
532
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700533 break;
534 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700535 boolean isEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700536 if ( mNotifyNfc && (wasEmpty != isEmpty) ) {
537 notifyNfcService(isEmpty);
538 }
539 }
540 }
541
542 private void notifyNfcService(boolean enablePolling) {
543
544 IBinder nfcServiceBinder = getBinderService(NFC_SERVICE_BINDER_NAME);
545 if (nfcServiceBinder == null) {
546 Slog.w(TAG, "Could not connect to NFC service to notify it of camera state");
547 return;
548 }
549 INfcAdapter nfcAdapterRaw = INfcAdapter.Stub.asInterface(nfcServiceBinder);
550 int flags = enablePolling ? ENABLE_POLLING_FLAGS : DISABLE_POLLING_FLAGS;
551 if (DEBUG) Slog.v(TAG, "Setting NFC reader mode to flags " + flags);
552 try {
553 nfcAdapterRaw.setReaderMode(nfcInterfaceToken, null, flags, null);
554 } catch (RemoteException e) {
555 Slog.w(TAG, "Could not notify NFC service, remote exception: " + e);
556 }
557 }
558
Ruben Brunk7f75da22015-04-30 17:46:30 -0700559 private static int[] toArray(Collection<Integer> c) {
560 int len = c.size();
561 int[] ret = new int[len];
562 int idx = 0;
563 for (Integer i : c) {
564 ret[idx++] = i;
565 }
566 return ret;
567 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700568
569 private static String cameraStateToString(int newCameraState) {
570 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700571 case ICameraServiceProxy.CAMERA_STATE_OPEN: return "CAMERA_STATE_OPEN";
572 case ICameraServiceProxy.CAMERA_STATE_ACTIVE: return "CAMERA_STATE_ACTIVE";
573 case ICameraServiceProxy.CAMERA_STATE_IDLE: return "CAMERA_STATE_IDLE";
574 case ICameraServiceProxy.CAMERA_STATE_CLOSED: return "CAMERA_STATE_CLOSED";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700575 default: break;
576 }
577 return "CAMERA_STATE_UNKNOWN";
578 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700579
580 private static String cameraFacingToString(int cameraFacing) {
581 switch (cameraFacing) {
582 case ICameraServiceProxy.CAMERA_FACING_BACK: return "CAMERA_FACING_BACK";
583 case ICameraServiceProxy.CAMERA_FACING_FRONT: return "CAMERA_FACING_FRONT";
584 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL: return "CAMERA_FACING_EXTERNAL";
585 default: break;
586 }
587 return "CAMERA_FACING_UNKNOWN";
588 }
589
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700590}