package com.ea.eadp.pushnotification.services; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.util.Base64; import androidx.annotation.NonNull; import com.ea.eadp.deviceid.DeviceIdService; import com.ea.eadp.http.models.HttpRequest; import com.ea.eadp.http.models.HttpRequestListener; import com.ea.eadp.http.models.HttpResponse; import com.ea.eadp.http.services.HttpService; import com.ea.eadp.pushnotification.forwarding.FCMMessageService; import com.ea.eadp.pushnotification.lifecycles.PushLifecycleCallbacks; import com.ea.eadp.pushnotification.listeners.IPushListener; import com.ea.eadp.pushnotification.models.PushNotificationConfig; import com.ea.nimble.Log; import com.ea.nimble.pushtng.PushNotification; import com.google.android.gms.common.GoogleApiAvailabilityLight; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.messaging.FirebaseMessaging; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; /* loaded from: classes2.dex */ public final class AndroidPushService implements IPushService { private static final String API_KEY_KEY = "apiKey"; private static final String API_SECRET_KEY = "apiSecret"; public static final String AUTHORIZATION = "Authorization"; private static final String DEVICE_ID_KEY = "deviceId"; private static final String EVENT_LIST_KEY = "eventListKey"; private static final String GAME_ID_KEY = "gameId"; private static final String LOG_TAG = "PushManager"; private static final String PUSH_SERVER_URL_KEY = "pushNotificationServerUrl"; private static final String SHARED_PREFS_FILENAME = "PushManagerConfigurationData"; private static final String TRACKING_PREFS_FILENAME = "PushManagerTrackingData"; private static final String TRACKING_STATE_KEY = "state"; private final String apiKey; private final String apiSecret; private String appId; private final Context context; private PushNotificationConfig currentConfig; private DeviceIdService deviceIdService; private final String gameId; private final HttpService httpService; private long inAppNotificationInterval; private Timer inAppTimer; private IPushListener pushListener; private final String pushNotificationServerUrl; private String startClientToken; private PushNotificationConfig startConfig; @Override // com.ea.eadp.pushnotification.services.IPushService public IPushListener getPushListener() { return this.pushListener; } @Override // com.ea.eadp.pushnotification.services.IPushService public void setPushListener(IPushListener iPushListener) { this.pushListener = iPushListener; } public AndroidPushService(HttpService httpService, DeviceIdService deviceIdService, Context context, IPushListener iPushListener, String str, String str2, String str3, String str4, String str5, int i) { Log.Helper.LOGIS(LOG_TAG, "Instantiating new push mgr", new Object[0]); this.httpService = httpService; this.deviceIdService = deviceIdService; this.context = context; this.pushListener = iPushListener; this.pushNotificationServerUrl = str; this.gameId = str2; this.appId = str3; this.apiKey = str4; this.apiSecret = str5; this.inAppNotificationInterval = i * 1000; } public AndroidPushService(Context context, HttpService httpService) { this.context = context; this.httpService = httpService; this.pushNotificationServerUrl = loadConfigData(PUSH_SERVER_URL_KEY); this.gameId = loadConfigData("gameId"); this.apiKey = loadConfigData(API_KEY_KEY); this.apiSecret = loadConfigData(API_SECRET_KEY); } @Override // com.ea.eadp.pushnotification.services.IPushService public void startWithConfig(final PushNotificationConfig pushNotificationConfig, final String str) { Log.Helper.LOGPUBLICFUNC(this); if (pushNotificationConfig == null) { Log.Helper.LOGES(LOG_TAG, "Error: Config data is null.", new Object[0]); IPushListener iPushListener = this.pushListener; if (iPushListener != null) { iPushListener.onConnectionError(0, "Config data is null"); return; } return; } if (this.inAppNotificationInterval > 0) { Timer timer = new Timer(); this.inAppTimer = timer; TimerTask timerTask = new TimerTask() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.1 @Override // java.util.TimerTask, java.lang.Runnable public void run() { AndroidPushService.this.getInAppNotifications(pushNotificationConfig.getUserAlias(), str); } }; long j = this.inAppNotificationInterval; timer.schedule(timerTask, j, j); } if (pushNotificationConfig.isDisabled()) { registerDevice(pushNotificationConfig); } else { new Thread(new Runnable() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.2 @Override // java.lang.Runnable public void run() { if (AndroidPushService.this.checkPlayServices()) { Log.Helper.LOGIS(AndroidPushService.LOG_TAG, "Attempt to register device with FCM", new Object[0]); try { FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.2.1 @Override // com.google.android.gms.tasks.OnCompleteListener public void onComplete(@NonNull Task task) { if (!task.isSuccessful()) { Log.Helper.LOGES(AndroidPushService.LOG_TAG, "Failed to get firebase instance id: %s", task.getException().getLocalizedMessage()); pushNotificationConfig.setDisabled(true); pushNotificationConfig.setDisabledReason(PushNotification.DISABLED_REASON_REGISTER_FAILURE); } else { String result = task.getResult(); pushNotificationConfig.setRegistrationIdentifier(result); pushNotificationConfig.setDisabled(false); pushNotificationConfig.setDisabledReason(null); Log.Helper.LOGIS(AndroidPushService.LOG_TAG, String.format("Device registered on FCM. Registration id: %s", result), new Object[0]); try { ((Application) AndroidPushService.this.context.getApplicationContext()).registerActivityLifecycleCallbacks(new PushLifecycleCallbacks()); } catch (Exception e) { Log.Helper.LOGES(AndroidPushService.LOG_TAG, "Failed to register activity lifecycle callbacks: %s", e.getLocalizedMessage()); } } AnonymousClass2 anonymousClass2 = AnonymousClass2.this; AndroidPushService.this.registerDevice(pushNotificationConfig); } }); return; } catch (Exception e) { Log.Helper.LOGES(AndroidPushService.LOG_TAG, "Failed to get registration ID from FCM: %s", e.getLocalizedMessage()); pushNotificationConfig.setDisabled(true); pushNotificationConfig.setDisabledReason(PushNotification.DISABLED_REASON_REGISTER_FAILURE); AndroidPushService.this.registerDevice(pushNotificationConfig); return; } } Log.Helper.LOGES(AndroidPushService.LOG_TAG, "Failed to find appropriate Google Play Service SDK on device.", new Object[0]); pushNotificationConfig.setDisabled(true); pushNotificationConfig.setDisabledReason(PushNotification.DISABLED_REASON_REGISTER_FAILURE); AndroidPushService.this.registerDevice(pushNotificationConfig); } }, "startWithConfig thread").start(); } } @Override // com.ea.eadp.pushnotification.services.IPushService public void updateToken(String str) { PushNotificationConfig pushNotificationConfig = this.currentConfig; if (pushNotificationConfig != null) { pushNotificationConfig.setRegistrationIdentifier(str); registerDevice(this.currentConfig); } } @Override // com.ea.eadp.pushnotification.services.IPushService public void registerDevice(final PushNotificationConfig pushNotificationConfig) { this.currentConfig = pushNotificationConfig; if (pushNotificationConfig == null) { Log.Helper.LOGES(LOG_TAG, "Error: Config data is null.", new Object[0]); IPushListener iPushListener = this.pushListener; if (iPushListener != null) { iPushListener.onConnectionError(0, "Config data is null"); return; } return; } if (pushNotificationConfig.getDeviceIdentifier() == null) { pushNotificationConfig.setDeviceIdentifier(this.deviceIdService.getDeviceId()); } Log.Helper.LOGIS(LOG_TAG, "Attempt to register device with EADP push notification service", new Object[0]); try { try { HttpRequest resource = this.httpService.getResource(String.format("%s/games/%s/devices", this.pushNotificationServerUrl, this.gameId)); resource.setHeader("Authorization", createAuthorizationHeader()); pushNotificationConfig.setAppId(this.appId); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TimeZone.class, new TimeZoneSerializer()); resource.setJsonBody(gsonBuilder.create().toJson(pushNotificationConfig)); resource.postAsync(new HttpRequestListener() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.3 @Override // com.ea.eadp.http.models.HttpRequestListener public void onComplete(HttpResponse httpResponse) { int code = httpResponse.getCode(); String body = httpResponse.getBody(); if (code >= 200 && code < 300) { Log.Helper.LOGIS(AndroidPushService.LOG_TAG, "Registration request successful!", new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, String.format("response: %s", body), new Object[0]); Log.Helper.LOGIS(AndroidPushService.LOG_TAG, String.format("Device registration with server complete. Registered id: %s", pushNotificationConfig.getRegistrationIdentifier()), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onRegistrationSuccess(code, body); return; } return; } String format = String.format("Registration request failed! Status: %s, Message: %s", Integer.valueOf(code), httpResponse.getMessage()); Log.Helper.LOGES(AndroidPushService.LOG_TAG, format, new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, String.format("response: %s", body), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onConnectionError(code, format); } } }); } catch (Exception e) { String format = String.format("Failed to register device with Exception: %s", e.getLocalizedMessage()); Log.Helper.LOGES(LOG_TAG, format, new Object[0]); IPushListener iPushListener2 = this.pushListener; if (iPushListener2 != null) { iPushListener2.onConnectionError(0, format); } } } finally { saveConfigData(pushNotificationConfig); } } @Override // com.ea.eadp.pushnotification.services.IPushService public void sendTrackingEvent(String str, String str2, String str3) { try { HttpRequest resource = this.httpService.getResource(String.format("%s/games/%s/events", this.pushNotificationServerUrl, this.gameId)); TrackingEvent trackingEvent = new TrackingEvent(str, str2, str3, loadConfigData(DEVICE_ID_KEY)); resource.setHeader("Authorization", createAuthorizationHeader()); resource.setJsonBody(new Gson().toJson(trackingEvent)); resource.postAsync(new HttpRequestListener() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.4 @Override // com.ea.eadp.http.models.HttpRequestListener public void onComplete(HttpResponse httpResponse) { int code = httpResponse.getCode(); if (code >= 200 && code < 300) { Log.Helper.LOGIS(AndroidPushService.LOG_TAG, "Tracking request successful!", new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, String.format("response: %s", httpResponse.getBody()), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onTrackingSuccess(code, httpResponse.getBody()); return; } return; } String format = String.format("Tracking request failed! Status: %s, Message: %s", Integer.valueOf(httpResponse.getCode()), httpResponse.getMessage()); Log.Helper.LOGES(AndroidPushService.LOG_TAG, format, new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, String.format("response: %s", httpResponse.getBody()), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onConnectionError(code, format); } } }); } catch (Exception e) { String format = String.format("Tracking request failed with Exception: %s", e.getMessage()); Log.Helper.LOGES(LOG_TAG, format, new Object[0]); IPushListener iPushListener = this.pushListener; if (iPushListener != null) { iPushListener.onConnectionError(0, format); } } } @Override // com.ea.eadp.pushnotification.services.IPushService public void persistTrackingEvent(String str, String str2, String str3) { List arrayList; SharedPreferences sharedPreferences = this.context.getApplicationContext().getSharedPreferences(TRACKING_PREFS_FILENAME, 0); String string = sharedPreferences.getString(EVENT_LIST_KEY, null); if (string != null) { arrayList = (List) new Gson().fromJson(string, List.class); } else { arrayList = new ArrayList(); } HashMap hashMap = new HashMap(); hashMap.put(FCMMessageService.PushIntentExtraKeys.PUSH_ID, str); hashMap.put(FCMMessageService.PushIntentExtraKeys.PN_TYPE, str2); hashMap.put("state", str3); if (arrayList == null) { Log.Helper.LOGE(this, "Unexpected Event List. Skipping Tracking event persistence.", new Object[0]); return; } arrayList.add(hashMap); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString(EVENT_LIST_KEY, new Gson().toJson(arrayList)); edit.apply(); } @Override // com.ea.eadp.pushnotification.services.IPushService public void sendPendingTrackingRequests() { Gson gson = new Gson(); SharedPreferences sharedPreferences = this.context.getSharedPreferences(TRACKING_PREFS_FILENAME, 0); String string = sharedPreferences.getString(EVENT_LIST_KEY, null); if (string != null) { List list = (List) gson.fromJson(string, List.class); if (list == null) { Log.Helper.LOGE(this, "Event List from Json is null!", new Object[0]); return; } for (Map map : list) { if (map != null && !map.isEmpty()) { sendTrackingEvent((String) map.get(FCMMessageService.PushIntentExtraKeys.PUSH_ID), (String) map.get(FCMMessageService.PushIntentExtraKeys.PN_TYPE), (String) map.get("state")); } } } SharedPreferences.Editor edit = sharedPreferences.edit(); edit.clear(); edit.apply(); } @Override // com.ea.eadp.pushnotification.services.IPushService public void getInAppNotifications(final String str, final String str2) { if (str != null) { new Thread(new Runnable() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.5 @Override // java.lang.Runnable public void run() { int i; HttpResponse httpResponse; try { HttpRequest resource = AndroidPushService.this.httpService.getResource(String.format("%s/games/%s/users/%s/inapp", AndroidPushService.this.pushNotificationServerUrl, AndroidPushService.this.gameId, str)); if (str2 != null) { resource.setHeader("Authorization", "Bearer " + str2); } httpResponse = resource.get(); i = httpResponse.getCode(); } catch (Exception e) { e = e; i = 0; } try { if (i >= 200 && i < 300) { Log.Helper.LOGIS(AndroidPushService.LOG_TAG, "Get In-App Notification request successful!", new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, "response: " + httpResponse.getBody(), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onGetInAppSuccess(i, httpResponse.getBody()); } } else { String format = String.format("Get In-App Notification request failed! Status: %s, Message: %s", Integer.valueOf(httpResponse.getCode()), httpResponse.getMessage()); Log.Helper.LOGES(AndroidPushService.LOG_TAG, format, new Object[0]); Log.Helper.LOGDS(AndroidPushService.LOG_TAG, String.format("response: %s", httpResponse.getBody()), new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onConnectionError(i, format); } } } catch (Exception e2) { e = e2; String format2 = String.format("In-App Notification request failed with Exception: %s", e.getMessage()); Log.Helper.LOGES(AndroidPushService.LOG_TAG, format2, new Object[0]); if (AndroidPushService.this.pushListener != null) { AndroidPushService.this.pushListener.onConnectionError(i, format2); } } } }, "getInAppNotifications thread").start(); return; } IPushListener iPushListener = this.pushListener; if (iPushListener != null) { iPushListener.onConnectionError(0, "UserAlias is null"); } } @Override // com.ea.eadp.pushnotification.services.IPushService public void onRestart() { if (this.inAppTimer == null || this.inAppNotificationInterval <= 0) { return; } Timer timer = new Timer(); this.inAppTimer = timer; TimerTask timerTask = new TimerTask() { // from class: com.ea.eadp.pushnotification.services.AndroidPushService.6 @Override // java.util.TimerTask, java.lang.Runnable public void run() { AndroidPushService androidPushService = AndroidPushService.this; androidPushService.getInAppNotifications(androidPushService.startConfig.getUserAlias(), AndroidPushService.this.startClientToken); } }; long j = this.inAppNotificationInterval; timer.schedule(timerTask, j, j); } @Override // com.ea.eadp.pushnotification.services.IPushService public void onStop() { Timer timer = this.inAppTimer; if (timer != null) { timer.cancel(); } } /* JADX INFO: Access modifiers changed from: private */ public boolean checkPlayServices() { return GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(this.context) == 0; } private void saveConfigData(PushNotificationConfig pushNotificationConfig) { SharedPreferences.Editor edit = this.context.getApplicationContext().getSharedPreferences(SHARED_PREFS_FILENAME, 0).edit(); if (pushNotificationConfig.getDeviceIdentifier() != null && !pushNotificationConfig.getDeviceIdentifier().equals("")) { edit.putString(DEVICE_ID_KEY, pushNotificationConfig.getDeviceIdentifier()); } String str = this.pushNotificationServerUrl; if (str != null && !str.equals("")) { edit.putString(PUSH_SERVER_URL_KEY, this.pushNotificationServerUrl); } String str2 = this.gameId; if (str2 != null && !str2.equals("")) { edit.putString("gameId", this.gameId); } String str3 = this.apiKey; if (str3 != null && !str3.equals("")) { edit.putString(API_KEY_KEY, this.apiKey); } String str4 = this.apiSecret; if (str4 != null && !str4.equals("")) { edit.putString(API_SECRET_KEY, this.apiSecret); } edit.apply(); } private String loadConfigData(String str) { return this.context.getApplicationContext().getSharedPreferences(SHARED_PREFS_FILENAME, 0).getString(str, null); } private String createAuthorizationHeader() { return String.format("Basic %s", Base64.encodeToString((this.apiKey + ':' + this.apiSecret).getBytes(StandardCharsets.UTF_8), 10)); } public static class TimeZoneSerializer implements JsonSerializer { private TimeZoneSerializer() { } @Override // com.google.gson.JsonSerializer public JsonElement serialize(TimeZone timeZone, Type type, JsonSerializationContext jsonSerializationContext) { return new JsonPrimitive(timeZone.getID()); } } }