1 /*
2  * Copyright 2017 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.app.servertransaction;
18 
19 import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.app.ActivityThread.ActivityClientRecord;
24 import android.app.ClientTransactionHandler;
25 import android.content.res.CompatibilityInfo;
26 import android.content.res.Configuration;
27 import android.os.IBinder;
28 import android.os.Parcel;
29 import android.os.Trace;
30 
31 import java.util.Objects;
32 
33 /**
34  * Activity move to a different display message.
35  * @hide
36  */
37 public class MoveToDisplayItem extends ActivityTransactionItem {
38 
39     private int mTargetDisplayId;
40     private Configuration mConfiguration;
41 
42     @Override
preExecute(ClientTransactionHandler client, IBinder token)43     public void preExecute(ClientTransactionHandler client, IBinder token) {
44         CompatibilityInfo.applyOverrideScaleIfNeeded(mConfiguration);
45         // Notify the client of an upcoming change in the token configuration. This ensures that
46         // batches of config change items only process the newest configuration.
47         client.updatePendingActivityConfiguration(token, mConfiguration);
48     }
49 
50     @Override
execute(ClientTransactionHandler client, ActivityClientRecord r, PendingTransactionActions pendingActions)51     public void execute(ClientTransactionHandler client, ActivityClientRecord r,
52             PendingTransactionActions pendingActions) {
53         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityMovedToDisplay");
54         client.handleActivityConfigurationChanged(r, mConfiguration, mTargetDisplayId);
55         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
56     }
57 
58 
59     // ObjectPoolItem implementation
60 
MoveToDisplayItem()61     private MoveToDisplayItem() {}
62 
63     /** Obtain an instance initialized with provided params. */
obtain(int targetDisplayId, @NonNull Configuration configuration)64     public static MoveToDisplayItem obtain(int targetDisplayId,
65             @NonNull Configuration configuration) {
66         if (configuration == null) {
67             throw new IllegalArgumentException("Configuration must not be null");
68         }
69 
70         MoveToDisplayItem instance = ObjectPool.obtain(MoveToDisplayItem.class);
71         if (instance == null) {
72             instance = new MoveToDisplayItem();
73         }
74         instance.mTargetDisplayId = targetDisplayId;
75         instance.mConfiguration = configuration;
76 
77         return instance;
78     }
79 
80     @Override
recycle()81     public void recycle() {
82         mTargetDisplayId = 0;
83         mConfiguration = Configuration.EMPTY;
84         ObjectPool.recycle(this);
85     }
86 
87 
88     // Parcelable implementation
89 
90     /** Write to Parcel. */
91     @Override
writeToParcel(Parcel dest, int flags)92     public void writeToParcel(Parcel dest, int flags) {
93         dest.writeInt(mTargetDisplayId);
94         dest.writeTypedObject(mConfiguration, flags);
95     }
96 
97     /** Read from Parcel. */
MoveToDisplayItem(Parcel in)98     private MoveToDisplayItem(Parcel in) {
99         mTargetDisplayId = in.readInt();
100         mConfiguration = in.readTypedObject(Configuration.CREATOR);
101     }
102 
103     public static final @NonNull Creator<MoveToDisplayItem> CREATOR =
104             new Creator<MoveToDisplayItem>() {
105         public MoveToDisplayItem createFromParcel(Parcel in) {
106             return new MoveToDisplayItem(in);
107         }
108 
109         public MoveToDisplayItem[] newArray(int size) {
110             return new MoveToDisplayItem[size];
111         }
112     };
113 
114     @Override
equals(@ullable Object o)115     public boolean equals(@Nullable Object o) {
116         if (this == o) {
117             return true;
118         }
119         if (o == null || getClass() != o.getClass()) {
120             return false;
121         }
122         final MoveToDisplayItem other = (MoveToDisplayItem) o;
123         return mTargetDisplayId == other.mTargetDisplayId
124                 && Objects.equals(mConfiguration, other.mConfiguration);
125     }
126 
127     @Override
hashCode()128     public int hashCode() {
129         int result = 17;
130         result = 31 * result + mTargetDisplayId;
131         result = 31 * result + mConfiguration.hashCode();
132         return result;
133     }
134 
135     @Override
toString()136     public String toString() {
137         return "MoveToDisplayItem{targetDisplayId=" + mTargetDisplayId
138                 + ",configuration=" + mConfiguration + "}";
139     }
140 }
141