Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 28 | import android.view.RemotableViewMethod; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityEvent; |
| 30 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 31 | import android.widget.RemoteViews.RemoteView; |
| 32 | |
| 33 | /** |
| 34 | * Simple {@link ViewAnimator} that will animate between two or more views |
| 35 | * that have been added to it. Only one child is shown at a time. If |
| 36 | * requested, can automatically flip between each child at a regular interval. |
| 37 | * |
| 38 | * @attr ref android.R.styleable#AdapterViewFlipper_flipInterval |
| 39 | * @attr ref android.R.styleable#AdapterViewFlipper_autoStart |
| 40 | */ |
| 41 | @RemoteView |
| 42 | public class AdapterViewFlipper extends AdapterViewAnimator { |
| 43 | private static final String TAG = "ViewFlipper"; |
| 44 | private static final boolean LOGD = false; |
| 45 | |
| 46 | private static final int DEFAULT_INTERVAL = 10000; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 47 | |
| 48 | private int mFlipInterval = DEFAULT_INTERVAL; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 49 | private boolean mAutoStart = false; |
| 50 | |
| 51 | private boolean mRunning = false; |
| 52 | private boolean mStarted = false; |
| 53 | private boolean mVisible = false; |
| 54 | private boolean mUserPresent = true; |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 55 | private boolean mAdvancedByHost = false; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 56 | |
| 57 | public AdapterViewFlipper(Context context) { |
| 58 | super(context); |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public AdapterViewFlipper(Context context, AttributeSet attrs) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 62 | this(context, attrs, 0); |
| 63 | } |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 64 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 65 | public AdapterViewFlipper(Context context, AttributeSet attrs, int defStyleAttr) { |
| 66 | this(context, attrs, defStyleAttr, 0); |
| 67 | } |
| 68 | |
| 69 | public AdapterViewFlipper( |
| 70 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 71 | super(context, attrs, defStyleAttr, defStyleRes); |
| 72 | |
| 73 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 74 | com.android.internal.R.styleable.AdapterViewFlipper, defStyleAttr, defStyleRes); |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 75 | mFlipInterval = a.getInt( |
Adam Cohen | 47256602 | 2011-07-22 16:28:21 -0700 | [diff] [blame] | 76 | com.android.internal.R.styleable.AdapterViewFlipper_flipInterval, DEFAULT_INTERVAL); |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 77 | mAutoStart = a.getBoolean( |
Adam Cohen | 47256602 | 2011-07-22 16:28:21 -0700 | [diff] [blame] | 78 | com.android.internal.R.styleable.AdapterViewFlipper_autoStart, false); |
Adam Cohen | 1b065cd | 2010-09-28 14:53:47 -0700 | [diff] [blame] | 79 | |
Adam Cohen | 47256602 | 2011-07-22 16:28:21 -0700 | [diff] [blame] | 80 | // A view flipper should cycle through the views |
| 81 | mLoopViews = true; |
Adam Cohen | 1b065cd | 2010-09-28 14:53:47 -0700 | [diff] [blame] | 82 | |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 83 | a.recycle(); |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 87 | @Override |
| 88 | public void onReceive(Context context, Intent intent) { |
| 89 | final String action = intent.getAction(); |
| 90 | if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 91 | mUserPresent = false; |
| 92 | updateRunning(); |
| 93 | } else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
| 94 | mUserPresent = true; |
| 95 | updateRunning(false); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | @Override |
| 101 | protected void onAttachedToWindow() { |
| 102 | super.onAttachedToWindow(); |
| 103 | |
| 104 | // Listen for broadcasts related to user-presence |
| 105 | final IntentFilter filter = new IntentFilter(); |
| 106 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 107 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Svetoslav | 985e566 | 2014-08-06 22:05:48 -0700 | [diff] [blame] | 108 | |
| 109 | // OK, this is gross but needed. This class is supported by the |
| 110 | // remote views machanism and as a part of that the remote views |
| 111 | // can be inflated by a context for another user without the app |
| 112 | // having interact users permission - just for loading resources. |
| 113 | // For exmaple, when adding widgets from a user profile to the |
| 114 | // home screen. Therefore, we register the receiver as the current |
| 115 | // user not the one the context is for. |
| 116 | getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), |
| 117 | filter, null, mHandler); |
| 118 | |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 119 | |
| 120 | if (mAutoStart) { |
| 121 | // Automatically start when requested |
| 122 | startFlipping(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void onDetachedFromWindow() { |
| 128 | super.onDetachedFromWindow(); |
| 129 | mVisible = false; |
| 130 | |
| 131 | getContext().unregisterReceiver(mReceiver); |
| 132 | updateRunning(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected void onWindowVisibilityChanged(int visibility) { |
| 137 | super.onWindowVisibilityChanged(visibility); |
| 138 | mVisible = (visibility == VISIBLE); |
| 139 | updateRunning(false); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setAdapter(Adapter adapter) { |
| 144 | super.setAdapter(adapter); |
| 145 | updateRunning(); |
| 146 | } |
| 147 | |
| 148 | /** |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 149 | * Returns the flip interval, in milliseconds. |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 150 | * |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 151 | * @return the flip interval in milliseconds |
| 152 | * |
| 153 | * @see #setFlipInterval(int) |
| 154 | * |
| 155 | * @attr ref android.R.styleable#AdapterViewFlipper_flipInterval |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 156 | */ |
Philip Milne | aac722a | 2012-03-26 13:30:26 -0700 | [diff] [blame] | 157 | public int getFlipInterval() { |
| 158 | return mFlipInterval; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * How long to wait before flipping to the next view. |
| 163 | * |
| 164 | * @param flipInterval flip interval in milliseconds |
| 165 | * |
| 166 | * @see #getFlipInterval() |
| 167 | * |
| 168 | * @attr ref android.R.styleable#AdapterViewFlipper_flipInterval |
| 169 | */ |
| 170 | public void setFlipInterval(int flipInterval) { |
| 171 | mFlipInterval = flipInterval; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Start a timer to cycle through child views |
| 176 | */ |
| 177 | public void startFlipping() { |
| 178 | mStarted = true; |
| 179 | updateRunning(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * No more flips |
| 184 | */ |
| 185 | public void stopFlipping() { |
| 186 | mStarted = false; |
| 187 | updateRunning(); |
| 188 | } |
| 189 | |
| 190 | /** |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 191 | * {@inheritDoc} |
| 192 | */ |
| 193 | @Override |
| 194 | @RemotableViewMethod |
| 195 | public void showNext() { |
| 196 | // if the flipper is currently flipping automatically, and showNext() is called |
| 197 | // we should we should make sure to reset the timer |
| 198 | if (mRunning) { |
| 199 | mHandler.removeMessages(FLIP_MSG); |
| 200 | Message msg = mHandler.obtainMessage(FLIP_MSG); |
| 201 | mHandler.sendMessageDelayed(msg, mFlipInterval); |
| 202 | } |
| 203 | super.showNext(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * {@inheritDoc} |
| 208 | */ |
| 209 | @Override |
| 210 | @RemotableViewMethod |
| 211 | public void showPrevious() { |
| 212 | // if the flipper is currently flipping automatically, and showPrevious() is called |
| 213 | // we should we should make sure to reset the timer |
| 214 | if (mRunning) { |
| 215 | mHandler.removeMessages(FLIP_MSG); |
| 216 | Message msg = mHandler.obtainMessage(FLIP_MSG); |
| 217 | mHandler.sendMessageDelayed(msg, mFlipInterval); |
| 218 | } |
| 219 | super.showPrevious(); |
| 220 | } |
| 221 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 222 | /** |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 223 | * Internal method to start or stop dispatching flip {@link Message} based |
| 224 | * on {@link #mRunning} and {@link #mVisible} state. |
| 225 | */ |
| 226 | private void updateRunning() { |
| 227 | // by default when we update running, we want the |
| 228 | // current view to animate in |
| 229 | updateRunning(true); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Internal method to start or stop dispatching flip {@link Message} based |
| 234 | * on {@link #mRunning} and {@link #mVisible} state. |
| 235 | * |
| 236 | * @param flipNow Determines whether or not to execute the animation now, in |
| 237 | * addition to queuing future flips. If omitted, defaults to |
| 238 | * true. |
| 239 | */ |
| 240 | private void updateRunning(boolean flipNow) { |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 241 | boolean running = !mAdvancedByHost && mVisible && mStarted && mUserPresent |
| 242 | && mAdapter != null; |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 243 | if (running != mRunning) { |
| 244 | if (running) { |
| 245 | showOnly(mWhichChild, flipNow); |
| 246 | Message msg = mHandler.obtainMessage(FLIP_MSG); |
| 247 | mHandler.sendMessageDelayed(msg, mFlipInterval); |
| 248 | } else { |
| 249 | mHandler.removeMessages(FLIP_MSG); |
| 250 | } |
| 251 | mRunning = running; |
| 252 | } |
| 253 | if (LOGD) { |
| 254 | Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted |
| 255 | + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns true if the child views are flipping. |
| 261 | */ |
| 262 | public boolean isFlipping() { |
| 263 | return mStarted; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Set if this view automatically calls {@link #startFlipping()} when it |
| 268 | * becomes attached to a window. |
| 269 | */ |
| 270 | public void setAutoStart(boolean autoStart) { |
| 271 | mAutoStart = autoStart; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns true if this view automatically calls {@link #startFlipping()} |
| 276 | * when it becomes attached to a window. |
| 277 | */ |
| 278 | public boolean isAutoStart() { |
| 279 | return mAutoStart; |
| 280 | } |
| 281 | |
| 282 | private final int FLIP_MSG = 1; |
| 283 | |
| 284 | private final Handler mHandler = new Handler() { |
| 285 | @Override |
| 286 | public void handleMessage(Message msg) { |
| 287 | if (msg.what == FLIP_MSG) { |
| 288 | if (mRunning) { |
| 289 | showNext(); |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | }; |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 294 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Called by an {@link android.appwidget.AppWidgetHost} to indicate that it will be |
| 297 | * automatically advancing the views of this {@link AdapterViewFlipper} by calling |
| 298 | * {@link AdapterViewFlipper#advance()} at some point in the future. This allows |
| 299 | * {@link AdapterViewFlipper} to prepare by no longer Advancing its children. |
| 300 | */ |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 301 | @Override |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 302 | public void fyiWillBeAdvancedByHostKThx() { |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 303 | mAdvancedByHost = true; |
| 304 | updateRunning(false); |
| 305 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 306 | |
| 307 | @Override |
| 308 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 309 | super.onInitializeAccessibilityEvent(event); |
| 310 | event.setClassName(AdapterViewFlipper.class.getName()); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 315 | super.onInitializeAccessibilityNodeInfo(info); |
| 316 | info.setClassName(AdapterViewFlipper.class.getName()); |
| 317 | } |
Adam Cohen | 3db4067 | 2010-07-19 22:41:57 -0700 | [diff] [blame] | 318 | } |