blob: 3133a51a5fb56124150c282014f7c73bde1326fb [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;
106
107 private boolean mCompleted;
108 private long mDurationOrStartTimeMs; // Either start time, or duration once completed
109
110 public CameraUsageEvent(int facing, String clientName) {
111 mCameraFacing = facing;
112 mClientName = clientName;
113 mDurationOrStartTimeMs = SystemClock.elapsedRealtime();
114 mCompleted = false;
115 }
116
117 public void markCompleted() {
118 if (mCompleted) {
119 return;
120 }
121 mCompleted = true;
122 mDurationOrStartTimeMs = SystemClock.elapsedRealtime() - mDurationOrStartTimeMs;
123 if (CameraServiceProxy.DEBUG) {
124 Slog.v(TAG, "A camera facing " + cameraFacingToString(mCameraFacing) +
125 " was in use by " + mClientName + " for " +
126 mDurationOrStartTimeMs + " ms");
127 }
128 }
129
130 /**
131 * Return duration of camera usage event, or 0 if the event is not done
132 */
133 public long getDuration() {
134 return mCompleted ? mDurationOrStartTimeMs : 0;
135 }
136 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700137
Ruben Brunka59a8702015-06-23 11:52:08 -0700138 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
139 @Override
140 public void onReceive(Context context, Intent intent) {
141 final String action = intent.getAction();
142 if (action == null) return;
143
144 switch (action) {
145 case Intent.ACTION_USER_ADDED:
146 case Intent.ACTION_USER_REMOVED:
147 case Intent.ACTION_USER_INFO_CHANGED:
148 case Intent.ACTION_MANAGED_PROFILE_ADDED:
149 case Intent.ACTION_MANAGED_PROFILE_REMOVED:
150 synchronized(mLock) {
151 // Return immediately if we haven't seen any users start yet
152 if (mEnabledCameraUsers == null) return;
153 switchUserLocked(mLastUser);
154 }
155 break;
156 default:
157 break; // do nothing
158 }
159
160 }
161 };
Ruben Brunk7f75da22015-04-30 17:46:30 -0700162
Ruben Brunk6664aee2015-05-19 17:20:24 -0700163 private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
164 @Override
165 public void pingForUserUpdate() {
Ruben Brunk28388e82015-06-01 18:41:28 -0700166 notifySwitchWithRetries(30);
Ruben Brunk6664aee2015-05-19 17:20:24 -0700167 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700168
169 @Override
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700170 public void notifyCameraState(String cameraId, int newCameraState, int facing,
171 String clientName) {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700172 String state = cameraStateToString(newCameraState);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700173 String facingStr = cameraFacingToString(facing);
174 if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700175 state + " for client " + clientName);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700176
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700177 updateActivityCount(cameraId, newCameraState, facing, clientName);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700178 }
Ruben Brunk6664aee2015-05-19 17:20:24 -0700179 };
180
Wei Wanged7f5482017-02-21 11:35:10 -0800181 public CameraServiceProxy(Context context) {
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700182 super(context);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700183 mContext = context;
Ruben Brunk28388e82015-06-01 18:41:28 -0700184 mHandlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_DISPLAY, /*allowTo*/false);
185 mHandlerThread.start();
186 mHandler = new Handler(mHandlerThread.getLooper(), this);
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700187
188 mNotifyNfc = SystemProperties.getInt(NFC_NOTIFICATION_PROP, 0) > 0;
189 if (DEBUG) Slog.v(TAG, "Notify NFC behavior is " + (mNotifyNfc ? "active" : "disabled"));
Ruben Brunk28388e82015-06-01 18:41:28 -0700190 }
191
192 @Override
193 public boolean handleMessage(Message msg) {
194 switch(msg.what) {
195 case MSG_SWITCH_USER: {
196 notifySwitchWithRetries(msg.arg1);
197 } break;
198 default: {
Wei Wanged7f5482017-02-21 11:35:10 -0800199 Slog.e(TAG, "CameraServiceProxy error, invalid message: " + msg.what);
Ruben Brunk28388e82015-06-01 18:41:28 -0700200 } break;
201 }
202 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700203 }
204
205 @Override
Ruben Brunk7f75da22015-04-30 17:46:30 -0700206 public void onStart() {
207 mUserManager = UserManager.get(mContext);
208 if (mUserManager == null) {
209 // Should never see this unless someone messes up the SystemServer service boot order.
Wei Wanged7f5482017-02-21 11:35:10 -0800210 throw new IllegalStateException("UserManagerService must start before" +
211 " CameraServiceProxy!");
Ruben Brunk7f75da22015-04-30 17:46:30 -0700212 }
Ruben Brunka59a8702015-06-23 11:52:08 -0700213
214 IntentFilter filter = new IntentFilter();
215 filter.addAction(Intent.ACTION_USER_ADDED);
216 filter.addAction(Intent.ACTION_USER_REMOVED);
217 filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
218 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
219 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
220 mContext.registerReceiver(mIntentReceiver, filter);
221
Ruben Brunk6664aee2015-05-19 17:20:24 -0700222 publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700223 publishLocalService(CameraServiceProxy.class, this);
224
225 CameraStatsJobService.schedule(mContext);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700226 }
227
228 @Override
229 public void onStartUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700230 synchronized(mLock) {
231 if (mEnabledCameraUsers == null) {
232 // Initialize mediaserver, or update mediaserver if we are recovering from a crash.
233 switchUserLocked(userHandle);
234 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700235 }
236 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700237
238 @Override
239 public void onSwitchUser(int userHandle) {
Ruben Brunk6664aee2015-05-19 17:20:24 -0700240 synchronized(mLock) {
241 switchUserLocked(userHandle);
242 }
243 }
244
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700245 /**
246 * Handle the death of the native camera service
247 */
248 @Override
249 public void binderDied() {
250 if (DEBUG) Slog.w(TAG, "Native camera service has died");
251 synchronized(mLock) {
252 mCameraServiceRaw = null;
253
254 // All cameras reset to idle on camera service death
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700255 boolean wasEmpty = mActiveCameraUsage.isEmpty();
256 mActiveCameraUsage.clear();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700257
258 if ( mNotifyNfc && !wasEmpty ) {
259 notifyNfcService(/*enablePolling*/ true);
260 }
261 }
262 }
263
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700264 /**
265 * Dump camera usage events to log.
266 * Package-private
267 */
268 void dumpUsageEvents() {
269 synchronized(mLock) {
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700270 // Randomize order of events so that it's not meaningful
271 Collections.shuffle(mCameraUsageHistory);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700272 for (CameraUsageEvent e : mCameraUsageHistory) {
273 if (DEBUG) {
274 Slog.v(TAG, "Camera: " + e.mClientName + " used a camera facing " +
275 cameraFacingToString(e.mCameraFacing) + " for " +
276 e.getDuration() + " ms");
277 }
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700278 int subtype = 0;
279 switch(e.mCameraFacing) {
280 case ICameraServiceProxy.CAMERA_FACING_BACK:
281 subtype = MetricsEvent.CAMERA_BACK_USED;
282 break;
283 case ICameraServiceProxy.CAMERA_FACING_FRONT:
284 subtype = MetricsEvent.CAMERA_FRONT_USED;
285 break;
286 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL:
287 subtype = MetricsEvent.CAMERA_EXTERNAL_USED;
288 break;
289 default:
290 continue;
291 }
292 LogMaker l = new LogMaker(MetricsEvent.ACTION_CAMERA_EVENT)
293 .setType(MetricsEvent.TYPE_ACTION)
294 .setSubtype(subtype)
295 .setLatency(e.getDuration())
296 .setPackageName(e.mClientName);
297 mLogger.write(l);
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700298 }
299 mCameraUsageHistory.clear();
300 }
301 CameraStatsJobService.schedule(mContext);
302 }
303
Ruben Brunk6664aee2015-05-19 17:20:24 -0700304 private void switchUserLocked(int userHandle) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700305 Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle);
Ruben Brunka59a8702015-06-23 11:52:08 -0700306 mLastUser = userHandle;
Ruben Brunk7f75da22015-04-30 17:46:30 -0700307 if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) {
308 // Some user handles have been added or removed, update mediaserver.
309 mEnabledCameraUsers = currentUserHandles;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800310 notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, currentUserHandles);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700311 }
312 }
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700313
Ruben Brunk7f75da22015-04-30 17:46:30 -0700314 private Set<Integer> getEnabledUserHandles(int currentUserHandle) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700315 int[] userProfiles = mUserManager.getEnabledProfileIds(currentUserHandle);
316 Set<Integer> handles = new ArraySet<>(userProfiles.length);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700317
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -0700318 for (int id : userProfiles) {
319 handles.add(id);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700320 }
321
322 return handles;
323 }
324
Ruben Brunk28388e82015-06-01 18:41:28 -0700325 private void notifySwitchWithRetries(int retries) {
326 synchronized(mLock) {
327 if (mEnabledCameraUsers == null) {
328 return;
329 }
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800330 if (notifyMediaserverLocked(ICameraService.EVENT_USER_SWITCHED, mEnabledCameraUsers)) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700331 retries = 0;
332 }
333 }
334 if (retries <= 0) {
335 return;
336 }
337 Slog.i(TAG, "Could not notify camera service of user switch, retrying...");
338 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SWITCH_USER, retries - 1, 0, null),
339 RETRY_DELAY_TIME);
340 }
341
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700342 private boolean notifyMediaserverLocked(int eventType, Set<Integer> updatedUserHandles) {
Ruben Brunk7f75da22015-04-30 17:46:30 -0700343 // Forward the user switch event to the native camera service running in the mediaserver
344 // process.
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700345 if (mCameraServiceRaw == null) {
346 IBinder cameraServiceBinder = getBinderService(CAMERA_SERVICE_BINDER_NAME);
347 if (cameraServiceBinder == null) {
348 Slog.w(TAG, "Could not notify mediaserver, camera service not available.");
349 return false; // Camera service not active, cannot evict user clients.
350 }
351 try {
352 cameraServiceBinder.linkToDeath(this, /*flags*/ 0);
353 } catch (RemoteException e) {
354 Slog.w(TAG, "Could not link to death of native camera service");
355 return false;
356 }
357
358 mCameraServiceRaw = ICameraService.Stub.asInterface(cameraServiceBinder);
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700359 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700360
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700361 try {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700362 mCameraServiceRaw.notifySystemEvent(eventType, toArray(updatedUserHandles));
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700363 } catch (RemoteException e) {
Ruben Brunk28388e82015-06-01 18:41:28 -0700364 Slog.w(TAG, "Could not notify mediaserver, remote exception: " + e);
Ruben Brunk7f75da22015-04-30 17:46:30 -0700365 // Not much we can do if camera service is dead.
Ruben Brunk28388e82015-06-01 18:41:28 -0700366 return false;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700367 }
Ruben Brunk28388e82015-06-01 18:41:28 -0700368 return true;
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700369 }
Ruben Brunk7f75da22015-04-30 17:46:30 -0700370
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700371 private void updateActivityCount(String cameraId, int newCameraState, int facing, String clientName) {
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700372 synchronized(mLock) {
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700373 // Update active camera list and notify NFC if necessary
374 boolean wasEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700375 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700376 case ICameraServiceProxy.CAMERA_STATE_OPEN:
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700377 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700378 case ICameraServiceProxy.CAMERA_STATE_ACTIVE:
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700379 CameraUsageEvent newEvent = new CameraUsageEvent(facing, clientName);
380 CameraUsageEvent oldEvent = mActiveCameraUsage.put(cameraId, newEvent);
381 if (oldEvent != null) {
382 Slog.w(TAG, "Camera " + cameraId + " was already marked as active");
383 oldEvent.markCompleted();
384 mCameraUsageHistory.add(oldEvent);
385 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700386 break;
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700387 case ICameraServiceProxy.CAMERA_STATE_IDLE:
388 case ICameraServiceProxy.CAMERA_STATE_CLOSED:
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700389 CameraUsageEvent doneEvent = mActiveCameraUsage.remove(cameraId);
390 if (doneEvent != null) {
391 doneEvent.markCompleted();
392 mCameraUsageHistory.add(doneEvent);
Eino-Ville Talvala31ad8a32017-07-10 16:23:50 -0700393 if (mCameraUsageHistory.size() > MAX_USAGE_HISTORY) {
394 dumpUsageEvents();
395 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700396 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700397 break;
398 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700399 boolean isEmpty = mActiveCameraUsage.isEmpty();
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700400 if ( mNotifyNfc && (wasEmpty != isEmpty) ) {
401 notifyNfcService(isEmpty);
402 }
403 }
404 }
405
406 private void notifyNfcService(boolean enablePolling) {
407
408 IBinder nfcServiceBinder = getBinderService(NFC_SERVICE_BINDER_NAME);
409 if (nfcServiceBinder == null) {
410 Slog.w(TAG, "Could not connect to NFC service to notify it of camera state");
411 return;
412 }
413 INfcAdapter nfcAdapterRaw = INfcAdapter.Stub.asInterface(nfcServiceBinder);
414 int flags = enablePolling ? ENABLE_POLLING_FLAGS : DISABLE_POLLING_FLAGS;
415 if (DEBUG) Slog.v(TAG, "Setting NFC reader mode to flags " + flags);
416 try {
417 nfcAdapterRaw.setReaderMode(nfcInterfaceToken, null, flags, null);
418 } catch (RemoteException e) {
419 Slog.w(TAG, "Could not notify NFC service, remote exception: " + e);
420 }
421 }
422
Ruben Brunk7f75da22015-04-30 17:46:30 -0700423 private static int[] toArray(Collection<Integer> c) {
424 int len = c.size();
425 int[] ret = new int[len];
426 int idx = 0;
427 for (Integer i : c) {
428 ret[idx++] = i;
429 }
430 return ret;
431 }
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700432
433 private static String cameraStateToString(int newCameraState) {
434 switch (newCameraState) {
Eino-Ville Talvalafb9b64c2017-06-28 12:44:03 -0700435 case ICameraServiceProxy.CAMERA_STATE_OPEN: return "CAMERA_STATE_OPEN";
436 case ICameraServiceProxy.CAMERA_STATE_ACTIVE: return "CAMERA_STATE_ACTIVE";
437 case ICameraServiceProxy.CAMERA_STATE_IDLE: return "CAMERA_STATE_IDLE";
438 case ICameraServiceProxy.CAMERA_STATE_CLOSED: return "CAMERA_STATE_CLOSED";
Eino-Ville Talvala2e3215c2015-08-20 15:43:57 -0700439 default: break;
440 }
441 return "CAMERA_STATE_UNKNOWN";
442 }
Eino-Ville Talvalae91012b2017-07-10 15:27:24 -0700443
444 private static String cameraFacingToString(int cameraFacing) {
445 switch (cameraFacing) {
446 case ICameraServiceProxy.CAMERA_FACING_BACK: return "CAMERA_FACING_BACK";
447 case ICameraServiceProxy.CAMERA_FACING_FRONT: return "CAMERA_FACING_FRONT";
448 case ICameraServiceProxy.CAMERA_FACING_EXTERNAL: return "CAMERA_FACING_EXTERNAL";
449 default: break;
450 }
451 return "CAMERA_FACING_UNKNOWN";
452 }
453
Ruben Brunk9d21ee52015-03-20 22:18:55 -0700454}