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

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

Introduce InstallRepository and InstallViewmodel

This change introduces the repository, viewmodel and viewmodel factory
and links the view to the viewmodel.

Bug: 182205982
Test: builds successfully
Test: No CTS Tests. Flag to use new app is turned off by default

Change-Id: I7c406c7bb2fa5e6aa20572d0681b664b500a72ee
parent 6343e91b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller.v2.model;

import android.content.Context;

public class InstallRepository {

    private static final String TAG = InstallRepository.class.getSimpleName();
    private final Context mContext;

    public InstallRepository(Context context) {
        mContext = context;
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -17,8 +17,13 @@
package com.android.packageinstaller.v2.ui;

import android.os.Bundle;
import android.view.Window;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import com.android.packageinstaller.v2.model.InstallRepository;
import com.android.packageinstaller.v2.viewmodel.InstallViewModel;
import com.android.packageinstaller.v2.viewmodel.InstallViewModelFactory;

public class InstallLaunch extends FragmentActivity {

@@ -27,9 +32,18 @@ public class InstallLaunch extends FragmentActivity {
    public static final String EXTRA_CALLING_PKG_NAME =
            InstallLaunch.class.getPackageName() + ".callingPkgName";
    private static final String TAG = InstallLaunch.class.getSimpleName();
    private InstallViewModel mInstallViewModel;
    private InstallRepository mInstallRepository;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mInstallRepository = new InstallRepository(getApplicationContext());
        mInstallViewModel = new ViewModelProvider(this,
                new InstallViewModelFactory(this.getApplication(), mInstallRepository)).get(
                InstallViewModel.class);
    }
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller.v2.viewmodel;

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

public class InstallViewModel extends AndroidViewModel {

    private static final String TAG = InstallViewModel.class.getSimpleName();
    private final InstallRepository mRepository;

    public InstallViewModel(@NonNull Application application, InstallRepository repository) {
        super(application);
        mRepository = repository;
    }
}
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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;

public class InstallViewModelFactory extends ViewModelProvider.AndroidViewModelFactory {

    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);
    }
}