package com.meishu.sdk.core.utils; import android.app.Activity; import android.app.Application; import android.content.Context; import android.os.Bundle; import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.gson.Gson; import com.meishu.sdk.core.AdSdk; import com.meishu.sdk.core.ad.AdType; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import org.jetbrains.annotations.NotNull; public class TestToolUtil { public static final String CONFIG_PID = "config_pid"; private static final String TAG = "TestToolUtil"; public static String bannerFileName = "banner.json"; public static String drawFileName = "draw.json"; public static String feedFileName = "feed.json"; public static String fullscreenFileName = "fullscreen.json"; public static String intertitialFileName = "intertitial.json"; private static boolean isLoaded = false; private static int mActivityCount = 0; public static String rewardFileName = "reward.json"; public static String splashFileName = "splash.json"; public static String testBannerJson; public static String testDrawJson; public static String testSplashJson; public static String testfeedJson; public static String testfullscreenJson; public static String testintertitialJson; public static String testrewardJson; static /* synthetic */ int access$008() { int i2 = mActivityCount; mActivityCount = i2 + 1; return i2; } static /* synthetic */ int access$010() { int i2 = mActivityCount; mActivityCount = i2 - 1; return i2; } public static String getAdTypeText(AdType adType) { if (adType != null) { switch (adType.value()) { case 3: return "Banner"; case 4: return "开屏"; case 5: return "插屏"; case 7: return "激励视频"; case 8: return "Draw"; case 9: return "全屏视频"; case 10: return "信息流"; } } return ""; } private static String getEtag(Context context) { String string = PreferenceUtil.getString(context, CONFIG_PID, ""); try { if (!TextUtils.isEmpty(string)) { return ((TestToolBean) new Gson().fromJson(string, TestToolBean.class)).getEtag(); } } catch (Exception e2) { e2.printStackTrace(); } return ""; } public static String getJson(Context context, String str) { StringBuilder sb = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(str), "utf-8")); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } sb.append(readLine.trim()); } } catch (IOException e2) { e2.printStackTrace(); } return sb.toString(); } public static String getJsonByType(Context context, AdType adType, boolean z) { if (adType != null) { switch (adType.value()) { case 3: return getTestBannerJson(context, z); case 4: return getTestSplashJson(context, z); case 5: return getTestIntertitialJson(context, z); case 7: return getTestRewardJson(context, z); case 8: return getTestDrawJson(context, z); case 9: return getTestFullscreenJson(context, z); case 10: return getTestFeedJson(context, z); } } return null; } public static String getTestBannerJson(Context context, boolean z) { if (z) { String json = getJson(context, "banner.json"); testBannerJson = json; saveToLocal(context, json, bannerFileName); } else if (TextUtils.isEmpty(testBannerJson)) { String readJsonFile = readJsonFile(context, bannerFileName); if (!TextUtils.isEmpty(readJsonFile)) { testBannerJson = readJsonFile; } else { testBannerJson = getJson(context, "banner.json"); } } return testBannerJson; } public static void getTestData(final Context context) { if (!isLoaded) { setApplicationListener(context); isLoaded = true; String localOaid = AdSdk.getLocalOaid(); String appId = AdSdk.adConfig().appId(); String etag = getEtag(context); String str = "https://sdk.1rtb.net/sdk/places?app_id=" + appId + "&device_id=" + localOaid + "&os=android&version=1.0.0.0"; if (!TextUtils.isEmpty(etag)) { str = str + "&etag=" + etag; } HttpUtil.asyncGetJson(str, null, new HttpGetJsonCallback<OriginalResponse>() { /* class com.meishu.sdk.core.utils.TestToolUtil.AnonymousClass1 */ @Override // com.meishu.sdk.core.utils.HttpGetJsonCallback public void onFailure(@NotNull IOException iOException) { LogUtil.e(TestToolUtil.TAG, iOException.toString()); } public void onResponse(OriginalResponse originalResponse) throws IOException { String str = null; try { if (originalResponse.getCode() == 200) { str = originalResponse.getBody(); PreferenceUtil.putString(context, TestToolUtil.CONFIG_PID, str); PreferenceUtil.putString(context, "pid_list", ""); } else if (originalResponse.getCode() == 304) { str = PreferenceUtil.getString(context, TestToolUtil.CONFIG_PID, ""); } else if (originalResponse.getCode() == 204) { AdSdk.isTestMode = false; } if (!TextUtils.isEmpty(str)) { if ("0".equals(((TestToolBean) new Gson().fromJson(str, TestToolBean.class)).getTest_status())) { AdSdk.isTestMode = false; } else { AdSdk.isTestMode = true; } if (AdSdk.isTestMode) { UiUtil.showFloatButton(context); } } } catch (Exception e2) { e2.printStackTrace(); } } }); } } public static String getTestDrawJson(Context context, boolean z) { if (z) { String json = getJson(context, "draw.json"); testDrawJson = json; saveToLocal(context, json, drawFileName); } else if (TextUtils.isEmpty(testDrawJson)) { String readJsonFile = readJsonFile(context, drawFileName); if (!TextUtils.isEmpty(readJsonFile)) { testDrawJson = readJsonFile; } else { testDrawJson = getJson(context, "draw.json"); } } return testDrawJson; } public static String getTestFeedJson(Context context, boolean z) { if (z) { String json = getJson(context, "feed.json"); testfeedJson = json; saveToLocal(context, json, feedFileName); } else if (TextUtils.isEmpty(testfeedJson)) { String readJsonFile = readJsonFile(context, feedFileName); if (!TextUtils.isEmpty(readJsonFile)) { testfeedJson = readJsonFile; } else { testfeedJson = getJson(context, "feed.json"); } } return testfeedJson; } public static String getTestFullscreenJson(Context context, boolean z) { if (z) { String json = getJson(context, "fullscreen.json"); testfullscreenJson = json; saveToLocal(context, json, fullscreenFileName); } else if (TextUtils.isEmpty(testfullscreenJson)) { String readJsonFile = readJsonFile(context, fullscreenFileName); if (!TextUtils.isEmpty(readJsonFile)) { testfullscreenJson = readJsonFile; } else { testfullscreenJson = getJson(context, "fullscreen.json"); } } return testfullscreenJson; } public static String getTestIntertitialJson(Context context, boolean z) { if (z) { String json = getJson(context, "intertitial.json"); testintertitialJson = json; saveToLocal(context, json, intertitialFileName); } else if (TextUtils.isEmpty(testintertitialJson)) { String readJsonFile = readJsonFile(context, intertitialFileName); if (!TextUtils.isEmpty(readJsonFile)) { testintertitialJson = readJsonFile; } else { testintertitialJson = getJson(context, "intertitial.json"); } } return testintertitialJson; } public static String getTestRewardJson(Context context, boolean z) { if (z) { String json = getJson(context, "reward.json"); testrewardJson = json; saveToLocal(context, json, rewardFileName); } else if (TextUtils.isEmpty(testrewardJson)) { String readJsonFile = readJsonFile(context, rewardFileName); if (!TextUtils.isEmpty(readJsonFile)) { testrewardJson = readJsonFile; } else { testrewardJson = getJson(context, "reward.json"); } } return testrewardJson; } public static String getTestSplashJson(Context context, boolean z) { if (z) { String json = getJson(context, "splash.json"); testSplashJson = json; saveToLocal(context, json, splashFileName); } else if (TextUtils.isEmpty(testSplashJson)) { String readJsonFile = readJsonFile(context, splashFileName); if (!TextUtils.isEmpty(readJsonFile)) { testSplashJson = readJsonFile; } else { testSplashJson = getJson(context, "splash.json"); } } return testSplashJson; } public static String readJsonFile(Context context, String str) { StringBuilder sb = new StringBuilder(); try { FileInputStream fileInputStream = new FileInputStream(new File(context.getExternalCacheDir() + "/.adConfig/jsonConfig/", str)); while (true) { int read = fileInputStream.read(); if (read == -1) { break; } sb.append((char) read); } fileInputStream.close(); } catch (Exception e2) { e2.printStackTrace(); } return sb.toString(); } public static String saveToLocal(Context context, String str, String str2) { File file = new File(context.getExternalCacheDir() + "/.adConfig/jsonConfig/"); try { if (!file.exists()) { file.mkdirs(); File file2 = new File(file, str2); FileOutputStream fileOutputStream = new FileOutputStream(file2); fileOutputStream.write(str.getBytes()); fileOutputStream.close(); return file2.getPath(); } return file + "/" + str2; } catch (Exception e2) { e2.printStackTrace(); return ""; } } private static void setApplicationListener(Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext instanceof Application) { ((Application) applicationContext).registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { /* class com.meishu.sdk.core.utils.TestToolUtil.AnonymousClass2 */ public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) { } public void onActivityDestroyed(@NonNull Activity activity) { } public void onActivityPaused(@NonNull Activity activity) { } public void onActivityResumed(@NonNull Activity activity) { } public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) { } public void onActivityStarted(@NonNull Activity activity) { TestToolUtil.access$008(); if (AdSdk.isTestMode && TestToolUtil.mActivityCount > 0) { try { Class.forName("com.ms.test_tools.view.FloatingView").getMethod("reShow", new Class[0]).invoke(null, new Object[0]); } catch (Exception e2) { e2.printStackTrace(); } } } public void onActivityStopped(@NonNull Activity activity) { TestToolUtil.access$010(); if (AdSdk.isTestMode && TestToolUtil.mActivityCount <= 0) { try { Class.forName("com.ms.test_tools.view.FloatingView").getMethod("hide", new Class[0]).invoke(null, new Object[0]); } catch (Exception e2) { e2.printStackTrace(); } } } }); } } public static void setJsonByType(Context context, AdType adType, String str) { if (adType != null) { switch (adType.value()) { case 3: setTestBannerJson(context, str); return; case 4: setTestSplashJson(context, str); return; case 5: setTestIntertitialJson(context, str); return; case 6: default: return; case 7: setTestRewardJson(context, str); return; case 8: setTestDrawJson(context, str); return; case 9: setTestFullscreenJson(context, str); return; case 10: setTestFeedJson(context, str); return; } } } public static void setTestBannerJson(Context context, String str) { testBannerJson = str; saveToLocal(context, str, bannerFileName); } public static void setTestDrawJson(Context context, String str) { testDrawJson = str; saveToLocal(context, str, drawFileName); } public static void setTestFeedJson(Context context, String str) { testfeedJson = str; saveToLocal(context, str, feedFileName); } public static void setTestFullscreenJson(Context context, String str) { testfullscreenJson = str; saveToLocal(context, str, fullscreenFileName); } public static void setTestIntertitialJson(Context context, String str) { testintertitialJson = str; saveToLocal(context, str, intertitialFileName); } public static void setTestRewardJson(Context context, String str) { testrewardJson = str; saveToLocal(context, str, rewardFileName); } public static void setTestSplashJson(Context context, String str) { testSplashJson = str; saveToLocal(context, str, splashFileName); } }