package cn.jiguang.ak; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.text.TextUtils; import cn.jiguang.af.a; import cn.jiguang.o.d; import cn.jiguang.v.c; import cn.jpush.android.service.WakedResultReceiver; import java.util.HashMap; import java.util.List; import java.util.Map; import me.leolin.shortcutbadger.impl.NewHtcHomeBadger; import org.json.JSONArray; import org.json.JSONObject; public class e { public static boolean a = false; private static WakedResultReceiver b; public static JSONArray a(Context context) { JSONObject a2; synchronized ("waked_cache_v2.json") { a2 = c.a(context, "waked_cache_v2.json"); } if (a2 == null) { a2 = new JSONObject(); } JSONArray optJSONArray = a2.optJSONArray("content"); if (optJSONArray != null && optJSONArray.length() != 0) { return a(context, optJSONArray); } a.a("JWakedHelper", "no save data"); return null; } public static JSONArray a(Context context, JSONArray jSONArray) { Map map; Integer valueOf; int i; if (jSONArray != null) { try { if (jSONArray.length() > 0) { HashMap hashMap = new HashMap(); for (int i2 = 0; i2 < jSONArray.length(); i2++) { JSONObject jSONObject = jSONArray.getJSONObject(i2); String optString = jSONObject.optString("type"); if ("android_awake_target2".equals(optString)) { String optString2 = jSONObject.optString("package"); String str = jSONObject.optBoolean("app_alive") ? "active" : "dead"; int optInt = jSONObject.optInt("wake_type"); if (hashMap.containsKey(optString2)) { Map map2 = (Map) hashMap.get(optString2); if (map2.containsKey(str)) { Map map3 = (Map) map2.get(str); if (map3.containsKey(Integer.valueOf(optInt))) { i = Integer.valueOf(((Integer) map3.get(Integer.valueOf(optInt))).intValue() + 1); valueOf = Integer.valueOf(optInt); } else { valueOf = Integer.valueOf(optInt); i = 1; } map3.put(valueOf, i); map = map3; } else { HashMap hashMap2 = new HashMap(); hashMap2.put(Integer.valueOf(optInt), 1); map = hashMap2; } map2.put(str, map == 1 ? 1 : 0); hashMap.put(optString2, map2); } else { HashMap hashMap3 = new HashMap(); hashMap3.put(Integer.valueOf(optInt), 1); HashMap hashMap4 = new HashMap(); hashMap4.put(str, hashMap3); hashMap.put(optString2, hashMap4); } } else { a.d("JWakedHelper", "unkown type :" + optString); } } JSONArray jSONArray2 = new JSONArray(); JSONObject jSONObject2 = new JSONObject(); JSONArray jSONArray3 = new JSONArray(); for (Map.Entry entry : hashMap.entrySet()) { JSONObject jSONObject3 = new JSONObject(); jSONObject3.put("package", entry.getKey()); for (Map.Entry entry2 : ((Map) entry.getValue()).entrySet()) { JSONArray jSONArray4 = new JSONArray(); for (Map.Entry entry3 : ((Map) entry2.getValue()).entrySet()) { JSONObject jSONObject4 = new JSONObject(); jSONObject4.put("wake_type", entry3.getKey()); jSONObject4.put(NewHtcHomeBadger.COUNT, entry3.getValue()); jSONArray4.put(jSONObject4); } jSONObject3.put((String) entry2.getKey(), jSONArray4); } jSONArray3.put(jSONObject3); } if (jSONArray3.length() > 0) { jSONObject2.put("from", jSONArray3); jSONObject2.put("itime", d.h(context)); jSONObject2.put("type", "android_awake_target2"); jSONArray2.put(jSONObject2); } if (jSONArray2.length() > 0) { return jSONArray2; } } } catch (Throwable th) { a.e("JWakedHelper", "merge waked json failed:" + th.getMessage()); } } return jSONArray; } private static JSONObject a(String str, int i, boolean z) { if (str == null) { str = ""; } try { JSONObject jSONObject = new JSONObject(); jSONObject.put("wake_type", i); jSONObject.put("package", str); jSONObject.put("app_alive", z); return jSONObject; } catch (Throwable th) { a.d("JWakedHelper", "packageWakedJson error:" + th.getMessage()); return null; } } private static void a(Context context, int i) { if (context == null) { a.d("JWakedHelper", "context is null,can not notify waked"); return; } b = c(context); if (b == null) { a.d("JWakedHelper", "waked receiver is null"); return; } HashMap hashMap = new HashMap(2); hashMap.put(WakedResultReceiver.CONTEXT_KEY, context); hashMap.put(WakedResultReceiver.WAKE_TYPE_KEY, Integer.valueOf(i)); b.onWakeMap(hashMap); } public static void a(Context context, Bundle bundle, int i) { try { a.a("JWakedHelper", "executeWakedAction."); a(context, i); b(context, bundle, i); } catch (Throwable th) { a.a("JWakedHelper", "executeWakedAction failed:" + th.getLocalizedMessage()); } } private static void a(Context context, JSONObject jSONObject) { if (jSONObject != null) { try { a.a("JWakedHelper", "save waked data : " + jSONObject.toString()); jSONObject.put("itime", d.h(context)); jSONObject.put("type", "android_awake_target2"); synchronized ("waked_cache_v2.json") { JSONObject a2 = c.a(context, "waked_cache_v2.json"); if (a2 == null) { a2 = new JSONObject(); } JSONArray optJSONArray = a2.optJSONArray("content"); if (optJSONArray == null) { optJSONArray = new JSONArray(); } optJSONArray.put(jSONObject); a2.put("content", optJSONArray); c.a(context, "waked_cache_v2.json", a2); } } catch (Throwable th) { a.d("JWakedHelper", "saveWakedData failed:" + th.getMessage()); } } } public static void b(Context context) { synchronized ("waked_cache_v2.json") { c.a(context, "waked_cache_v2.json", (String) null); } } public static void b(Context context, Bundle bundle, int i) { String str; String str2; if (bundle == null) { str = "JWakedHelper"; str2 = "bundle is null,give up save"; } else if (!b.b(context).u) { str = "JWakedHelper"; str2 = "server set do not report waked data,give up save"; } else { String string = bundle.getString("from_package"); if (string == null) { string = ""; } JSONObject a2 = a(string, i, a); a = true; if (a2 != null) { try { String string2 = bundle.getString("jg_extras"); if (!TextUtils.isEmpty(string2)) { a2.put("jg_extras", string2); } } catch (Throwable th) { a.d("JWakedHelper", "save waked extras error:" + th.getMessage()); } a(context, a2); return; } return; } a.d(str, str2); } private static WakedResultReceiver c(Context context) { WakedResultReceiver wakedResultReceiver = b; if (wakedResultReceiver != null) { return wakedResultReceiver; } try { Intent intent = new Intent(); intent.setAction("cn.jpush.android.intent.WakedReceiver"); intent.setPackage(context.getPackageName()); intent.addCategory(context.getPackageName()); List<ResolveInfo> queryBroadcastReceivers = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (queryBroadcastReceivers == null || queryBroadcastReceivers.size() == 0) { return null; } return (WakedResultReceiver) Class.forName(queryBroadcastReceivers.get(0).activityInfo.name).newInstance(); } catch (Throwable th) { a.d("JWakedHelper", "find waked receiver throwable:" + th.getMessage()); return null; } } }