Evan Rosky | ddedfd4 | 2019-10-04 13:38:38 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 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 android.view; |
| 18 | |
| 19 | import android.app.WindowConfiguration; |
| 20 | import android.content.pm.ActivityInfo; |
| 21 | import android.content.res.Configuration; |
| 22 | import android.graphics.Rect; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
| 26 | import android.util.ArrayMap; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * Represents a collection of operations on some WindowContainers that should be applied all at |
| 32 | * once. |
| 33 | * |
| 34 | * @hide |
| 35 | */ |
| 36 | public class WindowContainerTransaction implements Parcelable { |
| 37 | private final ArrayMap<IBinder, Change> mChanges = new ArrayMap<>(); |
| 38 | |
| 39 | public WindowContainerTransaction() {} |
| 40 | |
| 41 | protected WindowContainerTransaction(Parcel in) { |
| 42 | in.readMap(mChanges, null /* loader */); |
| 43 | } |
| 44 | |
| 45 | private Change getOrCreateChange(IBinder token) { |
| 46 | Change out = mChanges.get(token); |
| 47 | if (out == null) { |
| 48 | out = new Change(); |
| 49 | mChanges.put(token, out); |
| 50 | } |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Resize a container. |
| 56 | */ |
| 57 | public WindowContainerTransaction setBounds(IWindowContainer container, Rect bounds) { |
| 58 | Change chg = getOrCreateChange(container.asBinder()); |
| 59 | chg.mConfiguration.windowConfiguration.setBounds(bounds); |
| 60 | chg.mConfigSetMask |= ActivityInfo.CONFIG_WINDOW_CONFIGURATION; |
| 61 | chg.mWindowSetMask |= WindowConfiguration.WINDOW_CONFIG_BOUNDS; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | public Map<IBinder, Change> getChanges() { |
| 66 | return mChanges; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return "WindowContainerTransaction { changes = " + mChanges + " }"; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void writeToParcel(Parcel dest, int flags) { |
| 76 | dest.writeMap(mChanges); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int describeContents() { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | public static final Creator<WindowContainerTransaction> CREATOR = |
| 85 | new Creator<WindowContainerTransaction>() { |
| 86 | @Override |
| 87 | public WindowContainerTransaction createFromParcel(Parcel in) { |
| 88 | return new WindowContainerTransaction(in); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public WindowContainerTransaction[] newArray(int size) { |
| 93 | return new WindowContainerTransaction[size]; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Holds changes on a single WindowContainer including Configuration changes. |
| 99 | * |
| 100 | * @hide |
| 101 | */ |
| 102 | public static class Change implements Parcelable { |
| 103 | private final Configuration mConfiguration = new Configuration(); |
| 104 | private @ActivityInfo.Config int mConfigSetMask = 0; |
| 105 | private @WindowConfiguration.WindowConfig int mWindowSetMask = 0; |
| 106 | |
| 107 | public Change() {} |
| 108 | |
| 109 | protected Change(Parcel in) { |
| 110 | mConfiguration.readFromParcel(in); |
| 111 | mConfigSetMask = in.readInt(); |
| 112 | mWindowSetMask = in.readInt(); |
| 113 | } |
| 114 | |
| 115 | public Configuration getConfiguration() { |
| 116 | return mConfiguration; |
| 117 | } |
| 118 | |
| 119 | @ActivityInfo.Config |
| 120 | public int getConfigSetMask() { |
| 121 | return mConfigSetMask; |
| 122 | } |
| 123 | |
| 124 | @WindowConfiguration.WindowConfig |
| 125 | public int getWindowSetMask() { |
| 126 | return mWindowSetMask; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | final boolean changesBounds = |
| 132 | (mConfigSetMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0 |
| 133 | && ((mWindowSetMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) |
| 134 | != 0); |
| 135 | final boolean changesSss = |
| 136 | (mConfigSetMask & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0; |
| 137 | StringBuilder sb = new StringBuilder(); |
| 138 | sb.append('{'); |
| 139 | if (changesBounds) { |
| 140 | sb.append("bounds:" + mConfiguration.windowConfiguration.getBounds() + ","); |
| 141 | } |
| 142 | if (changesSss) { |
| 143 | sb.append("ssw:" + mConfiguration.smallestScreenWidthDp + ","); |
| 144 | } |
| 145 | sb.append("}"); |
| 146 | return sb.toString(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void writeToParcel(Parcel dest, int flags) { |
| 151 | mConfiguration.writeToParcel(dest, flags); |
| 152 | dest.writeInt(mConfigSetMask); |
| 153 | dest.writeInt(mWindowSetMask); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int describeContents() { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | public static final Creator<Change> CREATOR = new Creator<Change>() { |
| 162 | @Override |
| 163 | public Change createFromParcel(Parcel in) { |
| 164 | return new Change(in); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Change[] newArray(int size) { |
| 169 | return new Change[size]; |
| 170 | } |
| 171 | }; |
| 172 | } |
| 173 | } |