1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* Copyright (C) 2012 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.keyguard;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
public class KeyguardViewStateManager implements
SlidingChallengeLayout.OnChallengeScrolledListener,
ChallengeLayout.OnBouncerStateChangedListener {
private static final String TAG = "KeyguardViewStateManager";
private KeyguardWidgetPager mKeyguardWidgetPager;
private ChallengeLayout mChallengeLayout;
private KeyguardHostView mKeyguardHostView;
private int[] mTmpPoint = new int[2];
private int[] mTmpLoc = new int[2];
private KeyguardSecurityView mKeyguardSecurityContainer;
private static final int SCREEN_ON_HINT_DURATION = 1000;
private static final int SCREEN_ON_RING_HINT_DELAY = 300;
private static final boolean SHOW_INITIAL_PAGE_HINTS = false;
Handler mMainQueue = new Handler(Looper.myLooper());
int mLastScrollState = SlidingChallengeLayout.SCROLL_STATE_IDLE;
// Paged view state
private int mPageListeningToSlider = -1;
private int mCurrentPage = -1;
private int mPageIndexOnPageBeginMoving = -1;
int mChallengeTop = 0;
private final AnimatorListener mPauseListener = new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
mKeyguardSecurityContainer.onPause();
}
};
private final AnimatorListener mResumeListener = new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
if (((View)mKeyguardSecurityContainer).isShown()) {
mKeyguardSecurityContainer.onResume(0);
}
}
};
public KeyguardViewStateManager(KeyguardHostView hostView) {
mKeyguardHostView = hostView;
}
public void setPagedView(KeyguardWidgetPager pagedView) {
mKeyguardWidgetPager = pagedView;
updateEdgeSwiping();
}
public void setChallengeLayout(ChallengeLayout layout) {
mChallengeLayout = layout;
updateEdgeSwiping();
}
private void updateEdgeSwiping() {
if (mChallengeLayout != null && mKeyguardWidgetPager != null) {
if (mChallengeLayout.isChallengeOverlapping()) {
mKeyguardWidgetPager.setOnlyAllowEdgeSwipes(true);
} else {
mKeyguardWidgetPager.setOnlyAllowEdgeSwipes(false);
}
}
}
public boolean isChallengeShowing() {
if (mChallengeLayout != null) {
return mChallengeLayout.isChallengeShowing();
}
return false;
}
public boolean isChallengeOverlapping() {
if (mChallengeLayout != null) {
return mChallengeLayout.isChallengeOverlapping();
}
return false;
}
public void setSecurityViewContainer(KeyguardSecurityView container) {
mKeyguardSecurityContainer = container;
}
public void showBouncer(boolean show) {
mChallengeLayout.showBouncer();
}
public boolean isBouncing() {
return mChallengeLayout.isBouncing();
}
public void fadeOutSecurity(int duration) {
((View) mKeyguardSecurityContainer).animate().alpha(0f).setDuration(duration)
.setListener(mPauseListener);
}
public void fadeInSecurity(int duration) {
((View) mKeyguardSecurityContainer).animate().alpha(1f).setDuration(duration)
.setListener(mResumeListener);
}
public void onPageBeginMoving() {
if (mChallengeLayout.isChallengeOverlapping() &&
mChallengeLayout instanceof SlidingChallengeLayout) {
SlidingChallengeLayout scl = (SlidingChallengeLayout) mChallengeLayout;
scl.fadeOutChallenge();
mPageIndexOnPageBeginMoving = mKeyguardWidgetPager.getCurrentPage();
}
// We use mAppWidgetToShow to show a particular widget after you add it--
// once the user swipes a page we clear that behavior
if (mKeyguardHostView != null) {
mKeyguardHostView.clearAppWidgetToShow();
mKeyguardHostView.setOnDismissAction(null);
}
if (mHideHintsRunnable != null) {
mMainQueue.removeCallbacks(mHideHintsRunnable);
mHideHintsRunnable = null;
}
}
public void onPageEndMoving() {
mPageIndexOnPageBeginMoving = -1;
}
public void onPageSwitching(View newPage, int newPageIndex) {
if (mKeyguardWidgetPager != null && mChallengeLayout instanceof SlidingChallengeLayout) {
boolean isCameraPage = newPage instanceof CameraWidgetFrame;
if (isCameraPage) {
CameraWidgetFrame camera = (CameraWidgetFrame) newPage;
camera.setUseFastTransition(mKeyguardWidgetPager.isWarping());
}
SlidingChallengeLayout scl = (SlidingChallengeLayout) mChallengeLayout;
scl.setChallengeInteractive(!isCameraPage);
final int currentFlags = mKeyguardWidgetPager.getSystemUiVisibility();
final int newFlags = isCameraPage ? (currentFlags | View.STATUS_BAR_DISABLE_SEARCH)
: (currentFlags & ~View.STATUS_BAR_DISABLE_SEARCH);
mKeyguardWidgetPager.setSystemUiVisibility(newFlags);
}
// If the page we're settling to is the same as we started on, and the action of
// moving the page hid the security, we restore it immediately.
if (mPageIndexOnPageBeginMoving == mKeyguardWidgetPager.getNextPage() &&
mChallengeLayout instanceof SlidingChallengeLayout) {
SlidingChallengeLayout scl = (SlidingChallengeLayout) mChallengeLayout;
scl.fadeInChallenge();
mKeyguardWidgetPager.setWidgetToResetOnPageFadeOut(-1);
}
mPageIndexOnPageBeginMoving = -1;
}
public void onPageSwitched(View newPage, int newPageIndex) {
// Reset the previous page size and ensure the current page is sized appropriately.
// We only modify the page state if it is not currently under control by the slider.
// This prevents conflicts.
// If the page hasn't switched, don't bother with any of this
if (mCurrentPage == newPageIndex) return;
if (mKeyguardWidgetPager != null && mChallengeLayout != null) {
KeyguardWidgetFrame prevPage = mKeyguardWidgetPager.getWidgetPageAt(mCurrentPage);
if (prevPage != null && mCurrentPage != mPageListeningToSlider && mCurrentPage
!= mKeyguardWidgetPager.getWidgetToResetOnPageFadeOut()) {
prevPage.resetSize();
}
KeyguardWidgetFrame newCurPage = mKeyguardWidgetPager.getWidgetPageAt(newPageIndex);
boolean challengeOverlapping = mChallengeLayout.isChallengeOverlapping();
if (challengeOverlapping && !newCurPage.isSmall()
&& mPageListeningToSlider != newPageIndex) {
newCurPage.shrinkWidget(true);
}
}
mCurrentPage = newPageIndex;
}
public void onPageBeginWarp() {
fadeOutSecurity(SlidingChallengeLayout.CHALLENGE_FADE_OUT_DURATION);
View frame = mKeyguardWidgetPager.getPageAt(mKeyguardWidgetPager.getPageWarpIndex());
((KeyguardWidgetFrame)frame).showFrame(this);
}
public void onPageEndWarp() {
fadeInSecurity(SlidingChallengeLayout.CHALLENGE_FADE_IN_DURATION);
View frame = mKeyguardWidgetPager.getPageAt(mKeyguardWidgetPager.getPageWarpIndex());
((KeyguardWidgetFrame)frame).hideFrame(this);
}
private int getChallengeTopRelativeToFrame(KeyguardWidgetFrame frame, int top) {
mTmpPoint[0] = 0;
mTmpPoint[1] = top;
mapPoint((View) mChallengeLayout, frame, mTmpPoint);
return mTmpPoint[1];
}
/**
* Simple method to map a point from one view's coordinates to another's. Note: this method
* doesn't account for transforms, so if the views will be transformed, this should not be used.
*
* @param fromView The view to which the point is relative
* @param toView The view into which the point should be mapped
* @param pt The point
*/
private void mapPoint(View fromView, View toView, int pt[]) {
fromView.getLocationInWindow(mTmpLoc);
int x = mTmpLoc[0];
int y = mTmpLoc[1];
toView.getLocationInWindow(mTmpLoc);
int vX = mTmpLoc[0];
int vY = mTmpLoc[1];
pt[0] += x - vX;
pt[1] += y - vY;
}
private void userActivity() {
if (mKeyguardHostView != null) {
mKeyguardHostView.onUserActivityTimeoutChanged();
mKeyguardHostView.userActivity();
}
}
@Override
public void onScrollStateChanged(int scrollState) {
if (mKeyguardWidgetPager == null || mChallengeLayout == null) return;
boolean challengeOverlapping = mChallengeLayout.isChallengeOverlapping();
if (scrollState == SlidingChallengeLayout.SCROLL_STATE_IDLE) {
KeyguardWidgetFrame frame = mKeyguardWidgetPager.getWidgetPageAt(mPageListeningToSlider);
if (frame == null) return;
if (!challengeOverlapping) {
if (!mKeyguardWidgetPager.isPageMoving()) {
frame.resetSize();
userActivity();
} else {
mKeyguardWidgetPager.setWidgetToResetOnPageFadeOut(mPageListeningToSlider);
}
}
if (frame.isSmall()) {
// This is to make sure that if the scroller animation gets cut off midway
// that the frame doesn't stay in a partial down position.
frame.setFrameHeight(frame.getSmallFrameHeight());
}
if (scrollState != SlidingChallengeLayout.SCROLL_STATE_FADING) {
frame.hideFrame(this);
}
updateEdgeSwiping();
if (mChallengeLayout.isChallengeShowing()) {
mKeyguardSecurityContainer.onResume(KeyguardSecurityView.VIEW_REVEALED);
} else {
mKeyguardSecurityContainer.onPause();
}
mPageListeningToSlider = -1;
} else if (mLastScrollState == SlidingChallengeLayout.SCROLL_STATE_IDLE) {
// Whether dragging or settling, if the last state was idle, we use this signal
// to update the current page who will receive events from the sliding challenge.
// We resize the frame as appropriate.
mPageListeningToSlider = mKeyguardWidgetPager.getNextPage();
KeyguardWidgetFrame frame = mKeyguardWidgetPager.getWidgetPageAt(mPageListeningToSlider);
if (frame == null) return;
// Skip showing the frame and shrinking the widget if we are
if (!mChallengeLayout.isBouncing()) {
if (scrollState != SlidingChallengeLayout.SCROLL_STATE_FADING) {
frame.showFrame(this);
}
// As soon as the security begins sliding, the widget becomes small (if it wasn't
// small to begin with).
if (!frame.isSmall()) {
// We need to fetch the final page, in case the pages are in motion.
mPageListeningToSlider = mKeyguardWidgetPager.getNextPage();
frame.shrinkWidget(false);
}
} else {
if (!frame.isSmall()) {
// We need to fetch the final page, in case the pages are in motion.
mPageListeningToSlider = mKeyguardWidgetPager.getNextPage();
}
}
// View is on the move. Pause the security view until it completes.
mKeyguardSecurityContainer.onPause();
}
mLastScrollState = scrollState;
}
@Override
public void onScrollPositionChanged(float scrollPosition, int challengeTop) {
mChallengeTop = challengeTop;
KeyguardWidgetFrame frame = mKeyguardWidgetPager.getWidgetPageAt(mPageListeningToSlider);
if (frame != null && mLastScrollState != SlidingChallengeLayout.SCROLL_STATE_FADING) {
frame.adjustFrame(getChallengeTopRelativeToFrame(frame, mChallengeTop));
}
}
private Runnable mHideHintsRunnable = new Runnable() {
@Override
public void run() {
if (mKeyguardWidgetPager != null) {
mKeyguardWidgetPager.hideOutlinesAndSidePages();
}
}
};
public void showUsabilityHints() {
mMainQueue.postDelayed( new Runnable() {
@Override
public void run() {
mKeyguardSecurityContainer.showUsabilityHint();
}
} , SCREEN_ON_RING_HINT_DELAY);
if (SHOW_INITIAL_PAGE_HINTS) {
mKeyguardWidgetPager.showInitialPageHints();
}
if (mHideHintsRunnable != null) {
mMainQueue.postDelayed(mHideHintsRunnable, SCREEN_ON_HINT_DURATION);
}
}
// ChallengeLayout.OnBouncerStateChangedListener
@Override
public void onBouncerStateChanged(boolean bouncerActive) {
if (bouncerActive) {
mKeyguardWidgetPager.zoomOutToBouncer();
} else {
mKeyguardWidgetPager.zoomInFromBouncer();
if (mKeyguardHostView != null) {
mKeyguardHostView.setOnDismissAction(null);
}
}
}
}
|