1 /*
2  * Copyright 2019 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 androidx.navigation.dynamicfeatures
18 
19 import androidx.annotation.RestrictTo
20 import androidx.lifecycle.LiveData
21 import androidx.lifecycle.MutableLiveData
22 import com.google.android.play.core.splitinstall.SplitInstallManager
23 import com.google.android.play.core.splitinstall.SplitInstallSessionState
24 import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus
25 
26 /**
27  * Monitor installation progress of dynamic feature modules. This class enables you to subscribe to
28  * the current installation state via [getStatus]. You also can perform various checks on
29  * installation state directly through this monitor.
30  *
31  * In order to enable installation and monitoring of progress you'll have to provide an instance of
32  * this class to [DynamicExtras].
33  */
34 public class DynamicInstallMonitor {
35 
36     /** The occurred exception, if any. */
37     public var exception: Exception? = null
38         internal set
39 
40     /** Get a LiveData of [SplitInstallSessionStatus] with updates on the installation progress. */
41     public val status: LiveData<SplitInstallSessionState> = MutableLiveData()
42 
43     /**
44      * Check whether an installation is required.
45      *
46      * If this returns `true`, you should observe the LiveData returned by [status] for installation
47      * updates and handle them accordingly.
48      *
49      * @return `true` if installation is required, `false` otherwise.
50      */
51     public var isInstallRequired: Boolean = false
52         internal set(installRequired) {
53             field = installRequired
54             if (installRequired) {
55                 isUsed = true
56             }
57         }
58 
59     /** The session id from Play Core for this installation session. */
60     public var sessionId: Int = 0
61         internal set
62 
63     /** The [SplitInstallManager] used to monitor the installation if any was set. */
64     @set:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
65     @get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
66     public var splitInstallManager: SplitInstallManager? = null
67 
68     /** `true` if the monitor has been used to request an install, else `false`. */
69     internal var isUsed = false
70         private set
71 
72     /** Cancel the current split installation session in the SplitInstallManager. */
cancelInstallnull73     public fun cancelInstall() {
74         val splitInstallManager = splitInstallManager
75         if (splitInstallManager != null && sessionId != 0) {
76             splitInstallManager.cancelInstall(sessionId)
77         }
78     }
79 }
80