// Path: vector/src/app/java/im/vector/push/fcm/FcmHelper.java
// public class FcmHelper {
//     private static final String LOG_TAG = FcmHelper.class.getSimpleName();
// 
//     private static final String PREFS_KEY_FCM_TOKEN = "FCM_TOKEN";
// 
//     /**
//      * Retrieves the FCM registration token.
//      *
//      * @return the FCM token or null if not received from FCM
//      */
//     @Nullable
//     public static String getFcmToken(Context context) {
//         return PreferenceManager.getDefaultSharedPreferences(context).getString(PREFS_KEY_FCM_TOKEN, null);
//     }
// 
//     /**
//      * Store FCM token to the SharedPrefs
//      *
//      * @param context android context
//      * @param token   the token to store
//      */
//     public static void storeFcmToken(@NonNull Context context,
//                                      @Nullable String token) {
//         PreferenceManager.getDefaultSharedPreferences(context)
//                 .edit()
//                 .putString(PREFS_KEY_FCM_TOKEN, token)
//                 .apply();
//     }
// 
//     /**
//      * onNewToken may not be called on application upgrade, so ensure my shared pref is set
//      *
//      * @param activity the first launch Activity
//      */
//     public static void ensureFcmTokenIsRetrieved(final Activity activity) {
//         if (TextUtils.isEmpty(getFcmToken(activity))) {
// 
// 
//             //vfe: according to firebase doc
//             //'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
//             if (checkPlayServices(activity)) {
//                 try {
//                     FirebaseInstanceId.getInstance().getInstanceId()
//                             .addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
//                                 @Override
//                                 public void onSuccess(InstanceIdResult instanceIdResult) {
//                                     storeFcmToken(activity, instanceIdResult.getToken());
//                                 }
//                             })
//                             .addOnFailureListener(activity, new OnFailureListener() {
//                                 @Override
//                                 public void onFailure(@NonNull Exception e) {
//                                     Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
//                                 }
//                             });
//                 } catch (Throwable e) {
//                     Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
//                 }
//             } else {
//                 Toast.makeText(activity, R.string.no_valid_google_play_services_apk, Toast.LENGTH_SHORT).show();
//                 Log.e(LOG_TAG, "No valid Google Play Services found. Cannot use FCM.");
//             }
//         }
//     }
// 
//     /**
//      * Check the device to make sure it has the Google Play Services APK. If
//      * it doesn't, display a dialog that allows users to download the APK from
//      * the Google Play Store or enable it in the device's system settings.
//      */
//     private static boolean checkPlayServices(Activity activity) {
//         GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
//         int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
//         if (resultCode != ConnectionResult.SUCCESS) {
//             return false;
//         }
//         return true;
//     }
// }


import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.matrix.androidsdk.MXSession;
import org.matrix.androidsdk.core.Log;
import org.matrix.androidsdk.core.callback.ApiCallback;
import org.matrix.androidsdk.core.callback.SimpleApiCallback;
import org.matrix.androidsdk.core.listeners.IMXNetworkEventListener;
import org.matrix.androidsdk.core.model.MatrixError;
import org.matrix.androidsdk.data.Pusher;
import org.matrix.androidsdk.rest.model.PushersResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import im.vector.BuildConfig;
import im.vector.Matrix;
import im.vector.push.fcm.FcmHelper;
import im.vector.services.EventStreamServiceX;
import im.vector.util.PreferencesManager;
        }

        Matrix.getInstance(appContext).addNetworkEventListener(new IMXNetworkEventListener() {
            @Override
            public void onNetworkConnectionUpdate(boolean isConnected) {
                if (isConnected) {
                    // test if the server registration / unregistration should be done
                    if (useFcm()) {
                        // test if the user expect having notifications on his device but it was not yet done
                        if (areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.FCM_REGISTERED)) {
                            register(null);
                        } else if (!areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.SERVER_REGISTERED)) {
                            unregister(null);
                        }
                    }
                }
            }
        });

        mRegistrationState = getStoredRegistrationState();
        mRegistrationToken = getStoredRegistrationToken();
    }

    public void deepCheckRegistration(Context context) {
        if (isFcmRegistered()) {
            //Issue #2266 It might be possible that the FCMHelper saved token is different
            //than the push manager saved token, and that the pushManager is not aware.
            //And as per current code the pushMgr saved token is sent at each startup (resume?)
            //So anyway, might be a good thing to check that it is synced?
            //Very defensive code but, ya know :/
// Path: vector/src/app/java/im/vector/push/fcm/FcmHelper.java
// public class FcmHelper {
//     private static final String LOG_TAG = FcmHelper.class.getSimpleName();
// 
//     private static final String PREFS_KEY_FCM_TOKEN = "FCM_TOKEN";
// 
//     /**
//      * Retrieves the FCM registration token.
//      *
//      * @return the FCM token or null if not received from FCM
//      */
//     @Nullable
//     public static String getFcmToken(Context context) {
//         return PreferenceManager.getDefaultSharedPreferences(context).getString(PREFS_KEY_FCM_TOKEN, null);
//     }
// 
//     /**
//      * Store FCM token to the SharedPrefs
//      *
//      * @param context android context
//      * @param token   the token to store
//      */
//     public static void storeFcmToken(@NonNull Context context,
//                                      @Nullable String token) {
//         PreferenceManager.getDefaultSharedPreferences(context)
//                 .edit()
//                 .putString(PREFS_KEY_FCM_TOKEN, token)
//                 .apply();
//     }
// 
//     /**
//      * onNewToken may not be called on application upgrade, so ensure my shared pref is set
//      *
//      * @param activity the first launch Activity
//      */
//     public static void ensureFcmTokenIsRetrieved(final Activity activity) {
//         if (TextUtils.isEmpty(getFcmToken(activity))) {
// 
// 
//             //vfe: according to firebase doc
//             //'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
//             if (checkPlayServices(activity)) {
//                 try {
//                     FirebaseInstanceId.getInstance().getInstanceId()
//                             .addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
//                                 @Override
//                                 public void onSuccess(InstanceIdResult instanceIdResult) {
//                                     storeFcmToken(activity, instanceIdResult.getToken());
//                                 }
//                             })
//                             .addOnFailureListener(activity, new OnFailureListener() {
//                                 @Override
//                                 public void onFailure(@NonNull Exception e) {
//                                     Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
//                                 }
//                             });
//                 } catch (Throwable e) {
//                     Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
//                 }
//             } else {
//                 Toast.makeText(activity, R.string.no_valid_google_play_services_apk, Toast.LENGTH_SHORT).show();
//                 Log.e(LOG_TAG, "No valid Google Play Services found. Cannot use FCM.");
//             }
//         }
//     }
// 
//     /**
//      * Check the device to make sure it has the Google Play Services APK. If
//      * it doesn't, display a dialog that allows users to download the APK from
//      * the Google Play Store or enable it in the device's system settings.
//      */
//     private static boolean checkPlayServices(Activity activity) {
//         GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
//         int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
//         if (resultCode != ConnectionResult.SUCCESS) {
//             return false;
//         }
//         return true;
//     }
// }


// Path: vector/src/main/java/im/vector/push/PushManager.java
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.matrix.androidsdk.MXSession;
import org.matrix.androidsdk.core.Log;
import org.matrix.androidsdk.core.callback.ApiCallback;
import org.matrix.androidsdk.core.callback.SimpleApiCallback;
import org.matrix.androidsdk.core.listeners.IMXNetworkEventListener;
import org.matrix.androidsdk.core.model.MatrixError;
import org.matrix.androidsdk.data.Pusher;
import org.matrix.androidsdk.rest.model.PushersResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import im.vector.BuildConfig;
import im.vector.Matrix;
import im.vector.push.fcm.FcmHelper;
import im.vector.services.EventStreamServiceX;
import im.vector.util.PreferencesManager;

        }

        Matrix.getInstance(appContext).addNetworkEventListener(new IMXNetworkEventListener() {
            @Override
            public void onNetworkConnectionUpdate(boolean isConnected) {
                if (isConnected) {
                    // test if the server registration / unregistration should be done
                    if (useFcm()) {
                        // test if the user expect having notifications on his device but it was not yet done
                        if (areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.FCM_REGISTERED)) {
                            register(null);
                        } else if (!areDeviceNotificationsAllowed() && (mRegistrationState == RegistrationState.SERVER_REGISTERED)) {
                            unregister(null);
                        }
                    }
                }
            }
        });

        mRegistrationState = getStoredRegistrationState();
        mRegistrationToken = getStoredRegistrationToken();
    }

    public void deepCheckRegistration(Context context) {
        if (isFcmRegistered()) {
            //Issue #2266 It might be possible that the FCMHelper saved token is different
            //than the push manager saved token, and that the pushManager is not aware.
            //And as per current code the pushMgr saved token is sent at each startup (resume?)
            //So anyway, might be a good thing to check that it is synced?
            //Very defensive code but, ya know :/
            String fcmToken = FcmHelper.getFcmToken(context);