package com.unity3d.ads.api; import android.app.Activity; import android.net.Uri; import com.ubt.jimu.base.entities.Constant; import com.ubt.jimu.controller.data.widget.JockstickDataConverter; import com.unity3d.ads.log.DeviceLog; import com.unity3d.ads.properties.ClientProperties; import com.unity3d.ads.webview.bridge.WebViewCallback; import com.unity3d.ads.webview.bridge.WebViewExposed; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes2.dex */ public class Intent { public enum IntentError { COULDNT_PARSE_EXTRAS, COULDNT_PARSE_CATEGORIES, INTENT_WAS_NULL, JSON_EXCEPTION, ACTIVITY_WAS_NULL } private static class IntentException extends Exception { private IntentError error; private Object field; public IntentException(IntentError intentError, Object obj) { this.error = intentError; this.field = obj; } public IntentError getError() { return this.error; } public Object getField() { return this.field; } } @WebViewExposed public static void canOpenIntent(JSONObject jSONObject, WebViewCallback webViewCallback) { try { webViewCallback.invoke(Boolean.valueOf(checkIntentResolvable(intentFromMetadata(jSONObject)))); } catch (IntentException e) { DeviceLog.exception("Couldn't resolve intent", e); webViewCallback.error(e.getError(), e.getField()); } } @WebViewExposed public static void canOpenIntents(JSONArray jSONArray, WebViewCallback webViewCallback) { JSONObject jSONObject = new JSONObject(); int length = jSONArray.length(); for (int i = 0; i < length; i++) { JSONObject optJSONObject = jSONArray.optJSONObject(i); try { jSONObject.put(optJSONObject.optString(JockstickDataConverter.ID), checkIntentResolvable(intentFromMetadata(optJSONObject))); } catch (IntentException e) { DeviceLog.exception("Exception parsing intent", e); webViewCallback.error(e.getError(), e.getField()); return; } catch (JSONException e2) { webViewCallback.error(IntentError.JSON_EXCEPTION, e2.getMessage()); return; } } webViewCallback.invoke(jSONObject); } private static boolean checkIntentResolvable(android.content.Intent intent) { return ClientProperties.getApplicationContext().getPackageManager().resolveActivity(intent, 0) != null; } private static Activity getStartingActivity() { if (AdUnit.getAdUnitActivity() != null) { return AdUnit.getAdUnitActivity(); } if (ClientProperties.getActivity() != null) { return ClientProperties.getActivity(); } return null; } private static android.content.Intent intentFromMetadata(JSONObject jSONObject) throws IntentException { String str = (String) jSONObject.opt("className"); String str2 = (String) jSONObject.opt(Constant.SelectRobot.PACKAGE_NAME_KEY); String str3 = (String) jSONObject.opt("action"); String str4 = (String) jSONObject.opt("uri"); String str5 = (String) jSONObject.opt("mimeType"); JSONArray jSONArray = (JSONArray) jSONObject.opt("categories"); Integer num = (Integer) jSONObject.opt("flags"); JSONArray jSONArray2 = (JSONArray) jSONObject.opt("extras"); if (str2 != null && str == null && str3 == null && str5 == null) { android.content.Intent launchIntentForPackage = ClientProperties.getApplicationContext().getPackageManager().getLaunchIntentForPackage(str2); if (launchIntentForPackage == null || num.intValue() <= -1) { return launchIntentForPackage; } launchIntentForPackage.addFlags(num.intValue()); return launchIntentForPackage; } android.content.Intent intent = new android.content.Intent(); if (str != null && str2 != null) { intent.setClassName(str2, str); } if (str3 != null) { intent.setAction(str3); } if (str4 != null) { intent.setData(Uri.parse(str4)); } if (str5 != null) { intent.setType(str5); } if (num != null && num.intValue() > -1) { intent.setFlags(num.intValue()); } if (!setCategories(intent, jSONArray)) { throw new IntentException(IntentError.COULDNT_PARSE_CATEGORIES, jSONArray); } if (setExtras(intent, jSONArray2)) { return intent; } throw new IntentException(IntentError.COULDNT_PARSE_EXTRAS, jSONArray2); } @WebViewExposed public static void launch(JSONObject jSONObject, WebViewCallback webViewCallback) { android.content.Intent intent; String str = (String) jSONObject.opt("className"); String str2 = (String) jSONObject.opt(Constant.SelectRobot.PACKAGE_NAME_KEY); String str3 = (String) jSONObject.opt("action"); String str4 = (String) jSONObject.opt("uri"); String str5 = (String) jSONObject.opt("mimeType"); JSONArray jSONArray = (JSONArray) jSONObject.opt("categories"); Integer num = (Integer) jSONObject.opt("flags"); JSONArray jSONArray2 = (JSONArray) jSONObject.opt("extras"); if (str2 != null && str == null && str3 == null && str5 == null) { intent = ClientProperties.getApplicationContext().getPackageManager().getLaunchIntentForPackage(str2); if (intent != null && num.intValue() > -1) { intent.addFlags(num.intValue()); } } else { android.content.Intent intent2 = new android.content.Intent(); if (str != null && str2 != null) { intent2.setClassName(str2, str); } if (str3 != null) { intent2.setAction(str3); } if (str4 != null) { intent2.setData(Uri.parse(str4)); } if (str5 != null) { intent2.setType(str5); } if (num != null && num.intValue() > -1) { intent2.setFlags(num.intValue()); } if (!setCategories(intent2, jSONArray)) { webViewCallback.error(IntentError.COULDNT_PARSE_CATEGORIES, jSONArray); } if (!setExtras(intent2, jSONArray2)) { webViewCallback.error(IntentError.COULDNT_PARSE_EXTRAS, jSONArray2); } intent = intent2; } if (intent == null) { webViewCallback.error(IntentError.INTENT_WAS_NULL, new Object[0]); } else if (getStartingActivity() == null) { webViewCallback.error(IntentError.ACTIVITY_WAS_NULL, new Object[0]); } else { getStartingActivity().startActivity(intent); webViewCallback.invoke(new Object[0]); } } private static boolean setCategories(android.content.Intent intent, JSONArray jSONArray) { if (jSONArray == null || jSONArray.length() <= 0) { return true; } for (int i = 0; i < jSONArray.length(); i++) { try { intent.addCategory(jSONArray.getString(i)); } catch (Exception e) { DeviceLog.exception("Couldn't parse categories for intent", e); return false; } } return true; } private static boolean setExtra(android.content.Intent intent, String str, Object obj) { if (obj instanceof String) { intent.putExtra(str, (String) obj); return true; } if (obj instanceof Integer) { intent.putExtra(str, ((Integer) obj).intValue()); return true; } if (obj instanceof Double) { intent.putExtra(str, ((Double) obj).doubleValue()); return true; } if (obj instanceof Boolean) { intent.putExtra(str, ((Boolean) obj).booleanValue()); return true; } DeviceLog.error("Unable to parse launch intent extra " + str); return false; } private static boolean setExtras(android.content.Intent intent, JSONArray jSONArray) { if (jSONArray == null) { return true; } for (int i = 0; i < jSONArray.length(); i++) { try { JSONObject jSONObject = jSONArray.getJSONObject(i); if (!setExtra(intent, jSONObject.getString("key"), jSONObject.get("value"))) { return false; } } catch (Exception e) { DeviceLog.exception("Couldn't parse extras", e); return false; } } return true; } }