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 package com.android.server.compat;
17 
18 import android.content.Context;
19 
20 import com.android.internal.compat.AndroidBuildClassifier;
21 
22 import java.util.ArrayList;
23 
24 /**
25  * Helper class for creating a CompatConfig.
26  */
27 class CompatConfigBuilder {
28     private ArrayList<CompatChange> mChanges;
29     private AndroidBuildClassifier mBuildClassifier;
30     private Context mContext;
31 
CompatConfigBuilder(AndroidBuildClassifier buildClassifier, Context context)32     private CompatConfigBuilder(AndroidBuildClassifier buildClassifier, Context context) {
33         mChanges = new ArrayList<>();
34         mBuildClassifier = buildClassifier;
35         mContext = context;
36     }
37 
create(AndroidBuildClassifier buildClassifier, Context context)38     static CompatConfigBuilder create(AndroidBuildClassifier buildClassifier, Context context) {
39         return new CompatConfigBuilder(buildClassifier, context);
40     }
41 
addEnableAfterSdkChangeWithId(int sdk, long id)42     CompatConfigBuilder addEnableAfterSdkChangeWithId(int sdk, long id) {
43         mChanges.add(new CompatChange(id, "", sdk, -1, false, false, "", false));
44         return this;
45     }
46 
addEnableAfterSdkChangeWithIdAndName(int sdk, long id, String name)47     CompatConfigBuilder addEnableAfterSdkChangeWithIdAndName(int sdk, long id, String name) {
48         mChanges.add(new CompatChange(id, name, sdk, -1, false, false, "", false));
49         return this;
50     }
51 
addEnableAfterSdkChangeWithIdDefaultDisabled(int sdk, long id)52     CompatConfigBuilder addEnableAfterSdkChangeWithIdDefaultDisabled(int sdk, long id) {
53         mChanges.add(new CompatChange(id, "", sdk, -1, true, false, "", false));
54         return this;
55     }
56 
addEnableAfterSdkChangeWithIdAndDescription(int sdk, long id, String description)57     CompatConfigBuilder addEnableAfterSdkChangeWithIdAndDescription(int sdk, long id,
58             String description) {
59         mChanges.add(new CompatChange(id, "", sdk, -1, false, false, description, false));
60         return this;
61     }
62 
addEnableSinceSdkChangeWithId(int sdk, long id)63     CompatConfigBuilder addEnableSinceSdkChangeWithId(int sdk, long id) {
64         mChanges.add(new CompatChange(id, "", -1, sdk, false, false, "", false));
65         return this;
66     }
67 
addEnableSinceSdkChangeWithIdAndName(int sdk, long id, String name)68     CompatConfigBuilder addEnableSinceSdkChangeWithIdAndName(int sdk, long id, String name) {
69         mChanges.add(new CompatChange(id, name, -1, sdk, false, false, "", false));
70         return this;
71     }
72 
addEnableSinceSdkChangeWithIdDefaultDisabled(int sdk, long id)73     CompatConfigBuilder addEnableSinceSdkChangeWithIdDefaultDisabled(int sdk, long id) {
74         mChanges.add(new CompatChange(id, "", -1, sdk, true, false, "", false));
75         return this;
76     }
77 
addEnableSinceSdkChangeWithIdAndDescription(int sdk, long id, String description)78     CompatConfigBuilder addEnableSinceSdkChangeWithIdAndDescription(int sdk, long id,
79             String description) {
80         mChanges.add(new CompatChange(id, "", -1, sdk, false, false, description, false));
81         return this;
82     }
83 
addEnabledChangeWithId(long id)84     CompatConfigBuilder addEnabledChangeWithId(long id) {
85         mChanges.add(new CompatChange(id, "", -1, -1, false, false, "", false));
86         return this;
87     }
88 
addEnabledChangeWithIdAndName(long id, String name)89     CompatConfigBuilder addEnabledChangeWithIdAndName(long id, String name) {
90         mChanges.add(new CompatChange(id, name, -1, -1, false, false, "", false));
91         return this;
92     }
addEnabledChangeWithIdAndDescription(long id, String description)93     CompatConfigBuilder addEnabledChangeWithIdAndDescription(long id, String description) {
94         mChanges.add(new CompatChange(id, "", -1, -1, false, false, description, false));
95         return this;
96     }
97 
addDisabledChangeWithId(long id)98     CompatConfigBuilder addDisabledChangeWithId(long id) {
99         mChanges.add(new CompatChange(id, "", -1, -1, true, false, "", false));
100         return this;
101     }
102 
addDisabledChangeWithIdAndName(long id, String name)103     CompatConfigBuilder addDisabledChangeWithIdAndName(long id, String name) {
104         mChanges.add(new CompatChange(id, name, -1, -1, true, false, "", false));
105         return this;
106     }
107 
addDisabledChangeWithIdAndDescription(long id, String description)108     CompatConfigBuilder addDisabledChangeWithIdAndDescription(long id, String description) {
109         mChanges.add(new CompatChange(id, "", -1, -1, true, false, description, false));
110         return this;
111     }
112 
addLoggingOnlyChangeWithId(long id)113     CompatConfigBuilder addLoggingOnlyChangeWithId(long id) {
114         mChanges.add(new CompatChange(id, "", -1, -1, false, true, "", false));
115         return this;
116     }
117 
addEnabledOverridableChangeWithId(long id)118     CompatConfigBuilder addEnabledOverridableChangeWithId(long id) {
119         mChanges.add(new CompatChange(id, "", -1, -1, false, false, "", true));
120         return this;
121     }
122 
addDisabledOverridableChangeWithId(long id)123     CompatConfigBuilder addDisabledOverridableChangeWithId(long id) {
124         mChanges.add(new CompatChange(id, "", -1, -1, true, false, "", true));
125         return this;
126     }
127 
addEnabledSinceApexChangeWithId(int sdk, long id)128     CompatConfigBuilder addEnabledSinceApexChangeWithId(int sdk, long id) {
129         mChanges.add(new CompatChange(id, "", -1, sdk, false, false, "", false));
130         return this;
131     }
132 
build()133     CompatConfig build() {
134         CompatConfig config = new CompatConfig(mBuildClassifier, mContext);
135         config.forceNonDebuggableFinalForTest(false);
136         for (CompatChange change : mChanges) {
137             config.addChange(change);
138         }
139         return config;
140     }
141 }
142