| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.Message; |
| 25 |
import android.util.AttributeSet; |
| 26 |
import android.util.Log; |
| 27 |
import android.view.RemotableViewMethod; |
| 28 |
import android.widget.RemoteViews.RemoteView; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@RemoteView |
| 39 |
public class AdapterViewFlipper extends AdapterViewAnimator { |
| 40 |
private static final String TAG = "ViewFlipper"; |
| 41 |
private static final boolean LOGD = false; |
| 42 |
|
| 43 |
private static final int DEFAULT_INTERVAL = 10000; |
| 44 |
|
| 45 |
private int mFlipInterval = DEFAULT_INTERVAL; |
| 46 |
private boolean mAutoStart = false; |
| 47 |
|
| 48 |
private boolean mRunning = false; |
| 49 |
private boolean mStarted = false; |
| 50 |
private boolean mVisible = false; |
| 51 |
private boolean mUserPresent = true; |
| 52 |
private boolean mAdvancedByHost = false; |
| 53 |
|
| 54 |
public AdapterViewFlipper(Context context) { |
| 55 |
super(context); |
| 56 |
} |
| 57 |
|
| 58 |
public AdapterViewFlipper(Context context, AttributeSet attrs) { |
| 59 |
this(context, attrs, 0); |
| 60 |
} |
| 61 |
|
| 62 |
public AdapterViewFlipper(Context context, AttributeSet attrs, int defStyleAttr) { |
| 63 |
this(context, attrs, defStyleAttr, 0); |
| 64 |
} |
| 65 |
|
| 66 |
public AdapterViewFlipper( |
| 67 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 68 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 69 |
|
| 70 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 71 |
com.android.internal.R.styleable.AdapterViewFlipper, defStyleAttr, defStyleRes); |
| 72 |
saveAttributeDataForStyleable(context, com.android.internal.R.styleable.AdapterViewFlipper, |
| 73 |
attrs, a, defStyleAttr, defStyleRes); |
| 74 |
mFlipInterval = a.getInt( |
| 75 |
com.android.internal.R.styleable.AdapterViewFlipper_flipInterval, DEFAULT_INTERVAL); |
| 76 |
mAutoStart = a.getBoolean( |
| 77 |
com.android.internal.R.styleable.AdapterViewFlipper_autoStart, false); |
| 78 |
|
| 79 |
|
| 80 |
mLoopViews = true; |
| 81 |
|
| 82 |
a.recycle(); |
| 83 |
} |
| 84 |
|
| 85 |
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 86 |
@Override |
| 87 |
public void onReceive(Context context, Intent intent) { |
| 88 |
final String action = intent.getAction(); |
| 89 |
if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 90 |
mUserPresent = false; |
| 91 |
updateRunning(); |
| 92 |
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
| 93 |
mUserPresent = true; |
| 94 |
updateRunning(false); |
| 95 |
} |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
@Override |
| 100 |
protected void onAttachedToWindow() { |
| 101 |
super.onAttachedToWindow(); |
| 102 |
|
| 103 |
|
| 104 |
final IntentFilter filter = new IntentFilter(); |
| 105 |
filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 106 |
filter.addAction(Intent.ACTION_USER_PRESENT); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), |
| 116 |
filter, null, getHandler()); |
| 117 |
|
| 118 |
|
| 119 |
if (mAutoStart) { |
| 120 |
|
| 121 |
startFlipping(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
protected void onDetachedFromWindow() { |
| 127 |
super.onDetachedFromWindow(); |
| 128 |
mVisible = false; |
| 129 |
|
| 130 |
getContext().unregisterReceiver(mReceiver); |
| 131 |
updateRunning(); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
protected void onWindowVisibilityChanged(int visibility) { |
| 136 |
super.onWindowVisibilityChanged(visibility); |
| 137 |
mVisible = (visibility == VISIBLE); |
| 138 |
updateRunning(false); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void setAdapter(Adapter adapter) { |
| 143 |
super.setAdapter(adapter); |
| 144 |
updateRunning(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public int getFlipInterval() { |
| 157 |
return mFlipInterval; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public void setFlipInterval(int flipInterval) { |
| 170 |
mFlipInterval = flipInterval; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void startFlipping() { |
| 177 |
mStarted = true; |
| 178 |
updateRunning(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public void stopFlipping() { |
| 185 |
mStarted = false; |
| 186 |
updateRunning(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@Override |
| 193 |
@RemotableViewMethod |
| 194 |
public void showNext() { |
| 195 |
|
| 196 |
|
| 197 |
if (mRunning) { |
| 198 |
removeCallbacks(mFlipRunnable); |
| 199 |
postDelayed(mFlipRunnable, mFlipInterval); |
| 200 |
} |
| 201 |
super.showNext(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
@RemotableViewMethod |
| 209 |
public void showPrevious() { |
| 210 |
|
| 211 |
|
| 212 |
if (mRunning) { |
| 213 |
removeCallbacks(mFlipRunnable); |
| 214 |
postDelayed(mFlipRunnable, mFlipInterval); |
| 215 |
} |
| 216 |
super.showPrevious(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private void updateRunning() { |
| 224 |
|
| 225 |
|
| 226 |
updateRunning(true); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
private void updateRunning(boolean flipNow) { |
| 238 |
boolean running = !mAdvancedByHost && mVisible && mStarted && mUserPresent |
| 239 |
&& mAdapter != null; |
| 240 |
if (running != mRunning) { |
| 241 |
if (running) { |
| 242 |
showOnly(mWhichChild, flipNow); |
| 243 |
postDelayed(mFlipRunnable, mFlipInterval); |
| 244 |
} else { |
| 245 |
removeCallbacks(mFlipRunnable); |
| 246 |
} |
| 247 |
mRunning = running; |
| 248 |
} |
| 249 |
if (LOGD) { |
| 250 |
Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted |
| 251 |
+ ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public boolean isFlipping() { |
| 259 |
return mStarted; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public void setAutoStart(boolean autoStart) { |
| 267 |
mAutoStart = autoStart; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public boolean isAutoStart() { |
| 275 |
return mAutoStart; |
| 276 |
} |
| 277 |
|
| 278 |
private final Runnable mFlipRunnable = new Runnable() { |
| 279 |
@Override |
| 280 |
public void run() { |
| 281 |
if (mRunning) { |
| 282 |
showNext(); |
| 283 |
} |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
@Override |
| 294 |
public void fyiWillBeAdvancedByHostKThx() { |
| 295 |
mAdvancedByHost = true; |
| 296 |
updateRunning(false); |
| 297 |
} |
| 298 |
|
| 299 |
@Override |
| 300 |
public CharSequence getAccessibilityClassName() { |
| 301 |
return AdapterViewFlipper.class.getName(); |
| 302 |
} |
| 303 |
} |