package com.tencent.tmapkupdatesdk.internal.logic;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.tencent.tmapkupdatesdk.ApkUpdateListener;
import com.tencent.tmapkupdatesdk.ApkUpdateParam;
import com.tencent.tmapkupdatesdk.internal.logic.protocol.b;
import com.tencent.tmapkupdatesdk.internal.logic.protocol.jce.ApkFileInfo;
import com.tencent.tmapkupdatesdk.internal.logic.protocol.jce.AppInfoForUpdate;
import com.tencent.tmapkupdatesdk.internal.logic.protocol.jce.AppUpdateInfo;
import com.tencent.tmassistantbase.util.TMLog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class a
  extends Handler
{
  private static HandlerThread a;
  private static a b;
  private final ArrayList c = new ArrayList();
  private ConcurrentHashMap d = new ConcurrentHashMap();
  private final HashMap e = new HashMap();
  
  private a(Looper paramLooper)
  {
    super(paramLooper);
  }
  
  public static a a()
  {
    try
    {
      if (b == null)
      {
        a = new HandlerThread("apkupdate_asyctask");
        a.setPriority(10);
        a.start();
        b = new a(a.getLooper());
      }
      a localA = b;
      return localA;
    }
    finally {}
  }
  
  private int b(List paramList)
  {
    int j = 0;
    int i = j;
    if (paramList != null) {}
    for (;;)
    {
      try
      {
        i = paramList.size();
        if (i <= 0)
        {
          i = j;
          return i;
        }
        PackageManager localPackageManager = b.a().b().getPackageManager();
        Iterator localIterator = paramList.iterator();
        if (localIterator.hasNext())
        {
          paramList = (AppUpdateInfo)localIterator.next();
          boolean bool = this.e.containsKey(paramList.packageName);
          if (!bool) {
            try
            {
              PackageInfo localPackageInfo = localPackageManager.getPackageInfo(paramList.packageName, 0);
              com.tencent.tmapkupdatesdk.internal.a.a localA = new com.tencent.tmapkupdatesdk.internal.a.a();
              localA.a(localPackageInfo.applicationInfo.sourceDir);
              ApkFileInfo localApkFileInfo = new ApkFileInfo();
              localApkFileInfo.packageName = localPackageInfo.packageName;
              localApkFileInfo.apkId = paramList.apkId;
              String str = (String)this.d.get(localPackageInfo.packageName);
              paramList = str;
              if (TextUtils.isEmpty(str)) {
                paramList = b.a(localPackageInfo.packageName).toLowerCase();
              }
              localApkFileInfo.manifestMd5 = paramList;
              localApkFileInfo.fileCRC32 = localA.a();
              this.e.put(localApkFileInfo.packageName, localApkFileInfo);
            }
            catch (PackageManager.NameNotFoundException paramList)
            {
              paramList.printStackTrace();
              TMLog.e("TAG", "exception:", paramList);
            }
          }
        }
        else
        {
          i = this.e.size();
        }
      }
      finally {}
    }
  }
  
  /* Error */
  private void b()
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: ldc -79
    //   4: ldc -69
    //   6: invokestatic 191	com/tencent/tmassistantbase/util/TMLog:i	(Ljava/lang/String;Ljava/lang/String;)V
    //   9: aload_0
    //   10: getfield 35	com/tencent/tmapkupdatesdk/internal/logic/a:e	Ljava/util/HashMap;
    //   13: invokevirtual 185	java/util/HashMap:size	()I
    //   16: ifgt +13 -> 29
    //   19: ldc -79
    //   21: ldc -63
    //   23: invokestatic 191	com/tencent/tmassistantbase/util/TMLog:i	(Ljava/lang/String;Ljava/lang/String;)V
    //   26: aload_0
    //   27: monitorexit
    //   28: return
    //   29: new 20	java/util/ArrayList
    //   32: dup
    //   33: invokespecial 23	java/util/ArrayList:<init>	()V
    //   36: astore_1
    //   37: aload_1
    //   38: aload_0
    //   39: getfield 35	com/tencent/tmapkupdatesdk/internal/logic/a:e	Ljava/util/HashMap;
    //   42: aload_0
    //   43: getfield 35	com/tencent/tmapkupdatesdk/internal/logic/a:e	Ljava/util/HashMap;
    //   46: invokevirtual 197	java/util/HashMap:keySet	()Ljava/util/Set;
    //   49: invokeinterface 200 1 0
    //   54: invokeinterface 97 1 0
    //   59: invokevirtual 201	java/util/HashMap:get	(Ljava/lang/Object;)Ljava/lang/Object;
    //   62: checkcast 131	com/tencent/tmapkupdatesdk/internal/logic/protocol/jce/ApkFileInfo
    //   65: invokevirtual 204	java/util/ArrayList:add	(Ljava/lang/Object;)Z
    //   68: pop
    //   69: ldc -79
    //   71: ldc -50
    //   73: invokestatic 191	com/tencent/tmassistantbase/util/TMLog:i	(Ljava/lang/String;Ljava/lang/String;)V
    //   76: invokestatic 74	com/tencent/tmapkupdatesdk/internal/logic/protocol/b:a	()Lcom/tencent/tmapkupdatesdk/internal/logic/protocol/b;
    //   79: new 208	com/tencent/tmapkupdatesdk/internal/logic/protocol/a/b
    //   82: dup
    //   83: aload_1
    //   84: invokespecial 211	com/tencent/tmapkupdatesdk/internal/logic/protocol/a/b:<init>	(Ljava/util/List;)V
    //   87: invokevirtual 214	com/tencent/tmapkupdatesdk/internal/logic/protocol/b:a	(Ljava/lang/Runnable;)V
    //   90: ldc -79
    //   92: ldc -40
    //   94: invokestatic 191	com/tencent/tmassistantbase/util/TMLog:i	(Ljava/lang/String;Ljava/lang/String;)V
    //   97: goto -71 -> 26
    //   100: astore_1
    //   101: aload_0
    //   102: monitorexit
    //   103: aload_1
    //   104: athrow
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	105	0	this	a
    //   36	48	1	localArrayList	ArrayList
    //   100	4	1	localObject	Object
    // Exception table:
    //   from	to	target	type
    //   2	26	100	finally
    //   29	97	100	finally
  }
  
  public void a(ApkUpdateListener paramApkUpdateListener)
  {
    TMLog.i("TAG", "enter");
    if (!this.c.contains(paramApkUpdateListener)) {
      this.c.add(paramApkUpdateListener);
    }
    TMLog.i("TAG", "exit");
  }
  
  void a(List paramList)
  {
    TMLog.i("ApkUpdateSDKMessageHandler", "enter");
    if (paramList != null)
    {
      ArrayList localArrayList = new ArrayList();
      PackageManager localPackageManager = b.a().b().getPackageManager();
      long l1 = System.currentTimeMillis();
      paramList = paramList.iterator();
      while (paramList.hasNext())
      {
        Object localObject = (ApkUpdateParam)paramList.next();
        String str = ((ApkUpdateParam)localObject).packageName;
        int i = ((ApkUpdateParam)localObject).actionFlag;
        int j = ((ApkUpdateParam)localObject).targetVersionCode;
        int k = ((ApkUpdateParam)localObject).targetGrayVersionCode;
        if (!TextUtils.isEmpty(str)) {
          for (;;)
          {
            AppInfoForUpdate localAppInfoForUpdate;
            try
            {
              localObject = localPackageManager.getPackageInfo(str, 0);
              if (localObject == null) {
                break;
              }
              localAppInfoForUpdate = new AppInfoForUpdate();
              localAppInfoForUpdate.packageName = str;
              localAppInfoForUpdate.versionCode = ((PackageInfo)localObject).versionCode;
              localAppInfoForUpdate.signatureMd5 = b.a().b(str);
              localAppInfoForUpdate.manifestMd5 = b.a(str).toLowerCase();
              this.d.put(str, localAppInfoForUpdate.manifestMd5);
              if ((((PackageInfo)localObject).applicationInfo.flags & 0x1) > 0) {
                break label271;
              }
              localAppInfoForUpdate.appType = 1;
              localAppInfoForUpdate.versionName = ((PackageInfo)localObject).versionName;
              localAppInfoForUpdate.actionFlag = ((byte)i);
              localAppInfoForUpdate.grayVersionCode = com.tencent.tmapkupdatesdk.internal.c.a.a().a(str);
              localAppInfoForUpdate.targetVersionCode = j;
              localAppInfoForUpdate.targetGrayVersionCode = k;
              localArrayList.add(localAppInfoForUpdate);
            }
            catch (PackageManager.NameNotFoundException localNameNotFoundException)
            {
              localNameNotFoundException.printStackTrace();
              TMLog.e("TAG", "exception:", localNameNotFoundException);
            }
            break;
            label271:
            localAppInfoForUpdate.appType = 2;
          }
        }
      }
      long l2 = System.currentTimeMillis();
      TMLog.i("TAG", "updatecost=" + (l2 - l1));
      if (localArrayList.size() <= 0) {
        break label361;
      }
      paramList = a().obtainMessage();
      paramList.what = 6;
      paramList.obj = localArrayList;
      paramList.sendToTarget();
      TMLog.i("TAG", "send Message ApkUpdateMessageHandler.CheckUpdate");
    }
    for (;;)
    {
      TMLog.i("ApkUpdateSDKMessageHandler", "exit");
      return;
      label361:
      paramList = a().obtainMessage();
      paramList.what = 2;
      paramList.sendToTarget();
      TMLog.i("TAG", "send Message ApkUpdateMessageHandler.CheckUpdateFailed");
    }
  }
  
  public void b(ApkUpdateListener paramApkUpdateListener)
  {
    TMLog.i("TAG", "enter");
    if (paramApkUpdateListener == null)
    {
      TMLog.i("TAG", "listener == null");
      return;
    }
    Iterator localIterator = this.c.iterator();
    while (localIterator.hasNext()) {
      if ((ApkUpdateListener)localIterator.next() == paramApkUpdateListener) {
        localIterator.remove();
      }
    }
    TMLog.i("TAG", "exit");
  }
  
  public void handleMessage(Message paramMessage)
  {
    super.handleMessage(paramMessage);
    switch (paramMessage.what)
    {
    }
    do
    {
      Object localObject;
      do
      {
        for (;;)
        {
          return;
          TMLog.i("ApkUpdateSDKMessageHandler", "message type:1");
          localObject = this.c.iterator();
          while (((Iterator)localObject).hasNext()) {
            ((ApkUpdateListener)((Iterator)localObject).next()).onCheckUpdateSucceed((ArrayList)paramMessage.obj);
          }
          continue;
          TMLog.i("ApkUpdateSDKMessageHandler", "message type:2");
          paramMessage = this.c.iterator();
          while (paramMessage.hasNext()) {
            ((ApkUpdateListener)paramMessage.next()).onCheckUpdateFailed("UNKOWN");
          }
        }
        TMLog.i("ApkUpdateSDKMessageHandler", "message type:5");
        a((List)paramMessage.obj);
        return;
        TMLog.i("ApkUpdateSDKMessageHandler", "message type:6");
        paramMessage = (ArrayList)paramMessage.obj;
        b.a().a(new com.tencent.tmapkupdatesdk.internal.logic.protocol.a.a(paramMessage));
        return;
        TMLog.i("ApkUpdateSDKMessageHandler", "message type:7");
      } while (b((ArrayList)paramMessage.obj) <= 0);
      paramMessage = a().obtainMessage();
      paramMessage.what = 8;
      paramMessage.obj = null;
      paramMessage.sendToTarget();
      TMLog.i("ApkUpdateSDKMessageHandler", "send Message ApkUpdateMessageHandler.UploadApkDetail");
      return;
      TMLog.i("ApkUpdateSDKMessageHandler", "message type:8");
      if (paramMessage.obj == null)
      {
        b();
        return;
      }
      paramMessage = ((ArrayList)paramMessage.obj).iterator();
      while (paramMessage.hasNext())
      {
        localObject = (String)paramMessage.next();
        a().e.remove(localObject);
      }
    } while (a().e.isEmpty());
    b();
  }
}
