Home | History | Annotate | Download | only in featureflags
      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.development.featureflags;
     18 
     19 import android.content.Context;
     20 import android.support.v7.preference.PreferenceScreen;
     21 import android.util.FeatureFlagUtils;
     22 
     23 import com.android.settings.core.PreferenceControllerMixin;
     24 import com.android.settingslib.core.AbstractPreferenceController;
     25 import com.android.settingslib.core.lifecycle.Lifecycle;
     26 import com.android.settingslib.core.lifecycle.LifecycleObserver;
     27 import com.android.settingslib.core.lifecycle.events.OnStart;
     28 
     29 import java.util.Map;
     30 
     31 public class FeatureFlagsPreferenceController extends AbstractPreferenceController
     32         implements PreferenceControllerMixin, LifecycleObserver, OnStart {
     33 
     34     private PreferenceScreen mScreen;
     35 
     36     public FeatureFlagsPreferenceController(Context context, Lifecycle lifecycle) {
     37         super(context);
     38         if (lifecycle != null) {
     39             lifecycle.addObserver(this);
     40         }
     41     }
     42 
     43     @Override
     44     public boolean isAvailable() {
     45         return true;
     46     }
     47 
     48     @Override
     49     public String getPreferenceKey() {
     50         return null;
     51     }
     52 
     53     @Override
     54     public void displayPreference(PreferenceScreen screen) {
     55         super.displayPreference(screen);
     56         mScreen = screen;
     57     }
     58 
     59     @Override
     60     public void onStart() {
     61         if (mScreen == null) {
     62             return;
     63         }
     64         final Map<String, String> featureMap = FeatureFlagUtils.getAllFeatureFlags();
     65         if (featureMap == null) {
     66             return;
     67         }
     68         mScreen.removeAll();
     69         final Context prefContext = mScreen.getContext();
     70         for (String feature : featureMap.keySet()) {
     71             mScreen.addPreference(new FeatureFlagPreference(prefContext, feature));
     72         }
     73     }
     74 }
     75