Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2017 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.settings.widget;
     18 
     19 import android.content.Context;
     20 
     21 import com.android.settings.core.BasePreferenceController;
     22 import com.android.settingslib.core.AbstractPreferenceController;
     23 
     24 import java.util.ArrayList;
     25 import java.util.List;
     26 
     27 /**
     28  * A controller for generic Preference categories. If all controllers for its children reports
     29  * not-available, this controller will also report not-available, and subsequently will be hidden by
     30  * UI.
     31  */
     32 public class PreferenceCategoryController extends BasePreferenceController {
     33 
     34     private final String mKey;
     35     private final List<AbstractPreferenceController> mChildren;
     36 
     37     public PreferenceCategoryController(Context context, String key) {
     38         super(context, key);
     39         mKey = key;
     40         mChildren = new ArrayList<>();
     41     }
     42 
     43     @Override
     44     public int getAvailabilityStatus() {
     45         if (mChildren == null || mChildren.isEmpty()) {
     46             return UNSUPPORTED_ON_DEVICE;
     47         }
     48         // Category is available if any child is available
     49         for (AbstractPreferenceController controller : mChildren) {
     50             if (controller.isAvailable()) {
     51                 return AVAILABLE;
     52             }
     53         }
     54         return CONDITIONALLY_UNAVAILABLE;
     55     }
     56 
     57     @Override
     58     public String getPreferenceKey() {
     59         return mKey;
     60     }
     61 
     62     public PreferenceCategoryController setChildren(
     63             List<AbstractPreferenceController> childrenController) {
     64         mChildren.clear();
     65         if (childrenController != null) {
     66             mChildren.addAll(childrenController);
     67         }
     68         return this;
     69     }
     70 }
     71