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

Commit 32bc813f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add new preference class MasterSwitchPreference."

parents 1c75f44e 1432cb85
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -3324,17 +3324,6 @@
                       android:value="com.android.settings.applications.ProcessStatsSummary" />
        </activity-alias>

        <activity-alias android:name="BluetoothDashboardAlias"
                        android:targetActivity="Settings$BluetoothSettingsActivity">
            <intent-filter android:priority="7">
                <action android:name="com.android.settings.action.SETTINGS"/>
            </intent-filter>
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                       android:value="com.android.settings.bluetooth.BluetoothSettings" />
            <meta-data android:name="com.android.settings.category"
                       android:value="com.android.settings.category.ia.device" />
        </activity-alias>

        <activity-alias android:name="CastDashboardAlias"
                        android:targetActivity="Settings$WifiDisplaySettingsActivity">
            <intent-filter android:priority="6">
+42 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  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.
  -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="match_parent">

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:gravity="start|center_vertical"
      android:orientation="horizontal"
      android:paddingStart="20dp"
      android:paddingEnd="20dp"
      android:paddingTop="16dp"
      android:paddingBottom="16dp">
        <View
          android:layout_width="1dip"
          android:layout_height="match_parent"
          android:background="?android:attr/colorSecondary"/>
    </LinearLayout>

    <Switch android:id="@+id/switchWidget"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:gravity="center_vertical" />

</LinearLayout>
+7 −0
Original line number Diff line number Diff line
@@ -18,6 +18,13 @@
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/connected_devices_dashboard_title">

    <com.android.settings.widget.MasterSwitchPreference
      android:fragment="com.android.settings.bluetooth.BluetoothSettings"
      android:key="toggle_bluetooth"
      android:title="@string/bluetooth_settings_title"
      android:icon="@drawable/ic_settings_bluetooth"
      android:order="-7"/>

    <SwitchPreference
        android:key="toggle_nfc"
        android:title="@string/nfc_quick_toggle_title"
+35 −35
Original line number Diff line number Diff line
@@ -24,14 +24,13 @@ import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.widget.Switch;
import android.widget.Toast;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.WirelessUtils;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -41,9 +40,8 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
 * preference. It turns on/off Bluetooth and ensures the summary of the
 * preference reflects the current state.
 */
public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener {
    private final Switch mSwitch;
    private final SwitchBar mSwitchBar;
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
    private final SwitchWidgetController mSwitchWidget;
    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private Context mContext;
    private boolean mValidListener;
@@ -76,19 +74,18 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
        }
    };

    public BluetoothEnabler(Context context, SwitchBar switchBar,
            MetricsFeatureProvider metricsFeatureProvider) {
    public BluetoothEnabler(Context context, SwitchWidgetController switchWidget,
            MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager) {
        mContext = context;
        mMetricsFeatureProvider = metricsFeatureProvider;
        mSwitchBar = switchBar;
        mSwitch = switchBar.getSwitch();
        mSwitchWidget = switchWidget;
        mSwitchWidget.setListener(this);
        mValidListener = false;

        LocalBluetoothManager manager = Utils.getLocalBtManager(context);
        if (manager == null) {
            // Bluetooth is not supported
            mLocalAdapter = null;
            mSwitch.setEnabled(false);
            mSwitchWidget.setEnabled(false);
        } else {
            mLocalAdapter = manager.getBluetoothAdapter();
        }
@@ -96,16 +93,16 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
    }

    public void setupSwitchBar() {
        mSwitchBar.show();
        mSwitchWidget.setupView();
    }

    public void teardownSwitchBar() {
        mSwitchBar.hide();
        mSwitchWidget.teardownView();
    }

    public void resume(Context context) {
        if (mLocalAdapter == null) {
            mSwitch.setEnabled(false);
            mSwitchWidget.setEnabled(false);
            return;
        }

@@ -116,7 +113,7 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
        // Bluetooth state is not sticky, so set it manually
        handleStateChanged(mLocalAdapter.getBluetoothState());

        mSwitchBar.addOnSwitchChangeListener(this);
        mSwitchWidget.startListening();
        mContext.registerReceiver(mReceiver, mIntentFilter);
        mValidListener = true;
    }
@@ -125,47 +122,48 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
        if (mLocalAdapter == null) {
            return;
        }

        mSwitchBar.removeOnSwitchChangeListener(this);
        if (mValidListener) {
            mSwitchWidget.stopListening();
            mContext.unregisterReceiver(mReceiver);
            mValidListener = false;
        }
    }

    void handleStateChanged(int state) {
        switch (state) {
            case BluetoothAdapter.STATE_TURNING_ON:
                mSwitch.setEnabled(false);
                mSwitchWidget.setEnabled(false);
                break;
            case BluetoothAdapter.STATE_ON:
                setChecked(true);
                mSwitch.setEnabled(true);
                mSwitchWidget.setEnabled(true);
                updateSearchIndex(true);
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                mSwitch.setEnabled(false);
                mSwitchWidget.setEnabled(false);
                break;
            case BluetoothAdapter.STATE_OFF:
                setChecked(false);
                mSwitch.setEnabled(true);
                mSwitchWidget.setEnabled(true);
                updateSearchIndex(false);
                break;
            default:
                setChecked(false);
                mSwitch.setEnabled(true);
                mSwitchWidget.setEnabled(true);
                updateSearchIndex(false);
        }
    }

    private void setChecked(boolean isChecked) {
        if (isChecked != mSwitch.isChecked()) {
        if (isChecked != mSwitchWidget.isChecked()) {
            // set listener to null, so onCheckedChanged won't be called
            // if the checked status on Switch isn't changed by user click
            if (mValidListener) {
                mSwitchBar.removeOnSwitchChangeListener(this);
                mSwitchWidget.stopListening();
            }
            mSwitch.setChecked(isChecked);
            mSwitchWidget.setChecked(isChecked);
            if (mValidListener) {
                mSwitchBar.addOnSwitchChangeListener(this);
                mSwitchWidget.startListening();
            }
        }
    }
@@ -180,13 +178,14 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
    }

    @Override
    public void onSwitchChanged(Switch switchView, boolean isChecked) {
    public boolean onSwitchToggled(boolean isChecked) {
        // Show toast message if Bluetooth is not allowed in airplane mode
        if (isChecked &&
                !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
            // Reset switch to off
            switchView.setChecked(false);
            mSwitchWidget.setChecked(false);
            return false;
        }

        mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_BLUETOOTH_TOGGLE, isChecked);
@@ -197,12 +196,13 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
            // a) The switch should be OFF but it should still be togglable (enabled = True)
            // b) The switch bar should have OFF text.
            if (isChecked && !status) {
                switchView.setChecked(false);
                mSwitch.setEnabled(true);
                mSwitchBar.setTextViewLabel(false);
                return;
                mSwitchWidget.setChecked(false);
                mSwitchWidget.setEnabled(true);
                mSwitchWidget.updateTitle(false);
                return false;
            }
        }
        mSwitch.setEnabled(false);
        mSwitchWidget.setEnabled(false);
        return true;
    }
}
+100 −0
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.content.Context;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.widget.MasterSwitchController;
import com.android.settingslib.bluetooth.LocalBluetoothManager;

public class BluetoothMasterSwitchPreferenceController extends PreferenceController
        implements BluetoothSummaryHelper.OnSummaryChangeListener,
        LifecycleObserver, OnResume, OnPause, OnStart, OnStop {

    private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";

    private LocalBluetoothManager mBluetoothManager;
    private MasterSwitchPreference mBtPreference;
    private BluetoothEnabler mBluetoothEnabler;
    private BluetoothSummaryHelper mSummaryHelper;

    public BluetoothMasterSwitchPreferenceController(Context context,
            LocalBluetoothManager bluetoothManager) {
        super(context);
        mBluetoothManager = bluetoothManager;
        mSummaryHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
        mSummaryHelper.setOnSummaryChangeListener(this);
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mBtPreference = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH);
        mBluetoothEnabler = new BluetoothEnabler(mContext,
            new MasterSwitchController(mBtPreference),
            FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager);
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return KEY_TOGGLE_BLUETOOTH;
    }

    public void onResume() {
        mSummaryHelper.setListening(true);
    }

    @Override
    public void onPause() {
        mSummaryHelper.setListening(false);
    }

    @Override
    public void onStart() {
        if (mBluetoothEnabler != null) {
            mBluetoothEnabler.resume(mContext);
        }
    }

    @Override
    public void onStop() {
        if (mBluetoothEnabler != null) {
            mBluetoothEnabler.pause();
        }
    }

    @Override
    public void onSummaryChanged(String summary) {
        if (mBtPreference != null) {
            mBtPreference.setSummary(summary);
        }
    }

}
Loading