| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.content.res.Configuration; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
|
| 25 |
import java.io.PrintWriter; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class MergedConfiguration implements Parcelable { |
| 35 |
|
| 36 |
private final Configuration mGlobalConfig = new Configuration(); |
| 37 |
private final Configuration mOverrideConfig = new Configuration(); |
| 38 |
private final Configuration mMergedConfig = new Configuration(); |
| 39 |
|
| 40 |
public MergedConfiguration() { |
| 41 |
} |
| 42 |
|
| 43 |
public MergedConfiguration(Configuration globalConfig, Configuration overrideConfig) { |
| 44 |
setConfiguration(globalConfig, overrideConfig); |
| 45 |
} |
| 46 |
|
| 47 |
public MergedConfiguration(Configuration globalConfig) { |
| 48 |
setGlobalConfiguration(globalConfig); |
| 49 |
} |
| 50 |
|
| 51 |
public MergedConfiguration(MergedConfiguration mergedConfiguration) { |
| 52 |
setConfiguration(mergedConfiguration.getGlobalConfiguration(), |
| 53 |
mergedConfiguration.getOverrideConfiguration()); |
| 54 |
} |
| 55 |
|
| 56 |
private MergedConfiguration(Parcel in) { |
| 57 |
readFromParcel(in); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void writeToParcel(Parcel dest, int flags) { |
| 62 |
mGlobalConfig.writeToParcel(dest, flags); |
| 63 |
mOverrideConfig.writeToParcel(dest, flags); |
| 64 |
mMergedConfig.writeToParcel(dest, flags); |
| 65 |
} |
| 66 |
|
| 67 |
public void readFromParcel(Parcel source) { |
| 68 |
mGlobalConfig.readFromParcel(source); |
| 69 |
mOverrideConfig.readFromParcel(source); |
| 70 |
mMergedConfig.readFromParcel(source); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public int describeContents() { |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
public static final @android.annotation.NonNull Creator<MergedConfiguration> CREATOR = new Creator<MergedConfiguration>() { |
| 79 |
@Override |
| 80 |
public MergedConfiguration createFromParcel(Parcel in) { |
| 81 |
return new MergedConfiguration(in); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public MergedConfiguration[] newArray(int size) { |
| 86 |
return new MergedConfiguration[size]; |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setConfiguration(Configuration globalConfig, Configuration overrideConfig) { |
| 97 |
mGlobalConfig.setTo(globalConfig); |
| 98 |
mOverrideConfig.setTo(overrideConfig); |
| 99 |
updateMergedConfig(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public void setGlobalConfiguration(Configuration globalConfig) { |
| 108 |
mGlobalConfig.setTo(globalConfig); |
| 109 |
updateMergedConfig(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void setOverrideConfiguration(Configuration overrideConfig) { |
| 118 |
mOverrideConfig.setTo(overrideConfig); |
| 119 |
updateMergedConfig(); |
| 120 |
} |
| 121 |
|
| 122 |
public void setTo(MergedConfiguration config) { |
| 123 |
setConfiguration(config.mGlobalConfig, config.mOverrideConfig); |
| 124 |
} |
| 125 |
|
| 126 |
public void unset() { |
| 127 |
mGlobalConfig.unset(); |
| 128 |
mOverrideConfig.unset(); |
| 129 |
updateMergedConfig(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
@NonNull |
| 136 |
public Configuration getGlobalConfiguration() { |
| 137 |
return mGlobalConfig; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public Configuration getOverrideConfiguration() { |
| 144 |
return mOverrideConfig; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public Configuration getMergedConfiguration() { |
| 151 |
return mMergedConfig; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
private void updateMergedConfig() { |
| 156 |
mMergedConfig.setTo(mGlobalConfig); |
| 157 |
mMergedConfig.updateFrom(mOverrideConfig); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public String toString() { |
| 162 |
return "{mGlobalConfig=" + mGlobalConfig + " mOverrideConfig=" + mOverrideConfig + "}"; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public int hashCode() { |
| 167 |
return mMergedConfig.hashCode(); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public boolean equals(@Nullable Object that) { |
| 172 |
if (!(that instanceof MergedConfiguration)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
if (that == this) return true; |
| 177 |
return mMergedConfig.equals(((MergedConfiguration) that).mMergedConfig); |
| 178 |
} |
| 179 |
|
| 180 |
public void dump(PrintWriter pw, String prefix) { |
| 181 |
pw.println(prefix + "mGlobalConfig=" + mGlobalConfig); |
| 182 |
pw.println(prefix + "mOverrideConfig=" + mOverrideConfig); |
| 183 |
} |
| 184 |
} |