Home | History | Annotate | Download | only in datausage
      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 
     15 package com.android.settings.datausage;
     16 
     17 import android.content.Context;
     18 import android.net.NetworkTemplate;
     19 import android.support.v7.preference.Preference;
     20 import android.util.AttributeSet;
     21 import com.android.settings.DividedCategory;
     22 
     23 public class TemplatePreferenceCategory extends DividedCategory 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