| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.IntRange; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.BroadcastReceiver; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.Intent; |
| 24 |
import android.content.IntentFilter; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.os.Build; |
| 27 |
import android.os.Message; |
| 28 |
import android.util.AttributeSet; |
| 29 |
import android.util.Log; |
| 30 |
import android.view.inspector.InspectableProperty; |
| 31 |
import android.widget.RemoteViews.RemoteView; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@RemoteView |
| 42 |
public class ViewFlipper extends ViewAnimator { |
| 43 |
private static final String TAG = "ViewFlipper"; |
| 44 |
private static final boolean LOGD = false; |
| 45 |
|
| 46 |
private static final int DEFAULT_INTERVAL = 3000; |
| 47 |
|
| 48 |
private int mFlipInterval = DEFAULT_INTERVAL; |
| 49 |
private boolean mAutoStart = false; |
| 50 |
|
| 51 |
private boolean mRunning = false; |
| 52 |
private boolean mStarted = false; |
| 53 |
private boolean mVisible = false; |
| 54 |
@UnsupportedAppUsage |
| 55 |
private boolean mUserPresent = true; |
| 56 |
|
| 57 |
public ViewFlipper(Context context) { |
| 58 |
super(context); |
| 59 |
} |
| 60 |
|
| 61 |
public ViewFlipper(Context context, AttributeSet attrs) { |
| 62 |
super(context, attrs); |
| 63 |
|
| 64 |
TypedArray a = context.obtainStyledAttributes(attrs, |
| 65 |
com.android.internal.R.styleable.ViewFlipper); |
| 66 |
mFlipInterval = a.getInt( |
| 67 |
com.android.internal.R.styleable.ViewFlipper_flipInterval, DEFAULT_INTERVAL); |
| 68 |
mAutoStart = a.getBoolean( |
| 69 |
com.android.internal.R.styleable.ViewFlipper_autoStart, false); |
| 70 |
a.recycle(); |
| 71 |
} |
| 72 |
|
| 73 |
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 74 |
@Override |
| 75 |
public void onReceive(Context context, Intent intent) { |
| 76 |
final String action = intent.getAction(); |
| 77 |
if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 78 |
mUserPresent = false; |
| 79 |
updateRunning(); |
| 80 |
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
| 81 |
mUserPresent = true; |
| 82 |
updateRunning(false); |
| 83 |
} |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
@Override |
| 88 |
protected void onAttachedToWindow() { |
| 89 |
super.onAttachedToWindow(); |
| 90 |
|
| 91 |
|
| 92 |
final IntentFilter filter = new IntentFilter(); |
| 93 |
filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 94 |
filter.addAction(Intent.ACTION_USER_PRESENT); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), |
| 104 |
filter, null, getHandler()); |
| 105 |
|
| 106 |
if (mAutoStart) { |
| 107 |
|
| 108 |
startFlipping(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
protected void onDetachedFromWindow() { |
| 114 |
super.onDetachedFromWindow(); |
| 115 |
mVisible = false; |
| 116 |
|
| 117 |
getContext().unregisterReceiver(mReceiver); |
| 118 |
updateRunning(); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
protected void onWindowVisibilityChanged(int visibility) { |
| 123 |
super.onWindowVisibilityChanged(visibility); |
| 124 |
mVisible = visibility == VISIBLE; |
| 125 |
updateRunning(false); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
@android.view.RemotableViewMethod |
| 135 |
public void setFlipInterval(@IntRange(from = 0) int milliseconds) { |
| 136 |
mFlipInterval = milliseconds; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
@InspectableProperty |
| 145 |
@IntRange(from = 0) |
| 146 |
public int getFlipInterval() { |
| 147 |
return mFlipInterval; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public void startFlipping() { |
| 154 |
mStarted = true; |
| 155 |
updateRunning(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public void stopFlipping() { |
| 162 |
mStarted = false; |
| 163 |
updateRunning(); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public CharSequence getAccessibilityClassName() { |
| 168 |
return ViewFlipper.class.getName(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private void updateRunning() { |
| 176 |
updateRunning(true); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 188 |
private void updateRunning(boolean flipNow) { |
| 189 |
boolean running = mVisible && mStarted && mUserPresent; |
| 190 |
if (running != mRunning) { |
| 191 |
if (running) { |
| 192 |
showOnly(mWhichChild, flipNow); |
| 193 |
postDelayed(mFlipRunnable, mFlipInterval); |
| 194 |
} else { |
| 195 |
removeCallbacks(mFlipRunnable); |
| 196 |
} |
| 197 |
mRunning = running; |
| 198 |
} |
| 199 |
if (LOGD) { |
| 200 |
Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted |
| 201 |
+ ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@InspectableProperty(hasAttributeId = false) |
| 209 |
public boolean isFlipping() { |
| 210 |
return mStarted; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public void setAutoStart(boolean autoStart) { |
| 218 |
mAutoStart = autoStart; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@InspectableProperty |
| 226 |
public boolean isAutoStart() { |
| 227 |
return mAutoStart; |
| 228 |
} |
| 229 |
|
| 230 |
private final Runnable mFlipRunnable = new Runnable() { |
| 231 |
@Override |
| 232 |
public void run() { |
| 233 |
if (mRunning) { |
| 234 |
showNext(); |
| 235 |
postDelayed(mFlipRunnable, mFlipInterval); |
| 236 |
} |
| 237 |
} |
| 238 |
}; |
| 239 |
} |