1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.car.developeroptions.datausage;
16 
17 import android.content.Context;
18 import android.net.NetworkTemplate;
19 import android.util.AttributeSet;
20 
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceCategory;
23 
24 public class TemplatePreferenceCategory extends PreferenceCategory implements TemplatePreference {
25 
26     private NetworkTemplate mTemplate;
27     private int mSubId;
28 
TemplatePreferenceCategory(Context context, AttributeSet attrs)29     public TemplatePreferenceCategory(Context context, AttributeSet attrs) {
30         super(context, attrs);
31     }
32 
33     @Override
setTemplate(NetworkTemplate template, int subId, NetworkServices services)34     public void setTemplate(NetworkTemplate template, int subId,
35             NetworkServices services) {
36         mTemplate = template;
37         mSubId = subId;
38     }
39 
40     @Override
addPreference(Preference preference)41     public boolean addPreference(Preference preference) {
42         if (!(preference instanceof TemplatePreference)) {
43             throw new IllegalArgumentException(
44                     "TemplatePreferenceCategories can only hold TemplatePreferences");
45         }
46         return super.addPreference(preference);
47     }
48 
pushTemplates(NetworkServices services)49     public void pushTemplates(NetworkServices services) {
50         if (mTemplate == null) {
51             throw new RuntimeException("null mTemplate for " + getKey());
52         }
53         for (int i = 0; i < getPreferenceCount(); i++) {
54             ((TemplatePreference) getPreference(i)).setTemplate(mTemplate, mSubId, services);
55         }
56     }
57 
58 }
59