1 /*
2  * Copyright (C) 2018 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 android.view;
18 
19 import android.annotation.Nullable;
20 import android.graphics.Point;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.view.InsetsState.InternalInsetsType;
24 
25 import java.io.PrintWriter;
26 import java.util.function.Consumer;
27 
28 /**
29  * Represents a parcelable object to allow controlling a single {@link InsetsSource}.
30  * @hide
31  */
32 public class InsetsSourceControl implements Parcelable {
33 
34     private final @InternalInsetsType int mType;
35     private final @Nullable SurfaceControl mLeash;
36     private final Point mSurfacePosition;
37 
InsetsSourceControl(@nternalInsetsType int type, @Nullable SurfaceControl leash, Point surfacePosition)38     public InsetsSourceControl(@InternalInsetsType int type, @Nullable SurfaceControl leash,
39             Point surfacePosition) {
40         mType = type;
41         mLeash = leash;
42         mSurfacePosition = surfacePosition;
43     }
44 
InsetsSourceControl(InsetsSourceControl other)45     public InsetsSourceControl(InsetsSourceControl other) {
46         mType = other.mType;
47         if (other.mLeash != null) {
48             mLeash = new SurfaceControl(other.mLeash, "InsetsSourceControl");
49         } else {
50             mLeash = null;
51         }
52         mSurfacePosition = new Point(other.mSurfacePosition);
53     }
54 
getType()55     public int getType() {
56         return mType;
57     }
58 
59     /**
60      * Gets the leash for controlling insets source. If the system is controlling the insets source,
61      * for example, transient bars, the client will receive fake controls without leash in it.
62      *
63      * @return the leash.
64      */
getLeash()65     public @Nullable SurfaceControl getLeash() {
66         return mLeash;
67     }
68 
InsetsSourceControl(Parcel in)69     public InsetsSourceControl(Parcel in) {
70         mType = in.readInt();
71         mLeash = in.readParcelable(null /* loader */);
72         mSurfacePosition = in.readParcelable(null /* loader */);
73     }
74 
setSurfacePosition(int left, int top)75     public boolean setSurfacePosition(int left, int top) {
76         if (mSurfacePosition.equals(left, top)) {
77             return false;
78         }
79         mSurfacePosition.set(left, top);
80         return true;
81     }
82 
getSurfacePosition()83     public Point getSurfacePosition() {
84         return mSurfacePosition;
85     }
86 
87     @Override
describeContents()88     public int describeContents() {
89         return 0;
90     }
91 
92     @Override
writeToParcel(Parcel dest, int flags)93     public void writeToParcel(Parcel dest, int flags) {
94         dest.writeInt(mType);
95         dest.writeParcelable(mLeash, 0 /* flags*/);
96         dest.writeParcelable(mSurfacePosition, 0 /* flags*/);
97     }
98 
release(Consumer<SurfaceControl> surfaceReleaseConsumer)99     public void release(Consumer<SurfaceControl> surfaceReleaseConsumer) {
100         if (mLeash != null) {
101             surfaceReleaseConsumer.accept(mLeash);
102         }
103     }
104 
dump(String prefix, PrintWriter pw)105     public void dump(String prefix, PrintWriter pw) {
106         pw.print(prefix);
107         pw.print("InsetsSourceControl type="); pw.print(InsetsState.typeToString(mType));
108         pw.print(" mLeash="); pw.print(mLeash);
109         pw.print(" mSurfacePosition="); pw.print(mSurfacePosition);
110         pw.println();
111     }
112 
113     public static final @android.annotation.NonNull Creator<InsetsSourceControl> CREATOR
114             = new Creator<InsetsSourceControl>() {
115         public InsetsSourceControl createFromParcel(Parcel in) {
116             return new InsetsSourceControl(in);
117         }
118 
119         public InsetsSourceControl[] newArray(int size) {
120             return new InsetsSourceControl[size];
121         }
122     };
123 }
124