package org.commcare.appupdate;

import android.app.Activity;
import android.content.IntentSender;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.UpdateAvailability;
import org.commcare.activities.CommCareActivity;
import org.javarosa.core.services.Logger;
import javax.annotation.Nullable;

/**
 * @author $|-|!˅@M
 */
public class CommcareImmediateAppUpdateManager implements AppUpdateController, DefaultLifecycleObserver {

    private static final String TAG = CommcareImmediateAppUpdateManager.class.getSimpleName();

    private AppUpdateManager mAppUpdateManager;
    private AppUpdateInfo mAppUpdateInfo;
    private Activity activity;

    CommcareImmediateAppUpdateManager(CommCareActivity activity) {
        this.activity = activity;
        mAppUpdateManager = AppUpdateManagerFactory.create(activity.getApplicationContext());
        activity.getLifecycle().addObserver(this);
    }

    @Override
    public void onResume(@NonNull LifecycleOwner owner) {
        fetchAppUpdateInfo(() -> {
            if (mAppUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                startImmediateUpdate();
            }
        });
    }

    private void fetchAppUpdateInfo(Runnable callback) {
        mAppUpdateManager.getAppUpdateInfo()
                .addOnSuccessListener(info -> {
                    mAppUpdateInfo = info;
                    callback.run();
                })
                .addOnFailureListener(exception -> {
                    mAppUpdateInfo = null;
                    Logger.log(TAG, "fetchAppUpdateInfo|failed with : " + exception.getMessage());
                });
    }

    private void startImmediateUpdate() {
        try {
            boolean success = mAppUpdateManager.startUpdateFlowForResult(
                    mAppUpdateInfo,
                    AppUpdateType.IMMEDIATE,
                    activity,
                    IN_APP_UPDATE_REQUEST_CODE);
            if (!success) {
                Logger.log(TAG, "startUpdate|requested update cannot be started");
            }
        } catch (IntentSender.SendIntentException exception) {
            Logger.log(TAG, "startUpdate|failed with : " + exception.getMessage());
        }
    }

    //region AppUpdateController implementation
    @Override
    public void startUpdate(Activity activity) {
        fetchAppUpdateInfo(() -> {
            if (mAppUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                startImmediateUpdate();
            }
        });
    }

    @Nullable
    @Override
    public Integer availableVersionCode() {
        if (mAppUpdateInfo != null) {
            return mAppUpdateInfo.availableVersionCode();
        }
        return null;
    }
    //endregion
}

@@ -598,24 +598,59 @@
 reActivity;%0A
+import org.commcare.util.LogTypes;%0A
 import org.j
@@ -853,104 +853,8 @@
  %7B%0A%0A
-    private static final String TAG = CommcareImmediateAppUpdateManager.class.getSimpleName();%0A%0A
     
@@ -1895,19 +1895,39 @@
 ger.log(
-TAG
+LogTypes.TYPE_CC_UPDATE
 , %22fetch
@@ -2347,35 +2347,55 @@
      Logger.log(
-TAG
+LogTypes.TYPE_CC_UPDATE
 , %22startUpdate%7Cr
@@ -2535,11 +2535,31 @@
 log(
-TAG
+LogTypes.TYPE_CC_UPDATE
 , %22s

Use LogTypes instead of classname as log tag