package com.tencent.mm.plugin.ext;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Looper;
import android.os.Message;
import com.jg.EType;
import com.jg.JgMethodChecked;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.a.a;
import com.tencent.mm.model.aw;
import com.tencent.mm.model.c;
import com.tencent.mm.model.t;
import com.tencent.mm.pluginsdk.model.app.am;
import com.tencent.mm.pluginsdk.model.app.d;
import com.tencent.mm.pluginsdk.model.app.x;
import com.tencent.mm.pluginsdk.model.i;
import com.tencent.mm.pluginsdk.model.i$a;
import com.tencent.mm.pluginsdk.model.l;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ah;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.storage.bo;
import com.tencent.mm.storage.bz;
import com.tencent.mm.storage.ca;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map.Entry;

class b$7 extends ak {
    final /* synthetic */ b lPx;

    b$7(b bVar, Looper looper) {
        this.lPx = bVar;
        super(looper);
    }

    @JgMethodChecked(author = 20, fComment = "checked", lastDate = "20140514", reviewer = 20, vComment = {EType.INTENTCHECK})
    public final void handleMessage(Message message) {
        AppMethodBeat.i(20266);
        if (ah.getContext() == null || !aw.RK()) {
            ab.w("MicroMsg.SubCoreExt", "notifyMsgChangeHandler wrong status");
            AppMethodBeat.o(20266);
            return;
        }
        Cursor dui = b.brA().dui();
        if (dui == null) {
            ab.i("MicroMsg.SubCoreExt", "notifyMsgChangeHandler getValidOpenMsgListener cu == null");
            AppMethodBeat.o(20266);
            return;
        }
        ab.d("MicroMsg.SubCoreExt", "notifyMsgChangeHandler listener count = %s", new Object[]{Integer.valueOf(dui.getCount())});
        if (dui.getCount() <= 0) {
            dui.close();
            ab.i("MicroMsg.SubCoreExt", "notifyMsgChangeHandler cu.getCount() <= 0");
            AppMethodBeat.o(20266);
            return;
        }
        try {
            if (dui.moveToFirst()) {
                HashMap hashMap = new HashMap();
                synchronized (this.lPx) {
                    hashMap.putAll(b.a(this.lPx));
                    b.a(this.lPx).clear();
                }
                do {
                    LinkedList linkedList = new LinkedList();
                    ArrayList arrayList = new ArrayList();
                    if (hashMap.entrySet() == null) {
                        ab.w("MicroMsg.SubCoreExt", "notifyMsgChangeHandler waitForNotify.entrySet() == null");
                        AppMethodBeat.o(20266);
                        return;
                    }
                    bo boVar = new bo();
                    boVar.d(dui);
                    if (com.tencent.mm.sdk.platformtools.bo.isNullOrNil(boVar.field_appId) || com.tencent.mm.sdk.platformtools.bo.isNullOrNil(boVar.field_packageName)) {
                        ab.w("MicroMsg.SubCoreExt", "notifyMsgChangeHandler wrong msgListener, appid = %s, package = %s", new Object[]{boVar.field_appId, boVar.field_packageName});
                    } else {
                        for (Entry entry : hashMap.entrySet()) {
                            String nullAsNil = com.tencent.mm.sdk.platformtools.bo.nullAsNil((String) entry.getKey());
                            Integer num = (Integer) entry.getValue();
                            aw.ZK();
                            com.tencent.mm.storage.ak aoZ = c.XR().aoZ(nullAsNil);
                            if (aoZ != null && !com.tencent.mm.sdk.platformtools.bo.isNullOrNil(aoZ.field_username) && aoZ.field_unReadCount <= 0) {
                                ab.i("MicroMsg.SubCoreExt", "notifyMsgChangeHandler no unread in conversation");
                            } else if (!(t.nJ(nullAsNil) || t.kH(nullAsNil) || t.mZ(nullAsNil))) {
                                bz bzVar;
                                ca brB = b.brB();
                                String str = boVar.field_appId;
                                if (str == null || str.length() <= 0 || nullAsNil == null || nullAsNil.length() <= 0) {
                                    bzVar = null;
                                } else {
                                    Cursor a = brB.bSd.a("UserOpenIdInApp", null, "appId=? and username=? ", new String[]{com.tencent.mm.sdk.platformtools.bo.vA(str), com.tencent.mm.sdk.platformtools.bo.vA(nullAsNil)}, null, null, null, 2);
                                    if (a.moveToFirst()) {
                                        bzVar = new bz();
                                        bzVar.d(a);
                                        a.close();
                                    } else {
                                        ab.w("MicroMsg.scanner.UserOpenIdInAppStorage", "get null with appId-%s, username-%s:", new Object[]{str, nullAsNil});
                                        a.close();
                                        bzVar = null;
                                    }
                                }
                                if (bzVar == null || com.tencent.mm.sdk.platformtools.bo.isNullOrNil(bzVar.field_openId) || com.tencent.mm.sdk.platformtools.bo.isNullOrNil(bzVar.field_username)) {
                                    ab.d("MicroMsg.SubCoreExt", "notifyMsgChangeHandler openId is null, go get it.");
                                    Integer num2 = (Integer) b.a(this.lPx).get(nullAsNil);
                                    if (num2 == null) {
                                        b.a(this.lPx).put(nullAsNil, num);
                                    } else {
                                        b.a(this.lPx).put(nullAsNil, Integer.valueOf(num2.intValue() + num.intValue()));
                                    }
                                    linkedList.add(nullAsNil);
                                } else {
                                    ab.v("MicroMsg.SubCoreExt", "username[%s], appId[%s], openId[%s]", new Object[]{bzVar.field_username, bzVar.field_appId, bzVar.field_openId});
                                    arrayList.add(String.format("%s,%s,%s", new Object[]{bzVar.field_openId, num, Long.valueOf(System.currentTimeMillis() - 1600)}));
                                }
                            }
                        }
                        if (linkedList.size() > 0) {
                            ab.i("MicroMsg.SubCoreExt", "notifyMsgChangeHandler doGetGetUserInfoInApp");
                            b bVar = this.lPx;
                            String str2 = boVar.field_appId;
                            if (!com.tencent.mm.sdk.platformtools.bo.isNullOrNil(str2) && linkedList.size() > 0) {
                                i dgZ = i.dgZ();
                                Context context = ah.getContext();
                                i$a i_a = bVar.lPo;
                                ab.d("MicroMsg.GetUserInfoInAppLogic", "getGetUserInfoInApp");
                                if (!i.vbc) {
                                    dgZ.dha();
                                }
                                if (aw.RK() && context != null) {
                                    if (linkedList.size() == 0) {
                                        ab.d("MicroMsg.GetUserInfoInAppLogic", "userNameList empty");
                                    } else {
                                        dgZ.vbe = i_a;
                                        x lVar = new l(str2, linkedList);
                                        am.bYM();
                                        d.a(14, lVar);
                                    }
                                }
                            }
                        }
                        if (arrayList.size() <= 0) {
                            dui.close();
                            ab.i("MicroMsg.SubCoreExt", "notifyMsgChangeHandler userDataList.size() <= 0");
                            AppMethodBeat.o(20266);
                            return;
                        }
                        Object obj;
                        if (boVar.field_status == 1) {
                            obj = 1;
                        } else {
                            obj = null;
                        }
                        if (obj == null) {
                            ab.w("MicroMsg.SubCoreExt", "invalid msgListener, appid = %s, package = %s", new Object[]{boVar.field_appId, boVar.field_packageName});
                        } else if (!(com.tencent.mm.sdk.platformtools.bo.isNullOrNil(boVar.field_appId) || com.tencent.mm.sdk.platformtools.bo.isNullOrNil(boVar.field_packageName))) {
                            ab.i("MicroMsg.SubCoreExt", "notify app, appId = %s, package = %s", new Object[]{boVar.field_appId, boVar.field_packageName});
                            Intent intent = new Intent("com.tencent.mm.plugin.openapi.Intent.ACTION_NOTIFY_MSG");
                            intent.addCategory("com.tencent.mm.category." + boVar.field_packageName);
                            a.a(12, new 1(this, intent));
                            intent.putExtra("EXTRA_EXT_OPEN_NOTIFY_TYPE", "NEW_MESSAGE");
                            intent.putStringArrayListExtra("EXTRA_EXT_OPEN_USER_DATA", arrayList);
                            ah.getContext().sendBroadcast(intent);
                        }
                    }
                } while (dui.moveToNext());
            }
            dui.close();
            AppMethodBeat.o(20266);
        } catch (Exception e) {
            ab.e("MicroMsg.SubCoreExt", "Exception in handleMessage, %s", new Object[]{e.getMessage()});
            if (dui != null) {
                dui.close();
            }
            AppMethodBeat.o(20266);
        } catch (Throwable th) {
            while (true) {
                AppMethodBeat.o(20266);
                throw th;
            }
        }
    }
}
