1 /*
2  * Copyright (C) 2023 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.devicelockcontroller.policy;
18 
19 import androidx.annotation.IntDef;
20 
21 import com.google.common.util.concurrent.ListenableFuture;
22 
23 import java.lang.annotation.ElementType;
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.lang.annotation.Target;
27 
28 /**
29  * Interface for the provision flow state machine.
30  * Different user may have different provision states.
31  */
32 public interface ProvisionStateController {
33 
34     /**
35      * Returns the latest state of the provision.
36      *
37      * @return A {@link ListenableFuture} that will complete when pre-existing state changes, if
38      * any, are done. The result is the latest provision state.
39      */
getState()40     ListenableFuture<@ProvisionState Integer> getState();
41 
42     /**
43      * A convenience method that calls {@link this#setNextStateForEvent} for callers that does not
44      * need to know the result.
45      *
46      * @param event A {@link ProvisionEvent} that is used to identify the next state to move.
47      */
postSetNextStateForEventRequest(@rovisionEvent int event)48     void postSetNextStateForEventRequest(@ProvisionEvent int event);
49 
50     /**
51      * Move to next {@link ProvisionState} based on the input event {@link ProvisionEvent}.
52      *
53      * @param event A {@link ProvisionEvent} that is used to identify the next state to move.
54      * @return A {@link ListenableFuture} that will complete when both the state change and policies
55      * enforcement for next state are done. The result is the new state.
56      */
setNextStateForEvent(@rovisionEvent int event)57     ListenableFuture<Void> setNextStateForEvent(@ProvisionEvent int event);
58 
59     /**
60      * Notify that the device is ready for provisioning.
61      */
notifyProvisioningReady()62     ListenableFuture<Void> notifyProvisioningReady();
63 
64     /** Get the instance for {@link DeviceStateController} */
getDeviceStateController()65     DeviceStateController getDeviceStateController();
66 
67     /** Get the instance for {@link DevicePolicyController} */
getDevicePolicyController()68     DevicePolicyController getDevicePolicyController();
69 
70     /**
71      * Called after user has unlocked to trigger provision or enforce policies.
72      */
onUserUnlocked()73     ListenableFuture<Void> onUserUnlocked();
74 
75     /**
76      * Called when a user has completed set-up wizard.
77      */
onUserSetupCompleted()78     ListenableFuture<Void> onUserSetupCompleted();
79 
80     /**
81      * State definitions related to provisioning flow.
82      */
83     @Target(ElementType.TYPE_USE)
84     @Retention(RetentionPolicy.SOURCE)
85     @IntDef({
86             ProvisionState.UNPROVISIONED,
87             ProvisionState.PROVISION_IN_PROGRESS,
88             ProvisionState.PROVISION_SUCCEEDED,
89             ProvisionState.PROVISION_PAUSED,
90             ProvisionState.PROVISION_FAILED,
91             ProvisionState.KIOSK_PROVISIONED,
92     })
93     @interface ProvisionState {
94 
95         /* Not provisioned */
96         int UNPROVISIONED = 0;
97 
98         /* Provisioning flow is in progress. This is where kiosk app will be installed. */
99         int PROVISION_IN_PROGRESS = 1;
100 
101         /* Provisioning is paused */
102         int PROVISION_PAUSED = 2;
103 
104         /* Kiosk app provisioned */
105         int KIOSK_PROVISIONED = 3;
106 
107         /* Provisioning has succeeded */
108         int PROVISION_SUCCEEDED = 4;
109 
110         /* Provisioning has failed */
111         int PROVISION_FAILED = 5;
112     }
113 
114     /**
115      * Provision event definitions
116      */
117     @Retention(RetentionPolicy.SOURCE)
118     @IntDef({
119             ProvisionEvent.PROVISION_READY,
120             ProvisionEvent.PROVISION_PAUSE,
121             ProvisionEvent.PROVISION_SUCCESS,
122             ProvisionEvent.PROVISION_FAILURE,
123             ProvisionEvent.PROVISION_KIOSK,
124             ProvisionEvent.PROVISION_RESUME,
125             ProvisionEvent.PROVISION_RETRY,
126     })
127     @interface ProvisionEvent {
128 
129         /* Ready for provisioning */
130         int PROVISION_READY = 0;
131 
132         /* Pause provisioning */
133         int PROVISION_PAUSE = 1;
134 
135         /* Provisioning completed successfully */
136         int PROVISION_SUCCESS = 2;
137 
138         /* Provisioning failed to complete */
139         int PROVISION_FAILURE = 3;
140 
141         /* Provision Kiosk app */
142         int PROVISION_KIOSK = 4;
143 
144         /* Resume provisioning */
145         int PROVISION_RESUME = 5;
146 
147         /* Retry provision after failure */
148         int PROVISION_RETRY = 6;
149     }
150 }
151