AsyncTaskLoader.java 6.41 KB
package android.support.v4.content;

import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.RestrictTo;
import android.support.v4.os.OperationCanceledException;
import android.support.v4.util.TimeUtils;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class AsyncTaskLoader<D>
  extends Loader<D>
{
  volatile AsyncTaskLoader<D>.a a;
  volatile AsyncTaskLoader<D>.a b;
  long c;
  long d = -10000L;
  Handler e;
  private final Executor f;
  
  public AsyncTaskLoader(Context paramContext)
  {
    this(paramContext, ModernAsyncTask.d);
  }
  
  private AsyncTaskLoader(Context paramContext, Executor paramExecutor)
  {
    super(paramContext);
    this.f = paramExecutor;
  }
  
  final void a()
  {
    if ((this.b == null) && (this.a != null))
    {
      if (this.a.b)
      {
        this.a.b = false;
        this.e.removeCallbacks(this.a);
      }
      if ((this.c > 0L) && (SystemClock.uptimeMillis() < this.d + this.c))
      {
        this.a.b = true;
        this.e.postAtTime(this.a, this.d + this.c);
      }
    }
    else
    {
      return;
    }
    a locala = this.a;
    Executor localExecutor = this.f;
    if (locala.g != ModernAsyncTask.Status.PENDING) {}
    switch (ModernAsyncTask.4.a[locala.g.ordinal()])
    {
    default: 
      locala.g = ModernAsyncTask.Status.RUNNING;
      locala.e.b = null;
      localExecutor.execute(locala.f);
      return;
    case 1: 
      throw new IllegalStateException("Cannot execute task: the task is already running.");
    }
    throw new IllegalStateException("Cannot execute task: the task has already been executed (a task can be executed only once)");
  }
  
  final void a(AsyncTaskLoader<D>.a paramAsyncTaskLoader, D paramD)
  {
    onCanceled(paramD);
    if (this.b == paramAsyncTaskLoader)
    {
      rollbackContentChanged();
      this.d = SystemClock.uptimeMillis();
      this.b = null;
      deliverCancellation();
      a();
    }
  }
  
  public void cancelLoadInBackground() {}
  
  public void dump(String paramString, FileDescriptor paramFileDescriptor, PrintWriter paramPrintWriter, String[] paramArrayOfString)
  {
    super.dump(paramString, paramFileDescriptor, paramPrintWriter, paramArrayOfString);
    if (this.a != null)
    {
      paramPrintWriter.print(paramString);
      paramPrintWriter.print("mTask=");
      paramPrintWriter.print(this.a);
      paramPrintWriter.print(" waiting=");
      paramPrintWriter.println(this.a.b);
    }
    if (this.b != null)
    {
      paramPrintWriter.print(paramString);
      paramPrintWriter.print("mCancellingTask=");
      paramPrintWriter.print(this.b);
      paramPrintWriter.print(" waiting=");
      paramPrintWriter.println(this.b.b);
    }
    if (this.c != 0L)
    {
      paramPrintWriter.print(paramString);
      paramPrintWriter.print("mUpdateThrottle=");
      TimeUtils.formatDuration(this.c, paramPrintWriter);
      paramPrintWriter.print(" mLastLoadCompleteTime=");
      TimeUtils.formatDuration(this.d, SystemClock.uptimeMillis(), paramPrintWriter);
      paramPrintWriter.println();
    }
  }
  
  public boolean isLoadInBackgroundCanceled()
  {
    return this.b != null;
  }
  
  public abstract D loadInBackground();
  
  protected boolean onCancelLoad()
  {
    if (this.a != null)
    {
      if (this.b != null)
      {
        if (this.a.b)
        {
          this.a.b = false;
          this.e.removeCallbacks(this.a);
        }
        this.a = null;
      }
    }
    else {
      return false;
    }
    if (this.a.b)
    {
      this.a.b = false;
      this.e.removeCallbacks(this.a);
      this.a = null;
      return false;
    }
    a locala = this.a;
    locala.h.set(true);
    boolean bool = locala.f.cancel(false);
    if (bool)
    {
      this.b = this.a;
      cancelLoadInBackground();
    }
    this.a = null;
    return bool;
  }
  
  public void onCanceled(D paramD) {}
  
  protected void onForceLoad()
  {
    super.onForceLoad();
    cancelLoad();
    this.a = new a();
    a();
  }
  
  protected D onLoadInBackground()
  {
    return (D)loadInBackground();
  }
  
  public void setUpdateThrottle(long paramLong)
  {
    this.c = paramLong;
    if (paramLong != 0L) {
      this.e = new Handler();
    }
  }
  
  @RestrictTo({android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP})
  public void waitForLoader()
  {
    a locala = this.a;
    if (locala != null) {}
    try
    {
      locala.a.await();
      return;
    }
    catch (InterruptedException localInterruptedException) {}
  }
  
  final class a
    extends ModernAsyncTask<Void, Void, D>
    implements Runnable
  {
    final CountDownLatch a = new CountDownLatch(1);
    boolean b;
    
    a() {}
    
    private D c()
    {
      try
      {
        Object localObject = AsyncTaskLoader.this.onLoadInBackground();
        return (D)localObject;
      }
      catch (OperationCanceledException localOperationCanceledException)
      {
        if (!this.h.get()) {
          throw localOperationCanceledException;
        }
      }
      return null;
    }
    
    protected final void a(D paramD)
    {
      for (;;)
      {
        AsyncTaskLoader localAsyncTaskLoader;
        try
        {
          localAsyncTaskLoader = AsyncTaskLoader.this;
          if (localAsyncTaskLoader.a != this)
          {
            localAsyncTaskLoader.a(this, paramD);
            return;
          }
          if (localAsyncTaskLoader.isAbandoned())
          {
            localAsyncTaskLoader.onCanceled(paramD);
            continue;
          }
          localAsyncTaskLoader.commitContentChanged();
        }
        finally
        {
          this.a.countDown();
        }
        localAsyncTaskLoader.d = SystemClock.uptimeMillis();
        localAsyncTaskLoader.a = null;
        localAsyncTaskLoader.deliverResult(paramD);
      }
    }
    
    protected final void b(D paramD)
    {
      try
      {
        AsyncTaskLoader.this.a(this, paramD);
        return;
      }
      finally
      {
        this.a.countDown();
      }
    }
    
    public final void run()
    {
      this.b = false;
      AsyncTaskLoader.this.a();
    }
  }
}


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