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