Workarounds to avoid removing all prefs

am: 2071eda150

* commit '2071eda150c4ade320fa91ec99678114afbd223e':
  Workarounds to avoid removing all prefs
This commit is contained in:
Jason Monk
2016-02-26 16:05:35 +00:00
committed by android-build-merger
5 changed files with 72 additions and 29 deletions

View File

@@ -34,6 +34,7 @@ import android.support.v7.preference.PreferenceViewHolder;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@@ -97,6 +98,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
private View mEmptyView; private View mEmptyView;
private LinearLayoutManager mLayoutManager; private LinearLayoutManager mLayoutManager;
private HighlightablePreferenceGroupAdapter mAdapter; private HighlightablePreferenceGroupAdapter mAdapter;
private ArrayMap<String, Preference> mPreferenceCache;
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@@ -367,6 +369,28 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
return mAdapter; return mAdapter;
} }
protected void cacheRemoveAllPrefs(PreferenceGroup group) {
mPreferenceCache = new ArrayMap<String, Preference>();
final int N = group.getPreferenceCount();
for (int i = 0; i < N; i++) {
Preference p = group.getPreference(i);
if (TextUtils.isEmpty(p.getKey())) {
continue;
}
mPreferenceCache.put(p.getKey(), p);
}
}
protected Preference getCachedPreference(String key) {
return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
}
protected void removeCachedPrefs(PreferenceGroup group) {
for (Preference p : mPreferenceCache.values()) {
group.removePreference(p);
}
}
private void highlightPreference(String key) { private void highlightPreference(String key) {
final int position = canUseListViewForHighLighting(key); final int position = canUseListViewForHighLighting(key);
if (position >= 0) { if (position >= 0) {

View File

@@ -84,6 +84,10 @@ public final class BluetoothDevicePreference extends Preference implements
onDeviceAttributesChanged(); onDeviceAttributesChanged();
} }
void rebind() {
notifyChanged();
}
CachedBluetoothDevice getCachedDevice() { CachedBluetoothDevice getCachedDevice() {
return mCachedDevice; return mCachedDevice;
} }

View File

@@ -39,7 +39,6 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.settings.LinkifyUtils; import com.android.settings.LinkifyUtils;
@@ -77,6 +76,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES = private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES =
"android.btopp.intent.action.OPEN_RECEIVED_FILES"; "android.btopp.intent.action.OPEN_RECEIVED_FILES";
private static final String KEY_PAIRED_DEVICES = "paired_devices";
private static View mSettingsDialogView = null; private static View mSettingsDialogView = null;
private BluetoothEnabler mBluetoothEnabler; private BluetoothEnabler mBluetoothEnabler;
@@ -154,6 +155,21 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
void addPreferencesForActivity() { void addPreferencesForActivity() {
addPreferencesFromResource(R.xml.bluetooth_settings); addPreferencesFromResource(R.xml.bluetooth_settings);
mPairedDevicesCategory = new PreferenceCategory(getPrefContext());
mPairedDevicesCategory.setKey(KEY_PAIRED_DEVICES);
mPairedDevicesCategory.setOrder(1);
getPreferenceScreen().addPreference(mPairedDevicesCategory);
mAvailableDevicesCategory = new BluetoothProgressCategory(getActivity());
mAvailableDevicesCategory.setSelectable(false);
mAvailableDevicesCategory.setOrder(2);
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
mMyDevicePreference = new Preference(getPrefContext());
mMyDevicePreference.setSelectable(false);
mMyDevicePreference.setOrder(3);
getPreferenceScreen().addPreference(mMyDevicePreference);
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@@ -275,14 +291,15 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId, private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) { BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
cacheRemoveAllPrefs(preferenceGroup);
preferenceGroup.setTitle(titleId); preferenceGroup.setTitle(titleId);
getPreferenceScreen().addPreference(preferenceGroup);
setFilter(filter); setFilter(filter);
setDeviceListGroup(preferenceGroup); setDeviceListGroup(preferenceGroup);
if (addCachedDevices) { if (addCachedDevices) {
addCachedDevices(); addCachedDevices();
} }
preferenceGroup.setEnabled(true); preferenceGroup.setEnabled(true);
removeCachedPrefs(preferenceGroup);
} }
private void updateContent(int bluetoothState) { private void updateContent(int bluetoothState) {
@@ -291,8 +308,6 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
switch (bluetoothState) { switch (bluetoothState) {
case BluetoothAdapter.STATE_ON: case BluetoothAdapter.STATE_ON:
preferenceScreen.removeAll();
preferenceScreen.setOrderingAsAdded(true);
mDevicePreferenceMap.clear(); mDevicePreferenceMap.clear();
if (isUiRestricted()) { if (isUiRestricted()) {
@@ -301,44 +316,32 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
} }
// Paired devices category // Paired devices category
if (mPairedDevicesCategory == null) {
mPairedDevicesCategory = new PreferenceCategory(getPrefContext());
} else {
mPairedDevicesCategory.removeAll();
}
addDeviceCategory(mPairedDevicesCategory, addDeviceCategory(mPairedDevicesCategory,
R.string.bluetooth_preference_paired_devices, R.string.bluetooth_preference_paired_devices,
BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true); BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount(); int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount();
if (isUiRestricted() || numberOfPairedDevices <= 0) { if (isUiRestricted() || numberOfPairedDevices <= 0) {
preferenceScreen.removePreference(mPairedDevicesCategory); if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) != null) {
preferenceScreen.removePreference(mPairedDevicesCategory);
}
} else {
if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) == null) {
preferenceScreen.addPreference(mPairedDevicesCategory);
}
} }
// Available devices category // Available devices category
if (mAvailableDevicesCategory == null) {
mAvailableDevicesCategory = new BluetoothProgressCategory(getActivity());
mAvailableDevicesCategory.setSelectable(false);
} else {
mAvailableDevicesCategory.removeAll();
}
addDeviceCategory(mAvailableDevicesCategory, addDeviceCategory(mAvailableDevicesCategory,
R.string.bluetooth_preference_found_devices, R.string.bluetooth_preference_found_devices,
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted); BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
int numberOfAvailableDevices = mAvailableDevicesCategory.getPreferenceCount();
if (!mInitialScanStarted) { if (!mInitialScanStarted) {
startScanning(); startScanning();
} }
if (mMyDevicePreference == null) {
mMyDevicePreference = new Preference(getPrefContext());
}
mMyDevicePreference.setSummary(getResources().getString( mMyDevicePreference.setSummary(getResources().getString(
R.string.bluetooth_is_visible_message, mLocalAdapter.getName())); R.string.bluetooth_is_visible_message, mLocalAdapter.getName()));
mMyDevicePreference.setSelectable(false);
preferenceScreen.addPreference(mMyDevicePreference);
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();

View File

@@ -176,11 +176,19 @@ public abstract class DeviceListPreferenceFragment extends
return; return;
} }
BluetoothDevicePreference preference = new BluetoothDevicePreference( String key = cachedDevice.getDevice().getAddress();
getPrefContext(), cachedDevice); BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);
if (preference == null) {
preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
mDeviceListGroup.addPreference(preference);
} else {
// Tell the preference it is being re-used in case there is new info in the
// cached device.
preference.rebind();
}
initDevicePreference(preference); initDevicePreference(preference);
mDeviceListGroup.addPreference(preference);
mDevicePreferenceMap.put(cachedDevice, preference); mDevicePreferenceMap.put(cachedDevice, preference);
} }

View File

@@ -649,19 +649,22 @@ public class WifiSettings extends RestrictedSettingsFragment
boolean hasAvailableAccessPoints = false; boolean hasAvailableAccessPoints = false;
int index = 0; int index = 0;
cacheRemoveAllPrefs(getPreferenceScreen());
for (AccessPoint accessPoint : accessPoints) { for (AccessPoint accessPoint : accessPoints) {
// Ignore access points that are out of range. // Ignore access points that are out of range.
if (accessPoint.getLevel() != -1) { if (accessPoint.getLevel() != -1) {
String key = accessPoint.getBssid();
hasAvailableAccessPoints = true; hasAvailableAccessPoints = true;
if (accessPoint.getTag() != null) { LongPressAccessPointPreference pref = (LongPressAccessPointPreference)
final Preference pref = (Preference) accessPoint.getTag(); getCachedPreference(key);
if (pref != null) {
pref.setOrder(index++); pref.setOrder(index++);
getPreferenceScreen().addPreference(pref);
continue; continue;
} }
LongPressAccessPointPreference LongPressAccessPointPreference
preference = new LongPressAccessPointPreference(accessPoint, preference = new LongPressAccessPointPreference(accessPoint,
getPrefContext(), mUserBadgeCache, false, this); getPrefContext(), mUserBadgeCache, false, this);
preference.setKey(key);
preference.setOrder(index++); preference.setOrder(index++);
if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr()) if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr())
@@ -674,6 +677,7 @@ public class WifiSettings extends RestrictedSettingsFragment
accessPoint.setListener(this); accessPoint.setListener(this);
} }
} }
removeCachedPrefs(getPreferenceScreen());
if (!hasAvailableAccessPoints) { if (!hasAvailableAccessPoints) {
setProgressBarVisible(true); setProgressBarVisible(true);
Preference pref = new Preference(getContext()) { Preference pref = new Preference(getContext()) {