1/*
2 * Copyright (C) 2016 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
15package com.android.settings.datausage;
16
17import android.content.Context;
18import android.net.NetworkTemplate;
19import android.support.v7.preference.Preference;
20import android.support.v7.preference.PreferenceCategory;
21import android.util.AttributeSet;
22
23public class TemplatePreferenceCategory extends PreferenceCategory implements TemplatePreference {
24
25    private NetworkTemplate mTemplate;
26    private int mSubId;
27
28    public TemplatePreferenceCategory(Context context, AttributeSet attrs) {
29        super(context, attrs);
30    }
31
32    @Override
33    public void setTemplate(NetworkTemplate template, int subId,
34            NetworkServices services) {
35        mTemplate = template;
36        mSubId = subId;
37    }
38
39    @Override
40    public boolean addPreference(Preference preference) {
41        if (!(preference instanceof TemplatePreference)) {
42            throw new IllegalArgumentException(
43                    "TemplatePreferenceCategories can only hold TemplatePreferences");
44        }
45        return super.addPreference(preference);
46    }
47
48    public void pushTemplates(NetworkServices services) {
49        if (mTemplate == null) {
50            throw new RuntimeException("null mTemplate for " + getKey());
51        }
52        for (int i = 0; i < getPreferenceCount(); i++) {
53            ((TemplatePreference) getPreference(i)).setTemplate(mTemplate, mSubId, services);
54        }
55    }
56
57}
58