blob: 0ee55ed2e8325dacacd1f9164b34c7a38a3f172e [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 Talvala31ad8a32017-07-10 16:23:50 -070024import android.metrics.LogMaker;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070025import android.nfc.INfcAdapter;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070026import android.os.Binder;
Ruben Brunk28388e82015-06-01 18:41:28 -070027import android.os.Handler;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070028import android.os.IBinder;
Ruben Brunk28388e82015-06-01 18:41:28 -070029import android.os.Message;
30import android.os.Process;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070031import android.os.RemoteException;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070032import android.os.SystemClock;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070033import android.os.SystemProperties;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070034import android.os.UserManager;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070035import android.util.ArrayMap;
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070036import android.util.ArraySet;
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -070037import android.util.Slog;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070038
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070039import com.android.internal.logging.MetricsLogger;
40import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070041import com.android.server.LocalServices;
Ruben Brunk28388e82015-06-01 18:41:28 -070042import com.android.server.ServiceThread;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070043import com.android.server.SystemService;
44
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070045import java.util.ArrayList;
Ruben Brunk7f75da22015-04-30 17:46:30 -070046import java.util.Collection;
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070047import java.util.Collections;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070048import java.util.List;
Ruben Brunk7f75da22015-04-30 17:46:30 -070049import java.util.Set;
50
Ruben Brunk9d21ee52015-03-20 22:18:55 -070051/**
Wei Wanged7f5482017-02-21 11:35:10 -080052 * CameraServiceProxy is the system_server analog to the camera service running in mediaserver.
Ruben Brunk9d21ee52015-03-20 22:18:55 -070053 *
54 * @hide
55 */
Wei Wanged7f5482017-02-21 11:35:10 -080056public class CameraServiceProxy extends SystemService
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070057 implements Handler.Callback, IBinder.DeathRecipient {
Ruben Brunk28388e82015-06-01 18:41:28 -070058 private static final String TAG = "CameraService_proxy";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070059 private static final boolean DEBUG = false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070060
61 /**
62 * This must match the ICameraService.aidl definition
63 */
64 private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
65
Ruben Brunk6664aee2015-05-19 17:20:24 -070066 public static final String CAMERA_SERVICE_PROXY_BINDER_NAME = "media.camera.proxy";
67
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070068 // 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 Brunk28388e82015-06-01 18:41:28 -070072 // 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 Talvala31ad8a32017-07-10 16:23:50 -070077 // Maximum entries to keep in usage history before dumping out
78 private static final int MAX_USAGE_HISTORY = 100;
79
Ruben Brunk7f75da22015-04-30 17:46:30 -070080 private final Context mContext;
Ruben Brunk28388e82015-06-01 18:41:28 -070081 private final ServiceThread mHandlerThread;
82 private final Handler mHandler;
Ruben Brunk7f75da22015-04-30 17:46:30 -070083 private UserManager mUserManager;
Ruben Brunk6664aee2015-05-19 17:20:24 -070084
85 private final Object mLock = new Object();
Ruben Brunk7f75da22015-04-30 17:46:30 -070086 private Set<Integer> mEnabledCameraUsers;
Ruben Brunka59a8702015-06-23 11:52:08 -070087 private int mLastUser;
88
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070089 private ICameraService mCameraServiceRaw;
90
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070091 private final ArrayMap<String, CameraUsageEvent> mActiveCameraUsage = new ArrayMap<>();
92 private final List<CameraUsageEvent> mCameraUsageHistory = new ArrayList<>();
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070093 private final MetricsLogger mLogger = new MetricsLogger();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070094 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 Talvalae91012b2017-07-10 15:27:24 -070099
100 /**
101 * Structure to track camera usage
102 */
103 private static class CameraUsageEvent {
104 public final int mCameraFacing;
105 public final String mClientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000106 public final int mAPILevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700107
108 private boolean mCompleted;
109 private long mDurationOrStartTimeMs; // Either start time, or duration once completed
110
Emilian Peev6bf0a552018-02-10 02:15:49 +0000111 public CameraUsageEvent(int facing, String clientName, int apiLevel) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700112 mCameraFacing = facing;
113 mClientName = clientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000114 mAPILevel = apiLevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700115 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 Talvala2e3215c2015-08-20 15:43:57 -0700139
Ruben Brunka59a8702015-06-23 11:52:08 -0700140 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 Brunk7f75da22015-04-30 17:46:30 -0700164
Ruben Brunk6664aee2015-05-19 17:20:24 -0700165 private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
166 @Override
167 public void pingForUserUpdate() {
Emilian Peev44e19a02018-04-12 14:21:20 +0100168 if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
169 Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
170 " camera service UID!");
171 return;
172 }
Ruben Brunk28388e82015-06-01 18:41:28 -0700173 notifySwitchWithRetries(30);
Ruben Brunk6664aee2015-05-19 17:20:24 -0700174 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700175
176 @Override
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700177 public void notifyCameraState(String cameraId, int newCameraState, int facing,
Emilian Peev6bf0a552018-02-10 02:15:49 +0000178 String clientName, int apiLevel) {
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 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700184 String state = cameraStateToString(newCameraState);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700185 String facingStr = cameraFacingToString(facing);
186 if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
Emilian Peev6bf0a552018-02-10 02:15:49 +0000187 state + " for client " + clientName + " API Level " + apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700188
Emilian Peev6bf0a552018-02-10 02:15:49 +0000189 updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700190 }
Ruben Brunk6664aee2015-05-19 17:20:24 -0700191 };
192
Wei Wanged7f5482017-02-21 11:35:10 -0800193 public CameraServiceProxy(Context context) {
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700194 super(context);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700195 mContext = context;
Ruben Brunk28388e82015-06-01 18:41:28 -0700196 mHandlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_DISPLAY, /*allowTo*/false);
197 mHandlerThread.start();
198 mHandler = new Handler(mHandlerThread.getLooper(), this);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700199
200 mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0;
201 if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled"));
Ruben Brunk28388e82015-06-01 18:41:28 -0700202 }
203
204 @Override
205 public boolean handleMessage(Message msg) {
206 switch(msg.what) {
207 case MSG_SWITCH_USER: {
208 notifySwitchWithRetries(msg.arg1);
209 } break;
210 default: {
Wei Wanged7f5482017-02-21 11:35:10 -0800211 Slog.e(TAG, "CameraServiceProxy error, invalid message: " + msg.what);
Ruben Brunk28388e82015-06-01 18:41:28 -0700212 } break;
213 }
214 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700215 }
216
217 @Override
Ruben Brunk7f75da22015-04-30 17:46:30 -0700218 public void onStart() {
219 mUserManager = UserManager.get(mContext);
220 if (mUserManager == null) {
221 // Should never see this unless someone messes up the SystemServer service boot order.
Wei Wanged7f5482017-02-21 11:35:10 -0800222 throw new IllegalStateException("UserManagerService must start before" +
223 " CameraServiceProxy!");
Ruben Brunk7f75da22015-04-30 17:46:30 -0700224 }
Ruben Brunka59a8702015-06-23 11:52:08 -0700225
226 IntentFilter filter = new IntentFilter();
227 filter.addAction(Intent.ACTION_USER_ADDED);
228 filter.addAction(Intent.ACTION_USER_REMOVED);
229 filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
230 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
231 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
232 mContext.registerReceiver(mIntentReceiver, filter);
233
Ruben Brunk6664aee2015-05-19 17:20:24 -0700234 publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700235 publishLocalService(CameraServiceProxy.class, this);
236
237 CameraStatsJobService.schedule(mContext);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700238 }
239
240 @Override
241 public void onStartUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700242 synchronized(mLock) {
243 if (mEnabledCameraUsers == null) {
244 // Initialize mediaserver, or update mediaserver if we are recovering from a crash.
245 switchUserLocked(userHandle);
246 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700247 }
248 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700249
250 @Override
251 public void onSwitchUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700252 synchronized(mLock) {
253 switchUserLocked(userHandle);
254 }
255 }
256
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700257 /**
258 * Handle the death of the native camera service
259 */
260 @Override
261 public void binderDied() {
262 if (DEBUG) Slog.w(TAG, "Native camera service has died");
263 synchronized(mLock) {
264 mCameraServiceRaw = null;
265
266 // All cameras reset to idle on camera service death
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700267 boolean wasEmpty = mActiveCameraUsage.isEmpty();
268 mActiveCameraUsage.clear();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700269
270 if ( mNotifyNfc && !wasEmpty ) {
271 notifyNfcService(/*enablePolling*/ true);
272 }
273 }
274 }
275
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700276 /**
277 * Dump camera usage events to log.
278 * Package-private
279 */
280 void dumpUsageEvents() {
281 synchronized(mLock) {
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700282 // Randomize order of events so that it's not meaningful
283 Collections.shuffle(mCameraUsageHistory);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700284 for (CameraUsageEvent e : mCameraUsageHistory) {
285 if (DEBUG) {
286 Slog.v(TAG, "Camera: " + e.mClientName + " used a camera facing " +
287 cameraFacingToString(e.mCameraFacing) + " for " +
288 e.getDuration() + " ms");
289 }
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700290 int subtype = 0;
291 switch(e.mCameraFacing) {
292 case ICameraServiceProxy.CAMERA_FACING_BACK:
293 subtype = MetricsEvent.CAMERA_BACK_USED;
294 break;
295 case ICameraServiceProxy.CAMERA_FACING_FRONT:
296 subtype = MetricsEvent.CAMERA_FRONT_USED;
297 break;
298 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL:
299 subtype = MetricsEvent.CAMERA_EXTERNAL_USED;
300 break;
301 default:
302 continue;
303 }
304 LogMaker l = new LogMaker(MetricsEvent.ACTION_CAMERA_EVENT)
305 .setType(MetricsEvent.TYPE_ACTION)
306 .setSubtype(subtype)
307 .setLatency(e.getDuration())
Emilian Peev6bf0a552018-02-10 02:15:49 +0000308 .addTaggedData(MetricsEvent.FIELD_CAMERA_API_LEVEL, e.mAPILevel)
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700309 .setPackageName(e.mClientName);
310 mLogger.write(l);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700311 }
312 mCameraUsageHistory.clear();
313 }
Emilian Peev44e19a02018-04-12 14:21:20 +0100314 final long ident = Binder.clearCallingIdentity();
315 try {
316 CameraStatsJobService.schedule(mContext);
317 } finally {
318 Binder.restoreCallingIdentity(ident);
319 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700320 }
321
Ruben Brunk6664aee2015-05-19 17:20:24 -0700322 private void switchUserLocked(int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700323 Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle);
Ruben Brunka59a8702015-06-23 11:52:08 -0700324 mLastUser = userHandle;
Ruben Brunk7f75da22015-04-30 17:46:30 -0700325 if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) {
326 // Some user handles have been added or removed, update mediaserver.
327 mEnabledCameraUsers = currentUserHandles;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800328 notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, currentUserHandles);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700329 }
330 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700331
Ruben Brunk7f75da22015-04-30 17:46:30 -0700332 private Set<Integer> getEnabledUserHandles(int currentUserHandle) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700333 int[] userProfiles = mUserManager.getEnabledProfileIds(currentUserHandle);
334 Set<Integer> handles = new ArraySet<>(userProfiles.length);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700335
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700336 for (int id : userProfiles) {
337 handles.add(id);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700338 }
339
340 return handles;
341 }
342
Ruben Brunk28388e82015-06-01 18:41:28 -0700343 private void notifySwitchWithRetries(int retries) {
344 synchronized(mLock) {
345 if (mEnabledCameraUsers == null) {
346 return;
347 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800348 if (notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, mEnabledCameraUsers)) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700349 retries = 0;
350 }
351 }
352 if (retries <= 0) {
353 return;
354 }
355 Slog.i(TAG, "Could not notify camera service of user switch, retrying...");
356 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SWITCH_USER, retries - 1, 0, null),
357 RETRY_DELAY_TIME);
358 }
359
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700360 private boolean notifyMediaserverLocked(int eventType, Set<Integer> updatedUserHandles) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700361 // Forward the user switch event to the native camera service running in the mediaserver
362 // process.
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700363 if (mCameraServiceRaw == null) {
364 IBinder cameraServiceBinder = getBinderService(CAMERA_SERVICE_BINDER_NAME);
365 if (cameraServiceBinder == null) {
366 Slog.w(TAG, "Could not notify mediaserver, camera service not available.");
367 return false; // Camera service not active, cannot evict user clients.
368 }
369 try {
370 cameraServiceBinder.linkToDeath(this, /*flags*/ 0);
371 } catch (RemoteException e) {
372 Slog.w(TAG, "Could not link to death of native camera service");
373 return false;
374 }
375
376 mCameraServiceRaw = ICameraService.Stub.asInterface(cameraServiceBinder);
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700377 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700378
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700379 try {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700380 mCameraServiceRaw.notifySystemEvent(eventType, toArray(updatedUserHandles));
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700381 } catch (RemoteException e) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700382 Slog.w(TAG, "Could not notify mediaserver, remote exception: " + e);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700383 // Not much we can do if camera service is dead.
Ruben Brunk28388e82015-06-01 18:41:28 -0700384 return false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700385 }
Ruben Brunk28388e82015-06-01 18:41:28 -0700386 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700387 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700388
Emilian Peev6bf0a552018-02-10 02:15:49 +0000389 private void updateActivityCount(String cameraId, int newCameraState, int facing,
390 String clientName, int apiLevel) {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700391 synchronized(mLock) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700392 // Update active camera list and notify NFC if necessary
393 boolean wasEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700394 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700395 case ICameraServiceProxy.CAMERA_STATE_OPEN:
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700396 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700397 case ICameraServiceProxy.CAMERA_STATE_ACTIVE:
Emilian Peev6bf0a552018-02-10 02:15:49 +0000398 CameraUsageEvent newEvent = new CameraUsageEvent(facing, clientName, apiLevel);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700399 CameraUsageEvent oldEvent = mActiveCameraUsage.put(cameraId, newEvent);
400 if (oldEvent != null) {
401 Slog.w(TAG, "Camera " + cameraId + " was already marked as active");
402 oldEvent.markCompleted();
403 mCameraUsageHistory.add(oldEvent);
404 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700405 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700406 case ICameraServiceProxy.CAMERA_STATE_IDLE:
407 case ICameraServiceProxy.CAMERA_STATE_CLOSED:
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700408 CameraUsageEvent doneEvent = mActiveCameraUsage.remove(cameraId);
409 if (doneEvent != null) {
410 doneEvent.markCompleted();
411 mCameraUsageHistory.add(doneEvent);
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700412 if (mCameraUsageHistory.size() > MAX_USAGE_HISTORY) {
413 dumpUsageEvents();
414 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700415 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700416 break;
417 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700418 boolean isEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700419 if ( mNotifyNfc && (wasEmpty != isEmpty) ) {
420 notifyNfcService(isEmpty);
421 }
422 }
423 }
424
425 private void notifyNfcService(boolean enablePolling) {
426
427 IBinder nfcServiceBinder = getBinderService(NFC_SERVICE_BINDER_NAME);
428 if (nfcServiceBinder == null) {
429 Slog.w(TAG, "Could not connect to NFC service to notify it of camera state");
430 return;
431 }
432 INfcAdapter nfcAdapterRaw = INfcAdapter.Stub.asInterface(nfcServiceBinder);
433 int flags = enablePolling ? ENABLE_POLLING_FLAGS : DISABLE_POLLING_FLAGS;
434 if (DEBUG) Slog.v(TAG, "Setting NFC reader mode to flags " + flags);
435 try {
436 nfcAdapterRaw.setReaderMode(nfcInterfaceToken, null, flags, null);
437 } catch (RemoteException e) {
438 Slog.w(TAG, "Could not notify NFC service, remote exception: " + e);
439 }
440 }
441
Ruben Brunk7f75da22015-04-30 17:46:30 -0700442 private static int[] toArray(Collection<Integer> c) {
443 int len = c.size();
444 int[] ret = new int[len];
445 int idx = 0;
446 for (Integer i : c) {
447 ret[idx++] = i;
448 }
449 return ret;
450 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700451
452 private static String cameraStateToString(int newCameraState) {
453 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700454 case ICameraServiceProxy.CAMERA_STATE_OPEN: return "CAMERA_STATE_OPEN";
455 case ICameraServiceProxy.CAMERA_STATE_ACTIVE: return "CAMERA_STATE_ACTIVE";
456 case ICameraServiceProxy.CAMERA_STATE_IDLE: return "CAMERA_STATE_IDLE";
457 case ICameraServiceProxy.CAMERA_STATE_CLOSED: return "CAMERA_STATE_CLOSED";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700458 default: break;
459 }
460 return "CAMERA_STATE_UNKNOWN";
461 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700462
463 private static String cameraFacingToString(int cameraFacing) {
464 switch (cameraFacing) {
465 case ICameraServiceProxy.CAMERA_FACING_BACK: return "CAMERA_FACING_BACK";
466 case ICameraServiceProxy.CAMERA_FACING_FRONT: return "CAMERA_FACING_FRONT";
467 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL: return "CAMERA_FACING_EXTERNAL";
468 default: break;
469 }
470 return "CAMERA_FACING_UNKNOWN";
471 }
472
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700473}