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 androidx.window.extensions.area;
18 
19 import static androidx.window.extensions.area.WindowAreaComponent.SESSION_STATE_ACTIVE;
20 import static androidx.window.extensions.area.WindowAreaComponent.SESSION_STATE_INACTIVE;
21 
22 import android.content.Context;
23 import android.view.Display;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.window.extensions.core.util.function.Consumer;
28 
29 import java.util.Objects;
30 
31 /**
32  * Controller class that manages the creation of the {@link android.app.Presentation} object used
33  * to show content on the rear facing display. This controller notifies the session callback with
34  * {@link androidx.window.extensions.area.WindowAreaComponent.WindowAreaStatus} values when the
35  * feature is active, or when the feature has been ended.
36  */
37 class RearDisplayPresentationController {
38 
39     // Original context that requested to enable rear display presentation mode
40     @NonNull
41     private final Context mContext;
42     @NonNull
43     private final Consumer<@WindowAreaComponent.WindowAreaSessionState Integer> mStateConsumer;
44     @Nullable
45     private ExtensionWindowAreaPresentation mExtensionWindowAreaPresentation;
46 
47     /**
48      * Creates the RearDisplayPresentationController
49      * @param context Originating {@link android.content.Context} that is initiating the rear
50      *                display presentation session.
51      * @param stateConsumer {@link Consumer} that will be notified that the session is active when
52      *        the device state request is active and the session has been created. If the device
53      *        state request is cancelled, the callback will be notified that the session has been
54      *        ended. This could occur through a call to cancel the feature or if the device is
55      *        manipulated in a way that cancels any device state override.
56      */
RearDisplayPresentationController(@onNull Context context, @NonNull Consumer<@WindowAreaComponent.WindowAreaSessionState Integer> stateConsumer)57     RearDisplayPresentationController(@NonNull Context context,
58             @NonNull Consumer<@WindowAreaComponent.WindowAreaSessionState Integer> stateConsumer) {
59         Objects.requireNonNull(context);
60         Objects.requireNonNull(stateConsumer);
61 
62         mContext = context;
63         mStateConsumer = stateConsumer;
64     }
65 
startSession(@onNull Display rearDisplay)66     public void startSession(@NonNull Display rearDisplay) {
67         mExtensionWindowAreaPresentation =
68                 new RearDisplayPresentation(mContext, rearDisplay, mStateConsumer);
69         mStateConsumer.accept(SESSION_STATE_ACTIVE);
70     }
71 
endSession()72     public void endSession() {
73         mStateConsumer.accept(SESSION_STATE_INACTIVE);
74     }
75 
76     @Nullable
getWindowAreaPresentation()77     public ExtensionWindowAreaPresentation getWindowAreaPresentation() {
78         return mExtensionWindowAreaPresentation;
79     }
80 }
81