Add cellular data off conditional
Change-Id: I34980f3fc09bf5dd47eb296c5b1c5fccc87b5c90
This commit is contained in:
@@ -16,10 +16,8 @@
|
||||
package com.android.settings.dashboard.conditional;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.ConnectivityManager;
|
||||
import com.android.settings.R;
|
||||
@@ -38,13 +36,8 @@ public class AirplaneModeCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSilenceChanged(boolean silenced) {
|
||||
// Only need to listen for airplane mode changes when its been silenced.
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), Receiver.class),
|
||||
silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.conditional;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import com.android.internal.telephony.TelephonyIntents;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Settings;
|
||||
|
||||
public class CellularDataCondition extends Condition {
|
||||
|
||||
public CellularDataCondition(ConditionManager manager) {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshState() {
|
||||
ConnectivityManager connectivity = mManager.getContext().getSystemService(
|
||||
ConnectivityManager.class);
|
||||
TelephonyManager telephony = mManager.getContext().getSystemService(TelephonyManager.class);
|
||||
if (!connectivity.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)
|
||||
|| telephony.getSimState() != TelephonyManager.SIM_STATE_READY) {
|
||||
setActive(false);
|
||||
return;
|
||||
}
|
||||
setActive(!telephony.getDataEnabled());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getIcon() {
|
||||
return Icon.createWithResource(mManager.getContext(), R.drawable.ic_cellular_off);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle() {
|
||||
return mManager.getContext().getString(R.string.condition_cellular_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return mManager.getContext().getString(R.string.condition_cellular_summary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence[] getActions() {
|
||||
return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_on) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrimaryClick() {
|
||||
mManager.getContext().startActivity(new Intent(mManager.getContext(),
|
||||
Settings.DataUsageSummaryActivity.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActionClick(int index) {
|
||||
if (index == 0) {
|
||||
TelephonyManager telephony = mManager.getContext().getSystemService(
|
||||
TelephonyManager.class);
|
||||
telephony.setDataEnabled(true);
|
||||
setActive(false);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unexpected index " + index);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Receiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED.equals(
|
||||
intent.getAction())) {
|
||||
ConditionManager.get(context).getCondition(CellularDataCondition.class)
|
||||
.refreshState();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,9 +16,10 @@
|
||||
|
||||
package com.android.settings.dashboard.conditional;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.PersistableBundle;
|
||||
import android.util.Log;
|
||||
|
||||
public abstract class Condition {
|
||||
|
||||
@@ -81,8 +82,21 @@ public abstract class Condition {
|
||||
}
|
||||
}
|
||||
|
||||
protected void onSilenceChanged(boolean state) {
|
||||
// Optional enable/disable receivers based on silence state.
|
||||
private void onSilenceChanged(boolean silenced) {
|
||||
Class<?> clz = getReceiverClass();
|
||||
if (clz == null) {
|
||||
return;
|
||||
}
|
||||
// Only need to listen for changes when its been silenced.
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), clz),
|
||||
silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
}
|
||||
|
||||
protected Class<?> getReceiverClass() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean shouldShow() {
|
||||
|
@@ -27,8 +27,6 @@ import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@@ -132,6 +130,7 @@ public class ConditionManager {
|
||||
addIfMissing(HotspotCondition.class);
|
||||
addIfMissing(DndCondition.class);
|
||||
addIfMissing(BatterySaverCondition.class);
|
||||
addIfMissing(CellularDataCondition.class);
|
||||
}
|
||||
|
||||
private void addIfMissing(Class<? extends Condition> clz) {
|
||||
@@ -150,14 +149,10 @@ public class ConditionManager {
|
||||
return new DndCondition(this);
|
||||
} else if (BatterySaverCondition.class == clz) {
|
||||
return new BatterySaverCondition(this);
|
||||
} else if (CellularDataCondition.class == clz) {
|
||||
return new CellularDataCondition(this);
|
||||
}
|
||||
try {
|
||||
Constructor<?> constructor = clz.getConstructor(ConditionManager.class);
|
||||
return (Condition) constructor.newInstance(this);
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException
|
||||
| InvocationTargetException e) {
|
||||
}
|
||||
return null;
|
||||
throw new RuntimeException("Unexpected Condition " + clz);
|
||||
}
|
||||
|
||||
Context getContext() {
|
||||
|
@@ -19,13 +19,10 @@ import android.app.ActivityManager;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.StatusBarManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.provider.Settings;
|
||||
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import com.android.settings.R;
|
||||
|
||||
@@ -55,13 +52,8 @@ public class DndCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSilenceChanged(boolean silenced) {
|
||||
// Only need to listen for dnd mode changes when its been silenced.
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), Receiver.class),
|
||||
silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
}
|
||||
|
||||
private CharSequence getZenState() {
|
||||
|
@@ -16,10 +16,8 @@
|
||||
package com.android.settings.dashboard.conditional;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
@@ -44,13 +42,8 @@ public class HotspotCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSilenceChanged(boolean silenced) {
|
||||
// Only need to listen for hotspot changes when hotspot has been silenced.
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), Receiver.class),
|
||||
silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user