1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.permission.model.livedatatypes.v34
18 
19 import android.content.pm.PackageInstaller.PACKAGE_SOURCE_STORE
20 import android.content.pm.PackageInstaller.PACKAGE_SOURCE_UNSPECIFIED
21 
22 /**
23  * A lighter version of the system's InstallSourceInfo class, containing select information about
24  * the install source.
25  */
26 class LightInstallSourceInfo {
27     private constructor() {
28         initiatingPackageName = null
29         isPreloadedApp = false
30         supportsSafetyLabel = false
31     }
32 
33     constructor(packageSource: Int, initiatingPackage: String?) {
34         initiatingPackageName = initiatingPackage
35         // Stores should be setting PACKAGE_SOURCE_STORE, but it's not enforced. So include the
36         // default source of unspecified. All other sources should be explicitly set to another
37         // PACKAGE_SOURCE_ value
38         val isStoreInstalled =
39             initiatingPackageName != null &&
40                 (packageSource == PACKAGE_SOURCE_STORE ||
41                     packageSource == PACKAGE_SOURCE_UNSPECIFIED)
42 
43         isPreloadedApp =
44             initiatingPackageName == null && packageSource == PACKAGE_SOURCE_UNSPECIFIED
45         supportsSafetyLabel = isStoreInstalled || isPreloadedApp
46     }
47 
48     /** The package name of the install source (usually the app store) */
49     val initiatingPackageName: String?
50 
51     /** Whether packages from this install source support safety labels. */
52     val supportsSafetyLabel: Boolean
53 
54     /** Whether this install source indicates that the package was preloaded onto the device. */
55     val isPreloadedApp: Boolean
56 
57     companion object {
58         /** Indicates an unavailable install source. */
59         val INSTALL_SOURCE_UNAVAILABLE = LightInstallSourceInfo()
60     }
61 }
62