package com.kwad.sdk.utils; import android.content.Context; import android.content.SharedPreferences; import android.text.TextUtils; import android.util.Log; import com.kwad.sdk.core.e.b; import com.kwad.sdk.d; import com.kwad.sdk.service.ServiceProvider; import com.kwad.sdk.service.kwai.f; import com.kwad.sdk.utils.a.a; import com.kwad.sdk.utils.kwai.c; import com.kwad.sdk.utils.kwai.e; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; public final class w { private static AtomicInteger bBr = new AtomicInteger(0); private static volatile boolean bBs = false; private static volatile boolean bBt; /* access modifiers changed from: private */ public static void W(Context context, String str) { c aD = e.aD(context, str); if (!aD.contains("sp_to_kv_transfer_flag")) { SharedPreferences fz = ba.fz(str); if (fz == null) { aD.putBoolean("sp_to_kv_transfer_flag", true); return; } aD.putAll(fz.getAll()); aD.putBoolean("sp_to_kv_transfer_flag", true); ba.ah(str, "kv_to_sp_transfer_flag"); a(str, aD); } } /* access modifiers changed from: private */ public static void X(Context context, String str) { SharedPreferences fz = ba.fz(str); if (fz != null && !fz.contains("kv_to_sp_transfer_flag")) { c aD = e.aD(context, str); Map<String, Object> all = aD.getAll(); if (all.isEmpty()) { ba.i(str, "kv_to_sp_transfer_flag", true); return; } ba.b(str, all); ba.i(str, "kv_to_sp_transfer_flag", true); aD.remove("sp_to_kv_transfer_flag"); aD.release(); } } private static int Yj() { f fVar = (f) ServiceProvider.get(f.class); if (fVar == null) { b.w("Ks_UnionHelper", "sdkConfigProvider == null"); return 0; } int Jw = fVar.Jw(); b.d("Ks_UnionHelper", "config mode:" + Jw); return Jw; } private static void Yk() { if (!bBs) { bBr.set(Yn()); bBt = ba.j("kssdk_kv_mode", "downgrade", false); bBs = true; } } public static void Yl() { if (!Yo() && Yj() != 0) { com.kwad.sdk.core.threads.b.Oo().execute(new Runnable() { /* class com.kwad.sdk.utils.w.AnonymousClass3 */ public final void run() { a Ym = w.Ym(); if (Ym != null) { w.c(Ym); com.kwad.sdk.utils.a.b bVar = (com.kwad.sdk.utils.a.b) ServiceProvider.get(com.kwad.sdk.utils.a.b.class); if (bVar != null) { bVar.a(Ym); } } } }); } } public static a Ym() { a aVar = new a(); SharedPreferences fz = ba.fz("ksadsdk_kv_perf"); if (fz == null) { return null; } try { Map<String, ?> all = fz.getAll(); if (all == null) { return null; } int i = 0; for (Map.Entry<String, ?> entry : all.entrySet()) { i += ((Integer) entry.getValue()).intValue(); } aVar.bEi = i; SharedPreferences.Editor edit = fz.edit(); for (Map.Entry<String, ?> entry2 : all.entrySet()) { edit.putInt(entry2.getKey(), 0); } edit.apply(); d(aVar); e(aVar); return aVar; } catch (Throwable unused) { } } private static int Yn() { int c = ba.c("kssdk_kv_mode", "mode", 0); b.d("Ks_UnionHelper", "local mode:" + c); return c; } public static boolean Yo() { Yk(); return bBr.get() == 0; } private static int Yp() { Yk(); int Yj = bBt ? 0 : Yj(); b.d("Ks_UnionHelper", "targetMode:" + Yj); return Yj; } public static void Yq() { Yk(); int i = bBr.get(); int Yp = Yp(); boolean z = i != Yp; b.d("Ks_UnionHelper", "needTransfer:" + z); if (z) { transfer(Yp); } } private static void Yr() { com.kwad.sdk.core.threads.b.Oo().execute(new Runnable() { /* class com.kwad.sdk.utils.w.AnonymousClass4 */ public final void run() { try { Context context = x.getContext(); if (context != null) { for (String str : d.a.aPp) { w.X(context, str); } w.ee(0); w.bBr.set(0); } } catch (Exception e) { b.e("Ks_UnionHelper", Log.getStackTraceString(e)); } } }); } private static void Ys() { b.d("Ks_UnionHelper", "transferToKv"); com.kwad.sdk.core.threads.b.Oo().execute(new Runnable() { /* class com.kwad.sdk.utils.w.AnonymousClass5 */ public final void run() { try { Context context = x.getContext(); if (context != null) { for (String str : d.a.aPp) { w.W(context, str); } w.ee(1); w.bBr.set(1); } } catch (Exception e) { b.e("Ks_UnionHelper", Log.getStackTraceString(e)); } } }); } private static void a(String str, c cVar) { if (!d.a.aPq.contains(str)) { cVar.release(); } } public static void ac(final String str, final String str2) { com.kwad.sdk.core.threads.b.Oo().execute(new Runnable() { /* class com.kwad.sdk.utils.w.AnonymousClass1 */ public final void run() { int c = ba.c("ksadsdk_kv_perf", str, 0); if (TextUtils.isEmpty(str2)) { ba.ah("ksadsdk_kv_perf", str); return; } ba.b("ksadsdk_kv_perf", str, c + 1); } }); } public static void ad(final String str, final String str2) { com.kwad.sdk.core.threads.b.Oo().execute(new Runnable() { /* class com.kwad.sdk.utils.w.AnonymousClass2 */ public final void run() { SharedPreferences fz = ba.fz("ksadsdk_kv_perf"); if (fz == null || !fz.contains(str)) { return; } if (TextUtils.isEmpty(str2)) { ba.b("ksadsdk_kv_perf_failed", str, ba.c("ksadsdk_kv_perf_failed", str, 0) + 1); return; } ba.b("ksadsdk_kv_perf_success", str, ba.c("ksadsdk_kv_perf_success", str, 0) + 1); } }); } /* access modifiers changed from: private */ public static void c(a aVar) { if (((double) aVar.bEj) / ((double) (aVar.bEj + aVar.bEk)) > 0.10000000149011612d) { bBt = true; b.d("Ks_UnionHelper", "need downgrade"); ba.i("kssdk_kv_mode", "downgrade", true); } if (bBt) { Yq(); } } private static void d(a aVar) { SharedPreferences fz = ba.fz("ksadsdk_kv_perf_failed"); int i = 0; if (fz != null) { Map<String, ?> all = fz.getAll(); if (all != null) { for (Map.Entry<String, ?> entry : all.entrySet()) { i += ((Integer) entry.getValue()).intValue(); } } aVar.bEj = i; SharedPreferences.Editor edit = fz.edit(); edit.clear(); edit.apply(); return; } aVar.bEj = 0; } private static void e(a aVar) { SharedPreferences fz = ba.fz("ksadsdk_kv_perf_success"); int i = 0; if (fz != null) { Map<String, ?> all = fz.getAll(); if (all != null) { for (Map.Entry<String, ?> entry : all.entrySet()) { i += ((Integer) entry.getValue()).intValue(); } } aVar.bEk = i; SharedPreferences.Editor edit = fz.edit(); edit.clear(); edit.apply(); return; } aVar.bEk = 0; } static void ee(int i) { ba.b("kssdk_kv_mode", "mode", i); } private static void transfer(int i) { if (i == 0) { Yr(); } else if (i == 1) { Ys(); } } }