John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | */ |
| 16 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 18 | |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 19 | import android.content.Context; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 20 | import android.content.pm.PackageManager.NameNotFoundException; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 21 | import android.graphics.Color; |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 22 | import android.graphics.Point; |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 23 | import android.graphics.Rect; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 25 | import android.os.SystemClock; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 26 | import android.util.Log; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 28 | import android.view.MotionEvent; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 29 | import android.view.View; |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 30 | import android.view.ViewGroup; |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 31 | import android.view.WindowManager; |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.ImageView.ScaleType; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 35 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 36 | import com.android.keyguard.KeyguardActivityLauncher.CameraWidgetInfo; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 37 | |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 38 | public class CameraWidgetFrame extends KeyguardWidgetFrame implements View.OnClickListener { |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 39 | private static final String TAG = CameraWidgetFrame.class.getSimpleName(); |
| 40 | private static final boolean DEBUG = KeyguardHostView.DEBUG; |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 41 | private static final int WIDGET_ANIMATION_DURATION = 250; // ms |
| 42 | private static final int WIDGET_WAIT_DURATION = 650; // ms |
| 43 | private static final int RECOVERY_DELAY = 1000; // ms |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 44 | |
| 45 | interface Callbacks { |
| 46 | void onLaunchingCamera(); |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 47 | void onCameraLaunchedSuccessfully(); |
| 48 | void onCameraLaunchedUnsuccessfully(); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | private final Handler mHandler = new Handler(); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 52 | private final KeyguardActivityLauncher mActivityLauncher; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 53 | private final Callbacks mCallbacks; |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 54 | private final CameraWidgetInfo mWidgetInfo; |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 55 | private final WindowManager mWindowManager; |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 56 | private final Point mRenderedSize = new Point(); |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 57 | private final int[] mTmpLoc = new int[2]; |
| 58 | private final Rect mTmpRect = new Rect(); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 59 | |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 60 | private long mLaunchCameraStart; |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 61 | private boolean mActive; |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 62 | private boolean mTransitioning; |
| 63 | private boolean mDown; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 64 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 65 | private FixedSizeFrameLayout mPreview; |
| 66 | private View mFullscreenPreview; |
| 67 | |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 68 | private final Runnable mTransitionToCameraRunnable = new Runnable() { |
| 69 | @Override |
| 70 | public void run() { |
| 71 | transitionToCamera(); |
| 72 | }}; |
| 73 | |
| 74 | private final Runnable mTransitionToCameraEndAction = new Runnable() { |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 75 | @Override |
| 76 | public void run() { |
John Spurlock | dbe24b7 | 2012-11-01 13:01:05 -0400 | [diff] [blame] | 77 | if (!mTransitioning) |
| 78 | return; |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 79 | Handler worker = getWorkerHandler() != null ? getWorkerHandler() : mHandler; |
John Spurlock | 47cde77 | 2012-10-26 08:25:51 -0400 | [diff] [blame] | 80 | mLaunchCameraStart = SystemClock.uptimeMillis(); |
John Spurlock | dbe24b7 | 2012-11-01 13:01:05 -0400 | [diff] [blame] | 81 | if (DEBUG) Log.d(TAG, "Launching camera at " + mLaunchCameraStart); |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 82 | mActivityLauncher.launchCamera(worker, mSecureCameraActivityStartedRunnable); |
| 83 | }}; |
| 84 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 85 | private final Runnable mPostTransitionToCameraEndAction = new Runnable() { |
| 86 | @Override |
| 87 | public void run() { |
| 88 | mHandler.post(mTransitionToCameraEndAction); |
| 89 | }}; |
| 90 | |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 91 | private final Runnable mRecoverRunnable = new Runnable() { |
| 92 | @Override |
| 93 | public void run() { |
| 94 | recover(); |
| 95 | }}; |
| 96 | |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 97 | private final Runnable mRenderRunnable = new Runnable() { |
| 98 | @Override |
| 99 | public void run() { |
| 100 | render(); |
| 101 | }}; |
| 102 | |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 103 | private final Runnable mSecureCameraActivityStartedRunnable = new Runnable() { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 104 | @Override |
| 105 | public void run() { |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 106 | onSecureCameraActivityStarted(); |
| 107 | } |
| 108 | }; |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 109 | |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 110 | private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
| 111 | private boolean mShowing; |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 112 | void onKeyguardVisibilityChanged(boolean showing) { |
| 113 | if (mShowing == showing) |
| 114 | return; |
| 115 | mShowing = showing; |
| 116 | CameraWidgetFrame.this.onKeyguardVisibilityChanged(mShowing); |
| 117 | }; |
| 118 | }; |
| 119 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 120 | private static final class FixedSizeFrameLayout extends FrameLayout { |
| 121 | int width; |
| 122 | int height; |
| 123 | |
| 124 | FixedSizeFrameLayout(Context context) { |
| 125 | super(context); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 130 | measureChildren( |
| 131 | MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), |
| 132 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 133 | setMeasuredDimension(width, height); |
| 134 | } |
| 135 | } |
| 136 | |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 137 | private CameraWidgetFrame(Context context, Callbacks callbacks, |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 138 | KeyguardActivityLauncher activityLauncher, |
| 139 | CameraWidgetInfo widgetInfo, View previewWidget) { |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 140 | super(context); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 141 | mCallbacks = callbacks; |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 142 | mActivityLauncher = activityLauncher; |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 143 | mWidgetInfo = widgetInfo; |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 144 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 145 | KeyguardUpdateMonitor.getInstance(context).registerCallback(mCallback); |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 146 | |
| 147 | mPreview = new FixedSizeFrameLayout(context); |
| 148 | mPreview.addView(previewWidget); |
| 149 | addView(mPreview); |
| 150 | |
| 151 | View clickBlocker = new View(context); |
| 152 | clickBlocker.setBackgroundColor(Color.TRANSPARENT); |
| 153 | clickBlocker.setOnClickListener(this); |
| 154 | addView(clickBlocker); |
| 155 | |
| 156 | setContentDescription(context.getString(R.string.keyguard_accessibility_camera)); |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 157 | if (DEBUG) Log.d(TAG, "new CameraWidgetFrame instance " + instanceId()); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 158 | } |
| 159 | |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 160 | public static CameraWidgetFrame create(Context context, Callbacks callbacks, |
| 161 | KeyguardActivityLauncher launcher) { |
| 162 | if (context == null || callbacks == null || launcher == null) |
| 163 | return null; |
| 164 | |
| 165 | CameraWidgetInfo widgetInfo = launcher.getCameraWidgetInfo(); |
| 166 | if (widgetInfo == null) |
| 167 | return null; |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 168 | View previewWidget = getPreviewWidget(context, widgetInfo); |
| 169 | if (previewWidget == null) |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 170 | return null; |
| 171 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 172 | return new CameraWidgetFrame(context, callbacks, launcher, widgetInfo, previewWidget); |
| 173 | } |
| 174 | |
| 175 | private static View getPreviewWidget(Context context, CameraWidgetInfo widgetInfo) { |
| 176 | return widgetInfo.layoutId > 0 ? |
| 177 | inflateWidgetView(context, widgetInfo) : |
| 178 | inflateGenericWidgetView(context); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | private static View inflateWidgetView(Context context, CameraWidgetInfo widgetInfo) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 182 | if (DEBUG) Log.d(TAG, "inflateWidgetView: " + widgetInfo.contextPackage); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 183 | View widgetView = null; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 184 | Exception exception = null; |
| 185 | try { |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 186 | Context cameraContext = context.createPackageContext( |
| 187 | widgetInfo.contextPackage, Context.CONTEXT_RESTRICTED); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 188 | LayoutInflater cameraInflater = (LayoutInflater) |
| 189 | cameraContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 190 | cameraInflater = cameraInflater.cloneInContext(cameraContext); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 191 | widgetView = cameraInflater.inflate(widgetInfo.layoutId, null, false); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 192 | } catch (NameNotFoundException e) { |
| 193 | exception = e; |
| 194 | } catch (RuntimeException e) { |
| 195 | exception = e; |
| 196 | } |
| 197 | if (exception != null) { |
| 198 | Log.w(TAG, "Error creating camera widget view", exception); |
| 199 | } |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 200 | return widgetView; |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 201 | } |
| 202 | |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 203 | private static View inflateGenericWidgetView(Context context) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 204 | if (DEBUG) Log.d(TAG, "inflateGenericWidgetView"); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 205 | ImageView iv = new ImageView(context); |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 206 | iv.setImageResource(R.drawable.ic_lockscreen_camera); |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 207 | iv.setScaleType(ScaleType.CENTER); |
| 208 | iv.setBackgroundColor(Color.argb(127, 0, 0, 0)); |
| 209 | return iv; |
| 210 | } |
| 211 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 212 | private void render() { |
| 213 | final View root = getRootView(); |
| 214 | final int width = root.getWidth(); |
| 215 | final int height = root.getHeight(); |
| 216 | if (mRenderedSize.x == width && mRenderedSize.y == height) { |
| 217 | if (DEBUG) Log.d(TAG, String.format("Already rendered at size=%sx%s", width, height)); |
| 218 | return; |
| 219 | } |
| 220 | if (width == 0 || height == 0) { |
| 221 | return; |
John Spurlock | 47cde77 | 2012-10-26 08:25:51 -0400 | [diff] [blame] | 222 | } |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 223 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 224 | mPreview.width = width; |
| 225 | mPreview.height = height; |
| 226 | mPreview.requestLayout(); |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 227 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 228 | final int thisWidth = getWidth() - getPaddingLeft() - getPaddingRight(); |
| 229 | final int thisHeight = getHeight() - getPaddingTop() - getPaddingBottom(); |
| 230 | |
| 231 | final float pvScaleX = (float) thisWidth / width; |
| 232 | final float pvScaleY = (float) thisHeight / height; |
| 233 | final float pvScale = Math.min(pvScaleX, pvScaleY); |
| 234 | |
| 235 | final int pvWidth = (int) (pvScale * width); |
| 236 | final int pvHeight = (int) (pvScale * height); |
| 237 | |
| 238 | final float pvTransX = pvWidth < thisWidth ? (thisWidth - pvWidth) / 2 : 0; |
| 239 | final float pvTransY = pvHeight < thisHeight ? (thisHeight - pvHeight) / 2 : 0; |
| 240 | |
| 241 | mPreview.setPivotX(0); |
| 242 | mPreview.setPivotY(0); |
| 243 | mPreview.setScaleX(pvScale); |
| 244 | mPreview.setScaleY(pvScale); |
| 245 | mPreview.setTranslationX(pvTransX); |
| 246 | mPreview.setTranslationY(pvTransY); |
| 247 | |
| 248 | mRenderedSize.set(width, height); |
| 249 | if (DEBUG) Log.d(TAG, String.format("Rendered camera widget size=%sx%s instance=%s", |
| 250 | width, height, instanceId())); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private void transitionToCamera() { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 254 | if (mTransitioning || mDown) return; |
John Spurlock | cf69c56 | 2012-10-30 08:46:57 -0400 | [diff] [blame] | 255 | |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 256 | mTransitioning = true; |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 257 | |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 258 | enableWindowExitAnimation(false); |
John Spurlock | cf69c56 | 2012-10-30 08:46:57 -0400 | [diff] [blame] | 259 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 260 | mPreview.getLocationInWindow(mTmpLoc); |
| 261 | final float pvHeight = mPreview.getHeight() * mPreview.getScaleY(); |
| 262 | final float pvCenter = mTmpLoc[1] + pvHeight / 2f; |
| 263 | |
| 264 | final ViewGroup root = (ViewGroup) getRootView(); |
| 265 | if (mFullscreenPreview == null) { |
| 266 | mFullscreenPreview = getPreviewWidget(mContext, mWidgetInfo); |
| 267 | mFullscreenPreview.setClickable(false); |
| 268 | root.addView(mFullscreenPreview); |
| 269 | } |
| 270 | |
| 271 | root.getWindowVisibleDisplayFrame(mTmpRect); |
| 272 | final float fsHeight = mTmpRect.height(); |
| 273 | final float fsCenter = mTmpRect.top + fsHeight / 2; |
| 274 | |
| 275 | final float fsScaleY = pvHeight / fsHeight; |
| 276 | final float fsTransY = pvCenter - fsCenter; |
| 277 | final float fsScaleX = mPreview.getScaleX(); |
| 278 | |
| 279 | mPreview.setVisibility(View.GONE); |
| 280 | mFullscreenPreview.setVisibility(View.VISIBLE); |
| 281 | mFullscreenPreview.setTranslationY(fsTransY); |
| 282 | mFullscreenPreview.setScaleX(fsScaleX); |
| 283 | mFullscreenPreview.setScaleY(fsScaleY); |
| 284 | mFullscreenPreview |
| 285 | .animate() |
| 286 | .scaleX(1) |
| 287 | .scaleY(1) |
| 288 | .translationX(0) |
| 289 | .translationY(0) |
| 290 | .setDuration(WIDGET_ANIMATION_DURATION) |
| 291 | .withEndAction(mPostTransitionToCameraEndAction) |
| 292 | .start(); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 293 | mCallbacks.onLaunchingCamera(); |
| 294 | } |
| 295 | |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 296 | private void recover() { |
| 297 | if (DEBUG) Log.d(TAG, "recovering at " + SystemClock.uptimeMillis()); |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 298 | mCallbacks.onCameraLaunchedUnsuccessfully(); |
| 299 | reset(); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void setOnLongClickListener(OnLongClickListener l) { |
| 304 | // ignore |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 305 | } |
| 306 | |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 307 | @Override |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 308 | public void onClick(View v) { |
| 309 | if (DEBUG) Log.d(TAG, "clicked"); |
| 310 | if (mTransitioning) return; |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 311 | if (mActive) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 312 | cancelTransitionToCamera(); |
| 313 | transitionToCamera(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | @Override |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 318 | protected void onDetachedFromWindow() { |
| 319 | if (DEBUG) Log.d(TAG, "onDetachedFromWindow: instance " + instanceId() |
| 320 | + " at " + SystemClock.uptimeMillis()); |
| 321 | super.onDetachedFromWindow(); |
| 322 | KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mCallback); |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 323 | cancelTransitionToCamera(); |
| 324 | mHandler.removeCallbacks(mRecoverRunnable); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void onActive(boolean isActive) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 329 | mActive = isActive; |
| 330 | if (mActive) { |
| 331 | rescheduleTransitionToCamera(); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 332 | } else { |
| 333 | reset(); |
| 334 | } |
| 335 | } |
| 336 | |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 337 | @Override |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 338 | public boolean onUserInteraction(MotionEvent event) { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 339 | if (mTransitioning) { |
| 340 | if (DEBUG) Log.d(TAG, "onUserInteraction eaten: mTransitioning"); |
| 341 | return true; |
| 342 | } |
| 343 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 344 | getLocationOnScreen(mTmpLoc); |
| 345 | int rawBottom = mTmpLoc[1] + getHeight(); |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 346 | if (event.getRawY() > rawBottom) { |
| 347 | if (DEBUG) Log.d(TAG, "onUserInteraction eaten: below widget"); |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | int action = event.getAction(); |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 352 | mDown = action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE; |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 353 | if (mActive) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 354 | rescheduleTransitionToCamera(); |
| 355 | } |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 356 | if (DEBUG) Log.d(TAG, "onUserInteraction observed, not eaten"); |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | protected void onFocusLost() { |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 362 | if (DEBUG) Log.d(TAG, "onFocusLost at " + SystemClock.uptimeMillis()); |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 363 | cancelTransitionToCamera(); |
| 364 | super.onFocusLost(); |
| 365 | } |
| 366 | |
John Spurlock | dbe24b7 | 2012-11-01 13:01:05 -0400 | [diff] [blame] | 367 | public void onScreenTurnedOff() { |
| 368 | if (DEBUG) Log.d(TAG, "onScreenTurnedOff"); |
| 369 | reset(); |
| 370 | } |
| 371 | |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 372 | private void rescheduleTransitionToCamera() { |
| 373 | if (DEBUG) Log.d(TAG, "rescheduleTransitionToCamera at " + SystemClock.uptimeMillis()); |
| 374 | mHandler.removeCallbacks(mTransitionToCameraRunnable); |
| 375 | mHandler.postDelayed(mTransitionToCameraRunnable, WIDGET_WAIT_DURATION); |
| 376 | } |
| 377 | |
| 378 | private void cancelTransitionToCamera() { |
| 379 | if (DEBUG) Log.d(TAG, "cancelTransitionToCamera at " + SystemClock.uptimeMillis()); |
| 380 | mHandler.removeCallbacks(mTransitionToCameraRunnable); |
| 381 | } |
| 382 | |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 383 | private void onCameraLaunched() { |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 384 | mCallbacks.onCameraLaunchedSuccessfully(); |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 385 | reset(); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private void reset() { |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 389 | if (DEBUG) Log.d(TAG, "reset at " + SystemClock.uptimeMillis()); |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 390 | mLaunchCameraStart = 0; |
| 391 | mTransitioning = false; |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 392 | mDown = false; |
| 393 | cancelTransitionToCamera(); |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 394 | mHandler.removeCallbacks(mRecoverRunnable); |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 395 | mPreview.setVisibility(View.VISIBLE); |
| 396 | if (mFullscreenPreview != null) { |
| 397 | mFullscreenPreview.animate().cancel(); |
| 398 | mFullscreenPreview.setVisibility(View.GONE); |
| 399 | } |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 400 | enableWindowExitAnimation(true); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 401 | } |
| 402 | |
John Spurlock | dcc9681 | 2012-10-25 14:35:19 -0400 | [diff] [blame] | 403 | @Override |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 404 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 405 | if (DEBUG) Log.d(TAG, String.format("onSizeChanged new=%sx%s old=%sx%s at %s", |
| 406 | w, h, oldw, oldh, SystemClock.uptimeMillis())); |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 407 | mHandler.post(mRenderRunnable); |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 408 | super.onSizeChanged(w, h, oldw, oldh); |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 409 | } |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 410 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 411 | @Override |
| 412 | public void onBouncerShowing(boolean showing) { |
| 413 | if (showing) { |
| 414 | mTransitioning = false; |
| 415 | mHandler.post(mRecoverRunnable); |
| 416 | } |
| 417 | } |
| 418 | |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 419 | private void enableWindowExitAnimation(boolean isEnabled) { |
| 420 | View root = getRootView(); |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 421 | ViewGroup.LayoutParams lp = root.getLayoutParams(); |
| 422 | if (!(lp instanceof WindowManager.LayoutParams)) |
| 423 | return; |
| 424 | WindowManager.LayoutParams wlp = (WindowManager.LayoutParams) lp; |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 425 | int newWindowAnimations = isEnabled ? R.style.Animation_LockScreen : 0; |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 426 | if (newWindowAnimations != wlp.windowAnimations) { |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 427 | if (DEBUG) Log.d(TAG, "setting windowAnimations to: " + newWindowAnimations |
| 428 | + " at " + SystemClock.uptimeMillis()); |
John Spurlock | 7377982 | 2012-10-30 11:47:23 -0400 | [diff] [blame] | 429 | wlp.windowAnimations = newWindowAnimations; |
| 430 | mWindowManager.updateViewLayout(root, wlp); |
John Spurlock | e2ac520 | 2012-10-30 11:05:55 -0400 | [diff] [blame] | 431 | } |
| 432 | } |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 433 | |
| 434 | private void onKeyguardVisibilityChanged(boolean showing) { |
| 435 | if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged " + showing |
| 436 | + " at " + SystemClock.uptimeMillis()); |
| 437 | if (mTransitioning && !showing) { |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 438 | mTransitioning = false; |
| 439 | mHandler.removeCallbacks(mRecoverRunnable); |
| 440 | if (mLaunchCameraStart > 0) { |
| 441 | long launchTime = SystemClock.uptimeMillis() - mLaunchCameraStart; |
| 442 | if (DEBUG) Log.d(TAG, String.format("Camera took %sms to launch", launchTime)); |
| 443 | mLaunchCameraStart = 0; |
| 444 | onCameraLaunched(); |
| 445 | } |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
John Spurlock | 57f928f | 2012-11-02 13:09:25 -0400 | [diff] [blame] | 449 | private void onSecureCameraActivityStarted() { |
| 450 | if (DEBUG) Log.d(TAG, "onSecureCameraActivityStarted at " + SystemClock.uptimeMillis()); |
| 451 | mHandler.postDelayed(mRecoverRunnable, RECOVERY_DELAY); |
| 452 | } |
| 453 | |
John Spurlock | 6275b3f | 2012-11-04 13:48:42 -0500 | [diff] [blame] | 454 | private String instanceId() { |
| 455 | return Integer.toHexString(hashCode()); |
| 456 | } |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 457 | } |