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

Commit 6829dae0 authored by Flemmard's avatar Flemmard Committed by Steve Kondik
Browse files

Added Wifi/Bluetooth toggle on SystemUI

This patch regroups my two precedent that doesn't merge.
It creates the Wifi toggle
And the Bluetooth item with toggle on Statusbar settings.

Change-Id: If7f558225e67f760bfe979d6e61ed55cdecefc67
parent ec4c8f97
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -68,6 +68,38 @@
                style="@style/StatusBarPanelSettingsContents"
                android:text="@string/status_bar_settings_wifi_button"
                />
        <Switch
                android:id="@+id/wifi_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="5dp"
                />
    </LinearLayout>
    <View style="@style/StatusBarPanelSettingsPanelSeparator" />

    <!-- Bluetooth -->
    <LinearLayout
            android:id="@+id/bluetooth"
            style="@style/StatusBarPanelSettingsRow"
            >
        <ImageView
                android:id="@+id/bluetooth_icon"
                style="@style/StatusBarPanelSettingsIcon"
                android:src="@drawable/stat_sys_data_bluetooth"
                />
        <TextView
                android:id="@+id/bluetooth_label"
                style="@style/StatusBarPanelSettingsContents"
                android:text="@string/status_bar_settings_bluetooth_button"
                />
        <Switch
                android:id="@+id/bluetooth_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="5dp"
                />
    </LinearLayout>
    <View style="@style/StatusBarPanelSettingsPanelSeparator" />

+3 −0
Original line number Diff line number Diff line
@@ -96,6 +96,9 @@
    <!-- Name of the button that links to the Wifi settings screen. [CHAR LIMIT=NONE] -->
    <string name="status_bar_settings_wifi_button">Wi-Fi</string>

    <!-- Name of the button that links to the Bluetooth settings screen. [CHAR LIMIT=NONE] -->
    <string name="status_bar_settings_bluetooth_button">Bluetooth</string>

    <!-- Label in the system panel for airplane mode (all radios are turned off)[CHAR LIMIT=30] -->
    <string name="status_bar_settings_airplane">Airplane mode</string>

+82 −14
Original line number Diff line number Diff line
@@ -25,31 +25,53 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.CompoundButton;

import com.android.systemui.R;

public class BluetoothController extends BroadcastReceiver {
public class BluetoothController extends BroadcastReceiver 
        implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "StatusBar.BluetoothController";

    private final BluetoothAdapter mAdapter;
    private Context mContext;
    private ArrayList<ImageView> mIconViews = new ArrayList<ImageView>();

    private CompoundButton mCheckBox;
    private int mIconId = R.drawable.stat_sys_data_bluetooth;
    private int mContentDescriptionId = 0;
    private int mState = BluetoothAdapter.ERROR;
    private boolean mEnabled = false;

    public BluetoothController(Context context) {
        mContext = context;
	mAdapter =  BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        context.registerReceiver(this, filter);

	if (mAdapter != null) {
            handleAdapterStateChange(mAdapter.getState());
            handleConnectionStateChange(mAdapter.getConnectionState());
        }
        refreshViews();
    }

    public BluetoothController(Context context, CompoundButton checkbox) {
	mContext = context;
	mCheckBox = checkbox;
	mCheckBox.setChecked(mEnabled);
	mCheckBox.setOnCheckedChangeListener(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        context.registerReceiver(this, filter);

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            handleAdapterStateChange(adapter.getState());
            handleConnectionStateChange(adapter.getConnectionState());
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mAdapter != null) {
            handleAdapterStateChange(mAdapter.getState());
            handleConnectionStateChange(mAdapter.getConnectionState());
        }
        refreshViews();
    }
@@ -61,7 +83,6 @@ public class BluetoothController extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

	if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
	    handleAdapterStateChange(
				     intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
@@ -73,8 +94,55 @@ public class BluetoothController extends BroadcastReceiver {
        refreshViews();
    }

    public void onCheckedChanged(CompoundButton view, boolean checked) {
        if (checked != mEnabled) {
            mEnabled = checked;
	    setBluetoothEnabled(mEnabled);
	    setBluetoothStateInt(mAdapter.getState());
	    syncBluetoothState();
        }
    }

    public void setBluetoothEnabled(boolean enabled) {
        boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();

        if (success) {
            setBluetoothStateInt(enabled
				 ? BluetoothAdapter.STATE_TURNING_ON
                : BluetoothAdapter.STATE_TURNING_OFF);
        } else {
	    syncBluetoothState();
        }
    }

    boolean syncBluetoothState() {
        int currentState = mAdapter.getState();
        if (currentState != mState) {
            setBluetoothStateInt(mState);
            return true;
        }
	return false;
}

    synchronized void setBluetoothStateInt(int state) {
        mState = state;
	if (state == BluetoothAdapter.STATE_ON)
	    {
		if (mCheckBox != null)
		    mCheckBox.setChecked(true);
	    }
	else
	    if (mCheckBox != null)
		mCheckBox.setChecked(false);
    }


    public void handleAdapterStateChange(int adapterState) {
        mEnabled = (adapterState == BluetoothAdapter.STATE_ON);
	if (mCheckBox != null)
	    mCheckBox.setChecked(mEnabled);
    }

    public void handleConnectionStateChange(int connectionState) {
+160 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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.systemui.statusbar.policy;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Slog;
import android.widget.CompoundButton;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.util.concurrent.atomic.AtomicBoolean;

public class WifiController extends BroadcastReceiver
        implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "StatusBar.WifiController";

    private Context mContext;
    private CompoundButton mCheckBox;
    private boolean mStateMachineEvent;
    private final WifiManager mWifiManager;
    private AtomicBoolean mConnected = new AtomicBoolean(false);

    private boolean mWifi;

    public WifiController(Context context, CompoundButton checkbox) {
        mContext = context;
        mWifi = getWifi();
        mCheckBox = checkbox;
        checkbox.setChecked(mWifi);
        checkbox.setOnCheckedChangeListener(this);
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        context.registerReceiver(this, filter);

    }

    public void release() {
        mContext.unregisterReceiver(this);
    }

    public void onCheckedChanged(CompoundButton view, boolean checked) {
        //Do nothing if called as a result of a state machine event
        if (mStateMachineEvent) {
            return;
        }
        // Show toast message if Wi-Fi is not allowed in airplane mode

        // Disable tethering if enabling Wifi
        int wifiApState = mWifiManager.getWifiApState();
        if (mCheckBox.isChecked() && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
                (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
            mWifiManager.setWifiApEnabled(null, false);
        }

        if (mWifiManager.setWifiEnabled(mCheckBox.isChecked())) {
            // Intent has been taken into account, disable until new state is active
            mCheckBox.setChecked(false);
        }
    }

    public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                handleWifiStateChanged(intent.getIntExtra(
                        WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
            } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
                if (!mConnected.get()) {
                    handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
                            intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
                }
            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
                NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
                        WifiManager.EXTRA_NETWORK_INFO);
                mConnected.set(info.isConnected());
                handleStateChanged(info.getDetailedState());
            }
        }
    private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) {
    }

    private void handleWifiStateChanged(int state) {
        switch (state) {
            case WifiManager.WIFI_STATE_ENABLING:
                mCheckBox.setEnabled(false);
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                setSwitchChecked(true);
                mCheckBox.setEnabled(true);
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                mCheckBox.setEnabled(false);
                break;
            case WifiManager.WIFI_STATE_DISABLED:
                setSwitchChecked(false);
                mCheckBox.setEnabled(true);
                break;
            default:
                setSwitchChecked(false);
                mCheckBox.setEnabled(true);
                break;
        }
    }

    private void setSwitchChecked(boolean checked) {
        if (checked != mCheckBox.isChecked()) {
            mStateMachineEvent = true;
            mCheckBox.setChecked(checked);
            mStateMachineEvent = false;
        }
    }

    private boolean getWifi() {
        ContentResolver cr = mContext.getContentResolver();
        return 0 != Settings.System.getInt(cr, Settings.System.AIRPLANE_MODE_ON, 0);
    }

    // TODO: Fix this racy API by adding something better to TelephonyManager or
    // ConnectivityService.
    private void unsafe(final boolean enabled) {
        AsyncTask.execute(new Runnable() {
                public void run() {
                    Settings.System.putInt(
                            mContext.getContentResolver(),
                            Settings.System.AIRPLANE_MODE_ON,
                            enabled ? 1 : 0);
                    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
                    intent.putExtra("state", enabled);
                    mContext.sendBroadcast(intent);
                }
            });
    }
}
+21 −0
Original line number Diff line number Diff line
@@ -32,9 +32,12 @@ import com.android.systemui.R;
import com.android.systemui.statusbar.policy.AirplaneModeController;
import com.android.systemui.statusbar.policy.AutoRotateController;
import com.android.systemui.statusbar.policy.BrightnessController;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.DoNotDisturbController;
import com.android.systemui.statusbar.policy.ToggleSlider;
import com.android.systemui.statusbar.policy.VolumeController;
import com.android.systemui.statusbar.policy.WifiController;


public class SettingsView extends LinearLayout implements View.OnClickListener {
    static final String TAG = "SettingsView";
@@ -43,6 +46,8 @@ public class SettingsView extends LinearLayout implements View.OnClickListener {
    AutoRotateController mRotate;
    BrightnessController mBrightness;
    DoNotDisturbController mDoNotDisturb;
    BluetoothController mBluetooth;
    WifiController mWifi;

    public SettingsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
@@ -61,6 +66,11 @@ public class SettingsView extends LinearLayout implements View.OnClickListener {
        mAirplane = new AirplaneModeController(context,
                (CompoundButton)findViewById(R.id.airplane_checkbox));
        findViewById(R.id.network).setOnClickListener(this);
        findViewById(R.id.bluetooth).setOnClickListener(this);
        mBluetooth = new BluetoothController(context,
					     (CompoundButton)findViewById(R.id.bluetooth_checkbox));
        mWifi = new WifiController(context,
				(CompoundButton)findViewById(R.id.wifi_checkbox));
        mRotate = new AutoRotateController(context,
                (CompoundButton)findViewById(R.id.rotate_checkbox));
        mBrightness = new BrightnessController(context,
@@ -82,6 +92,9 @@ public class SettingsView extends LinearLayout implements View.OnClickListener {
            case R.id.network:
                onClickNetwork();
                break;
            case R.id.bluetooth:
                onClickBluetooth();
                break;
            case R.id.settings:
                onClickSettings();
                break;
@@ -100,6 +113,14 @@ public class SettingsView extends LinearLayout implements View.OnClickListener {
        getStatusBarManager().collapse();
    }

// Bluetooth
    // ----------------------------
    private void onClickBluetooth() {
        getContext().startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        getStatusBarManager().collapse();
    }

    // Settings
    // ----------------------------
    private void onClickSettings() {