package com.google.firebase.messaging; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.PowerManager; import android.util.Log; import java.io.IOException; /* loaded from: classes3.dex */ public class TopicsSyncTask implements Runnable { public static final Object TOPIC_SYNC_TASK_LOCK = new Object(); public static Boolean hasAccessNetworkStatePermission; public static Boolean hasWakeLockPermission; public final Context context; public final Metadata metadata; public final long nextDelaySeconds; public final PowerManager.WakeLock syncWakeLock; public final TopicsSubscriber topicsSubscriber; public TopicsSyncTask(TopicsSubscriber topicsSubscriber, Context context, Metadata metadata, long j) { this.topicsSubscriber = topicsSubscriber; this.context = context; this.nextDelaySeconds = j; this.metadata = metadata; this.syncWakeLock = ((PowerManager) context.getSystemService("power")).newWakeLock(1, "wake:com.google.firebase.messaging"); } @Override // java.lang.Runnable public void run() { if (hasWakeLockPermission(this.context)) { this.syncWakeLock.acquire(Constants.WAKE_LOCK_ACQUIRE_TIMEOUT_MILLIS); } try { try { this.topicsSubscriber.setSyncScheduledOrRunning(true); } catch (Throwable th) { if (hasWakeLockPermission(this.context)) { try { this.syncWakeLock.release(); } catch (RuntimeException unused) { } } throw th; } } catch (IOException e) { Log.e("FirebaseMessaging", "Failed to sync topics. Won't retry sync. " + e.getMessage()); this.topicsSubscriber.setSyncScheduledOrRunning(false); if (!hasWakeLockPermission(this.context)) { return; } } if (!this.metadata.isGmscorePresent()) { this.topicsSubscriber.setSyncScheduledOrRunning(false); if (hasWakeLockPermission(this.context)) { try { this.syncWakeLock.release(); return; } catch (RuntimeException unused2) { return; } } return; } if (hasAccessNetworkStatePermission(this.context) && !isDeviceConnected()) { new ConnectivityChangeReceiver(this).registerReceiver(); if (hasWakeLockPermission(this.context)) { try { this.syncWakeLock.release(); return; } catch (RuntimeException unused3) { return; } } return; } if (this.topicsSubscriber.syncTopics()) { this.topicsSubscriber.setSyncScheduledOrRunning(false); } else { this.topicsSubscriber.syncWithDelaySecondsInternal(this.nextDelaySeconds); } if (!hasWakeLockPermission(this.context)) { return; } try { this.syncWakeLock.release(); } catch (RuntimeException unused4) { } } public final synchronized boolean isDeviceConnected() { boolean z; try { ConnectivityManager connectivityManager = (ConnectivityManager) this.context.getSystemService("connectivity"); NetworkInfo activeNetworkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null; if (activeNetworkInfo != null) { z = activeNetworkInfo.isConnected(); } } catch (Throwable th) { throw th; } return z; } public static boolean isLoggable() { return Log.isLoggable("FirebaseMessaging", 3); } public static boolean hasWakeLockPermission(Context context) { boolean booleanValue; boolean booleanValue2; synchronized (TOPIC_SYNC_TASK_LOCK) { try { Boolean bool = hasWakeLockPermission; if (bool == null) { booleanValue = hasPermission(context, "android.permission.WAKE_LOCK", bool); } else { booleanValue = bool.booleanValue(); } Boolean valueOf = Boolean.valueOf(booleanValue); hasWakeLockPermission = valueOf; booleanValue2 = valueOf.booleanValue(); } catch (Throwable th) { throw th; } } return booleanValue2; } public static boolean hasAccessNetworkStatePermission(Context context) { boolean booleanValue; boolean booleanValue2; synchronized (TOPIC_SYNC_TASK_LOCK) { try { Boolean bool = hasAccessNetworkStatePermission; if (bool == null) { booleanValue = hasPermission(context, "android.permission.ACCESS_NETWORK_STATE", bool); } else { booleanValue = bool.booleanValue(); } Boolean valueOf = Boolean.valueOf(booleanValue); hasAccessNetworkStatePermission = valueOf; booleanValue2 = valueOf.booleanValue(); } catch (Throwable th) { throw th; } } return booleanValue2; } public static boolean hasPermission(Context context, String str, Boolean bool) { if (bool != null) { return bool.booleanValue(); } boolean z = context.checkCallingOrSelfPermission(str) == 0; if (!z && Log.isLoggable("FirebaseMessaging", 3)) { createPermissionMissingLog(str); } return z; } public static String createPermissionMissingLog(String str) { return "Missing Permission: " + str + ". This permission should normally be included by the manifest merger, but may needed to be manually added to your manifest"; } public class ConnectivityChangeReceiver extends BroadcastReceiver { public TopicsSyncTask task; public ConnectivityChangeReceiver(TopicsSyncTask topicsSyncTask) { this.task = topicsSyncTask; } @Override // android.content.BroadcastReceiver public synchronized void onReceive(Context context, Intent intent) { TopicsSyncTask topicsSyncTask = this.task; if (topicsSyncTask == null) { return; } if (topicsSyncTask.isDeviceConnected()) { TopicsSyncTask.isLoggable(); this.task.topicsSubscriber.scheduleSyncTaskWithDelaySeconds(this.task, 0L); context.unregisterReceiver(this); this.task = null; } } public void registerReceiver() { TopicsSyncTask.isLoggable(); TopicsSyncTask.this.context.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); } } }