2525import android .os .SystemClock ;
2626import android .util .Log ;
2727import android .view .LayoutInflater ;
28+ import android .view .MotionEvent ;
2829import android .view .View ;
2930import android .widget .FrameLayout ;
3031import android .widget .ImageView ;
3132import android .widget .ImageView .ScaleType ;
3233
3334import com .android .internal .policy .impl .keyguard .KeyguardActivityLauncher .CameraWidgetInfo ;
3435
35- public class CameraWidgetFrame extends KeyguardWidgetFrame {
36+ public class CameraWidgetFrame extends KeyguardWidgetFrame implements View . OnClickListener {
3637 private static final String TAG = CameraWidgetFrame .class .getSimpleName ();
3738 private static final boolean DEBUG = KeyguardHostView .DEBUG ;
3839 private static final int WIDGET_ANIMATION_DURATION = 250 ;
40+ private static final int WIDGET_WAIT_DURATION = 650 ;
3941
4042 interface Callbacks {
4143 void onLaunchingCamera ();
@@ -49,6 +51,10 @@ interface Callbacks {
4951 private View mWidgetView ;
5052 private long mLaunchCameraStart ;
5153 private boolean mRendered ;
54+ private boolean mActive ;
55+ private boolean mChallengeActive ;
56+ private boolean mTransitioning ;
57+ private boolean mDown ;
5258
5359 private final Runnable mLaunchCameraRunnable = new Runnable () {
5460 @ Override
@@ -63,6 +69,12 @@ public void run() {
6369 render ();
6470 }};
6571
72+ private final Runnable mTransitionToCameraRunnable = new Runnable () {
73+ @ Override
74+ public void run () {
75+ transitionToCamera ();
76+ }};
77+
6678 private CameraWidgetFrame (Context context , Callbacks callbacks ,
6779 KeyguardActivityLauncher activityLauncher ) {
6880 super (context );
@@ -93,10 +105,12 @@ public static CameraWidgetFrame create(Context context, Callbacks callbacks,
93105 CameraWidgetFrame cameraWidgetFrame = new CameraWidgetFrame (context , callbacks , launcher );
94106 cameraWidgetFrame .addView (preview );
95107 cameraWidgetFrame .mWidgetView = widgetView ;
108+ preview .setOnClickListener (cameraWidgetFrame );
96109 return cameraWidgetFrame ;
97110 }
98111
99112 private static View inflateWidgetView (Context context , CameraWidgetInfo widgetInfo ) {
113+ if (DEBUG ) Log .d (TAG , "inflateWidgetView: " + widgetInfo .contextPackage );
100114 View widgetView = null ;
101115 Exception exception = null ;
102116 try {
@@ -118,6 +132,7 @@ private static View inflateWidgetView(Context context, CameraWidgetInfo widgetIn
118132 }
119133
120134 private static View inflateGenericWidgetView (Context context ) {
135+ if (DEBUG ) Log .d (TAG , "inflateGenericWidgetView" );
121136 ImageView iv = new ImageView (context );
122137 iv .setImageResource (com .android .internal .R .drawable .ic_lockscreen_camera );
123138 iv .setScaleType (ScaleType .CENTER );
@@ -154,6 +169,9 @@ public void render() {
154169 }
155170
156171 private void transitionToCamera () {
172+ if (mTransitioning || mChallengeActive || mDown ) return ;
173+ if (DEBUG ) Log .d (TAG , "Transitioning to camera..." );
174+ mTransitioning = true ;
157175 int startWidth = getChildAt (0 ).getWidth ();
158176 int startHeight = getChildAt (0 ).getHeight ();
159177
@@ -173,11 +191,22 @@ private void transitionToCamera() {
173191 mCallbacks .onLaunchingCamera ();
174192 }
175193
194+ @ Override
195+ public void onClick (View v ) {
196+ if (DEBUG ) Log .d (TAG , "clicked" );
197+ if (mTransitioning ) return ;
198+ if (mActive && !mChallengeActive ) {
199+ cancelTransitionToCamera ();
200+ transitionToCamera ();
201+ }
202+ }
203+
176204 @ Override
177205 public void onWindowFocusChanged (boolean hasWindowFocus ) {
178206 super .onWindowFocusChanged (hasWindowFocus );
179-
207+ if ( DEBUG ) Log . d ( TAG , "onWindowFocusChanged: " + hasWindowFocus );
180208 if (!hasWindowFocus ) {
209+ mTransitioning = false ;
181210 if (mLaunchCameraStart > 0 ) {
182211 long launchTime = SystemClock .uptimeMillis () - mLaunchCameraStart ;
183212 if (DEBUG ) Log .d (TAG , String .format ("Camera took %sms to launch" , launchTime ));
@@ -189,23 +218,69 @@ public void onWindowFocusChanged(boolean hasWindowFocus) {
189218
190219 @ Override
191220 public void onActive (boolean isActive ) {
192- if (isActive ) {
193- mHandler .post (new Runnable (){
194- @ Override
195- public void run () {
196- transitionToCamera ();
197- }});
221+ mActive = isActive ;
222+ if (mActive ) {
223+ rescheduleTransitionToCamera ();
198224 } else {
199225 reset ();
200226 }
201227 }
202228
229+ @ Override
230+ public boolean onUserInteraction (int action ) {
231+ if (mTransitioning ) return true ;
232+ if (DEBUG ) Log .d (TAG , "onUserInteraction " + action );
233+ mDown = action == MotionEvent .ACTION_DOWN || action == MotionEvent .ACTION_MOVE ;
234+ if (mActive && !mChallengeActive ) {
235+ rescheduleTransitionToCamera ();
236+ }
237+ return false ;
238+ }
239+
240+ @ Override
241+ protected void onFocusLost () {
242+ Log .d (TAG , "onFocusLost" );
243+ cancelTransitionToCamera ();
244+ super .onFocusLost ();
245+ }
246+
247+ @ Override
248+ public void onChallengeActive (boolean challengeActive ) {
249+ if (DEBUG ) Log .d (TAG , "onChallengeActive: " + challengeActive );
250+ mChallengeActive = challengeActive ;
251+ if (mTransitioning ) return ;
252+ if (mActive ) {
253+ if (mChallengeActive ) {
254+ cancelTransitionToCamera ();
255+ } else {
256+ rescheduleTransitionToCamera ();
257+ }
258+ }
259+ }
260+
261+ private void rescheduleTransitionToCamera () {
262+ if (DEBUG ) Log .d (TAG , "rescheduleTransitionToCamera at " + SystemClock .uptimeMillis ());
263+ mHandler .removeCallbacks (mTransitionToCameraRunnable );
264+ mHandler .postDelayed (mTransitionToCameraRunnable , WIDGET_WAIT_DURATION );
265+ }
266+
267+ private void cancelTransitionToCamera () {
268+ if (DEBUG ) Log .d (TAG , "cancelTransitionToCamera at " + SystemClock .uptimeMillis ());
269+ mHandler .removeCallbacks (mTransitionToCameraRunnable );
270+ }
271+
203272 private void onCameraLaunched () {
204- reset ();
205273 mCallbacks .onCameraLaunched ();
274+ reset ();
206275 }
207276
208277 private void reset () {
278+ if (DEBUG ) Log .d (TAG , "reset" );
279+ mLaunchCameraStart = 0 ;
280+ mTransitioning = false ;
281+ mChallengeActive = false ;
282+ mDown = false ;
283+ cancelTransitionToCamera ();
209284 animate ().cancel ();
210285 setScaleX (1 );
211286 setScaleY (1 );
0 commit comments