summaryrefslogtreecommitdiffstats
path: root/src/com
diff options
context:
space:
mode:
authorRoman Birg <roman@cyngn.com>2015-01-05 13:32:20 -0800
committerDanesh M <daneshm90@gmail.com>2015-12-15 18:31:05 -0800
commitc5cd017e7e168c5b69072dacda74aa2f298c7ab0 (patch)
treec061f473d41602c0ce7058c7507d62d89ee796e6 /src/com
parente1f1ba9036f23f439dbeab6b48a16bcf97db3b93 (diff)
downloadpackages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.gz
packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.bz2
packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.zip
Settings: add switches for dashboard items
Change-Id: Ibff81510270745807a4b133457d60d47dd629df6 Signed-off-by: Roman Birg <roman@cyngn.com>
Diffstat (limited to 'src/com')
-rw-r--r--src/com/android/settings/SettingsActivity.java5
-rw-r--r--src/com/android/settings/WirelessSettings.java1
-rw-r--r--src/com/android/settings/bluetooth/BluetoothEnabler.java81
-rw-r--r--src/com/android/settings/dashboard/DashboardSummary.java13
-rw-r--r--src/com/android/settings/dashboard/DashboardTile.java16
-rw-r--r--src/com/android/settings/dashboard/DashboardTileView.java46
-rw-r--r--src/com/android/settings/dashboard/GenericSwitchToggle.java92
-rw-r--r--src/com/android/settings/dashboard/MobileNetworksEnabler.java159
-rw-r--r--src/com/android/settings/wifi/WifiEnabler.java92
9 files changed, 411 insertions, 94 deletions
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 658da1dd5..06137086e 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -1181,6 +1181,11 @@ public class SettingsActivity extends Activity
com.android.internal.R.styleable.PreferenceHeader_fragment);
sa.recycle();
+ sa = context.obtainStyledAttributes(attrs, R.styleable.DashboardTile);
+ tile.switchControl = sa.getString(
+ R.styleable.DashboardTile_switchClass);
+ sa.recycle();
+
if (curBundle == null) {
curBundle = new Bundle();
}
diff --git a/src/com/android/settings/WirelessSettings.java b/src/com/android/settings/WirelessSettings.java
index 75d115b5d..8ef592ead 100644
--- a/src/com/android/settings/WirelessSettings.java
+++ b/src/com/android/settings/WirelessSettings.java
@@ -533,4 +533,5 @@ public class WirelessSettings extends SettingsPreferenceFragment implements Inde
return result;
}
};
+
}
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java
index 4f442ceb5..7d6bebecc 100644
--- a/src/com/android/settings/bluetooth/BluetoothEnabler.java
+++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java
@@ -24,11 +24,13 @@ import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
+import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
+import com.android.settings.dashboard.GenericSwitchToggle;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.WirelessUtils;
@@ -40,13 +42,9 @@ 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 Context mContext;
- private Switch mSwitch;
- private SwitchBar mSwitchBar;
- private boolean mValidListener;
- private final LocalBluetoothAdapter mLocalAdapter;
- private final IntentFilter mIntentFilter;
+public final class BluetoothEnabler extends GenericSwitchToggle {
+ private LocalBluetoothAdapter mLocalAdapter;
+ private IntentFilter mIntentFilter;
private static final String EVENT_DATA_IS_BT_ON = "is_bluetooth_on";
private static final int EVENT_UPDATE_INDEX = 0;
@@ -75,16 +73,23 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
};
public BluetoothEnabler(Context context, SwitchBar switchBar) {
- mContext = context;
- mSwitchBar = switchBar;
- mSwitch = switchBar.getSwitch();
- mValidListener = false;
+ super(context, switchBar);
- LocalBluetoothManager manager = Utils.getLocalBtManager(context);
+ init();
+ }
+
+ public BluetoothEnabler(Context context, Switch switch_) {
+ super(context, switch_);
+
+ init();
+ }
+
+ private void init() {
+ LocalBluetoothManager manager = Utils.getLocalBtManager(mContext);
if (manager == null) {
// Bluetooth is not supported
mLocalAdapter = null;
- mSwitch.setEnabled(false);
+ setEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
@@ -99,74 +104,56 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
mSwitchBar.hide();
}
+ @Override
public void resume(Context context) {
+ super.resume(context);
if (mLocalAdapter == null) {
- mSwitch.setEnabled(false);
+ setEnabled(false);
return;
}
- if (mContext != context) {
- mContext = context;
- }
-
// Bluetooth state is not sticky, so set it manually
handleStateChanged(mLocalAdapter.getBluetoothState());
- mSwitchBar.addOnSwitchChangeListener(this);
mContext.registerReceiver(mReceiver, mIntentFilter);
- mValidListener = true;
}
+ @Override
public void pause() {
+ super.pause();
if (mLocalAdapter == null) {
return;
}
- mSwitchBar.removeOnSwitchChangeListener(this);
mContext.unregisterReceiver(mReceiver);
- mValidListener = false;
}
void handleStateChanged(int state) {
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
- mSwitch.setEnabled(false);
+ setEnabled(false);
break;
case BluetoothAdapter.STATE_ON:
setChecked(true);
- mSwitch.setEnabled(true);
+ setEnabled(true);
updateSearchIndex(true);
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
- mSwitch.setEnabled(false);
+ setEnabled(false);
break;
case BluetoothAdapter.STATE_OFF:
setChecked(false);
- mSwitch.setEnabled(true);
+ setEnabled(true);
updateSearchIndex(false);
break;
default:
setChecked(false);
- mSwitch.setEnabled(true);
+ setEnabled(true);
updateSearchIndex(false);
}
}
- private void setChecked(boolean isChecked) {
- if (isChecked != mSwitch.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);
- }
- mSwitch.setChecked(isChecked);
- if (mValidListener) {
- mSwitchBar.addOnSwitchChangeListener(this);
- }
- }
- }
-
private void updateSearchIndex(boolean isBluetoothOn) {
mHandler.removeMessages(EVENT_UPDATE_INDEX);
@@ -178,12 +165,15 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
+ if (mStateMachineEvent) {
+ return;
+ }
// 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);
+ setChecked(false);
}
MetricsLogger.action(mContext, MetricsLogger.ACTION_BLUETOOTH_TOGGLE, isChecked);
@@ -191,6 +181,11 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
if (mLocalAdapter != null) {
mLocalAdapter.setBluetoothEnabled(isChecked);
}
- mSwitch.setEnabled(false);
+ setEnabled(false);
+ }
+
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ super.onCheckedChanged(buttonView, isChecked);
}
}
diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java
index 38ed29766..e01a8435d 100644
--- a/src/com/android/settings/dashboard/DashboardSummary.java
+++ b/src/com/android/settings/dashboard/DashboardSummary.java
@@ -35,6 +35,7 @@ import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
+import android.widget.Switch;
import android.widget.TextView;
import com.android.internal.logging.MetricsLogger;
@@ -42,6 +43,7 @@ import com.android.settings.HelpUtils;
import com.android.settings.InstrumentedFragment;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
+import com.android.settings.widget.SwitchBar;
import java.util.List;
@@ -158,7 +160,8 @@ public class DashboardSummary extends InstrumentedFragment {
DashboardTileView tileView = new DashboardTileView(context);
updateTileView(context, res, tile, tileView.getImageView(),
- tileView.getTitleTextView(), tileView.getStatusTextView());
+ tileView.getTitleTextView(), tileView.getStatusTextView(),
+ tileView.getSwitchView());
tileView.setTile(tile);
@@ -173,7 +176,7 @@ public class DashboardSummary extends InstrumentedFragment {
}
private void updateTileView(Context context, Resources res, DashboardTile tile,
- ImageView tileIcon, TextView tileTextView, TextView statusTextView) {
+ ImageView tileIcon, TextView tileTextView, TextView statusTextView, Switch switchBar) {
if (!TextUtils.isEmpty(tile.iconPkg)) {
try {
@@ -213,6 +216,12 @@ public class DashboardSummary extends InstrumentedFragment {
} else {
statusTextView.setVisibility(View.GONE);
}
+
+ if (tile.switchControl != null) {
+ switchBar.setVisibility(View.VISIBLE);
+ } else {
+ switchBar.setVisibility(View.GONE);
+ }
}
private void sendRebuildUI() {
diff --git a/src/com/android/settings/dashboard/DashboardTile.java b/src/com/android/settings/dashboard/DashboardTile.java
index 5e7e49a7c..ecb9a48f1 100644
--- a/src/com/android/settings/dashboard/DashboardTile.java
+++ b/src/com/android/settings/dashboard/DashboardTile.java
@@ -81,6 +81,13 @@ public class DashboardTile implements Parcelable {
public String iconPkg;
/**
+ * Optional location of a class which implements GenericSwitchTile
+ * to be displayed on the dashboard.
+ * @attr ref R.styleable#DashbaordTile_switchClass
+ */
+ public String switchControl;
+
+ /**
* Full class name of the fragment to display when this tile is
* selected.
* @attr ref android.R.styleable#PreferenceHeader_fragment
@@ -164,6 +171,12 @@ public class DashboardTile implements Parcelable {
userHandle.get(i).writeToParcel(dest, flags);
}
dest.writeBundle(extras);
+ if (switchControl != null) {
+ dest.writeInt(1);
+ dest.writeString(switchControl);
+ } else {
+ dest.writeInt(0);
+ }
}
public void readFromParcel(Parcel in) {
@@ -184,6 +197,9 @@ public class DashboardTile implements Parcelable {
userHandle.add(UserHandle.CREATOR.createFromParcel(in));
}
extras = in.readBundle();
+ if (in.readInt() != 0) {
+ switchControl = in.readString();
+ }
}
DashboardTile(Parcel in) {
diff --git a/src/com/android/settings/dashboard/DashboardTileView.java b/src/com/android/settings/dashboard/DashboardTileView.java
index 0896b8262..6790136fa 100644
--- a/src/com/android/settings/dashboard/DashboardTileView.java
+++ b/src/com/android/settings/dashboard/DashboardTileView.java
@@ -23,12 +23,16 @@ import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
+import android.widget.Switch;
import android.widget.TextView;
import com.android.settings.ProfileSelectDialog;
import com.android.settings.R;
import com.android.settings.Utils;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
public class DashboardTileView extends FrameLayout implements View.OnClickListener {
private static final int DEFAULT_COL_SPAN = 1;
@@ -37,6 +41,8 @@ public class DashboardTileView extends FrameLayout implements View.OnClickListen
private TextView mTitleTextView;
private TextView mStatusTextView;
private View mDivider;
+ private Switch mSwitch;
+ private GenericSwitchToggle mSwitchToggle;
private int mColSpan = DEFAULT_COL_SPAN;
@@ -55,6 +61,7 @@ public class DashboardTileView extends FrameLayout implements View.OnClickListen
mTitleTextView = (TextView) view.findViewById(R.id.title);
mStatusTextView = (TextView) view.findViewById(R.id.status);
mDivider = view.findViewById(R.id.tile_divider);
+ mSwitch = (Switch) view.findViewById(R.id.dashboard_switch);
setOnClickListener(this);
setBackgroundResource(R.drawable.dashboard_tile_background);
@@ -73,8 +80,41 @@ public class DashboardTileView extends FrameLayout implements View.OnClickListen
return mImageView;
}
+ @Override
+ protected void onAttachedToWindow() {
+ super.onAttachedToWindow();
+ if (mSwitchToggle != null) {
+ mSwitchToggle.resume(getContext());
+ }
+ }
+
+ @Override
+ protected void onDetachedFromWindow() {
+ super.onDetachedFromWindow();
+ if (mSwitchToggle != null) {
+ mSwitchToggle.pause();
+ }
+ }
+
public void setTile(DashboardTile tile) {
mTile = tile;
+
+ if (mTile.switchControl != null) {
+ try {
+ Class<?> clazz = getClass().getClassLoader().loadClass(mTile.switchControl);
+ Constructor<?> constructor = clazz.getConstructor(Context.class, Switch.class);
+ GenericSwitchToggle sw = (GenericSwitchToggle) constructor.newInstance(
+ getContext(), mSwitch);
+ mSwitchToggle = sw;
+ mSwitchToggle.resume(getContext());
+ } catch (ClassNotFoundException
+ | NoSuchMethodException
+ | InvocationTargetException
+ | InstantiationException
+ | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
}
public void setDividerVisibility(boolean visible) {
@@ -105,4 +145,10 @@ public class DashboardTileView extends FrameLayout implements View.OnClickListen
}
}
}
+
+ public Switch getSwitchView() {
+ return mSwitch;
+ }
+
+
}
diff --git a/src/com/android/settings/dashboard/GenericSwitchToggle.java b/src/com/android/settings/dashboard/GenericSwitchToggle.java
new file mode 100644
index 000000000..95eec5cf3
--- /dev/null
+++ b/src/com/android/settings/dashboard/GenericSwitchToggle.java
@@ -0,0 +1,92 @@
+package com.android.settings.dashboard;
+
+import android.content.Context;
+import android.widget.CompoundButton;
+import android.widget.Switch;
+import com.android.settings.widget.SwitchBar;
+
+public abstract class GenericSwitchToggle implements SwitchBar.OnSwitchChangeListener,
+ CompoundButton.OnCheckedChangeListener {
+
+ protected Context mContext;
+ protected Switch mSwitch;
+ protected SwitchBar mSwitchBar;
+
+ protected boolean mStateMachineEvent;
+ protected boolean mListeningToOnSwitchChange = false;
+
+ public GenericSwitchToggle(Context context, Switch switch_) {
+ mContext = context;
+ mSwitch = switch_;
+ }
+
+ public GenericSwitchToggle(Context context, SwitchBar switch_) {
+ mContext = context;
+ mSwitchBar = switch_;
+ }
+
+ public void pause() {
+ if (mListeningToOnSwitchChange) {
+ if (mSwitchBar != null) {
+ mSwitchBar.removeOnSwitchChangeListener(this);
+ }
+ if (mSwitch != null) {
+ mSwitch.setOnCheckedChangeListener(null);
+ }
+ mListeningToOnSwitchChange = false;
+ }
+ }
+
+ public void resume(Context context) {
+ mContext = context;
+
+ if (!mListeningToOnSwitchChange) {
+ if (mSwitchBar != null) {
+ mSwitchBar.addOnSwitchChangeListener(this);
+ mListeningToOnSwitchChange = true;
+ }
+ if (mSwitch != null) {
+ mSwitch.setOnCheckedChangeListener(this);
+ mListeningToOnSwitchChange = true;
+ }
+ }
+ }
+
+ public void teardownSwitchBar() {
+ if (mSwitchBar == null) {
+ return;
+ }
+ if (mListeningToOnSwitchChange) {
+ mSwitchBar.removeOnSwitchChangeListener(this);
+ mListeningToOnSwitchChange = false;
+ }
+ mSwitchBar.hide();
+ }
+
+ protected void setChecked(boolean checked) {
+ mStateMachineEvent = true;
+ if (mSwitchBar != null) {
+ mSwitchBar.setChecked(checked);
+ }
+ if (mSwitch != null) {
+ mSwitch.setChecked(checked);
+ }
+ mStateMachineEvent = false;
+ }
+
+ protected void setEnabled(boolean enabled) {
+ if (mSwitchBar != null) {
+ mSwitchBar.setEnabled(enabled);
+ }
+ if (mSwitch != null) {
+ mSwitch.setEnabled(enabled);
+ }
+ }
+
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ onSwitchChanged(mSwitch, isChecked);
+ }
+
+ public abstract void onSwitchChanged(Switch switchView, boolean isChecked);
+}
diff --git a/src/com/android/settings/dashboard/MobileNetworksEnabler.java b/src/com/android/settings/dashboard/MobileNetworksEnabler.java
new file mode 100644
index 000000000..d7c202a21
--- /dev/null
+++ b/src/com/android/settings/dashboard/MobileNetworksEnabler.java
@@ -0,0 +1,159 @@
+/*
+ * 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.settings.dashboard;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.net.wifi.SupplicantState;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
+import android.os.Handler;
+import android.os.Message;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+import android.widget.CompoundButton;
+import android.widget.Switch;
+import android.widget.Toast;
+
+import com.android.settings.AirplaneModeEnabler;
+import com.android.settings.R;
+import com.android.settings.WirelessSettings;
+import com.android.settings.search.Index;
+import com.android.settings.widget.SwitchBar;
+import com.android.settings.wifi.WifiSettings;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class MobileNetworksEnabler extends GenericSwitchToggle {
+ private TelephonyManager mTelephonyManager;
+ private IntentFilter mIntentFilter;
+
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ if (TelephonyManager.ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED.equals(action)) {
+ updateState();
+ }
+ }
+ };
+
+ public MobileNetworksEnabler(Context context, SwitchBar switchBar) {
+ super(context, switchBar);
+
+ init();
+ setupSwitches();
+ }
+
+ public MobileNetworksEnabler(Context context, Switch switch_) {
+ super(context, switch_);
+
+ init();
+ setupSwitches();
+ }
+
+ private void init() {
+ mTelephonyManager = (TelephonyManager)
+ mContext.getSystemService(Context.TELEPHONY_SERVICE);
+ mIntentFilter = new IntentFilter(
+ TelephonyManager.ACTION_PRECISE_DATA_CONNECTION_STATE_CHANGED);
+ }
+
+ private void setupSwitches() {
+ updateState();
+ if (mSwitchBar != null) {
+ mSwitchBar.show();
+ }
+ }
+
+ private void updateState() {
+ switch (mTelephonyManager.getDataState()) {
+ case TelephonyManager.DATA_CONNECTED:
+ case TelephonyManager.DATA_SUSPENDED:
+ setEnabled(true);
+ setChecked(true);
+ break;
+
+ case TelephonyManager.DATA_CONNECTING:
+ setChecked(true);
+ setEnabled(false);
+ break;
+
+ case TelephonyManager.DATA_DISCONNECTED:
+ setEnabled(true);
+ setChecked(false);
+ break;
+
+ default:
+ case TelephonyManager.DATA_UNKNOWN:
+ setEnabled(false);
+ setChecked(false);
+ break;
+ }
+ }
+
+ @Override
+ public void resume(Context context) {
+ super.resume(context);
+ mContext.registerReceiver(mReceiver, mIntentFilter);
+ }
+
+ @Override
+ public void pause() {
+ super.pause();
+ mContext.unregisterReceiver(mReceiver);
+ }
+
+ private boolean isAirplaneModeOn() {
+ return (Settings.Global.getInt(mContext.getContentResolver(),
+ Settings.Global.AIRPLANE_MODE_ON, 0) != 0);
+ }
+
+ public boolean isRadioAllowed(String type) {
+ if (!isAirplaneModeOn()) {
+ return true;
+ }
+ // Here we use the same logic in onCreate().
+ String toggleable = Settings.Global.getString(mContext.getContentResolver(),
+ Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
+ return toggleable != null && toggleable.contains(type);
+ }
+
+ @Override
+ public void onSwitchChanged(Switch switchView, boolean isChecked) {
+ //Do nothing if called as a result of a state machine event
+ if (mStateMachineEvent) {
+ return;
+ }
+ if (isChecked && !isRadioAllowed(Settings.Global.RADIO_CELL)) {
+ Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
+ setChecked(false);
+ return;
+ }
+
+ mTelephonyManager.setDataEnabled(isChecked);
+ }
+
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ super.onCheckedChanged(buttonView, isChecked);
+ }
+}
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index fe52cf241..108405a00 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -27,26 +27,25 @@ import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
+import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
+import com.android.settings.dashboard.GenericSwitchToggle;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.WirelessUtils;
import java.util.concurrent.atomic.AtomicBoolean;
-public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
- private Context mContext;
- private SwitchBar mSwitchBar;
- private boolean mListeningToOnSwitchChange = false;
+public class WifiEnabler extends GenericSwitchToggle {
private AtomicBoolean mConnected = new AtomicBoolean(false);
- private final WifiManager mWifiManager;
- private boolean mStateMachineEvent;
- private final IntentFilter mIntentFilter;
+ private WifiManager mWifiManager;
+ private IntentFilter mIntentFilter;
+
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -85,76 +84,71 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
};
public WifiEnabler(Context context, SwitchBar switchBar) {
- mContext = context;
- mSwitchBar = switchBar;
+ super(context, switchBar);
+
+ init();
+ setupSwitches();
+ }
- mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+ public WifiEnabler(Context context, Switch switch_) {
+ super(context, switch_);
+
+ init();
+ setupSwitches();
+ }
+
+ private void init() {
+ mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
// The order matters! We really should not depend on this. :(
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
-
- setupSwitchBar();
}
- public void setupSwitchBar() {
+ private void setupSwitches() {
final int state = mWifiManager.getWifiState();
handleWifiStateChanged(state);
- if (!mListeningToOnSwitchChange) {
- mSwitchBar.addOnSwitchChangeListener(this);
- mListeningToOnSwitchChange = true;
+ if (mSwitchBar != null) {
+ mSwitchBar.show();
}
- mSwitchBar.show();
}
- public void teardownSwitchBar() {
- if (mListeningToOnSwitchChange) {
- mSwitchBar.removeOnSwitchChangeListener(this);
- mListeningToOnSwitchChange = false;
- }
- mSwitchBar.hide();
- }
+ @Override
public void resume(Context context) {
- mContext = context;
+ super.resume(context);
// Wi-Fi state is sticky, so just let the receiver update UI
mContext.registerReceiver(mReceiver, mIntentFilter);
- if (!mListeningToOnSwitchChange) {
- mSwitchBar.addOnSwitchChangeListener(this);
- mListeningToOnSwitchChange = true;
- }
}
+ @Override
public void pause() {
+ super.pause();
mContext.unregisterReceiver(mReceiver);
- if (mListeningToOnSwitchChange) {
- mSwitchBar.removeOnSwitchChangeListener(this);
- mListeningToOnSwitchChange = false;
- }
}
private void handleWifiStateChanged(int state) {
switch (state) {
case WifiManager.WIFI_STATE_ENABLING:
- mSwitchBar.setEnabled(false);
+ setEnabled(false);
break;
case WifiManager.WIFI_STATE_ENABLED:
- setSwitchBarChecked(true);
- mSwitchBar.setEnabled(true);
+ setChecked(true);
+ setEnabled(true);
updateSearchIndex(true);
break;
case WifiManager.WIFI_STATE_DISABLING:
- mSwitchBar.setEnabled(false);
+ setEnabled(false);
break;
case WifiManager.WIFI_STATE_DISABLED:
- setSwitchBarChecked(false);
- mSwitchBar.setEnabled(true);
+ setChecked(false);
+ setEnabled(true);
updateSearchIndex(false);
break;
default:
- setSwitchBarChecked(false);
- mSwitchBar.setEnabled(true);
+ setChecked(false);
+ setEnabled(false);
updateSearchIndex(false);
}
}
@@ -168,11 +162,6 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
mHandler.sendMessage(msg);
}
- private void setSwitchBarChecked(boolean checked) {
- mStateMachineEvent = true;
- mSwitchBar.setChecked(checked);
- mStateMachineEvent = false;
- }
private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) {
// After the refactoring from a CheckBoxPreference to a Switch, this method is useless since
@@ -199,8 +188,7 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
// Show toast message if Wi-Fi is not allowed in airplane mode
if (isChecked && !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
- // Reset switch to off. No infinite check/listenenr loop.
- mSwitchBar.setChecked(false);
+ setChecked(false);
return;
}
@@ -214,8 +202,14 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
isChecked ? MetricsLogger.ACTION_WIFI_ON : MetricsLogger.ACTION_WIFI_OFF);
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
- mSwitchBar.setEnabled(true);
+ setEnabled(true);
+
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
}
+
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ super.onCheckedChanged(buttonView, isChecked);
+ }
}