| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import static android.widget.RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID; |
| 20 |
import static android.widget.RemoteViews.EXTRA_REMOTEADAPTER_ON_LIGHT_BACKGROUND; |
| 21 |
|
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.WorkerThread; |
| 24 |
import android.app.IServiceConnection; |
| 25 |
import android.appwidget.AppWidgetHostView; |
| 26 |
import android.appwidget.AppWidgetManager; |
| 27 |
import android.compat.annotation.UnsupportedAppUsage; |
| 28 |
import android.content.ComponentName; |
| 29 |
import android.content.Context; |
| 30 |
import android.content.Intent; |
| 31 |
import android.content.ServiceConnection; |
| 32 |
import android.content.pm.ActivityInfo; |
| 33 |
import android.content.pm.ApplicationInfo; |
| 34 |
import android.content.res.Configuration; |
| 35 |
import android.os.Build; |
| 36 |
import android.os.Handler; |
| 37 |
import android.os.HandlerThread; |
| 38 |
import android.os.IBinder; |
| 39 |
import android.os.Looper; |
| 40 |
import android.os.Message; |
| 41 |
import android.os.RemoteException; |
| 42 |
import android.util.Log; |
| 43 |
import android.util.SparseArray; |
| 44 |
import android.util.SparseBooleanArray; |
| 45 |
import android.util.SparseIntArray; |
| 46 |
import android.view.LayoutInflater; |
| 47 |
import android.view.View; |
| 48 |
import android.view.View.MeasureSpec; |
| 49 |
import android.view.ViewGroup; |
| 50 |
import android.widget.RemoteViews.InteractionHandler; |
| 51 |
|
| 52 |
import com.android.internal.widget.IRemoteViewsFactory; |
| 53 |
|
| 54 |
import java.lang.ref.WeakReference; |
| 55 |
import java.util.Arrays; |
| 56 |
import java.util.HashMap; |
| 57 |
import java.util.LinkedList; |
| 58 |
import java.util.concurrent.Executor; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback { |
| 76 |
|
| 77 |
private static final String TAG = "RemoteViewsAdapter"; |
| 78 |
|
| 79 |
|
| 80 |
private static final int DEFAULT_CACHE_SIZE = 40; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private static final int UNBIND_SERVICE_DELAY = 5000; |
| 85 |
|
| 86 |
|
| 87 |
private static final int DEFAULT_LOADING_VIEW_HEIGHT = 50; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private static final HashMap<RemoteViewsCacheKey, FixedSizeRemoteViewsCache> |
| 92 |
sCachedRemoteViewsCaches = new HashMap<>(); |
| 93 |
private static final HashMap<RemoteViewsCacheKey, Runnable> |
| 94 |
sRemoteViewsCacheRemoveRunnables = new HashMap<>(); |
| 95 |
|
| 96 |
private static HandlerThread sCacheRemovalThread; |
| 97 |
private static Handler sCacheRemovalQueue; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private static final int REMOTE_VIEWS_CACHE_DURATION = 5000; |
| 103 |
|
| 104 |
private final Context mContext; |
| 105 |
private final Intent mIntent; |
| 106 |
private final int mAppWidgetId; |
| 107 |
private final boolean mOnLightBackground; |
| 108 |
private final Executor mAsyncViewLoadExecutor; |
| 109 |
|
| 110 |
private InteractionHandler mRemoteViewsInteractionHandler; |
| 111 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 112 |
private final FixedSizeRemoteViewsCache mCache; |
| 113 |
private int mVisibleWindowLowerBound; |
| 114 |
private int mVisibleWindowUpperBound; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private RemoteViewsFrameLayoutRefSet mRequestedViews; |
| 119 |
|
| 120 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 121 |
private final HandlerThread mWorkerThread; |
| 122 |
|
| 123 |
private final Handler mMainHandler; |
| 124 |
private final RemoteServiceHandler mServiceHandler; |
| 125 |
private final RemoteAdapterConnectionCallback mCallback; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private boolean mDataReady = false; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private ApplicationInfo mLastRemoteViewAppInfo; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public interface RemoteAdapterConnectionCallback { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
boolean onRemoteAdapterConnected(); |
| 146 |
|
| 147 |
void onRemoteAdapterDisconnected(); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void deferNotifyDataSetChanged(); |
| 154 |
|
| 155 |
void setRemoteViewsAdapter(Intent intent, boolean isAsync); |
| 156 |
} |
| 157 |
|
| 158 |
public static class AsyncRemoteAdapterAction implements Runnable { |
| 159 |
|
| 160 |
private final RemoteAdapterConnectionCallback mCallback; |
| 161 |
private final Intent mIntent; |
| 162 |
|
| 163 |
public AsyncRemoteAdapterAction(RemoteAdapterConnectionCallback callback, Intent intent) { |
| 164 |
mCallback = callback; |
| 165 |
mIntent = intent; |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void run() { |
| 170 |
mCallback.setRemoteViewsAdapter(mIntent, true); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
static final int MSG_REQUEST_BIND = 1; |
| 175 |
static final int MSG_NOTIFY_DATA_SET_CHANGED = 2; |
| 176 |
static final int MSG_LOAD_NEXT_ITEM = 3; |
| 177 |
static final int MSG_UNBIND_SERVICE = 4; |
| 178 |
|
| 179 |
private static final int MSG_MAIN_HANDLER_COMMIT_METADATA = 1; |
| 180 |
private static final int MSG_MAIN_HANDLER_SUPER_NOTIFY_DATA_SET_CHANGED = 2; |
| 181 |
private static final int MSG_MAIN_HANDLER_REMOTE_ADAPTER_CONNECTED = 3; |
| 182 |
private static final int MSG_MAIN_HANDLER_REMOTE_ADAPTER_DISCONNECTED = 4; |
| 183 |
private static final int MSG_MAIN_HANDLER_REMOTE_VIEWS_LOADED = 5; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private static class RemoteServiceHandler extends Handler implements ServiceConnection { |
| 189 |
|
| 190 |
private final WeakReference<RemoteViewsAdapter> mAdapter; |
| 191 |
private final Context mContext; |
| 192 |
|
| 193 |
private IRemoteViewsFactory mRemoteViewsFactory; |
| 194 |
|
| 195 |
|
| 196 |
private boolean mNotifyDataSetChangedPending = false; |
| 197 |
private boolean mBindRequested = false; |
| 198 |
|
| 199 |
RemoteServiceHandler(Looper workerLooper, RemoteViewsAdapter adapter, Context context) { |
| 200 |
super(workerLooper); |
| 201 |
mAdapter = new WeakReference<>(adapter); |
| 202 |
mContext = context; |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void onServiceConnected(ComponentName name, IBinder service) { |
| 207 |
|
| 208 |
mRemoteViewsFactory = IRemoteViewsFactory.Stub.asInterface(service); |
| 209 |
enqueueDeferredUnbindServiceMessage(); |
| 210 |
|
| 211 |
RemoteViewsAdapter adapter = mAdapter.get(); |
| 212 |
if (adapter == null) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
if (mNotifyDataSetChangedPending) { |
| 217 |
mNotifyDataSetChangedPending = false; |
| 218 |
Message msg = Message.obtain(this, MSG_NOTIFY_DATA_SET_CHANGED); |
| 219 |
handleMessage(msg); |
| 220 |
msg.recycle(); |
| 221 |
} else { |
| 222 |
if (!sendNotifyDataSetChange(false)) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
adapter.updateTemporaryMetaData(mRemoteViewsFactory); |
| 229 |
adapter.mMainHandler.sendEmptyMessage(MSG_MAIN_HANDLER_COMMIT_METADATA); |
| 230 |
adapter.mMainHandler.sendEmptyMessage(MSG_MAIN_HANDLER_REMOTE_ADAPTER_CONNECTED); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public void onServiceDisconnected(ComponentName name) { |
| 236 |
mRemoteViewsFactory = null; |
| 237 |
RemoteViewsAdapter adapter = mAdapter.get(); |
| 238 |
if (adapter != null) { |
| 239 |
adapter.mMainHandler.sendEmptyMessage(MSG_MAIN_HANDLER_REMOTE_ADAPTER_DISCONNECTED); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public void handleMessage(Message msg) { |
| 245 |
RemoteViewsAdapter adapter = mAdapter.get(); |
| 246 |
|
| 247 |
switch (msg.what) { |
| 248 |
case MSG_REQUEST_BIND: { |
| 249 |
if (adapter == null || mRemoteViewsFactory != null) { |
| 250 |
enqueueDeferredUnbindServiceMessage(); |
| 251 |
} |
| 252 |
if (mBindRequested) { |
| 253 |
return; |
| 254 |
} |
| 255 |
int flags = Context.BIND_AUTO_CREATE |
| 256 |
| Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE; |
| 257 |
final IServiceConnection sd = mContext.getServiceDispatcher(this, this, flags); |
| 258 |
Intent intent = (Intent) msg.obj; |
| 259 |
int appWidgetId = msg.arg1; |
| 260 |
try { |
| 261 |
mBindRequested = AppWidgetManager.getInstance(mContext) |
| 262 |
.bindRemoteViewsService(mContext, appWidgetId, intent, sd, flags); |
| 263 |
} catch (Exception e) { |
| 264 |
Log.e(TAG, "Failed to bind remoteViewsService: " + e.getMessage()); |
| 265 |
} |
| 266 |
return; |
| 267 |
} |
| 268 |
case MSG_NOTIFY_DATA_SET_CHANGED: { |
| 269 |
enqueueDeferredUnbindServiceMessage(); |
| 270 |
if (adapter == null) { |
| 271 |
return; |
| 272 |
} |
| 273 |
if (mRemoteViewsFactory == null) { |
| 274 |
mNotifyDataSetChangedPending = true; |
| 275 |
adapter.requestBindService(); |
| 276 |
return; |
| 277 |
} |
| 278 |
if (!sendNotifyDataSetChange(true)) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
synchronized (adapter.mCache) { |
| 284 |
adapter.mCache.reset(); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
adapter.updateTemporaryMetaData(mRemoteViewsFactory); |
| 289 |
int newCount; |
| 290 |
int[] visibleWindow; |
| 291 |
synchronized (adapter.mCache.getTemporaryMetaData()) { |
| 292 |
newCount = adapter.mCache.getTemporaryMetaData().count; |
| 293 |
visibleWindow = adapter.getVisibleWindow(newCount); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
for (int position : visibleWindow) { |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
if (position < newCount) { |
| 304 |
adapter.updateRemoteViews(mRemoteViewsFactory, position, false); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
adapter.mMainHandler.sendEmptyMessage(MSG_MAIN_HANDLER_COMMIT_METADATA); |
| 310 |
adapter.mMainHandler.sendEmptyMessage( |
| 311 |
MSG_MAIN_HANDLER_SUPER_NOTIFY_DATA_SET_CHANGED); |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
case MSG_LOAD_NEXT_ITEM: { |
| 316 |
if (adapter == null || mRemoteViewsFactory == null) { |
| 317 |
return; |
| 318 |
} |
| 319 |
removeMessages(MSG_UNBIND_SERVICE); |
| 320 |
|
| 321 |
final int position = adapter.mCache.getNextIndexToLoad(); |
| 322 |
if (position > -1) { |
| 323 |
|
| 324 |
adapter.updateRemoteViews(mRemoteViewsFactory, position, true); |
| 325 |
|
| 326 |
|
| 327 |
sendEmptyMessage(MSG_LOAD_NEXT_ITEM); |
| 328 |
} else { |
| 329 |
|
| 330 |
enqueueDeferredUnbindServiceMessage(); |
| 331 |
} |
| 332 |
return; |
| 333 |
} |
| 334 |
case MSG_UNBIND_SERVICE: { |
| 335 |
unbindNow(); |
| 336 |
return; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
protected void unbindNow() { |
| 342 |
if (mBindRequested) { |
| 343 |
mBindRequested = false; |
| 344 |
mContext.unbindService(this); |
| 345 |
} |
| 346 |
mRemoteViewsFactory = null; |
| 347 |
} |
| 348 |
|
| 349 |
private boolean sendNotifyDataSetChange(boolean always) { |
| 350 |
try { |
| 351 |
if (always || !mRemoteViewsFactory.isCreated()) { |
| 352 |
mRemoteViewsFactory.onDataSetChanged(); |
| 353 |
} |
| 354 |
return true; |
| 355 |
} catch (RemoteException | RuntimeException e) { |
| 356 |
Log.e(TAG, "Error in updateNotifyDataSetChanged(): " + e.getMessage()); |
| 357 |
return false; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
private void enqueueDeferredUnbindServiceMessage() { |
| 362 |
removeMessages(MSG_UNBIND_SERVICE); |
| 363 |
sendEmptyMessageDelayed(MSG_UNBIND_SERVICE, UNBIND_SERVICE_DELAY); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static class RemoteViewsFrameLayout extends AppWidgetHostView { |
| 372 |
private final FixedSizeRemoteViewsCache mCache; |
| 373 |
|
| 374 |
public int cacheIndex = -1; |
| 375 |
|
| 376 |
public RemoteViewsFrameLayout(Context context, FixedSizeRemoteViewsCache cache) { |
| 377 |
super(context); |
| 378 |
mCache = cache; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public void onRemoteViewsLoaded(RemoteViews view, InteractionHandler handler, |
| 390 |
boolean forceApplyAsync) { |
| 391 |
setInteractionHandler(handler); |
| 392 |
applyRemoteViews(view, forceApplyAsync || ((view != null) && view.prefersAsyncApply())); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
@Override |
| 400 |
protected View getDefaultView() { |
| 401 |
int viewHeight = mCache.getMetaData().getLoadingTemplate(getContext()).defaultHeight; |
| 402 |
|
| 403 |
TextView loadingTextView = (TextView) LayoutInflater.from(getContext()).inflate( |
| 404 |
com.android.internal.R.layout.remote_views_adapter_default_loading_view, |
| 405 |
this, false); |
| 406 |
loadingTextView.setHeight(viewHeight); |
| 407 |
return loadingTextView; |
| 408 |
} |
| 409 |
|
| 410 |
@Override |
| 411 |
protected Context getRemoteContextEnsuringCorrectCachedApkPath() { |
| 412 |
return null; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
protected View getErrorView() { |
| 417 |
|
| 418 |
return getDefaultView(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
private class RemoteViewsFrameLayoutRefSet |
| 427 |
extends SparseArray<LinkedList<RemoteViewsFrameLayout>> { |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public void add(int position, RemoteViewsFrameLayout layout) { |
| 433 |
LinkedList<RemoteViewsFrameLayout> refs = get(position); |
| 434 |
|
| 435 |
|
| 436 |
if (refs == null) { |
| 437 |
refs = new LinkedList<>(); |
| 438 |
put(position, refs); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
layout.cacheIndex = position; |
| 443 |
refs.add(layout); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public void notifyOnRemoteViewsLoaded(int position, RemoteViews view) { |
| 451 |
if (view == null) return; |
| 452 |
|
| 453 |
|
| 454 |
final LinkedList<RemoteViewsFrameLayout> refs = removeReturnOld(position); |
| 455 |
if (refs != null) { |
| 456 |
|
| 457 |
for (final RemoteViewsFrameLayout ref : refs) { |
| 458 |
ref.onRemoteViewsLoaded(view, mRemoteViewsInteractionHandler, true); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public void removeView(RemoteViewsFrameLayout rvfl) { |
| 467 |
if (rvfl.cacheIndex < 0) { |
| 468 |
return; |
| 469 |
} |
| 470 |
final LinkedList<RemoteViewsFrameLayout> refs = get(rvfl.cacheIndex); |
| 471 |
if (refs != null) { |
| 472 |
refs.remove(rvfl); |
| 473 |
} |
| 474 |
rvfl.cacheIndex = -1; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
private static class RemoteViewsMetaData { |
| 482 |
int count; |
| 483 |
int viewTypeCount; |
| 484 |
boolean hasStableIds; |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
LoadingViewTemplate loadingTemplate; |
| 490 |
|
| 491 |
|
| 492 |
private final SparseIntArray mTypeIdIndexMap = new SparseIntArray(); |
| 493 |
|
| 494 |
public RemoteViewsMetaData() { |
| 495 |
reset(); |
| 496 |
} |
| 497 |
|
| 498 |
public void set(RemoteViewsMetaData d) { |
| 499 |
synchronized (d) { |
| 500 |
count = d.count; |
| 501 |
viewTypeCount = d.viewTypeCount; |
| 502 |
hasStableIds = d.hasStableIds; |
| 503 |
loadingTemplate = d.loadingTemplate; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
public void reset() { |
| 508 |
count = 0; |
| 509 |
|
| 510 |
|
| 511 |
viewTypeCount = 1; |
| 512 |
hasStableIds = true; |
| 513 |
loadingTemplate = null; |
| 514 |
mTypeIdIndexMap.clear(); |
| 515 |
} |
| 516 |
|
| 517 |
public int getMappedViewType(int typeId) { |
| 518 |
int mappedTypeId = mTypeIdIndexMap.get(typeId, -1); |
| 519 |
if (mappedTypeId == -1) { |
| 520 |
|
| 521 |
mappedTypeId = mTypeIdIndexMap.size() + 1; |
| 522 |
mTypeIdIndexMap.put(typeId, mappedTypeId); |
| 523 |
} |
| 524 |
return mappedTypeId; |
| 525 |
} |
| 526 |
|
| 527 |
public boolean isViewTypeInRange(int typeId) { |
| 528 |
int mappedType = getMappedViewType(typeId); |
| 529 |
return (mappedType < viewTypeCount); |
| 530 |
} |
| 531 |
|
| 532 |
public synchronized LoadingViewTemplate getLoadingTemplate(Context context) { |
| 533 |
if (loadingTemplate == null) { |
| 534 |
loadingTemplate = new LoadingViewTemplate(null, context); |
| 535 |
} |
| 536 |
return loadingTemplate; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
private static class RemoteViewsIndexMetaData { |
| 544 |
int typeId; |
| 545 |
long itemId; |
| 546 |
|
| 547 |
public RemoteViewsIndexMetaData(RemoteViews v, long itemId) { |
| 548 |
set(v, itemId); |
| 549 |
} |
| 550 |
|
| 551 |
public void set(RemoteViews v, long id) { |
| 552 |
itemId = id; |
| 553 |
if (v != null) { |
| 554 |
typeId = v.getLayoutId(); |
| 555 |
} else { |
| 556 |
typeId = 0; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
private static final int CACHE_RESET_CONFIG_FLAGS = ActivityInfo.CONFIG_FONT_SCALE |
| 565 |
| ActivityInfo.CONFIG_UI_MODE | ActivityInfo.CONFIG_DENSITY |
| 566 |
| ActivityInfo.CONFIG_ASSETS_PATHS; |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private static class FixedSizeRemoteViewsCache { |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
private final RemoteViewsMetaData mMetaData = new RemoteViewsMetaData(); |
| 577 |
private final RemoteViewsMetaData mTemporaryMetaData = new RemoteViewsMetaData(); |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
private final SparseArray<RemoteViewsIndexMetaData> mIndexMetaData = new SparseArray<>(); |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
private final SparseArray<RemoteViews> mIndexRemoteViews = new SparseArray<>(); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
private final SparseBooleanArray mIndicesToLoad = new SparseBooleanArray(); |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
private int mLastRequestedIndex; |
| 599 |
|
| 600 |
|
| 601 |
private int mPreloadLowerBound; |
| 602 |
private int mPreloadUpperBound; |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
private final int mMaxCount; |
| 610 |
private final int mMaxCountSlack; |
| 611 |
private static final float sMaxCountSlackPercent = 0.75f; |
| 612 |
private static final int sMaxMemoryLimitInBytes = 2 * 1024 * 1024; |
| 613 |
|
| 614 |
|
| 615 |
private final Configuration mConfiguration; |
| 616 |
|
| 617 |
FixedSizeRemoteViewsCache(int maxCacheSize, Configuration configuration) { |
| 618 |
mMaxCount = maxCacheSize; |
| 619 |
mMaxCountSlack = Math.round(sMaxCountSlackPercent * (mMaxCount / 2)); |
| 620 |
mPreloadLowerBound = 0; |
| 621 |
mPreloadUpperBound = -1; |
| 622 |
mLastRequestedIndex = -1; |
| 623 |
|
| 624 |
mConfiguration = new Configuration(configuration); |
| 625 |
} |
| 626 |
|
| 627 |
public void insert(int position, RemoteViews v, long itemId, int[] visibleWindow) { |
| 628 |
|
| 629 |
if (mIndexRemoteViews.size() >= mMaxCount) { |
| 630 |
mIndexRemoteViews.remove(getFarthestPositionFrom(position, visibleWindow)); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
int pruneFromPosition = (mLastRequestedIndex > -1) ? mLastRequestedIndex : position; |
| 635 |
while (getRemoteViewsBitmapMemoryUsage() >= sMaxMemoryLimitInBytes) { |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
int trimIndex = getFarthestPositionFrom(pruneFromPosition, visibleWindow); |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
if (trimIndex < 0) { |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
mIndexRemoteViews.remove(trimIndex); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
final RemoteViewsIndexMetaData metaData = mIndexMetaData.get(position); |
| 654 |
if (metaData != null) { |
| 655 |
metaData.set(v, itemId); |
| 656 |
} else { |
| 657 |
mIndexMetaData.put(position, new RemoteViewsIndexMetaData(v, itemId)); |
| 658 |
} |
| 659 |
mIndexRemoteViews.put(position, v); |
| 660 |
} |
| 661 |
|
| 662 |
public RemoteViewsMetaData getMetaData() { |
| 663 |
return mMetaData; |
| 664 |
} |
| 665 |
public RemoteViewsMetaData getTemporaryMetaData() { |
| 666 |
return mTemporaryMetaData; |
| 667 |
} |
| 668 |
public RemoteViews getRemoteViewsAt(int position) { |
| 669 |
return mIndexRemoteViews.get(position); |
| 670 |
} |
| 671 |
public RemoteViewsIndexMetaData getMetaDataAt(int position) { |
| 672 |
return mIndexMetaData.get(position); |
| 673 |
} |
| 674 |
|
| 675 |
public void commitTemporaryMetaData() { |
| 676 |
synchronized (mTemporaryMetaData) { |
| 677 |
synchronized (mMetaData) { |
| 678 |
mMetaData.set(mTemporaryMetaData); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
private int getRemoteViewsBitmapMemoryUsage() { |
| 684 |
|
| 685 |
int mem = 0; |
| 686 |
for (int i = mIndexRemoteViews.size() - 1; i >= 0; i--) { |
| 687 |
final RemoteViews v = mIndexRemoteViews.valueAt(i); |
| 688 |
if (v != null) { |
| 689 |
mem += v.estimateMemoryUsage(); |
| 690 |
} |
| 691 |
} |
| 692 |
return mem; |
| 693 |
} |
| 694 |
|
| 695 |
private int getFarthestPositionFrom(int pos, int[] visibleWindow) { |
| 696 |
|
| 697 |
int maxDist = 0; |
| 698 |
int maxDistIndex = -1; |
| 699 |
int maxDistNotVisible = 0; |
| 700 |
int maxDistIndexNotVisible = -1; |
| 701 |
for (int i = mIndexRemoteViews.size() - 1; i >= 0; i--) { |
| 702 |
int index = mIndexRemoteViews.keyAt(i); |
| 703 |
int dist = Math.abs(index-pos); |
| 704 |
if (dist > maxDistNotVisible && Arrays.binarySearch(visibleWindow, index) < 0) { |
| 705 |
|
| 706 |
|
| 707 |
maxDistIndexNotVisible = index; |
| 708 |
maxDistNotVisible = dist; |
| 709 |
} |
| 710 |
if (dist >= maxDist) { |
| 711 |
|
| 712 |
|
| 713 |
maxDistIndex = index; |
| 714 |
maxDist = dist; |
| 715 |
} |
| 716 |
} |
| 717 |
if (maxDistIndexNotVisible > -1) { |
| 718 |
return maxDistIndexNotVisible; |
| 719 |
} |
| 720 |
return maxDistIndex; |
| 721 |
} |
| 722 |
|
| 723 |
public void queueRequestedPositionToLoad(int position) { |
| 724 |
mLastRequestedIndex = position; |
| 725 |
synchronized (mIndicesToLoad) { |
| 726 |
mIndicesToLoad.put(position, true); |
| 727 |
} |
| 728 |
} |
| 729 |
public boolean queuePositionsToBePreloadedFromRequestedPosition(int position) { |
| 730 |
|
| 731 |
if (mPreloadLowerBound <= position && position <= mPreloadUpperBound) { |
| 732 |
int center = (mPreloadUpperBound + mPreloadLowerBound) / 2; |
| 733 |
if (Math.abs(position - center) < mMaxCountSlack) { |
| 734 |
return false; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
int count; |
| 739 |
synchronized (mMetaData) { |
| 740 |
count = mMetaData.count; |
| 741 |
} |
| 742 |
synchronized (mIndicesToLoad) { |
| 743 |
|
| 744 |
for (int i = mIndicesToLoad.size() - 1; i >= 0; i--) { |
| 745 |
if (!mIndicesToLoad.valueAt(i)) { |
| 746 |
mIndicesToLoad.removeAt(i); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
int halfMaxCount = mMaxCount / 2; |
| 752 |
mPreloadLowerBound = position - halfMaxCount; |
| 753 |
mPreloadUpperBound = position + halfMaxCount; |
| 754 |
int effectiveLowerBound = Math.max(0, mPreloadLowerBound); |
| 755 |
int effectiveUpperBound = Math.min(mPreloadUpperBound, count - 1); |
| 756 |
for (int i = effectiveLowerBound; i <= effectiveUpperBound; ++i) { |
| 757 |
if (mIndexRemoteViews.indexOfKey(i) < 0 && !mIndicesToLoad.get(i)) { |
| 758 |
|
| 759 |
mIndicesToLoad.put(i, false); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
return true; |
| 764 |
} |
| 765 |
|
| 766 |
public int getNextIndexToLoad() { |
| 767 |
|
| 768 |
|
| 769 |
synchronized (mIndicesToLoad) { |
| 770 |
|
| 771 |
int index = mIndicesToLoad.indexOfValue(true); |
| 772 |
if (index < 0) { |
| 773 |
|
| 774 |
index = mIndicesToLoad.indexOfValue(false); |
| 775 |
} |
| 776 |
if (index < 0) { |
| 777 |
return -1; |
| 778 |
} else { |
| 779 |
int key = mIndicesToLoad.keyAt(index); |
| 780 |
mIndicesToLoad.removeAt(index); |
| 781 |
return key; |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
public boolean containsRemoteViewAt(int position) { |
| 787 |
return mIndexRemoteViews.indexOfKey(position) >= 0; |
| 788 |
} |
| 789 |
public boolean containsMetaDataAt(int position) { |
| 790 |
return mIndexMetaData.indexOfKey(position) >= 0; |
| 791 |
} |
| 792 |
|
| 793 |
public void reset() { |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
mPreloadLowerBound = 0; |
| 799 |
mPreloadUpperBound = -1; |
| 800 |
mLastRequestedIndex = -1; |
| 801 |
mIndexRemoteViews.clear(); |
| 802 |
mIndexMetaData.clear(); |
| 803 |
synchronized (mIndicesToLoad) { |
| 804 |
mIndicesToLoad.clear(); |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
static class RemoteViewsCacheKey { |
| 810 |
final Intent.FilterComparison filter; |
| 811 |
final int widgetId; |
| 812 |
|
| 813 |
RemoteViewsCacheKey(Intent.FilterComparison filter, int widgetId) { |
| 814 |
this.filter = filter; |
| 815 |
this.widgetId = widgetId; |
| 816 |
} |
| 817 |
|
| 818 |
@Override |
| 819 |
public boolean equals(@Nullable Object o) { |
| 820 |
if (!(o instanceof RemoteViewsCacheKey)) { |
| 821 |
return false; |
| 822 |
} |
| 823 |
RemoteViewsCacheKey other = (RemoteViewsCacheKey) o; |
| 824 |
return other.filter.equals(filter) && other.widgetId == widgetId; |
| 825 |
} |
| 826 |
|
| 827 |
@Override |
| 828 |
public int hashCode() { |
| 829 |
return (filter == null ? 0 : filter.hashCode()) ^ (widgetId << 2); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
public RemoteViewsAdapter(Context context, Intent intent, |
| 834 |
RemoteAdapterConnectionCallback callback, boolean useAsyncLoader) { |
| 835 |
mContext = context; |
| 836 |
mIntent = intent; |
| 837 |
|
| 838 |
if (mIntent == null) { |
| 839 |
throw new IllegalArgumentException("Non-null Intent must be specified."); |
| 840 |
} |
| 841 |
|
| 842 |
mAppWidgetId = intent.getIntExtra(EXTRA_REMOTEADAPTER_APPWIDGET_ID, -1); |
| 843 |
mRequestedViews = new RemoteViewsFrameLayoutRefSet(); |
| 844 |
mOnLightBackground = intent.getBooleanExtra(EXTRA_REMOTEADAPTER_ON_LIGHT_BACKGROUND, false); |
| 845 |
|
| 846 |
|
| 847 |
intent.removeExtra(EXTRA_REMOTEADAPTER_APPWIDGET_ID); |
| 848 |
intent.removeExtra(EXTRA_REMOTEADAPTER_ON_LIGHT_BACKGROUND); |
| 849 |
|
| 850 |
|
| 851 |
mWorkerThread = new HandlerThread("RemoteViewsCache-loader"); |
| 852 |
mWorkerThread.start(); |
| 853 |
mMainHandler = new Handler(Looper.myLooper(), this); |
| 854 |
mServiceHandler = new RemoteServiceHandler(mWorkerThread.getLooper(), this, |
| 855 |
context.getApplicationContext()); |
| 856 |
mAsyncViewLoadExecutor = useAsyncLoader ? new HandlerThreadExecutor(mWorkerThread) : null; |
| 857 |
mCallback = callback; |
| 858 |
|
| 859 |
if (sCacheRemovalThread == null) { |
| 860 |
sCacheRemovalThread = new HandlerThread("RemoteViewsAdapter-cachePruner"); |
| 861 |
sCacheRemovalThread.start(); |
| 862 |
sCacheRemovalQueue = new Handler(sCacheRemovalThread.getLooper()); |
| 863 |
} |
| 864 |
|
| 865 |
RemoteViewsCacheKey key = new RemoteViewsCacheKey(new Intent.FilterComparison(mIntent), |
| 866 |
mAppWidgetId); |
| 867 |
|
| 868 |
synchronized(sCachedRemoteViewsCaches) { |
| 869 |
FixedSizeRemoteViewsCache cache = sCachedRemoteViewsCaches.get(key); |
| 870 |
Configuration config = context.getResources().getConfiguration(); |
| 871 |
if (cache == null |
| 872 |
|| (cache.mConfiguration.diff(config) & CACHE_RESET_CONFIG_FLAGS) != 0) { |
| 873 |
mCache = new FixedSizeRemoteViewsCache(DEFAULT_CACHE_SIZE, config); |
| 874 |
} else { |
| 875 |
mCache = sCachedRemoteViewsCaches.get(key); |
| 876 |
synchronized (mCache.mMetaData) { |
| 877 |
if (mCache.mMetaData.count > 0) { |
| 878 |
|
| 879 |
|
| 880 |
mDataReady = true; |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
if (!mDataReady) { |
| 885 |
requestBindService(); |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
@Override |
| 891 |
protected void finalize() throws Throwable { |
| 892 |
try { |
| 893 |
mServiceHandler.unbindNow(); |
| 894 |
mWorkerThread.quit(); |
| 895 |
} finally { |
| 896 |
super.finalize(); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 901 |
public boolean isDataReady() { |
| 902 |
return mDataReady; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
public void setRemoteViewsInteractionHandler(InteractionHandler handler) { |
| 907 |
mRemoteViewsInteractionHandler = handler; |
| 908 |
} |
| 909 |
|
| 910 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 911 |
public void saveRemoteViewsCache() { |
| 912 |
final RemoteViewsCacheKey key = new RemoteViewsCacheKey( |
| 913 |
new Intent.FilterComparison(mIntent), mAppWidgetId); |
| 914 |
|
| 915 |
synchronized(sCachedRemoteViewsCaches) { |
| 916 |
|
| 917 |
if (sRemoteViewsCacheRemoveRunnables.containsKey(key)) { |
| 918 |
sCacheRemovalQueue.removeCallbacks(sRemoteViewsCacheRemoveRunnables.get(key)); |
| 919 |
sRemoteViewsCacheRemoveRunnables.remove(key); |
| 920 |
} |
| 921 |
|
| 922 |
int metaDataCount = 0; |
| 923 |
int numRemoteViewsCached = 0; |
| 924 |
synchronized (mCache.mMetaData) { |
| 925 |
metaDataCount = mCache.mMetaData.count; |
| 926 |
} |
| 927 |
synchronized (mCache) { |
| 928 |
numRemoteViewsCached = mCache.mIndexRemoteViews.size(); |
| 929 |
} |
| 930 |
if (metaDataCount > 0 && numRemoteViewsCached > 0) { |
| 931 |
sCachedRemoteViewsCaches.put(key, mCache); |
| 932 |
} |
| 933 |
|
| 934 |
Runnable r = () -> { |
| 935 |
synchronized (sCachedRemoteViewsCaches) { |
| 936 |
if (sCachedRemoteViewsCaches.containsKey(key)) { |
| 937 |
sCachedRemoteViewsCaches.remove(key); |
| 938 |
} |
| 939 |
if (sRemoteViewsCacheRemoveRunnables.containsKey(key)) { |
| 940 |
sRemoteViewsCacheRemoveRunnables.remove(key); |
| 941 |
} |
| 942 |
} |
| 943 |
}; |
| 944 |
sRemoteViewsCacheRemoveRunnables.put(key, r); |
| 945 |
sCacheRemovalQueue.postDelayed(r, REMOTE_VIEWS_CACHE_DURATION); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
@WorkerThread |
| 950 |
private void updateTemporaryMetaData(IRemoteViewsFactory factory) { |
| 951 |
try { |
| 952 |
|
| 953 |
|
| 954 |
boolean hasStableIds = factory.hasStableIds(); |
| 955 |
int viewTypeCount = factory.getViewTypeCount(); |
| 956 |
int count = factory.getCount(); |
| 957 |
LoadingViewTemplate loadingTemplate = |
| 958 |
new LoadingViewTemplate(factory.getLoadingView(), mContext); |
| 959 |
if ((count > 0) && (loadingTemplate.remoteViews == null)) { |
| 960 |
RemoteViews firstView = factory.getViewAt(0); |
| 961 |
if (firstView != null) { |
| 962 |
loadingTemplate.loadFirstViewHeight(firstView, mContext, |
| 963 |
new HandlerThreadExecutor(mWorkerThread)); |
| 964 |
} |
| 965 |
} |
| 966 |
final RemoteViewsMetaData tmpMetaData = mCache.getTemporaryMetaData(); |
| 967 |
synchronized (tmpMetaData) { |
| 968 |
tmpMetaData.hasStableIds = hasStableIds; |
| 969 |
|
| 970 |
tmpMetaData.viewTypeCount = viewTypeCount + 1; |
| 971 |
tmpMetaData.count = count; |
| 972 |
tmpMetaData.loadingTemplate = loadingTemplate; |
| 973 |
} |
| 974 |
} catch (RemoteException | RuntimeException e) { |
| 975 |
Log.e("RemoteViewsAdapter", "Error in updateMetaData: " + e.getMessage()); |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
synchronized (mCache.getMetaData()) { |
| 980 |
mCache.getMetaData().reset(); |
| 981 |
} |
| 982 |
synchronized (mCache) { |
| 983 |
mCache.reset(); |
| 984 |
} |
| 985 |
mMainHandler.sendEmptyMessage(MSG_MAIN_HANDLER_SUPER_NOTIFY_DATA_SET_CHANGED); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
@WorkerThread |
| 990 |
private void updateRemoteViews(IRemoteViewsFactory factory, int position, |
| 991 |
boolean notifyWhenLoaded) { |
| 992 |
|
| 993 |
final RemoteViews remoteViews; |
| 994 |
final long itemId; |
| 995 |
try { |
| 996 |
remoteViews = factory.getViewAt(position); |
| 997 |
itemId = factory.getItemId(position); |
| 998 |
|
| 999 |
if (remoteViews == null) { |
| 1000 |
throw new RuntimeException("Null remoteViews"); |
| 1001 |
} |
| 1002 |
} catch (RemoteException | RuntimeException e) { |
| 1003 |
Log.e(TAG, "Error in updateRemoteViews(" + position + "): " + e.getMessage()); |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
return; |
| 1008 |
} |
| 1009 |
|
| 1010 |
if (remoteViews.mApplication != null) { |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
if (mLastRemoteViewAppInfo != null |
| 1015 |
&& remoteViews.hasSameAppInfo(mLastRemoteViewAppInfo)) { |
| 1016 |
|
| 1017 |
|
| 1018 |
remoteViews.mApplication = mLastRemoteViewAppInfo; |
| 1019 |
} else { |
| 1020 |
mLastRemoteViewAppInfo = remoteViews.mApplication; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
int layoutId = remoteViews.getLayoutId(); |
| 1025 |
RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1026 |
boolean viewTypeInRange; |
| 1027 |
int cacheCount; |
| 1028 |
synchronized (metaData) { |
| 1029 |
viewTypeInRange = metaData.isViewTypeInRange(layoutId); |
| 1030 |
cacheCount = mCache.mMetaData.count; |
| 1031 |
} |
| 1032 |
synchronized (mCache) { |
| 1033 |
if (viewTypeInRange) { |
| 1034 |
int[] visibleWindow = getVisibleWindow(cacheCount); |
| 1035 |
|
| 1036 |
mCache.insert(position, remoteViews, itemId, visibleWindow); |
| 1037 |
|
| 1038 |
if (notifyWhenLoaded) { |
| 1039 |
|
| 1040 |
|
| 1041 |
Message.obtain(mMainHandler, MSG_MAIN_HANDLER_REMOTE_VIEWS_LOADED, position, 0, |
| 1042 |
remoteViews).sendToTarget(); |
| 1043 |
} |
| 1044 |
} else { |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
Log.e(TAG, "Error: widget's RemoteViewsFactory returns more view types than " + |
| 1050 |
" indicated by getViewTypeCount() "); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1056 |
public Intent getRemoteViewsServiceIntent() { |
| 1057 |
return mIntent; |
| 1058 |
} |
| 1059 |
|
| 1060 |
public int getCount() { |
| 1061 |
final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1062 |
synchronized (metaData) { |
| 1063 |
return metaData.count; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
public Object getItem(int position) { |
| 1068 |
|
| 1069 |
return null; |
| 1070 |
} |
| 1071 |
|
| 1072 |
public long getItemId(int position) { |
| 1073 |
synchronized (mCache) { |
| 1074 |
if (mCache.containsMetaDataAt(position)) { |
| 1075 |
return mCache.getMetaDataAt(position).itemId; |
| 1076 |
} |
| 1077 |
return 0; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
public int getItemViewType(int position) { |
| 1082 |
final int typeId; |
| 1083 |
synchronized (mCache) { |
| 1084 |
if (mCache.containsMetaDataAt(position)) { |
| 1085 |
typeId = mCache.getMetaDataAt(position).typeId; |
| 1086 |
} else { |
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1092 |
synchronized (metaData) { |
| 1093 |
return metaData.getMappedViewType(typeId); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1103 |
public void setVisibleRangeHint(int lowerBound, int upperBound) { |
| 1104 |
mVisibleWindowLowerBound = lowerBound; |
| 1105 |
mVisibleWindowUpperBound = upperBound; |
| 1106 |
} |
| 1107 |
|
| 1108 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 1109 |
|
| 1110 |
|
| 1111 |
synchronized (mCache) { |
| 1112 |
RemoteViews rv = mCache.getRemoteViewsAt(position); |
| 1113 |
boolean isInCache = (rv != null); |
| 1114 |
boolean hasNewItems = false; |
| 1115 |
|
| 1116 |
if (convertView != null && convertView instanceof RemoteViewsFrameLayout) { |
| 1117 |
mRequestedViews.removeView((RemoteViewsFrameLayout) convertView); |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (!isInCache) { |
| 1121 |
|
| 1122 |
|
| 1123 |
requestBindService(); |
| 1124 |
} else { |
| 1125 |
|
| 1126 |
hasNewItems = mCache.queuePositionsToBePreloadedFromRequestedPosition(position); |
| 1127 |
} |
| 1128 |
|
| 1129 |
final RemoteViewsFrameLayout layout; |
| 1130 |
if (convertView instanceof RemoteViewsFrameLayout) { |
| 1131 |
layout = (RemoteViewsFrameLayout) convertView; |
| 1132 |
} else { |
| 1133 |
layout = new RemoteViewsFrameLayout(parent.getContext(), mCache); |
| 1134 |
layout.setExecutor(mAsyncViewLoadExecutor); |
| 1135 |
layout.setOnLightBackground(mOnLightBackground); |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (isInCache) { |
| 1139 |
|
| 1140 |
layout.onRemoteViewsLoaded(rv, mRemoteViewsInteractionHandler, false); |
| 1141 |
if (hasNewItems) { |
| 1142 |
mServiceHandler.sendEmptyMessage(MSG_LOAD_NEXT_ITEM); |
| 1143 |
} |
| 1144 |
} else { |
| 1145 |
|
| 1146 |
|
| 1147 |
layout.onRemoteViewsLoaded( |
| 1148 |
mCache.getMetaData().getLoadingTemplate(mContext).remoteViews, |
| 1149 |
mRemoteViewsInteractionHandler, |
| 1150 |
false); |
| 1151 |
mRequestedViews.add(position, layout); |
| 1152 |
mCache.queueRequestedPositionToLoad(position); |
| 1153 |
mServiceHandler.sendEmptyMessage(MSG_LOAD_NEXT_ITEM); |
| 1154 |
} |
| 1155 |
return layout; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
public int getViewTypeCount() { |
| 1160 |
final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1161 |
synchronized (metaData) { |
| 1162 |
return metaData.viewTypeCount; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
public boolean hasStableIds() { |
| 1167 |
final RemoteViewsMetaData metaData = mCache.getMetaData(); |
| 1168 |
synchronized (metaData) { |
| 1169 |
return metaData.hasStableIds; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
public boolean isEmpty() { |
| 1174 |
return getCount() <= 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
private int[] getVisibleWindow(int count) { |
| 1181 |
int lower = mVisibleWindowLowerBound; |
| 1182 |
int upper = mVisibleWindowUpperBound; |
| 1183 |
|
| 1184 |
if ((lower == 0 && upper == 0) || lower < 0 || upper < 0) { |
| 1185 |
return new int[0]; |
| 1186 |
} |
| 1187 |
|
| 1188 |
int[] window; |
| 1189 |
if (lower <= upper) { |
| 1190 |
window = new int[upper + 1 - lower]; |
| 1191 |
for (int i = lower, j = 0; i <= upper; i++, j++){ |
| 1192 |
window[j] = i; |
| 1193 |
} |
| 1194 |
} else { |
| 1195 |
|
| 1196 |
|
| 1197 |
count = Math.max(count, lower); |
| 1198 |
window = new int[count - lower + upper + 1]; |
| 1199 |
int j = 0; |
| 1200 |
|
| 1201 |
for (int i = 0; i <= upper; i++, j++) { |
| 1202 |
window[j] = i; |
| 1203 |
} |
| 1204 |
for (int i = lower; i < count; i++, j++) { |
| 1205 |
window[j] = i; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
return window; |
| 1209 |
} |
| 1210 |
|
| 1211 |
public void notifyDataSetChanged() { |
| 1212 |
mServiceHandler.removeMessages(MSG_UNBIND_SERVICE); |
| 1213 |
mServiceHandler.sendEmptyMessage(MSG_NOTIFY_DATA_SET_CHANGED); |
| 1214 |
} |
| 1215 |
|
| 1216 |
void superNotifyDataSetChanged() { |
| 1217 |
super.notifyDataSetChanged(); |
| 1218 |
} |
| 1219 |
|
| 1220 |
@Override |
| 1221 |
public boolean handleMessage(Message msg) { |
| 1222 |
switch (msg.what) { |
| 1223 |
case MSG_MAIN_HANDLER_COMMIT_METADATA: { |
| 1224 |
mCache.commitTemporaryMetaData(); |
| 1225 |
return true; |
| 1226 |
} |
| 1227 |
case MSG_MAIN_HANDLER_SUPER_NOTIFY_DATA_SET_CHANGED: { |
| 1228 |
superNotifyDataSetChanged(); |
| 1229 |
return true; |
| 1230 |
} |
| 1231 |
case MSG_MAIN_HANDLER_REMOTE_ADAPTER_CONNECTED: { |
| 1232 |
if (mCallback != null) { |
| 1233 |
mCallback.onRemoteAdapterConnected(); |
| 1234 |
} |
| 1235 |
return true; |
| 1236 |
} |
| 1237 |
case MSG_MAIN_HANDLER_REMOTE_ADAPTER_DISCONNECTED: { |
| 1238 |
if (mCallback != null) { |
| 1239 |
mCallback.onRemoteAdapterDisconnected(); |
| 1240 |
} |
| 1241 |
return true; |
| 1242 |
} |
| 1243 |
case MSG_MAIN_HANDLER_REMOTE_VIEWS_LOADED: { |
| 1244 |
mRequestedViews.notifyOnRemoteViewsLoaded(msg.arg1, (RemoteViews) msg.obj); |
| 1245 |
return true; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
return false; |
| 1249 |
} |
| 1250 |
|
| 1251 |
private void requestBindService() { |
| 1252 |
mServiceHandler.removeMessages(MSG_UNBIND_SERVICE); |
| 1253 |
Message.obtain(mServiceHandler, MSG_REQUEST_BIND, mAppWidgetId, 0, mIntent).sendToTarget(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
private static class HandlerThreadExecutor implements Executor { |
| 1257 |
private final HandlerThread mThread; |
| 1258 |
|
| 1259 |
HandlerThreadExecutor(HandlerThread thread) { |
| 1260 |
mThread = thread; |
| 1261 |
} |
| 1262 |
|
| 1263 |
@Override |
| 1264 |
public void execute(Runnable runnable) { |
| 1265 |
if (Thread.currentThread().getId() == mThread.getId()) { |
| 1266 |
runnable.run(); |
| 1267 |
} else { |
| 1268 |
new Handler(mThread.getLooper()).post(runnable); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
private static class LoadingViewTemplate { |
| 1274 |
public final RemoteViews remoteViews; |
| 1275 |
public int defaultHeight; |
| 1276 |
|
| 1277 |
LoadingViewTemplate(RemoteViews views, Context context) { |
| 1278 |
remoteViews = views; |
| 1279 |
|
| 1280 |
float density = context.getResources().getDisplayMetrics().density; |
| 1281 |
defaultHeight = Math.round(DEFAULT_LOADING_VIEW_HEIGHT * density); |
| 1282 |
} |
| 1283 |
|
| 1284 |
public void loadFirstViewHeight( |
| 1285 |
RemoteViews firstView, Context context, Executor executor) { |
| 1286 |
|
| 1287 |
firstView.applyAsync(context, new RemoteViewsFrameLayout(context, null), executor, |
| 1288 |
new RemoteViews.OnViewAppliedListener() { |
| 1289 |
@Override |
| 1290 |
public void onViewApplied(View v) { |
| 1291 |
try { |
| 1292 |
v.measure( |
| 1293 |
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
| 1294 |
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| 1295 |
defaultHeight = v.getMeasuredHeight(); |
| 1296 |
} catch (Exception e) { |
| 1297 |
onError(e); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
@Override |
| 1302 |
public void onError(Exception e) { |
| 1303 |
|
| 1304 |
Log.w(TAG, "Error inflating first RemoteViews", e); |
| 1305 |
} |
| 1306 |
}); |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |