AsyncListUtil.java 11.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
package android.support.v7.util;

import android.support.annotation.UiThread;
import android.support.annotation.WorkerThread;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import fr;

public class AsyncListUtil<T>
{
  final Class<T> a;
  final int b;
  final DataCallback<T> c;
  final ViewCallback d;
  final TileList<T> e;
  final ThreadUtil.MainThreadCallback<T> f;
  final ThreadUtil.BackgroundCallback<T> g;
  final int[] h = new int[2];
  final int[] i = new int[2];
  final int[] j = new int[2];
  boolean k;
  int l = 0;
  int m = 0;
  int n = this.m;
  final SparseIntArray o = new SparseIntArray();
  private int p = 0;
  private final ThreadUtil.MainThreadCallback<T> q = new ThreadUtil.MainThreadCallback()
  {
    private boolean a(int paramAnonymousInt)
    {
      return paramAnonymousInt == AsyncListUtil.this.n;
    }
    
    public final void addTile(int paramAnonymousInt, TileList.Tile<T> paramAnonymousTile)
    {
      if (!a(paramAnonymousInt)) {
        AsyncListUtil.this.g.recycleTile(paramAnonymousTile);
      }
      label249:
      for (;;)
      {
        return;
        TileList localTileList = AsyncListUtil.this.e;
        paramAnonymousInt = localTileList.b.indexOfKey(paramAnonymousTile.mStartPosition);
        Object localObject;
        int i;
        int j;
        if (paramAnonymousInt < 0)
        {
          localTileList.b.put(paramAnonymousTile.mStartPosition, paramAnonymousTile);
          localObject = null;
          if (localObject != null)
          {
            Log.e("AsyncListUtil", "duplicate tile @" + ((TileList.Tile)localObject).mStartPosition);
            AsyncListUtil.this.g.recycleTile((TileList.Tile)localObject);
          }
          i = paramAnonymousTile.mStartPosition;
          j = paramAnonymousTile.mItemCount;
          paramAnonymousInt = 0;
        }
        for (;;)
        {
          if (paramAnonymousInt >= AsyncListUtil.this.o.size()) {
            break label249;
          }
          int k = AsyncListUtil.this.o.keyAt(paramAnonymousInt);
          if ((paramAnonymousTile.mStartPosition <= k) && (k < j + i))
          {
            AsyncListUtil.this.o.removeAt(paramAnonymousInt);
            AsyncListUtil.this.d.onItemLoaded(k);
            continue;
            TileList.Tile localTile = (TileList.Tile)localTileList.b.valueAt(paramAnonymousInt);
            localTileList.b.setValueAt(paramAnonymousInt, paramAnonymousTile);
            localObject = localTile;
            if (localTileList.c != localTile) {
              break;
            }
            localTileList.c = paramAnonymousTile;
            localObject = localTile;
            break;
          }
          paramAnonymousInt += 1;
        }
      }
    }
    
    public final void removeTile(int paramAnonymousInt1, int paramAnonymousInt2)
    {
      if (!a(paramAnonymousInt1)) {
        return;
      }
      TileList localTileList = AsyncListUtil.this.e;
      TileList.Tile localTile = (TileList.Tile)localTileList.b.get(paramAnonymousInt2);
      if (localTileList.c == localTile) {
        localTileList.c = null;
      }
      localTileList.b.delete(paramAnonymousInt2);
      if (localTile == null)
      {
        Log.e("AsyncListUtil", "tile not found @" + paramAnonymousInt2);
        return;
      }
      AsyncListUtil.this.g.recycleTile(localTile);
    }
    
    public final void updateItemCount(int paramAnonymousInt1, int paramAnonymousInt2)
    {
      if (!a(paramAnonymousInt1)) {
        return;
      }
      AsyncListUtil.this.l = paramAnonymousInt2;
      AsyncListUtil.this.d.onDataRefresh();
      AsyncListUtil.this.m = AsyncListUtil.this.n;
      paramAnonymousInt1 = 0;
      while (paramAnonymousInt1 < AsyncListUtil.this.e.b.size())
      {
        AsyncListUtil.this.g.recycleTile((TileList.Tile)AsyncListUtil.this.e.b.valueAt(paramAnonymousInt1));
        paramAnonymousInt1 += 1;
      }
      AsyncListUtil.this.e.b.clear();
      AsyncListUtil.this.k = false;
      AsyncListUtil.this.a();
    }
  };
  private final ThreadUtil.BackgroundCallback<T> r = new ThreadUtil.BackgroundCallback()
  {
    final SparseBooleanArray a = new SparseBooleanArray();
    private TileList.Tile<T> c;
    private int d;
    private int e;
    private int f;
    private int g;
    
    private int a(int paramAnonymousInt)
    {
      return paramAnonymousInt - paramAnonymousInt % AsyncListUtil.this.b;
    }
    
    private void a(int paramAnonymousInt1, int paramAnonymousInt2, int paramAnonymousInt3, boolean paramAnonymousBoolean)
    {
      int i = paramAnonymousInt1;
      if (i <= paramAnonymousInt2)
      {
        if (paramAnonymousBoolean) {}
        for (int j = paramAnonymousInt2 + paramAnonymousInt1 - i;; j = i)
        {
          AsyncListUtil.this.g.loadTile(j, paramAnonymousInt3);
          i += AsyncListUtil.this.b;
          break;
        }
      }
    }
    
    private void b(int paramAnonymousInt)
    {
      this.a.delete(paramAnonymousInt);
      AsyncListUtil.this.f.removeTile(this.d, paramAnonymousInt);
    }
    
    public final void loadTile(int paramAnonymousInt1, int paramAnonymousInt2)
    {
      if (this.a.get(paramAnonymousInt1)) {
        return;
      }
      TileList.Tile localTile;
      if (this.c != null)
      {
        localTile = this.c;
        this.c = this.c.a;
        localTile.mStartPosition = paramAnonymousInt1;
        localTile.mItemCount = Math.min(AsyncListUtil.this.b, this.e - localTile.mStartPosition);
        AsyncListUtil.this.c.fillData(localTile.mItems, localTile.mStartPosition, localTile.mItemCount);
        paramAnonymousInt1 = AsyncListUtil.this.c.getMaxCachedTiles();
      }
      for (;;)
      {
        if (this.a.size() < paramAnonymousInt1) {
          break label235;
        }
        int i = this.a.keyAt(0);
        int j = this.a.keyAt(this.a.size() - 1);
        int k = this.f - i;
        int m = j - this.g;
        if ((k > 0) && ((k >= m) || (paramAnonymousInt2 == 2)))
        {
          b(i);
          continue;
          localTile = new TileList.Tile(AsyncListUtil.this.a, AsyncListUtil.this.b);
          break;
        }
        if ((m <= 0) || ((k >= m) && (paramAnonymousInt2 != 1))) {
          break label235;
        }
        b(j);
      }
      label235:
      this.a.put(localTile.mStartPosition, true);
      AsyncListUtil.this.f.addTile(this.d, localTile);
    }
    
    public final void recycleTile(TileList.Tile<T> paramAnonymousTile)
    {
      AsyncListUtil.this.c.recycleData(paramAnonymousTile.mItems, paramAnonymousTile.mItemCount);
      paramAnonymousTile.a = this.c;
      this.c = paramAnonymousTile;
    }
    
    public final void refresh(int paramAnonymousInt)
    {
      this.d = paramAnonymousInt;
      this.a.clear();
      this.e = AsyncListUtil.this.c.refreshData();
      AsyncListUtil.this.f.updateItemCount(this.d, this.e);
    }
    
    public final void updateRange(int paramAnonymousInt1, int paramAnonymousInt2, int paramAnonymousInt3, int paramAnonymousInt4, int paramAnonymousInt5)
    {
      if (paramAnonymousInt1 > paramAnonymousInt2) {
        return;
      }
      paramAnonymousInt1 = a(paramAnonymousInt1);
      paramAnonymousInt2 = a(paramAnonymousInt2);
      this.f = a(paramAnonymousInt3);
      this.g = a(paramAnonymousInt4);
      if (paramAnonymousInt5 == 1)
      {
        a(this.f, paramAnonymousInt2, paramAnonymousInt5, true);
        a(AsyncListUtil.this.b + paramAnonymousInt2, this.g, paramAnonymousInt5, false);
        return;
      }
      a(paramAnonymousInt1, this.g, paramAnonymousInt5, false);
      a(this.f, paramAnonymousInt1 - AsyncListUtil.this.b, paramAnonymousInt5, true);
    }
  };
  
  public AsyncListUtil(Class<T> paramClass, int paramInt, DataCallback<T> paramDataCallback, ViewCallback paramViewCallback)
  {
    this.a = paramClass;
    this.b = paramInt;
    this.c = paramDataCallback;
    this.d = paramViewCallback;
    this.e = new TileList(this.b);
    paramClass = new fr();
    this.f = paramClass.a(this.q);
    this.g = paramClass.a(this.r);
    refresh();
  }
  
  private boolean b()
  {
    return this.n != this.m;
  }
  
  final void a()
  {
    this.d.getItemRangeInto(this.h);
    if ((this.h[0] > this.h[1]) || (this.h[0] < 0)) {}
    while (this.h[1] >= this.l) {
      return;
    }
    if (!this.k) {
      this.p = 0;
    }
    for (;;)
    {
      this.i[0] = this.h[0];
      this.i[1] = this.h[1];
      this.d.extendRangeInto(this.h, this.j, this.p);
      this.j[0] = Math.min(this.h[0], Math.max(this.j[0], 0));
      this.j[1] = Math.max(this.h[1], Math.min(this.j[1], this.l - 1));
      this.g.updateRange(this.h[0], this.h[1], this.j[0], this.j[1], this.p);
      return;
      if ((this.h[0] > this.i[1]) || (this.i[0] > this.h[1])) {
        this.p = 0;
      } else if (this.h[0] < this.i[0]) {
        this.p = 1;
      } else if (this.h[0] > this.i[0]) {
        this.p = 2;
      }
    }
  }
  
  public T getItem(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.l)) {
      throw new IndexOutOfBoundsException(paramInt + " is not within 0 and " + this.l);
    }
    Object localObject = this.e;
    int i1;
    if (((TileList)localObject).c != null)
    {
      TileList.Tile localTile = ((TileList)localObject).c;
      if (localTile.mStartPosition <= paramInt)
      {
        i1 = localTile.mStartPosition;
        if (paramInt < localTile.mItemCount + i1)
        {
          i1 = 1;
          if (i1 != 0) {
            break label162;
          }
        }
      }
    }
    else
    {
      i1 = ((TileList)localObject).a;
      i1 = ((TileList)localObject).b.indexOfKey(paramInt - paramInt % i1);
      if (i1 >= 0) {
        break label147;
      }
    }
    for (localObject = null;; localObject = localObject.mItems[(paramInt - localObject.mStartPosition)])
    {
      if ((localObject == null) && (!b())) {
        this.o.put(paramInt, 0);
      }
      return (T)localObject;
      i1 = 0;
      break;
      label147:
      ((TileList)localObject).c = ((TileList.Tile)((TileList)localObject).b.valueAt(i1));
      label162:
      localObject = ((TileList)localObject).c;
    }
  }
  
  public int getItemCount()
  {
    return this.l;
  }
  
  public void onRangeChanged()
  {
    if (b()) {
      return;
    }
    a();
    this.k = true;
  }
  
  public void refresh()
  {
    this.o.clear();
    ThreadUtil.BackgroundCallback localBackgroundCallback = this.g;
    int i1 = this.n + 1;
    this.n = i1;
    localBackgroundCallback.refresh(i1);
  }
  
  public static abstract class DataCallback<T>
  {
    @WorkerThread
    public abstract void fillData(T[] paramArrayOfT, int paramInt1, int paramInt2);
    
    @WorkerThread
    public int getMaxCachedTiles()
    {
      return 10;
    }
    
    @WorkerThread
    public void recycleData(T[] paramArrayOfT, int paramInt) {}
    
    @WorkerThread
    public abstract int refreshData();
  }
  
  public static abstract class ViewCallback
  {
    public static final int HINT_SCROLL_ASC = 2;
    public static final int HINT_SCROLL_DESC = 1;
    public static final int HINT_SCROLL_NONE = 0;
    
    @UiThread
    public void extendRangeInto(int[] paramArrayOfInt1, int[] paramArrayOfInt2, int paramInt)
    {
      int i = paramArrayOfInt1[1] - paramArrayOfInt1[0] + 1;
      int j = i / 2;
      int m = paramArrayOfInt1[0];
      int k;
      if (paramInt == 1)
      {
        k = i;
        paramArrayOfInt2[0] = (m - k);
        k = paramArrayOfInt1[1];
        if (paramInt != 2) {
          break label65;
        }
      }
      for (;;)
      {
        paramArrayOfInt2[1] = (k + i);
        return;
        k = j;
        break;
        label65:
        i = j;
      }
    }
    
    @UiThread
    public abstract void getItemRangeInto(int[] paramArrayOfInt);
    
    @UiThread
    public abstract void onDataRefresh();
    
    @UiThread
    public abstract void onItemLoaded(int paramInt);
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/android/support/v7/util/AsyncListUtil.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */