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