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

Commit 43ee6d9e authored by Sumedh Sen's avatar Sumedh Sen
Browse files

Convert code in files from Java to Kotlin (viewmodel)

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: Iaa7667f767f41b43d00b20e8348f7bab66a3d55d
parent eb020095
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -35,7 +35,10 @@ android_app {
    name: "PackageInstaller",
    defaults: ["platform_app_defaults"],

    srcs: ["src/**/*.java"],
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],

    certificate: "platform",
    privileged: true,
@@ -62,7 +65,10 @@ android_app {
    name: "PackageInstaller_tablet",
    defaults: ["platform_app_defaults"],

    srcs: ["src/**/*.java"],
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],

    certificate: "platform",
    privileged: true,
@@ -91,7 +97,10 @@ android_app {
    name: "PackageInstaller_tv",
    defaults: ["platform_app_defaults"],

    srcs: ["src/**/*.java"],
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],

    certificate: "platform",
    privileged: true,
+46 −56
Original line number Diff line number Diff line
@@ -14,92 +14,82 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.viewmodel;
package com.android.packageinstaller.v2.viewmodel

import android.app.Application;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import com.android.packageinstaller.v2.model.InstallRepository;
import com.android.packageinstaller.v2.model.InstallRepository.CallerInfo;
import com.android.packageinstaller.v2.model.installstagedata.InstallStage;
import com.android.packageinstaller.v2.model.installstagedata.InstallStaging;
import android.app.Application
import android.content.Intent
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.android.packageinstaller.v2.model.InstallRepository
import com.android.packageinstaller.v2.model.installstagedata.InstallStage
import com.android.packageinstaller.v2.model.installstagedata.InstallStaging

class InstallViewModel(application: Application, val repository: InstallRepository) :
    AndroidViewModel(application) {

public class InstallViewModel extends AndroidViewModel {

    private static final String TAG = InstallViewModel.class.getSimpleName();
    private final InstallRepository mRepository;
    private final MediatorLiveData<InstallStage> mCurrentInstallStage = new MediatorLiveData<>(
            new InstallStaging());

    public InstallViewModel(@NonNull Application application, InstallRepository repository) {
        super(application);
        mRepository = repository;
    companion object {
        private val LOG_TAG = InstallViewModel::class.java.simpleName
    }

    public MutableLiveData<InstallStage> getCurrentInstallStage() {
        return mCurrentInstallStage;
    }
    private val _currentInstallStage = MediatorLiveData<InstallStage>(InstallStaging())
    val currentInstallStage: MutableLiveData<InstallStage>
        get() = _currentInstallStage

    public void preprocessIntent(Intent intent, CallerInfo callerInfo) {
        InstallStage stage = mRepository.performPreInstallChecks(intent, callerInfo);
        if (stage.getStageCode() == InstallStage.STAGE_ABORTED) {
            mCurrentInstallStage.setValue(stage);
    fun preprocessIntent(intent: Intent, callerInfo: InstallRepository.CallerInfo) {
        val stage = repository.performPreInstallChecks(intent, callerInfo)
        if (stage.stageCode == InstallStage.STAGE_ABORTED) {
            _currentInstallStage.value = stage
        } else {
            // Since staging is an async operation, we will get the staging result later in time.
            // Result of the file staging will be set in InstallRepository#mStagingResult.
            // As such, mCurrentInstallStage will need to add another MutableLiveData
            // as a data source
            mRepository.stageForInstall();
            mCurrentInstallStage.addSource(mRepository.getStagingResult(), installStage -> {
                if (installStage.getStageCode() != InstallStage.STAGE_READY) {
                    mCurrentInstallStage.setValue(installStage);
            repository.stageForInstall()
            _currentInstallStage.addSource(repository.stagingResult) { installStage: InstallStage ->
                if (installStage.stageCode != InstallStage.STAGE_READY) {
                    _currentInstallStage.value = installStage
                } else {
                    checkIfAllowedAndInitiateInstall();
                    checkIfAllowedAndInitiateInstall()
                }
            });
            }
        }

    public MutableLiveData<Integer> getStagingProgress() {
        return mRepository.getStagingProgress();
    }

    private void checkIfAllowedAndInitiateInstall() {
        InstallStage stage = mRepository.requestUserConfirmation();
        mCurrentInstallStage.setValue(stage);
    val stagingProgress: MutableLiveData<Int>
        get() = repository.stagingProgress

    private fun checkIfAllowedAndInitiateInstall() {
        val stage = repository.requestUserConfirmation()
        _currentInstallStage.value = stage
    }

    public void forcedSkipSourceCheck() {
        InstallStage stage = mRepository.forcedSkipSourceCheck();
        mCurrentInstallStage.setValue(stage);
    fun forcedSkipSourceCheck() {
        val stage = repository.forcedSkipSourceCheck()
        _currentInstallStage.value = stage
    }

    public void cleanupInstall() {
        mRepository.cleanupInstall();
    fun cleanupInstall() {
        repository.cleanupInstall()
    }

    public void reattemptInstall() {
        InstallStage stage = mRepository.reattemptInstall();
        mCurrentInstallStage.setValue(stage);
    fun reattemptInstall() {
        val stage = repository.reattemptInstall()
        _currentInstallStage.value = stage
    }

    public void initiateInstall() {
    fun initiateInstall() {
        // Since installing is an async operation, we will get the install result later in time.
        // Result of the installation will be set in InstallRepository#mInstallResult.
        // As such, mCurrentInstallStage will need to add another MutableLiveData as a data source
        mRepository.initiateInstall();
        mCurrentInstallStage.addSource(mRepository.getInstallResult(), installStage -> {
        repository.initiateInstall()
        _currentInstallStage.addSource(repository.installResult) { installStage: InstallStage? ->
            if (installStage != null) {
                mCurrentInstallStage.setValue(installStage);
                _currentInstallStage.value = installStage
            }
        });
        }

    public int getStagedSessionId() {
        return mRepository.getStagedSessionId();
    }

    val stagedSessionId: Int
        get() = repository.stagedSessionId
}
+12 −24
Original line number Diff line number Diff line
@@ -14,32 +14,20 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.viewmodel;
package com.android.packageinstaller.v2.viewmodel

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.android.packageinstaller.v2.model.InstallRepository;
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.android.packageinstaller.v2.model.InstallRepository

public class InstallViewModelFactory extends ViewModelProvider.AndroidViewModelFactory {
class InstallViewModelFactory(val application: Application, val repository: InstallRepository) :
    ViewModelProvider.AndroidViewModelFactory(application) {

    private final InstallRepository mRepository;
    private final Application mApplication;

    public InstallViewModelFactory(Application application, InstallRepository repository) {
    // Calling super class' ctor ensures that create method is called correctly and the right
    // ctor of InstallViewModel is used. If we fail to do that, the default ctor:
    // InstallViewModel(application) is used, and repository isn't initialized in the viewmodel
        super(application);
        mApplication = application;
        mRepository = repository;
    }

    @NonNull
    @Override
    @SuppressWarnings("unchecked")
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        return (T) new InstallViewModel(mApplication, mRepository);
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return InstallViewModel(application, repository) as T
    }
}
+28 −35
Original line number Diff line number Diff line
@@ -14,56 +14,49 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.viewmodel;
package com.android.packageinstaller.v2.viewmodel

import android.app.Application;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import com.android.packageinstaller.v2.model.UninstallRepository;
import com.android.packageinstaller.v2.model.UninstallRepository.CallerInfo;
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallStage;
import android.app.Application
import android.content.Intent
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.android.packageinstaller.v2.model.UninstallRepository
import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallStage

public class UninstallViewModel extends AndroidViewModel {
class UninstallViewModel(application: Application, val repository: UninstallRepository) :
    AndroidViewModel(application) {

    private static final String TAG = UninstallViewModel.class.getSimpleName();
    private final UninstallRepository mRepository;
    private final MediatorLiveData<UninstallStage> mCurrentUninstallStage =
        new MediatorLiveData<>();

    public UninstallViewModel(@NonNull Application application, UninstallRepository repository) {
        super(application);
        mRepository = repository;
    companion object {
        private val LOG_TAG = UninstallViewModel::class.java.simpleName
    }

    public MutableLiveData<UninstallStage> getCurrentUninstallStage() {
        return mCurrentUninstallStage;
    }
    private val _currentUninstallStage = MediatorLiveData<UninstallStage>()
    val currentUninstallStage: MutableLiveData<UninstallStage>
        get() = _currentUninstallStage

    public void preprocessIntent(Intent intent, CallerInfo callerInfo) {
        UninstallStage stage = mRepository.performPreUninstallChecks(intent, callerInfo);
        if (stage.getStageCode() != UninstallStage.STAGE_ABORTED) {
            stage = mRepository.generateUninstallDetails();
    fun preprocessIntent(intent: Intent, callerInfo: UninstallRepository.CallerInfo) {
        var stage = repository.performPreUninstallChecks(intent, callerInfo)
        if (stage.stageCode != UninstallStage.STAGE_ABORTED) {
            stage = repository.generateUninstallDetails()
        }
        mCurrentUninstallStage.setValue(stage);
        _currentUninstallStage.value = stage
    }

    public void initiateUninstall(boolean keepData) {
        mRepository.initiateUninstall(keepData);
    fun initiateUninstall(keepData: Boolean) {
        repository.initiateUninstall(keepData)
        // Since uninstall is an async operation, we will get the uninstall result later in time.
        // Result of the uninstall will be set in UninstallRepository#mUninstallResult.
        // As such, mCurrentUninstallStage will need to add another MutableLiveData
        // As such, _currentUninstallStage will need to add another MutableLiveData
        // as a data source
        mCurrentUninstallStage.addSource(mRepository.getUninstallResult(), uninstallStage -> {
        _currentUninstallStage.addSource(repository.uninstallResult) { uninstallStage: UninstallStage? ->
            if (uninstallStage != null) {
                mCurrentUninstallStage.setValue(uninstallStage);
                _currentUninstallStage.value = uninstallStage
            }
        }
        });
    }

    public void cancelInstall() {
        mRepository.cancelInstall();
    fun cancelInstall() {
        repository.cancelInstall()
    }
}
+13 −25
Original line number Diff line number Diff line
@@ -14,33 +14,21 @@
 * limitations under the License.
 */

package com.android.packageinstaller.v2.viewmodel;
package com.android.packageinstaller.v2.viewmodel

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.android.packageinstaller.v2.model.UninstallRepository;
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.android.packageinstaller.v2.model.UninstallRepository

public class UninstallViewModelFactory extends ViewModelProvider.AndroidViewModelFactory {
class UninstallViewModelFactory(val application: Application, val repository: UninstallRepository) :
    ViewModelProvider.AndroidViewModelFactory(application) {

    private final UninstallRepository mRepository;
    private final Application mApplication;

    public UninstallViewModelFactory(Application application, UninstallRepository repository) {
    // Calling super class' ctor ensures that create method is called correctly and the right
    // ctor of UninstallViewModel is used. If we fail to do that, the default ctor:
    // UninstallViewModel(application) is used, and repository isn't initialized in
    // the viewmodel
        super(application);
        mApplication = application;
        mRepository = repository;
    }

    @NonNull
    @Override
    @SuppressWarnings("unchecked")
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        return (T) new UninstallViewModel(mApplication, mRepository);
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return UninstallViewModel(application, repository) as T
    }
}