Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1169174 authored by Sumedh Sen's avatar Sumedh Sen
Browse files

Convert code in files from Java to Kotlin (view)

This is the 2nd CL in a 2-part change from Java to Kotlin. This CL
changes java code to kotlin.

Bug: 182205982
Test: builds successfully

Change-Id: I93372c99434ece23f3bdf873f4e9e2390363f6fe
parent 1279ff97
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -14,25 +14,28 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.ui;
package com.android.packageinstaller.v2.ui

import android.content.Intent;
import android.content.Intent

public interface InstallActionListener {
interface InstallActionListener {
    /**
     * Method to handle a positive response from the user.
     */
    fun onPositiveResponse(reasonCode: Int)

    /**
     * Method to handle a positive response from the user
     * Method to dispatch intent for toggling "install from unknown sources" setting for a package.
     */
    void onPositiveResponse(int stageCode);
    fun sendUnknownAppsIntent(sourcePackageName: String)

    /**
     * Method to dispatch intent for toggling "install from unknown sources" setting for a package
     * Method to handle a negative response from the user.
     */
    void sendUnknownAppsIntent(String packageName);
    fun onNegativeResponse(stageCode: Int)

    /**
     * Method to handle a negative response from the user
     * Launch the intent to open the newly installed / updated app.
     */
    void onNegativeResponse(int stageCode);
    void openInstalledApp(Intent intent);
    fun openInstalledApp(intent: Intent?)
}
+234 −240

File changed.

Preview size limit exceeded, changes collapsed.

+4 −6
Original line number Diff line number Diff line
@@ -14,11 +14,9 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.ui;
package com.android.packageinstaller.v2.ui

public interface UninstallActionListener {

    void onPositiveResponse(boolean keepData);

    void onNegativeResponse();
interface UninstallActionListener {
    fun onPositiveResponse(keepData: Boolean)
    fun onNegativeResponse()
}
+118 −118
Original line number Diff line number Diff line
@@ -14,120 +14,127 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.ui;

import static android.os.Process.INVALID_UID;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import com.android.packageinstaller.v2.model.UninstallRepository;
import com.android.packageinstaller.v2.model.UninstallRepository.CallerInfo;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallAborted;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallFailed;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallStage;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallSuccess;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallUninstalling;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallUserActionRequired;
import com.android.packageinstaller.v2.ui.fragments.UninstallConfirmationFragment;
import com.android.packageinstaller.v2.ui.fragments.UninstallErrorFragment;
import com.android.packageinstaller.v2.ui.fragments.UninstallUninstallingFragment;
import com.android.packageinstaller.v2.viewmodel.UninstallViewModel;
import com.android.packageinstaller.v2.viewmodel.UninstallViewModelFactory;

public class UninstallLaunch extends FragmentActivity implements UninstallActionListener {

    public static final String EXTRA_CALLING_PKG_UID =
        UninstallLaunch.class.getPackageName() + ".callingPkgUid";
    public static final String EXTRA_CALLING_ACTIVITY_NAME =
        UninstallLaunch.class.getPackageName() + ".callingActivityName";
    public static final String TAG = UninstallLaunch.class.getSimpleName();
    private static final String TAG_DIALOG = "dialog";

    private UninstallViewModel mUninstallViewModel;
    private UninstallRepository mUninstallRepository;
    private FragmentManager mFragmentManager;
    private NotificationManager mNotificationManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
package com.android.packageinstaller.v2.ui

import android.app.Activity
import android.app.NotificationManager
import android.content.Intent
import android.os.Bundle
import android.os.Process
import android.util.Log
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import com.android.packageinstaller.v2.model.UninstallRepository
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallAborted
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallFailed
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallStage
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallSuccess
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallUninstalling
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallUserActionRequired
import com.android.packageinstaller.v2.ui.fragments.UninstallConfirmationFragment
import com.android.packageinstaller.v2.ui.fragments.UninstallErrorFragment
import com.android.packageinstaller.v2.ui.fragments.UninstallUninstallingFragment
import com.android.packageinstaller.v2.viewmodel.UninstallViewModel
import com.android.packageinstaller.v2.viewmodel.UninstallViewModelFactory

class UninstallLaunch : FragmentActivity(), UninstallActionListener {

    companion object {
        @JvmField val EXTRA_CALLING_PKG_UID =
            UninstallLaunch::class.java.packageName + ".callingPkgUid"
        @JvmField val EXTRA_CALLING_ACTIVITY_NAME =
            UninstallLaunch::class.java.packageName + ".callingActivityName"
        val LOG_TAG = UninstallLaunch::class.java.simpleName
        private const val TAG_DIALOG = "dialog"
    }

    private var uninstallViewModel: UninstallViewModel? = null
    private var uninstallRepository: UninstallRepository? = null
    private var fragmentManager: FragmentManager? = null
    private var notificationManager: NotificationManager? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS)

        // Never restore any state, esp. never create any fragments. The data in the fragment might
        // be stale, if e.g. the app was uninstalled while the activity was destroyed.
        super.onCreate(null);

        mFragmentManager = getSupportFragmentManager();
        mNotificationManager = getSystemService(NotificationManager.class);
        super.onCreate(null)
        fragmentManager = supportFragmentManager
        notificationManager = getSystemService(NotificationManager::class.java)

        mUninstallRepository = new UninstallRepository(getApplicationContext());
        mUninstallViewModel = new ViewModelProvider(this,
            new UninstallViewModelFactory(this.getApplication(), mUninstallRepository)).get(
            UninstallViewModel.class);
        uninstallRepository = UninstallRepository(applicationContext)
        uninstallViewModel = ViewModelProvider(
            this, UninstallViewModelFactory(this.application, uninstallRepository!!)
        ).get(UninstallViewModel::class.java)

        Intent intent = getIntent();
        CallerInfo callerInfo = new CallerInfo(
        val intent = intent
        val callerInfo = UninstallRepository.CallerInfo(
            intent.getStringExtra(EXTRA_CALLING_ACTIVITY_NAME),
            intent.getIntExtra(EXTRA_CALLING_PKG_UID, INVALID_UID));
        mUninstallViewModel.preprocessIntent(intent, callerInfo);

        mUninstallViewModel.getCurrentUninstallStage().observe(this,
            this::onUninstallStageChange);
            intent.getIntExtra(EXTRA_CALLING_PKG_UID, Process.INVALID_UID)
        )
        uninstallViewModel!!.preprocessIntent(intent, callerInfo)
        uninstallViewModel!!.currentUninstallStage.observe(this) { uninstallStage: UninstallStage ->
            onUninstallStageChange(uninstallStage)
        }
    }

    /**
     * Main controller of the UI. This method shows relevant dialogs / fragments based on the
     * uninstall stage
     */
    private void onUninstallStageChange(UninstallStage uninstallStage) {
        if (uninstallStage.getStageCode() == UninstallStage.STAGE_ABORTED) {
            UninstallAborted aborted = (UninstallAborted) uninstallStage;
            if (aborted.getAbortReason() == UninstallAborted.ABORT_REASON_APP_UNAVAILABLE ||
                aborted.getAbortReason() == UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED) {
                UninstallErrorFragment errorDialog = new UninstallErrorFragment(aborted);
                showDialogInner(errorDialog);
    private fun onUninstallStageChange(uninstallStage: UninstallStage) {
        when (uninstallStage.stageCode) {
            UninstallStage.STAGE_ABORTED -> {
                val aborted = uninstallStage as UninstallAborted
                if (aborted.abortReason == UninstallAborted.ABORT_REASON_APP_UNAVAILABLE ||
                    aborted.abortReason == UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED
                ) {
                    val errorDialog = UninstallErrorFragment(aborted)
                    showDialogInner(errorDialog)
                } else {
                setResult(aborted.getActivityResultCode(), null, true);
            }
        } else if (uninstallStage.getStageCode() == UninstallStage.STAGE_USER_ACTION_REQUIRED) {
            UninstallUserActionRequired uar = (UninstallUserActionRequired) uninstallStage;
            UninstallConfirmationFragment confirmationDialog = new UninstallConfirmationFragment(
                uar);
            showDialogInner(confirmationDialog);
        } else if (uninstallStage.getStageCode() == UninstallStage.STAGE_UNINSTALLING) {
                    setResult(aborted.activityResultCode, null, true)
                }
            }

            UninstallStage.STAGE_USER_ACTION_REQUIRED -> {
                val uar = uninstallStage as UninstallUserActionRequired
                val confirmationDialog = UninstallConfirmationFragment(uar)
                showDialogInner(confirmationDialog)
            }

            UninstallStage.STAGE_UNINSTALLING -> {
                // TODO: This shows a fragment whether or not user requests a result or not.
                //  Originally, if the user does not request a result, we used to show a notification.
                //  And a fragment if the user requests a result back. Should we consolidate and
                //  show a fragment always?
            UninstallUninstalling uninstalling = (UninstallUninstalling) uninstallStage;
            UninstallUninstallingFragment uninstallingDialog = new UninstallUninstallingFragment(
                uninstalling);
            showDialogInner(uninstallingDialog);
        } else if (uninstallStage.getStageCode() == UninstallStage.STAGE_FAILED) {
            UninstallFailed failed = (UninstallFailed) uninstallStage;
                val uninstalling = uninstallStage as UninstallUninstalling
                val uninstallingDialog = UninstallUninstallingFragment(uninstalling)
                showDialogInner(uninstallingDialog)
            }

            UninstallStage.STAGE_FAILED -> {
                val failed = uninstallStage as UninstallFailed
                if (!failed.returnResult()) {
                mNotificationManager.notify(failed.getUninstallId(),
                    failed.getUninstallNotification());
                    notificationManager!!.notify(failed.uninstallId, failed.uninstallNotification)
                }
            setResult(failed.getActivityResultCode(), failed.getResultIntent(), true);
        } else if (uninstallStage.getStageCode() == UninstallStage.STAGE_SUCCESS) {
            UninstallSuccess success = (UninstallSuccess) uninstallStage;
            if (success.getMessage() != null) {
                Toast.makeText(this, success.getMessage(), Toast.LENGTH_LONG).show();
                setResult(failed.activityResultCode, failed.resultIntent, true)
            }

            UninstallStage.STAGE_SUCCESS -> {
                val success = uninstallStage as UninstallSuccess
                if (success.message != null) {
                    Toast.makeText(this, success.message, Toast.LENGTH_LONG).show()
                }
                setResult(success.activityResultCode, success.resultIntent, true)
            }

            else -> {
                Log.e(LOG_TAG, "Invalid stage: " + uninstallStage.stageCode)
                showDialogInner(null)
            }
            setResult(success.getActivityResultCode(), success.getResultIntent(), true);
        } else {
            Log.e(TAG, "Invalid stage: " + uninstallStage.getStageCode());
            showDialogInner(null);
        }
    }

@@ -136,32 +143,25 @@ public class UninstallLaunch extends FragmentActivity implements UninstallAction
     *
     * @param newDialog The new dialog to display
     */
    private void showDialogInner(DialogFragment newDialog) {
        DialogFragment currentDialog = (DialogFragment) mFragmentManager.findFragmentByTag(
            TAG_DIALOG);
        if (currentDialog != null) {
            currentDialog.dismissAllowingStateLoss();
        }
        if (newDialog != null) {
            newDialog.show(mFragmentManager, TAG_DIALOG);
        }
    private fun showDialogInner(newDialog: DialogFragment?) {
        val currentDialog = fragmentManager!!.findFragmentByTag(TAG_DIALOG) as DialogFragment?
        currentDialog?.dismissAllowingStateLoss()
        newDialog?.show(fragmentManager!!, TAG_DIALOG)
    }

    public void setResult(int resultCode, Intent data, boolean shouldFinish) {
        super.setResult(resultCode, data);
    fun setResult(resultCode: Int, data: Intent?, shouldFinish: Boolean) {
        super.setResult(resultCode, data)
        if (shouldFinish) {
            finish();
            finish()
        }
    }

    @Override
    public void onPositiveResponse(boolean keepData) {
        mUninstallViewModel.initiateUninstall(keepData);
    override fun onPositiveResponse(keepData: Boolean) {
        uninstallViewModel!!.initiateUninstall(keepData)
    }

    @Override
    public void onNegativeResponse() {
        mUninstallViewModel.cancelInstall();
        setResult(Activity.RESULT_FIRST_USER, null, true);
    override fun onNegativeResponse() {
        uninstallViewModel!!.cancelInstall()
        setResult(Activity.RESULT_FIRST_USER, null, true)
    }
}