1 /* 2 * Copyright (C) 2018 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.utils; 18 19 import android.text.TextUtils; 20 import android.util.ArrayMap; 21 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceGroup; 24 25 /** 26 * Class that helps track which {@link Preference}s in a {@link PreferenceGroup} are still being 27 * used, and remove unused ones. 28 */ 29 public class PreferenceGroupChildrenCache { 30 31 private ArrayMap<String, Preference> mPreferenceCache; 32 cacheRemoveAllPrefs(PreferenceGroup group)33 public void cacheRemoveAllPrefs(PreferenceGroup group) { 34 mPreferenceCache = new ArrayMap<>(); 35 final int N = group.getPreferenceCount(); 36 for (int i = 0; i < N; i++) { 37 Preference p = group.getPreference(i); 38 if (TextUtils.isEmpty(p.getKey())) { 39 continue; 40 } 41 mPreferenceCache.put(p.getKey(), p); 42 } 43 } 44 removeCachedPrefs(PreferenceGroup group)45 public void removeCachedPrefs(PreferenceGroup group) { 46 for (Preference p : mPreferenceCache.values()) { 47 group.removePreference(p); 48 } 49 mPreferenceCache = null; 50 } 51 getCachedPreference(String key)52 public Preference getCachedPreference(String key) { 53 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null; 54 } 55 getCachedCount()56 public int getCachedCount() { 57 return mPreferenceCache != null ? mPreferenceCache.size() : 0; 58 } 59 } 60