Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07fffc38 authored by jackqdyulei's avatar jackqdyulei
Browse files

Remove obsolete bluetooth pages

1. Remove BluetoothSettingsObsolete
2. Remove DeviceListPreferenceObsoleteFragment
3. Update master swtich, now constantly point to new page
4. Update the tests

Future cl will remove the page switch API in BluetoothFeatureProvider

Bug: 63444548
Test: Test still pass
Change-Id: I24fb5cd03cf30044edb7201426e11e0a818f0a7f
Merged-In: I24fb5cd03cf30044edb7201426e11e0a818f0a7f
parent 8dc795d6
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -85,11 +85,8 @@ public class BluetoothMasterSwitchPreferenceController extends PreferenceControl
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (KEY_TOGGLE_BLUETOOTH.equals(preference.getKey())) {
            final String fragmentClass = mBluetoothFeatureProvider.isPairingPageEnabled() ?
                    BluetoothSettings.class.getName() :
                    BluetoothSettingsObsolete.class.getName();
            mActivity.startPreferencePanelAsUser(mFragment, fragmentClass, null, R.string.bluetooth,
                    null, new UserHandle(UserHandle.myUserId()));
            mActivity.startPreferencePanelAsUser(mFragment, BluetoothSettings.class.getName(), null,
                    R.string.bluetooth, null, new UserHandle(UserHandle.myUserId()));
            return true;
        }
        return super.handlePreferenceTreeClick(preference);
+0 −625

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −232
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
import android.util.Log;

import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;

import java.util.Collection;
import java.util.WeakHashMap;

/**
 * Parent class for settings fragments that contain a list of Bluetooth
 * devices.
 *
 * This fragment stores old implementation of {@link DeviceListPreferenceFragment} and is
 * deprecated, please use {@link DeviceListPreferenceFragment} instead.
 *
 * @see BluetoothSettingsObsolete
 * @see DevicePickerFragment
 */
@Deprecated
public abstract class DeviceListPreferenceObsoleteFragment extends
        RestrictedDashboardFragment implements BluetoothCallback {

    private static final String TAG = "DeviceListPreferenceFragment";

    private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
    private static final String KEY_BT_SCAN = "bt_scan";

    private BluetoothDeviceFilter.Filter mFilter;

    BluetoothDevice mSelectedDevice;

    LocalBluetoothAdapter mLocalAdapter;
    LocalBluetoothManager mLocalManager;

    private PreferenceGroup mDeviceListGroup;

    final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
            new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();

    DeviceListPreferenceObsoleteFragment(String restrictedKey) {
        super(restrictedKey);
        mFilter = BluetoothDeviceFilter.ALL_FILTER;
    }

    final void setFilter(BluetoothDeviceFilter.Filter filter) {
        mFilter = filter;
    }

    final void setFilter(int filterType) {
        mFilter = BluetoothDeviceFilter.getFilter(filterType);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocalManager = Utils.getLocalBtManager(getActivity());
        if (mLocalManager == null) {
            Log.e(TAG, "Bluetooth is not supported on this device");
            return;
        }
        mLocalAdapter = mLocalManager.getBluetoothAdapter();

        addPreferencesForActivity();

        mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
    }

    void setDeviceListGroup(PreferenceGroup preferenceGroup) {
        mDeviceListGroup = preferenceGroup;
    }

    /** Add preferences from the subclass. */
    abstract void addPreferencesForActivity();

    @Override
    public void onStart() {
        super.onStart();
        if (mLocalManager == null || isUiRestricted()) return;

        mLocalManager.setForegroundActivity(getActivity());
        mLocalManager.getEventManager().registerCallback(this);

        updateProgressUi(mLocalAdapter.isDiscovering());
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mLocalManager == null || isUiRestricted()) {
            return;
        }

        removeAllDevices();
        mLocalManager.setForegroundActivity(null);
        mLocalManager.getEventManager().unregisterCallback(this);
    }

    void removeAllDevices() {
        mLocalAdapter.stopScanning();
        mDevicePreferenceMap.clear();
        mDeviceListGroup.removeAll();
    }

    void addCachedDevices() {
        Collection<CachedBluetoothDevice> cachedDevices =
                mLocalManager.getCachedDeviceManager().getCachedDevicesCopy();
        for (CachedBluetoothDevice cachedDevice : cachedDevices) {
            onDeviceAdded(cachedDevice);
        }
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (KEY_BT_SCAN.equals(preference.getKey())) {
            mLocalAdapter.startScanning(true);
            return true;
        }

        if (preference instanceof BluetoothDevicePreference) {
            BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;
            CachedBluetoothDevice device = btPreference.getCachedDevice();
            mSelectedDevice = device.getDevice();
            onDevicePreferenceClick(btPreference);
            return true;
        }

        return super.onPreferenceTreeClick(preference);
    }

    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
        btPreference.onClicked();
    }

    public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
        if (mDevicePreferenceMap.get(cachedDevice) != null) {
            return;
        }

        // Prevent updates while the list shows one of the state messages
        if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return;

        if (mFilter.matches(cachedDevice.getDevice())) {
            createDevicePreference(cachedDevice);
        }
    }

    void createDevicePreference(CachedBluetoothDevice cachedDevice) {
        if (mDeviceListGroup == null) {
            Log.w(TAG, "Trying to create a device preference before the list group/category "
                    + "exists!");
            return;
        }

        String key = cachedDevice.getDevice().getAddress();
        BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);

        if (preference == null) {
            preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
            preference.setKey(key);
            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);
        mDevicePreferenceMap.put(cachedDevice, preference);
    }

    /**
     * Overridden in {@link BluetoothSettings} to add a listener.
     * @param preference the newly added preference
     */
    void initDevicePreference(BluetoothDevicePreference preference) {
        // Does nothing by default
    }

    public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
        BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
        if (preference != null) {
            mDeviceListGroup.removePreference(preference);
        }
    }

    public void onScanningStateChanged(boolean started) {
        updateProgressUi(started);
    }

    private void updateProgressUi(boolean start) {
        if (mDeviceListGroup instanceof BluetoothProgressCategory) {
            ((BluetoothProgressCategory) mDeviceListGroup).setProgress(start);
        }
    }

    public void onBluetoothStateChanged(int bluetoothState) {
        if (bluetoothState == BluetoothAdapter.STATE_OFF) {
            updateProgressUi(false);
        }
    }

    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { }
}
+0 −2
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ import com.android.settings.applications.assist.ManageAssist;
import com.android.settings.backup.BackupSettingsActivity;
import com.android.settings.backup.BackupSettingsFragment;
import com.android.settings.bluetooth.BluetoothSettings;
import com.android.settings.bluetooth.BluetoothSettingsObsolete;
import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment;
import com.android.settings.support.SupportDashboardActivity;
import com.android.settings.datausage.DataUsageMeteredSettings;
@@ -133,7 +132,6 @@ public final class SearchIndexableResources {
        addIndex(SavedAccessPointsWifiSettings.class, NO_DATA_RES_ID,
                R.drawable.ic_settings_wireless);
        addIndex(BluetoothSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_bluetooth);
        addIndex(BluetoothSettingsObsolete.class, NO_DATA_RES_ID, R.drawable.ic_settings_bluetooth);
        addIndex(SimSettings.class, NO_DATA_RES_ID, R.drawable.ic_sim_sd);
        addIndex(DataUsageSummary.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
        addIndex(DataUsageMeteredSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
+0 −19
Original line number Diff line number Diff line
@@ -127,23 +127,4 @@ public class BluetoothMasterSwitchPreferenceControllerTest {

        verify(mPreference).setSummary("test summary");
    }

    @Test
    public void testHandlePreferenceTreeClick_pairPageEnabled_showNewPage() {
        when(mFeatureFactory.bluetoothFeatureProvider.isPairingPageEnabled()).thenReturn(true);

        mController.handlePreferenceTreeClick(mPreference);

        verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
                eq(BluetoothSettings.class.getName()), any(), eq(R.string.bluetooth), any(), any());
    }

    @Test
    public void testHandlePreferenceTreeClick_pairPageDisabled_showOldPage() {
        mController.handlePreferenceTreeClick(mPreference);

        verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
                eq(BluetoothSettingsObsolete.class.getName()), any(), eq(R.string.bluetooth), any(),
                any());
    }
}
Loading