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 com.android.internal.compat;
18 
19 
20 import android.compat.Compatibility.ChangeConfig;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.HashSet;
25 import java.util.Set;
26 
27 /**
28  * Parcelable containing compat config overrides for a given application.
29  * @hide
30  */
31 public final class CompatibilityChangeConfig implements Parcelable {
32     private final ChangeConfig mChangeConfig;
33 
CompatibilityChangeConfig(ChangeConfig changeConfig)34     public CompatibilityChangeConfig(ChangeConfig changeConfig) {
35         mChangeConfig = changeConfig;
36     }
37 
38     /**
39      * Changes forced to be enabled.
40      */
enabledChanges()41     public Set<Long> enabledChanges() {
42         return mChangeConfig.getEnabledSet();
43     }
44 
45     /**
46      * Changes forced to be disabled.
47      */
disabledChanges()48     public Set<Long> disabledChanges() {
49         return mChangeConfig.getDisabledSet();
50     }
51 
52     /**
53      * Returns if a change is enabled or disabled in this config.
54      */
isChangeEnabled(long changeId)55     public boolean isChangeEnabled(long changeId) {
56         if (mChangeConfig.isForceEnabled(changeId)) {
57             return true;
58         } else if (mChangeConfig.isForceDisabled(changeId)) {
59             return false;
60         }
61         throw new IllegalStateException("Change " + changeId + " is not defined.");
62     }
63 
CompatibilityChangeConfig(Parcel in)64     private CompatibilityChangeConfig(Parcel in) {
65         long[] enabledArray = in.createLongArray();
66         long[] disabledArray = in.createLongArray();
67         Set<Long> enabled = toLongSet(enabledArray);
68         Set<Long> disabled = toLongSet(disabledArray);
69         mChangeConfig = new ChangeConfig(enabled, disabled);
70     }
71 
toLongSet(long[] values)72     private static Set<Long> toLongSet(long[] values) {
73         Set<Long> ret = new HashSet<>();
74         for (long value: values) {
75             ret.add(value);
76         }
77         return ret;
78     }
79 
80     @Override
describeContents()81     public int describeContents() {
82         return 0;
83     }
84 
85     @Override
writeToParcel(Parcel dest, int flags)86     public void writeToParcel(Parcel dest, int flags) {
87         long[] enabled = mChangeConfig.getEnabledChangesArray();
88         long[] disabled = mChangeConfig.getDisabledChangesArray();
89 
90         dest.writeLongArray(enabled);
91         dest.writeLongArray(disabled);
92     }
93 
94     public static final Parcelable.Creator<CompatibilityChangeConfig> CREATOR =
95             new Parcelable.Creator<CompatibilityChangeConfig>() {
96 
97                 @Override
98                 public CompatibilityChangeConfig createFromParcel(Parcel in) {
99                     return new CompatibilityChangeConfig(in);
100                 }
101 
102                 @Override
103                 public CompatibilityChangeConfig[] newArray(int size) {
104                     return new CompatibilityChangeConfig[size];
105                 }
106             };
107 }
108