| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.content.IntentSender; |
| 5 | import android.text.TextUtils; |
| 6 | import android.view.WindowManager; |
| 7 | |
| 8 | import com.google.android.gms.common.ConnectionResult; |
| 9 | import com.google.android.gms.common.GoogleApiAvailability; |
| 10 | import com.google.android.play.core.appupdate.AppUpdateManager; |
| 11 | import com.google.android.play.core.appupdate.AppUpdateManagerFactory; |
| 12 | import com.google.android.play.core.install.InstallStateUpdatedListener; |
| 13 | import com.google.android.play.core.install.model.AppUpdateType; |
| 14 | import com.google.android.play.core.install.model.InstallStatus; |
| 15 | import com.google.android.play.core.install.model.UpdateAvailability; |
| 16 | import com.google.firebase.crashlytics.FirebaseCrashlytics; |
| 17 | import com.google.firebase.iid.FirebaseInstanceId; |
| 18 | |
| 19 | import org.h2.util.IOUtils; |
| 20 | import org.telegram.messenger.AndroidUtilities; |
| 21 | import org.telegram.messenger.ApplicationLoader; |
| 22 | import org.telegram.messenger.BuildConfig; |
| 23 | import org.telegram.messenger.BuildVars; |
| 24 | import org.telegram.messenger.FileLog; |
| 25 | import org.telegram.messenger.GcmPushListenerService; |
| 26 | import org.telegram.messenger.LocaleController; |
| 27 | import org.telegram.messenger.R; |
| 28 | import org.telegram.messenger.SharedConfig; |
| 29 | import org.telegram.messenger.Utilities; |
| 30 | import org.telegram.tgnet.ConnectionsManager; |
| 31 | |
| 32 | import javax.validation.constraints.NotNull; |
| 33 | |
| 34 | import kotlin.Unit; |
| 35 | import tw.nekomimi.nekogram.utils.UIUtil; |
| 36 | |
| 37 | public class ExternalGcm { |
| 38 | |
| 39 | @SuppressWarnings("ConstantConditions") |
| 40 | private static boolean noGcm = BuildConfig.BUILD_TYPE.contains("NoGcm"); |
| 41 | |
| 42 | private static Boolean hasPlayServices; |
| 43 | |
| 44 | public static void initPlayServices() { |
| 45 | |
| 46 | UIUtil.runOnIoDispatcher(() -> { |
| 47 | if (hasPlayServices = checkPlayServices()) { |
| 48 | final String currentPushString = SharedConfig.pushString; |
| 49 | if (!TextUtils.isEmpty(currentPushString)) { |
| 50 | if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) { |
| 51 | FileLog.d("GCM regId = " + currentPushString); |
| 52 | } |
| 53 | } else { |
| 54 | if (BuildVars.LOGS_ENABLED) { |
| 55 | FileLog.d("GCM Registration not found."); |
| 56 | } |
| 57 | } |
| 58 | Utilities.globalQueue.postRunnable(() -> { |
| 59 | try { |
| 60 | FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> { |
| 61 | String token = instanceIdResult.getToken(); |
| 62 | if (!TextUtils.isEmpty(token)) { |
| 63 | GcmPushListenerService.sendRegistrationToServer(token); |
| 64 | } |
| 65 | }).addOnFailureListener(e -> { |
| 66 | if (BuildVars.LOGS_ENABLED) { |
| 67 | FileLog.d("Failed to get regid"); |
| 68 | } |
| 69 | SharedConfig.pushStringStatus = "__FIREBASE_FAILED__"; |
| 70 | GcmPushListenerService.sendRegistrationToServer(null); |
| 71 | }); |
| 72 | } catch (Throwable e) { |
| 73 | FileLog.e(e); |
| 74 | } |
| 75 | }); |
| 76 | } else { |
| 77 | if (BuildVars.LOGS_ENABLED) { |
| 78 | FileLog.d("No valid Google Play Services APK found."); |
| 79 | } |
| 80 | SharedConfig.pushStringStatus = "__NO_GOOGLE_PLAY_SERVICES__"; |
| 81 | ConnectionsManager.setRegId(null, SharedConfig.pushStringStatus); |
| 82 | } |
| 83 | }, 2300); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | public static boolean checkPlayServices() { |
| 88 | if (noGcm) return false; |
| 89 | if (hasPlayServices != null) return hasPlayServices; |
| 90 | try { |
| 91 | int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(ApplicationLoader.applicationContext); |
| 92 | hasPlayServices = resultCode == ConnectionResult.SUCCESS; |
| 93 | } catch (Exception e) { |
| 94 | hasPlayServices = false; |
| 95 | FileLog.e(e); |
| 96 | } |
| 97 | return hasPlayServices; |
| 98 | } |
| 99 | |
| 100 | public static void sendRegistrationToServer() { |
| 101 | if (!checkPlayServices()) return; |
| 102 | GcmPushListenerService.sendRegistrationToServer(SharedConfig.pushString); |
| 103 | } |
| 104 | |
| 105 | public static void reportLog(@NotNull String report) { |
| 106 | if (!checkPlayServices()) return; |
| 107 | UIUtil.runOnIoDispatcher(() -> FirebaseCrashlytics.getInstance().log(report)); |
| 108 | } |
| 109 | |
| 110 | public static void recordException(@NotNull Throwable throwable) { |
| 111 | if (!checkPlayServices()) return; |
| 112 | UIUtil.runOnIoDispatcher(() -> FirebaseCrashlytics.getInstance().recordException(throwable)); |
| 113 | } |
| 114 | |
| 115 | public static void checkUpdate(Activity ctx) { |
| 116 | if (!checkPlayServices()) return; |
| 117 | |
| 118 | AppUpdateManager manager = AppUpdateManagerFactory.create(ctx); |
| 119 | |
| 120 | InstallStateUpdatedListener listener = (installState) -> { |
| 121 | |
| 122 | if (installState.installStatus() == InstallStatus.DOWNLOADED) { |
| 123 | |
| 124 | BottomBuilder builder = new BottomBuilder(ctx); |
| 125 | |
| 126 | builder.addTitle(LocaleController.getString("UpdateDownloaded", R.string.UpdateDownloaded)); |
| 127 | |
| 128 | builder.addItem(LocaleController.getString("UpdateUpdate", R.string.UpdateUpdate), R.drawable.baseline_system_update_24, false, (it) -> { |
| 129 | |
| 130 | manager.completeUpdate(); |
| 131 | |
| 132 | return Unit.INSTANCE; |
| 133 | |
| 134 | }); |
| 135 | |
| 136 | builder.addItem(LocaleController.getString("UpdateLater", R.string.UpdateLater), R.drawable.baseline_watch_later_24, false, null); |
| 137 | |
| 138 | try { |
| 139 | |
| 140 | builder.show(); |
| 141 | |
| 142 | } catch (WindowManager.BadTokenException e) { |
| 143 | |
| 144 | manager.completeUpdate(); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | }; |
| 151 | |
| 152 | manager.registerListener(listener); |
| 153 | |
| 154 | manager.getAppUpdateInfo().addOnSuccessListener((appUpdateInfo) -> { |
| 155 | |
| 156 | if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.availableVersionCode() <= BuildConfig.VERSION_CODE) { |
| 157 | |
| 158 | FileLog.d("update available"); |
| 159 | |
| 160 | try { |
| 161 | |
| 162 | manager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, ctx, 114514); |
| 163 | |
| 164 | } catch (IntentSender.SendIntentException ignored) { |
| 165 | } |
| 166 | |
| 167 | } else { |
| 168 | |
| 169 | FileLog.d("no updates"); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | }); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |