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 android.annotation.Nullable;
20 import android.app.ClientTransactionHandler;
21 import android.content.res.CompatibilityInfo;
22 import android.content.res.Configuration;
23 import android.os.IBinder;
24 import android.os.Parcel;
25 
26 import java.util.Objects;
27 
28 /**
29  * App configuration change message.
30  * @hide
31  */
32 public class ConfigurationChangeItem extends ClientTransactionItem {
33 
34     private Configuration mConfiguration;
35     private int mDeviceId;
36 
37     @Override
preExecute(android.app.ClientTransactionHandler client, IBinder token)38     public void preExecute(android.app.ClientTransactionHandler client, IBinder token) {
39         CompatibilityInfo.applyOverrideScaleIfNeeded(mConfiguration);
40         client.updatePendingConfiguration(mConfiguration);
41     }
42 
43     @Override
execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions)44     public void execute(ClientTransactionHandler client, IBinder token,
45             PendingTransactionActions pendingActions) {
46         client.handleConfigurationChanged(mConfiguration, mDeviceId);
47     }
48 
49 
50     // ObjectPoolItem implementation
51 
ConfigurationChangeItem()52     private ConfigurationChangeItem() {}
53 
54     /** Obtain an instance initialized with provided params. */
obtain(Configuration config, int deviceId)55     public static ConfigurationChangeItem obtain(Configuration config, int deviceId) {
56         ConfigurationChangeItem instance = ObjectPool.obtain(ConfigurationChangeItem.class);
57         if (instance == null) {
58             instance = new ConfigurationChangeItem();
59         }
60         instance.mConfiguration = config;
61         instance.mDeviceId = deviceId;
62 
63         return instance;
64     }
65 
66     @Override
recycle()67     public void recycle() {
68         mConfiguration = null;
69         mDeviceId = 0;
70         ObjectPool.recycle(this);
71     }
72 
73 
74     // Parcelable implementation
75 
76     /** Write to Parcel. */
77     @Override
writeToParcel(Parcel dest, int flags)78     public void writeToParcel(Parcel dest, int flags) {
79         dest.writeTypedObject(mConfiguration, flags);
80         dest.writeInt(mDeviceId);
81     }
82 
83     /** Read from Parcel. */
ConfigurationChangeItem(Parcel in)84     private ConfigurationChangeItem(Parcel in) {
85         mConfiguration = in.readTypedObject(Configuration.CREATOR);
86         mDeviceId = in.readInt();
87     }
88 
89     public static final @android.annotation.NonNull Creator<ConfigurationChangeItem> CREATOR =
90             new Creator<ConfigurationChangeItem>() {
91         public ConfigurationChangeItem createFromParcel(Parcel in) {
92             return new ConfigurationChangeItem(in);
93         }
94 
95         public ConfigurationChangeItem[] newArray(int size) {
96             return new ConfigurationChangeItem[size];
97         }
98     };
99 
100     @Override
equals(@ullable Object o)101     public boolean equals(@Nullable Object o) {
102         if (this == o) {
103             return true;
104         }
105         if (o == null || getClass() != o.getClass()) {
106             return false;
107         }
108         final ConfigurationChangeItem other = (ConfigurationChangeItem) o;
109         return Objects.equals(mConfiguration, other.mConfiguration)
110                 && mDeviceId == other.mDeviceId;
111     }
112 
113     @Override
hashCode()114     public int hashCode() {
115         int result = 17;
116         result = 31 * result + mDeviceId;
117         result = 31 * result + mConfiguration.hashCode();
118         return result;
119     }
120 
121     @Override
toString()122     public String toString() {
123         return "ConfigurationChangeItem{deviceId=" + mDeviceId + ", config" + mConfiguration + "}";
124     }
125 }
126