| 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 androidx.annotation.Keep; |
| 9 | |
| 10 | import com.google.android.gms.common.ConnectionResult; |
| 11 | import com.google.android.gms.common.GoogleApiAvailability; |
| 12 | import com.google.android.play.core.appupdate.AppUpdateManager; |
| 13 | import com.google.android.play.core.appupdate.AppUpdateManagerFactory; |
| 14 | import com.google.android.play.core.install.InstallStateUpdatedListener; |
| 15 | import com.google.android.play.core.install.model.AppUpdateType; |
| 16 | import com.google.android.play.core.install.model.InstallStatus; |
| 17 | import com.google.android.play.core.install.model.UpdateAvailability; |
| 18 | import com.google.firebase.iid.FirebaseInstanceId; |
| 19 | |
| 20 | import org.telegram.messenger.ApplicationLoader; |
| 21 | import org.telegram.messenger.BuildConfig; |
| 22 | import org.telegram.messenger.BuildVars; |
| 23 | import org.telegram.messenger.FileLog; |
| 24 | import org.telegram.messenger.GcmPushListenerService; |
| 25 | import org.telegram.messenger.LocaleController; |
| 26 | import org.telegram.messenger.R; |
| 27 | import org.telegram.messenger.SharedConfig; |
| 28 | import org.telegram.messenger.Utilities; |
| 29 | import org.telegram.tgnet.ConnectionsManager; |
| 30 | |
| 31 | import kotlin.Unit; |
| 32 | import tw.nekomimi.nekogram.utils.UIUtil; |
| 33 | |
| 34 | @Keep |
| 35 | public class GcmImpl implements ExternalGcm.Interface { |
| 36 | |
| 37 | private static Boolean hasPlayServices; |
| 38 | |
| 39 | @Override |
| 40 | public boolean checkPlayServices() { |
| 41 | if (hasPlayServices != null) return hasPlayServices; |
| 42 | try { |
| 43 | int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(ApplicationLoader.applicationContext); |
| 44 | hasPlayServices = resultCode == ConnectionResult.SUCCESS; |
| 45 | } catch (Exception e) { |
| 46 | hasPlayServices = false; |
| 47 | FileLog.e(e); |
| 48 | } |
| 49 | return hasPlayServices; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void initPlayServices() { |
| 54 | UIUtil.runOnIoDispatcher(() -> { |
| 55 | if (hasPlayServices = checkPlayServices()) { |
| 56 | final String currentPushString = SharedConfig.pushString; |
| 57 | if (!TextUtils.isEmpty(currentPushString)) { |
| 58 | if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) { |
| 59 | FileLog.d("GCM regId = " + currentPushString); |
| 60 | } |
| 61 | } else { |
| 62 | if (BuildVars.LOGS_ENABLED) { |
| 63 | FileLog.d("GCM Registration not found."); |
| 64 | } |
| 65 | } |
| 66 | Utilities.globalQueue.postRunnable(() -> { |
| 67 | try { |
| 68 | FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> { |
| 69 | String token = instanceIdResult.getToken(); |
| 70 | if (!TextUtils.isEmpty(token)) { |
| 71 | GcmPushListenerService.sendRegistrationToServer(token); |
| 72 | } |
| 73 | }).addOnFailureListener(e -> { |
| 74 | if (BuildVars.LOGS_ENABLED) { |
| 75 | FileLog.d("Failed to get regid"); |
| 76 | } |
| 77 | SharedConfig.pushStringStatus = "__FIREBASE_FAILED__"; |
| 78 | GcmPushListenerService.sendRegistrationToServer(null); |
| 79 | }); |
| 80 | } catch (Throwable e) { |
| 81 | FileLog.e(e); |
| 82 | } |
| 83 | }); |
| 84 | } else { |
| 85 | if (BuildVars.LOGS_ENABLED) { |
| 86 | FileLog.d("No valid Google Play Services APK found."); |
| 87 | } |
| 88 | SharedConfig.pushStringStatus = "__NO_GOOGLE_PLAY_SERVICES__"; |
| 89 | ConnectionsManager.setRegId(null, SharedConfig.pushStringStatus); |
| 90 | } |
| 91 | }, 2300); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void sendRegistrationToServer() { |
| 96 | if (!checkPlayServices()) return; |
| 97 | GcmPushListenerService.sendRegistrationToServer(SharedConfig.pushString); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void checkUpdate(Activity ctx) { |
| 102 | if (!checkPlayServices()) return; |
| 103 | AppUpdateManager manager = AppUpdateManagerFactory.create(ctx); |
| 104 | InstallStateUpdatedListener listener = (installState) -> { |
| 105 | if (installState.installStatus() == InstallStatus.DOWNLOADED) { |
| 106 | BottomBuilder builder = new BottomBuilder(ctx); |
| 107 | builder.addTitle(LocaleController.getString("UpdateDownloaded", R.string.UpdateDownloaded)); |
| 108 | builder.addItem(LocaleController.getString("UpdateUpdate", R.string.UpdateUpdate), R.drawable.baseline_system_update_24, false, (it) -> { |
| 109 | manager.completeUpdate(); |
| 110 | return Unit.INSTANCE; |
| 111 | }); |
| 112 | builder.addItem(LocaleController.getString("UpdateLater", R.string.UpdateLater), R.drawable.baseline_watch_later_24, false, null); |
| 113 | try { |
| 114 | builder.show(); |
| 115 | } catch (WindowManager.BadTokenException e) { |
| 116 | manager.completeUpdate(); |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | manager.registerListener(listener); |
| 121 | manager.getAppUpdateInfo().addOnSuccessListener((appUpdateInfo) -> { |
| 122 | if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.availableVersionCode() <= BuildConfig.VERSION_CODE) { |
| 123 | FileLog.d("update available"); |
| 124 | try { |
| 125 | manager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, ctx, 114514); |
| 126 | } catch (IntentSender.SendIntentException ignored) { |
| 127 | } |
| 128 | } else { |
| 129 | FileLog.d("no updates"); |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |