blob: 527539d8ce0d7927e651ffb05f0e6d3618d333f3 [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;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070042import com.android.server.LocalServices;
Ruben Brunk28388e82015-06-01 18:41:28 -070043import com.android.server.ServiceThread;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070044import com.android.server.SystemService;
45
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070046import java.util.ArrayList;
Ruben Brunk7f75da22015-04-30 17:46:30 -070047import java.util.Collection;
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070048import java.util.Collections;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070049import java.util.List;
Ruben Brunk7f75da22015-04-30 17:46:30 -070050import java.util.Set;
51
Ruben Brunk9d21ee52015-03-20 22:18:55 -070052/**
Wei Wanged7f5482017-02-21 11:35:10 -080053 * CameraServiceProxy is the system_server analog to the camera service running in mediaserver.
Ruben Brunk9d21ee52015-03-20 22:18:55 -070054 *
55 * @hide
56 */
Wei Wanged7f5482017-02-21 11:35:10 -080057public class CameraServiceProxy extends SystemService
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070058 implements Handler.Callback, IBinder.DeathRecipient {
Ruben Brunk28388e82015-06-01 18:41:28 -070059 private static final String TAG = "CameraService_proxy";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070060 private static final boolean DEBUG = false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -070061
62 /**
63 * This must match the ICameraService.aidl definition
64 */
65 private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
66
Ruben Brunk6664aee2015-05-19 17:20:24 -070067 public static final String CAMERA_SERVICE_PROXY_BINDER_NAME = "media.camera.proxy";
68
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070069 // 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 Brunk28388e82015-06-01 18:41:28 -070073 // 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 Talvala31ad8a32017-07-10 16:23:50 -070078 // Maximum entries to keep in usage history before dumping out
79 private static final int MAX_USAGE_HISTORY = 100;
80
Ruben Brunk7f75da22015-04-30 17:46:30 -070081 private final Context mContext;
Ruben Brunk28388e82015-06-01 18:41:28 -070082 private final ServiceThread mHandlerThread;
83 private final Handler mHandler;
Ruben Brunk7f75da22015-04-30 17:46:30 -070084 private UserManager mUserManager;
Ruben Brunk6664aee2015-05-19 17:20:24 -070085
86 private final Object mLock = new Object();
Ruben Brunk7f75da22015-04-30 17:46:30 -070087 private Set<Integer> mEnabledCameraUsers;
Ruben Brunka59a8702015-06-23 11:52:08 -070088 private int mLastUser;
89
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070090 private ICameraService mCameraServiceRaw;
91
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -070092 private final ArrayMap<String, CameraUsageEvent> mActiveCameraUsage = new ArrayMap<>();
93 private final List<CameraUsageEvent> mCameraUsageHistory = new ArrayList<>();
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -070094 private final MetricsLogger mLogger = new MetricsLogger();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -070095 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 Talvalae91012b2017-07-10 15:27:24 -0700100
101 /**
102 * Structure to track camera usage
103 */
104 private static class CameraUsageEvent {
105 public final int mCameraFacing;
106 public final String mClientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000107 public final int mAPILevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700108
109 private boolean mCompleted;
110 private long mDurationOrStartTimeMs; // Either start time, or duration once completed
111
Emilian Peev6bf0a552018-02-10 02:15:49 +0000112 public CameraUsageEvent(int facing, String clientName, int apiLevel) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700113 mCameraFacing = facing;
114 mClientName = clientName;
Emilian Peev6bf0a552018-02-10 02:15:49 +0000115 mAPILevel = apiLevel;
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700116 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 Talvala2e3215c2015-08-20 15:43:57 -0700140
Ruben Brunka59a8702015-06-23 11:52:08 -0700141 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 Brunk7f75da22015-04-30 17:46:30 -0700165
Ruben Brunk6664aee2015-05-19 17:20:24 -0700166 private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
167 @Override
168 public void pingForUserUpdate() {
Emilian Peev44e19a02018-04-12 14:21:20 +0100169 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 Brunk28388e82015-06-01 18:41:28 -0700174 notifySwitchWithRetries(30);
Ruben Brunk6664aee2015-05-19 17:20:24 -0700175 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700176
177 @Override
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700178 public void notifyCameraState(String cameraId, int newCameraState, int facing,
Emilian Peev6bf0a552018-02-10 02:15:49 +0000179 String clientName, int apiLevel) {
Emilian Peev44e19a02018-04-12 14:21:20 +0100180 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 Talvala2e3215c2015-08-20 15:43:57 -0700185 String state = cameraStateToString(newCameraState);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700186 String facingStr = cameraFacingToString(facing);
187 if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
Emilian Peev6bf0a552018-02-10 02:15:49 +0000188 state + " for client " + clientName + " API Level " + apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700189
Emilian Peev6bf0a552018-02-10 02:15:49 +0000190 updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700191 }
Ruben Brunk6664aee2015-05-19 17:20:24 -0700192 };
193
Wei Wanged7f5482017-02-21 11:35:10 -0800194 public CameraServiceProxy(Context context) {
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700195 super(context);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700196 mContext = context;
Ruben Brunk28388e82015-06-01 18:41:28 -0700197 mHandlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_DISPLAY, /*allowTo*/false);
198 mHandlerThread.start();
199 mHandler = new Handler(mHandlerThread.getLooper(), this);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700200
201 mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0;
202 if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled"));
Ruben Brunk28388e82015-06-01 18:41:28 -0700203 }
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 Wanged7f5482017-02-21 11:35:10 -0800212 Slog.e(TAG, "CameraServiceProxy error, invalid message: " + msg.what);
Ruben Brunk28388e82015-06-01 18:41:28 -0700213 } break;
214 }
215 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700216 }
217
218 @Override
Ruben Brunk7f75da22015-04-30 17:46:30 -0700219 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 Wanged7f5482017-02-21 11:35:10 -0800223 throw new IllegalStateException("UserManagerService must start before" +
224 " CameraServiceProxy!");
Ruben Brunk7f75da22015-04-30 17:46:30 -0700225 }
Ruben Brunka59a8702015-06-23 11:52:08 -0700226
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 Brunk6664aee2015-05-19 17:20:24 -0700235 publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700236 publishLocalService(CameraServiceProxy.class, this);
237
238 CameraStatsJobService.schedule(mContext);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700239 }
240
241 @Override
242 public void onStartUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700243 synchronized(mLock) {
244 if (mEnabledCameraUsers == null) {
245 // Initialize mediaserver, or update mediaserver if we are recovering from a crash.
246 switchUserLocked(userHandle);
247 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700248 }
249 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700250
251 @Override
252 public void onSwitchUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700253 synchronized(mLock) {
254 switchUserLocked(userHandle);
255 }
256 }
257
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700258 /**
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 Talvalae91012b2017-07-10 15:27:24 -0700268 boolean wasEmpty = mActiveCameraUsage.isEmpty();
269 mActiveCameraUsage.clear();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700270
271 if ( mNotifyNfc && !wasEmpty ) {
272 notifyNfcService(/*enablePolling*/ true);
273 }
274 }
275 }
276
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700277 /**
278 * Dump camera usage events to log.
279 * Package-private
280 */
281 void dumpUsageEvents() {
282 synchronized(mLock) {
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700283 // Randomize order of events so that it's not meaningful
284 Collections.shuffle(mCameraUsageHistory);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700285 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 Talvala31ad8a32017-07-10 16:23:50 -0700291 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 Peev6bf0a552018-02-10 02:15:49 +0000309 .addTaggedData(MetricsEvent.FIELD_CAMERA_API_LEVEL, e.mAPILevel)
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700310 .setPackageName(e.mClientName);
311 mLogger.write(l);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700312 }
313 mCameraUsageHistory.clear();
314 }
Emilian Peev44e19a02018-04-12 14:21:20 +0100315 final long ident = Binder.clearCallingIdentity();
316 try {
317 CameraStatsJobService.schedule(mContext);
318 } finally {
319 Binder.restoreCallingIdentity(ident);
320 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700321 }
322
Ruben Brunk6664aee2015-05-19 17:20:24 -0700323 private void switchUserLocked(int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700324 Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle);
Ruben Brunka59a8702015-06-23 11:52:08 -0700325 mLastUser = userHandle;
Ruben Brunk7f75da22015-04-30 17:46:30 -0700326 if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) {
327 // Some user handles have been added or removed, update mediaserver.
328 mEnabledCameraUsers = currentUserHandles;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800329 notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, currentUserHandles);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700330 }
331 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700332
Ruben Brunk7f75da22015-04-30 17:46:30 -0700333 private Set<Integer> getEnabledUserHandles(int currentUserHandle) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700334 int[] userProfiles = mUserManager.getEnabledProfileIds(currentUserHandle);
335 Set<Integer> handles = new ArraySet<>(userProfiles.length);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700336
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700337 for (int id : userProfiles) {
338 handles.add(id);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700339 }
340
341 return handles;
342 }
343
Ruben Brunk28388e82015-06-01 18:41:28 -0700344 private void notifySwitchWithRetries(int retries) {
345 synchronized(mLock) {
346 if (mEnabledCameraUsers == null) {
347 return;
348 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800349 if (notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, mEnabledCameraUsers)) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700350 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 Talvala2e3215c2015-08-20 15:43:57 -0700361 private boolean notifyMediaserverLocked(int eventType, Set<Integer> updatedUserHandles) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700362 // Forward the user switch event to the native camera service running in the mediaserver
363 // process.
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700364 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 Brunk9d21ee52015-03-20 22:18:55 -0700378 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700379
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700380 try {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700381 mCameraServiceRaw.notifySystemEvent(eventType, toArray(updatedUserHandles));
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700382 } catch (RemoteException e) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700383 Slog.w(TAG, "Could not notify mediaserver, remote exception: " + e);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700384 // Not much we can do if camera service is dead.
Ruben Brunk28388e82015-06-01 18:41:28 -0700385 return false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700386 }
Ruben Brunk28388e82015-06-01 18:41:28 -0700387 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700388 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700389
Emilian Peev6bf0a552018-02-10 02:15:49 +0000390 private void updateActivityCount(String cameraId, int newCameraState, int facing,
391 String clientName, int apiLevel) {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700392 synchronized(mLock) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700393 // Update active camera list and notify NFC if necessary
394 boolean wasEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700395 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700396 case ICameraServiceProxy.CAMERA_STATE_OPEN:
Eino-Ville Talvala06075ee2018-10-19 13:09:39 -0700397 // 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 Talvala2e3215c2015-08-20 15:43:57 -0700410 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700411 case ICameraServiceProxy.CAMERA_STATE_ACTIVE:
Emilian Peev6bf0a552018-02-10 02:15:49 +0000412 CameraUsageEvent newEvent = new CameraUsageEvent(facing, clientName, apiLevel);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700413 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 Talvala2e3215c2015-08-20 15:43:57 -0700419 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700420 case ICameraServiceProxy.CAMERA_STATE_IDLE:
421 case ICameraServiceProxy.CAMERA_STATE_CLOSED:
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700422 CameraUsageEvent doneEvent = mActiveCameraUsage.remove(cameraId);
423 if (doneEvent != null) {
424 doneEvent.markCompleted();
425 mCameraUsageHistory.add(doneEvent);
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700426 if (mCameraUsageHistory.size() > MAX_USAGE_HISTORY) {
427 dumpUsageEvents();
428 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700429 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700430 break;
431 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700432 boolean isEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700433 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 Brunk7f75da22015-04-30 17:46:30 -0700456 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 Talvala2e3215c2015-08-20 15:43:57 -0700465
466 private static String cameraStateToString(int newCameraState) {
467 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700468 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 Talvala2e3215c2015-08-20 15:43:57 -0700472 default: break;
473 }
474 return "CAMERA_STATE_UNKNOWN";
475 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700476
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 Brunk9d21ee52015-03-20 22:18:55 -0700487}