diff options
author | Roman Birg <roman@cyngn.com> | 2015-01-05 13:32:20 -0800 |
---|---|---|
committer | Danesh M <daneshm90@gmail.com> | 2015-12-15 18:31:05 -0800 |
commit | c5cd017e7e168c5b69072dacda74aa2f298c7ab0 (patch) | |
tree | c061f473d41602c0ce7058c7507d62d89ee796e6 /src/com/android/settings/dashboard | |
parent | e1f1ba9036f23f439dbeab6b48a16bcf97db3b93 (diff) | |
download | packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.zip packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.gz packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.bz2 |
Settings: add switches for dashboard items
Change-Id: Ibff81510270745807a4b133457d60d47dd629df6
Signed-off-by: Roman Birg <roman@cyngn.com>
Diffstat (limited to 'src/com/android/settings/dashboard')
5 files changed, 324 insertions, 2 deletions
diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java index 38ed297..e01a843 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 5e7e49a..ecb9a48 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 0896b82..6790136 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 0000000..95eec5c --- /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 0000000..d7c202a --- /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); + } +} |