diff options
author | Sol Boucher <solb@google.com> | 2014-06-09 14:37:08 -0700 |
---|---|---|
committer | Ed Heyl <edheyl@google.com> | 2014-06-18 10:24:45 -0700 |
commit | cef46862d6937bc98bf1a6b087c5daa22b5239f3 (patch) | |
tree | 5c06e89ff81de08f86dfb963cda27648be623dfe /camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java | |
parent | da52dbdc5ffc76c1c875229086e91475eb150090 (diff) | |
download | android_frameworks_ex-cef46862d6937bc98bf1a6b087c5daa22b5239f3.tar.gz android_frameworks_ex-cef46862d6937bc98bf1a6b087c5daa22b5239f3.tar.bz2 android_frameworks_ex-cef46862d6937bc98bf1a6b087c5daa22b5239f3.zip |
Refactor CameraManager hierarchy to support new implementations
This relocates several components of the implementation class
AndroidCameraManagerImpl to higher levels in the inheritence hierarchy, where
they can be reused by other implementations of the CameraManager interface:
- AndroidCameraManager.DispatchThread is moved into its own file
- The core (history-tracking) pieces of CameraManager.CameraHandler are moved
into their own HistoryHandler class, which the former is made to inherit
- The constants defined in AndroidCameraManagerImpl for use in its nested
CameraHandler class are moved into a separate class called CameraActions
- AndroidCameraManagerImpl.CameraStateHolder is moved into its own file
- Those CallbackForward classes that aren't tied to Android's Camera1 API are
moved directly into the CameraManager interface
Change-Id: I5f3e1eb72039a0018ce2277e3ec6289bfa4ccec3
Diffstat (limited to 'camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java')
-rw-r--r-- | camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java b/camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java new file mode 100644 index 0000000..67713c9 --- /dev/null +++ b/camera2/portability/src/com/android/ex/camera2/portability/DispatchThread.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ex.camera2.portability; + +import android.os.Handler; +import android.os.HandlerThread; +import android.os.SystemClock; + +import com.android.ex.camera2.portability.debug.Log; + +import java.util.LinkedList; +import java.util.Queue; + +class DispatchThread extends Thread { + private static final Log.Tag TAG = new Log.Tag("DispatchThread"); + private static final long MAX_MESSAGE_QUEUE_LENGTH = 256; + + private final Queue<Runnable> mJobQueue; + private Boolean mIsEnded; + private Handler mCameraHandler; + private HandlerThread mCameraHandlerThread; + + public DispatchThread(Handler cameraHandler, HandlerThread cameraHandlerThread) { + super("Camera Job Dispatch Thread"); + mJobQueue = new LinkedList<Runnable>(); + mIsEnded = new Boolean(false); + mCameraHandler = cameraHandler; + mCameraHandlerThread = cameraHandlerThread; + } + + /** + * Queues up the job. + * + * @param job The job to run. + */ + public void runJob(Runnable job) { + if (isEnded()) { + throw new IllegalStateException( + "Trying to run job on interrupted dispatcher thread"); + } + synchronized (mJobQueue) { + if (mJobQueue.size() == MAX_MESSAGE_QUEUE_LENGTH) { + throw new RuntimeException("Camera master thread job queue full"); + } + + mJobQueue.add(job); + mJobQueue.notifyAll(); + } + } + + /** + * Queues up the job and wait for it to be done. + * + * @param job The job to run. + * @param timeoutMs Timeout limit in milliseconds. + * @param jobMsg The message to log when the job runs timeout. + * @return Whether the job finishes before timeout. + */ + public void runJobSync(final Runnable job, Object waitLock, long timeoutMs, String jobMsg) { + String timeoutMsg = "Timeout waiting " + timeoutMs + "ms for " + jobMsg; + synchronized (waitLock) { + long timeoutBound = SystemClock.uptimeMillis() + timeoutMs; + try { + runJob(job); + waitLock.wait(timeoutMs); + if (SystemClock.uptimeMillis() > timeoutBound) { + throw new IllegalStateException(timeoutMsg); + } + } catch (InterruptedException ex) { + if (SystemClock.uptimeMillis() > timeoutBound) { + throw new IllegalStateException(timeoutMsg); + } + } + } + } + + /** + * Gracefully ends this thread. Will stop after all jobs are processed. + */ + public void end() { + synchronized (mIsEnded) { + mIsEnded = true; + } + synchronized(mJobQueue) { + mJobQueue.notifyAll(); + } + } + + private boolean isEnded() { + synchronized (mIsEnded) { + return mIsEnded; + } + } + + @Override + public void run() { + while(true) { + Runnable job = null; + synchronized (mJobQueue) { + while (mJobQueue.size() == 0 && !isEnded()) { + try { + mJobQueue.wait(); + } catch (InterruptedException ex) { + Log.w(TAG, "Dispatcher thread wait() interrupted, exiting"); + break; + } + } + + job = mJobQueue.poll(); + } + + if (job == null) { + // mJobQueue.poll() returning null means wait() is + // interrupted and the queue is empty. + if (isEnded()) { + break; + } + continue; + } + + job.run(); + + synchronized (DispatchThread.this) { + mCameraHandler.post(new Runnable() { + @Override + public void run() { + synchronized (DispatchThread.this) { + DispatchThread.this.notifyAll(); + } + } + }); + try { + DispatchThread.this.wait(); + } catch (InterruptedException ex) { + // TODO: do something here. + } + } + } + mCameraHandlerThread.quitSafely(); + } +} |