The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 25 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 28 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.widget.RemoteViews.RemoteView; |
| 30 | |
| 31 | /** |
| 32 | * Simple {@link ViewAnimator} that will animate between two or more views |
| 33 | * that have been added to it. Only one child is shown at a time. If |
| 34 | * requested, can automatically flip between each child at a regular interval. |
| 35 | * |
| 36 | * @attr ref android.R.styleable#ViewFlipper_flipInterval |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 37 | * @attr ref android.R.styleable#ViewFlipper_autoStart |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | */ |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 39 | @RemoteView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | public class ViewFlipper extends ViewAnimator { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 41 | private static final String TAG = "ViewFlipper"; |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 42 | private static final boolean LOGD = false; |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 43 | |
| 44 | private static final int DEFAULT_INTERVAL = 3000; |
| 45 | |
| 46 | private int mFlipInterval = DEFAULT_INTERVAL; |
| 47 | private boolean mAutoStart = false; |
| 48 | |
| 49 | private boolean mRunning = false; |
| 50 | private boolean mStarted = false; |
| 51 | private boolean mVisible = false; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 52 | @UnsupportedAppUsage |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 53 | private boolean mUserPresent = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
| 55 | public ViewFlipper(Context context) { |
| 56 | super(context); |
| 57 | } |
| 58 | |
| 59 | public ViewFlipper(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
| 61 | |
| 62 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 63 | com.android.internal.R.styleable.ViewFlipper); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 64 | mFlipInterval = a.getInt( |
| 65 | com.android.internal.R.styleable.ViewFlipper_flipInterval, DEFAULT_INTERVAL); |
| 66 | mAutoStart = a.getBoolean( |
| 67 | com.android.internal.R.styleable.ViewFlipper_autoStart, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | a.recycle(); |
| 69 | } |
| 70 | |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 71 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 72 | @Override |
| 73 | public void onReceive(Context context, Intent intent) { |
| 74 | final String action = intent.getAction(); |
| 75 | if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 76 | mUserPresent = false; |
| 77 | updateRunning(); |
| 78 | } else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
| 79 | mUserPresent = true; |
Mason Tang | f70036b | 2010-06-14 17:47:24 -0700 | [diff] [blame] | 80 | updateRunning(false); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | @Override |
| 86 | protected void onAttachedToWindow() { |
| 87 | super.onAttachedToWindow(); |
| 88 | |
| 89 | // Listen for broadcasts related to user-presence |
| 90 | final IntentFilter filter = new IntentFilter(); |
| 91 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 92 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Svetoslav | 39999cf | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 93 | |
| 94 | // OK, this is gross but needed. This class is supported by the |
| 95 | // remote views machanism and as a part of that the remote views |
| 96 | // can be inflated by a context for another user without the app |
| 97 | // having interact users permission - just for loading resources. |
| 98 | // For exmaple, when adding widgets from a user profile to the |
| 99 | // home screen. Therefore, we register the receiver as the current |
| 100 | // user not the one the context is for. |
| 101 | getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 102 | filter, null, getHandler()); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 103 | |
| 104 | if (mAutoStart) { |
| 105 | // Automatically start when requested |
| 106 | startFlipping(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onDetachedFromWindow() { |
| 112 | super.onDetachedFromWindow(); |
| 113 | mVisible = false; |
| 114 | |
| 115 | getContext().unregisterReceiver(mReceiver); |
| 116 | updateRunning(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | protected void onWindowVisibilityChanged(int visibility) { |
| 121 | super.onWindowVisibilityChanged(visibility); |
| 122 | mVisible = visibility == VISIBLE; |
Mason Tang | f70036b | 2010-06-14 17:47:24 -0700 | [diff] [blame] | 123 | updateRunning(false); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 124 | } |
| 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | /** |
| 127 | * How long to wait before flipping to the next view |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 128 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | * @param milliseconds |
| 130 | * time in milliseconds |
| 131 | */ |
| 132 | @android.view.RemotableViewMethod |
| 133 | public void setFlipInterval(int milliseconds) { |
| 134 | mFlipInterval = milliseconds; |
| 135 | } |
| 136 | |
| 137 | /** |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 138 | * Get the delay before flipping to the next view. |
| 139 | * |
| 140 | * @return delay time in milliseconds |
| 141 | */ |
| 142 | @InspectableProperty |
| 143 | public int getFlipInterval() { |
| 144 | return mFlipInterval; |
| 145 | } |
| 146 | |
| 147 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | * Start a timer to cycle through child views |
| 149 | */ |
| 150 | public void startFlipping() { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 151 | mStarted = true; |
| 152 | updateRunning(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * No more flips |
| 157 | */ |
| 158 | public void stopFlipping() { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 159 | mStarted = false; |
| 160 | updateRunning(); |
| 161 | } |
| 162 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 163 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 164 | public CharSequence getAccessibilityClassName() { |
| 165 | return ViewFlipper.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Internal method to start or stop dispatching flip {@link Message} based |
| 170 | * on {@link #mRunning} and {@link #mVisible} state. |
| 171 | */ |
| 172 | private void updateRunning() { |
Mason Tang | f70036b | 2010-06-14 17:47:24 -0700 | [diff] [blame] | 173 | updateRunning(true); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Internal method to start or stop dispatching flip {@link Message} based |
| 178 | * on {@link #mRunning} and {@link #mVisible} state. |
| 179 | * |
| 180 | * @param flipNow Determines whether or not to execute the animation now, in |
| 181 | * addition to queuing future flips. If omitted, defaults to |
| 182 | * true. |
| 183 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 184 | @UnsupportedAppUsage |
Mason Tang | f70036b | 2010-06-14 17:47:24 -0700 | [diff] [blame] | 185 | private void updateRunning(boolean flipNow) { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 186 | boolean running = mVisible && mStarted && mUserPresent; |
| 187 | if (running != mRunning) { |
| 188 | if (running) { |
Mason Tang | f70036b | 2010-06-14 17:47:24 -0700 | [diff] [blame] | 189 | showOnly(mWhichChild, flipNow); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 190 | postDelayed(mFlipRunnable, mFlipInterval); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 191 | } else { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 192 | removeCallbacks(mFlipRunnable); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 193 | } |
| 194 | mRunning = running; |
| 195 | } |
| 196 | if (LOGD) { |
| 197 | Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted |
| 198 | + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning); |
| 199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns true if the child views are flipping. |
| 204 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 205 | @InspectableProperty(hasAttributeId = false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | public boolean isFlipping() { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 207 | return mStarted; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Set if this view automatically calls {@link #startFlipping()} when it |
| 212 | * becomes attached to a window. |
| 213 | */ |
| 214 | public void setAutoStart(boolean autoStart) { |
| 215 | mAutoStart = autoStart; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns true if this view automatically calls {@link #startFlipping()} |
| 220 | * when it becomes attached to a window. |
| 221 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 222 | @InspectableProperty |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 223 | public boolean isAutoStart() { |
| 224 | return mAutoStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 227 | private final Runnable mFlipRunnable = new Runnable() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | @Override |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 229 | public void run() { |
| 230 | if (mRunning) { |
| 231 | showNext(); |
| 232 | postDelayed(mFlipRunnable, mFlipInterval); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | }; |
| 236 | } |