Home | History | Annotate | Download | only in enterprise
      1 
      2 /*
      3  * Copyright (C) 2017 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      6  * except in compliance with the License. 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 distributed under the
     11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     12  * KIND, either express or implied. See the License for the specific language governing
     13  * permissions and limitations under the License.
     14  */
     15 
     16 package com.android.settings.enterprise;
     17 
     18 import android.content.Context;
     19 import android.support.v7.preference.Preference;
     20 
     21 import com.android.settings.core.DynamicAvailabilityPreferenceController;
     22 import com.android.settings.core.PreferenceAvailabilityObserver;
     23 import com.android.settingslib.core.lifecycle.Lifecycle;
     24 
     25 import java.util.HashSet;
     26 import java.util.List;
     27 import java.util.Set;
     28 
     29 /**
     30  * A controller that hides a {@link android.support.v7.preference.PreferenceGroup} when none of the
     31  * {@link Preference}s inside it are visible.
     32  *
     33  * TODO(b/62051162): Use {@link android.support.v7.preference.PreferenceGroup}'s native ability to
     34  * hide itself when all {@link Preference}s inside it are invisible when that functionality becomes
     35  * available. This custom controller will still be needed to remove the
     36  * {@link android.support.v7.preference.PreferenceGroup} from the search index as required (by
     37  * having {@link #isAvailable()} return {@code false} if the method returns {@code false} for all
     38  * {@link Preference}s in the {@link android.support.v7.preference.PreferenceGroup}).
     39  */
     40 public class ExposureChangesCategoryPreferenceController
     41         extends DynamicAvailabilityPreferenceController implements PreferenceAvailabilityObserver {
     42 
     43     private static final String KEY_EXPOSURE_CHANGES_CATEGORY = "exposure_changes_category";
     44     private final Set<String> mAvailablePrefs = new HashSet<String>();
     45     private Preference mPreference = null;
     46     private boolean mControllingUi;
     47 
     48     /**
     49      * When {@code controllingUi} is {@code true}, some of the preferences may have their visibility
     50      * determined asynchronously. In this case, {@link #isAvailable()} must always return {@code
     51      * true} and the group should be hidden using {@link Preference#setVisible()} if all preferences
     52      * report that they are invisible.
     53      * When {@code controllingUi} is {@code false}, we are running on the search indexer thread and
     54      * visibility must be determined synchronously. {@link #isAvailable()} can rely on all
     55      * preferences having their visibility determined already and should return whether the group is
     56      * visible or not.
     57      */
     58     public ExposureChangesCategoryPreferenceController(Context context, Lifecycle lifecycle,
     59             List<DynamicAvailabilityPreferenceController> controllers, boolean controllingUi) {
     60         super(context, lifecycle);
     61         mControllingUi = controllingUi;
     62         for (final DynamicAvailabilityPreferenceController controller : controllers) {
     63             controller.setAvailabilityObserver(this);
     64         }
     65     }
     66 
     67     @Override
     68     public void onPreferenceAvailabilityUpdated(String key, boolean available) {
     69         if (available) {
     70             mAvailablePrefs.add(key);
     71         } else {
     72             mAvailablePrefs.remove(key);
     73         }
     74         available = haveAnyVisiblePreferences();
     75         if (mControllingUi) {
     76             notifyOnAvailabilityUpdate(available);
     77         }
     78         if (mPreference != null) {
     79             mPreference.setVisible(available);
     80         }
     81     }
     82 
     83     @Override
     84     public void updateState(Preference preference) {
     85         mPreference = preference;
     86         mPreference.setVisible(haveAnyVisiblePreferences());
     87     }
     88 
     89     @Override
     90     public boolean isAvailable() {
     91         if (mControllingUi) {
     92             // When running on the main UI thread, some preferences determine their visibility
     93             // asynchronously. Always return true here and determine the pref group's actual
     94             // visibility as the other preferences report their visibility asynchronously via
     95             // onPreferenceAvailabilityUpdated().
     96             return true;
     97         }
     98         final boolean available = haveAnyVisiblePreferences();
     99         notifyOnAvailabilityUpdate(available);
    100         return available;
    101     }
    102 
    103     @Override
    104     public String getPreferenceKey() {
    105         return KEY_EXPOSURE_CHANGES_CATEGORY;
    106     }
    107 
    108     private boolean haveAnyVisiblePreferences() {
    109         return mAvailablePrefs.size() > 0;
    110     }
    111 }
    112