MediaCache.java 9.58 KB
package com.bitstrips.imoji.persistence;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
import android.widget.ImageView;
import com.bitstrips.imoji.injection.ForApplication;
import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Picasso.Builder;
import com.squareup.picasso.RequestCreator;
import hj;
import hk;
import hk.a;
import hl;
import hm;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class MediaCache
{
  @VisibleForTesting
  boolean a = false;
  @VisibleForTesting
  final ConcurrentHashMap<String, String> b = new ConcurrentHashMap();
  @VisibleForTesting
  hl c;
  private final Context d;
  private final Picasso e;
  private final Handler f;
  private final hj g;
  
  @Inject
  public MediaCache(@ForApplication Context paramContext)
  {
    this(paramContext, new hj(paramContext), new Handler(paramContext.getMainLooper()));
  }
  
  private MediaCache(Context paramContext, hj paramhj, Handler paramHandler)
  {
    this.d = paramContext;
    this.e = new Picasso.Builder(paramContext).addRequestHandler(new ImageFileRequestHandler(paramContext)).downloader(new hm(this, paramContext)).build();
    this.g = paramhj;
    this.f = paramHandler;
    new a().execute(new Void[0]);
  }
  
  public void cancelRequest(ImageView paramImageView)
  {
    this.e.cancelRequest(paramImageView);
  }
  
  public void flush()
  {
    this.f.post(new Runnable()
    {
      public final void run()
      {
        MediaCache.this.b.clear();
        if (MediaCache.this.a)
        {
          hl localhl = MediaCache.this.c;
          localhl.c.clear();
          localhl.a(new hk(hk.a.c, null, null));
        }
      }
    });
  }
  
  @Nullable
  public File getCachedImage(@NonNull String paramString)
  {
    Object localObject = String.valueOf(paramString.hashCode());
    if (!paramString.equals((String)this.b.get(localObject))) {
      paramString = null;
    }
    do
    {
      return paramString;
      localObject = new File(this.d.getCacheDir(), (String)localObject);
      paramString = (String)localObject;
    } while (((File)localObject).exists());
    return null;
  }
  
  public RequestCreator load(@NonNull Uri paramUri)
  {
    return load(paramUri.toString());
  }
  
  public RequestCreator load(@NonNull String paramString)
  {
    File localFile = getCachedImage(paramString);
    if (localFile != null) {
      return this.e.load(localFile);
    }
    return this.e.load(paramString);
  }
  
  public void preload(@NonNull String paramString)
  {
    preload(paramString, null);
  }
  
  public void preload(@NonNull String paramString, @Nullable Callback paramCallback)
  {
    load(paramString).resize(1, 1).memoryPolicy(MemoryPolicy.NO_STORE, new MemoryPolicy[0]).fetch(paramCallback);
  }
  
  public void shutdown()
  {
    this.f.post(new Runnable()
    {
      public final void run()
      {
        if (MediaCache.this.a)
        {
          hl localhl = MediaCache.this.c;
          localhl.d = true;
          if (localhl.e == null) {
            localhl.a.close();
          }
        }
      }
    });
  }
  
  @WorkerThread
  public File storeMediaData(@NonNull final String paramString, @NonNull InputStream paramInputStream)
  {
    final String str = String.valueOf(paramString.hashCode());
    File localFile = new File(this.d.getCacheDir(), str);
    try
    {
      BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(localFile, false));
      byte[] arrayOfByte = new byte['Ѐ'];
      for (;;)
      {
        int i = paramInputStream.read(arrayOfByte);
        if (i == -1) {
          break;
        }
        localBufferedOutputStream.write(arrayOfByte, 0, i);
      }
      localBufferedOutputStream.close();
      this.f.post(new Runnable()
      {
        public final void run()
        {
          MediaCache.a(MediaCache.this, str, paramString);
        }
      });
      return localFile;
    }
    catch (IOException paramString) {}
    return null;
  }
  
  @VisibleForTesting
  final class a
    extends AsyncTask<Void, Void, Void>
  {
    a() {}
    
    /* Error */
    private Void a()
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 14	com/bitstrips/imoji/persistence/MediaCache$a:a	Lcom/bitstrips/imoji/persistence/MediaCache;
      //   4: invokestatic 24	com/bitstrips/imoji/persistence/MediaCache:a	(Lcom/bitstrips/imoji/persistence/MediaCache;)Lhj;
      //   7: invokevirtual 30	hj:getWritableDatabase	()Landroid/database/sqlite/SQLiteDatabase;
      //   10: astore_2
      //   11: aload_0
      //   12: getfield 14	com/bitstrips/imoji/persistence/MediaCache$a:a	Lcom/bitstrips/imoji/persistence/MediaCache;
      //   15: invokestatic 24	com/bitstrips/imoji/persistence/MediaCache:a	(Lcom/bitstrips/imoji/persistence/MediaCache;)Lhj;
      //   18: astore_1
      //   19: aload_0
      //   20: getfield 14	com/bitstrips/imoji/persistence/MediaCache$a:a	Lcom/bitstrips/imoji/persistence/MediaCache;
      //   23: getfield 34	com/bitstrips/imoji/persistence/MediaCache:b	Ljava/util/concurrent/ConcurrentHashMap;
      //   26: astore_3
      //   27: aload_2
      //   28: aload_1
      //   29: getfield 37	hj:a	Lcom/bitstrips/imoji/persistence/MediaCacheTable;
      //   32: invokevirtual 43	com/bitstrips/imoji/persistence/MediaCacheTable:getName	()Ljava/lang/String;
      //   35: getstatic 47	com/bitstrips/imoji/persistence/MediaCacheTable:PROJECTION	[Ljava/lang/String;
      //   38: aconst_null
      //   39: aconst_null
      //   40: aconst_null
      //   41: aconst_null
      //   42: aconst_null
      //   43: invokevirtual 53	android/database/sqlite/SQLiteDatabase:query	(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;
      //   46: astore_1
      //   47: aload_1
      //   48: invokeinterface 59 1 0
      //   53: ifeq +62 -> 115
      //   56: aload_3
      //   57: aload_1
      //   58: iconst_0
      //   59: invokeinterface 63 2 0
      //   64: aload_1
      //   65: iconst_1
      //   66: invokeinterface 63 2 0
      //   71: invokeinterface 69 3 0
      //   76: pop
      //   77: goto -30 -> 47
      //   80: astore_3
      //   81: aload_1
      //   82: ifnull +9 -> 91
      //   85: aload_1
      //   86: invokeinterface 72 1 0
      //   91: aload_0
      //   92: getfield 14	com/bitstrips/imoji/persistence/MediaCache$a:a	Lcom/bitstrips/imoji/persistence/MediaCache;
      //   95: new 74	hl
      //   98: dup
      //   99: aload_2
      //   100: aload_0
      //   101: getfield 14	com/bitstrips/imoji/persistence/MediaCache$a:a	Lcom/bitstrips/imoji/persistence/MediaCache;
      //   104: invokestatic 24	com/bitstrips/imoji/persistence/MediaCache:a	(Lcom/bitstrips/imoji/persistence/MediaCache;)Lhj;
      //   107: invokespecial 77	hl:<init>	(Landroid/database/sqlite/SQLiteDatabase;Lhj;)V
      //   110: putfield 81	com/bitstrips/imoji/persistence/MediaCache:c	Lhl;
      //   113: aconst_null
      //   114: areturn
      //   115: aload_1
      //   116: ifnull -25 -> 91
      //   119: aload_1
      //   120: invokeinterface 72 1 0
      //   125: goto -34 -> 91
      //   128: aload_1
      //   129: ifnull +9 -> 138
      //   132: aload_1
      //   133: invokeinterface 72 1 0
      //   138: aload_2
      //   139: athrow
      //   140: astore_2
      //   141: goto -13 -> 128
      //   144: astore_1
      //   145: aconst_null
      //   146: astore_1
      //   147: goto -66 -> 81
      //   150: astore_1
      //   151: aconst_null
      //   152: areturn
      //   153: astore_2
      //   154: aconst_null
      //   155: astore_1
      //   156: goto -28 -> 128
      // Local variable table:
      //   start	length	slot	name	signature
      //   0	159	0	this	a
      //   18	115	1	localObject1	Object
      //   144	1	1	localSQLiteException1	android.database.sqlite.SQLiteException
      //   146	1	1	localObject2	Object
      //   150	1	1	localSQLiteException2	android.database.sqlite.SQLiteException
      //   155	1	1	localObject3	Object
      //   10	129	2	localSQLiteDatabase	SQLiteDatabase
      //   140	1	2	localObject4	Object
      //   153	1	2	localObject5	Object
      //   26	31	3	localConcurrentHashMap	ConcurrentHashMap
      //   80	1	3	localSQLiteException3	android.database.sqlite.SQLiteException
      // Exception table:
      //   from	to	target	type
      //   47	77	80	android/database/sqlite/SQLiteException
      //   47	77	140	finally
      //   27	47	144	android/database/sqlite/SQLiteException
      //   0	27	150	android/database/sqlite/SQLiteException
      //   85	91	150	android/database/sqlite/SQLiteException
      //   91	113	150	android/database/sqlite/SQLiteException
      //   119	125	150	android/database/sqlite/SQLiteException
      //   132	138	150	android/database/sqlite/SQLiteException
      //   138	140	150	android/database/sqlite/SQLiteException
      //   27	47	153	finally
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/bitstrips/imoji/persistence/MediaCache.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */