Add cellular data off conditional

Change-Id: I34980f3fc09bf5dd47eb296c5b1c5fccc87b5c90
This commit is contained in:
Jason Monk
2015-12-15 15:54:26 -05:00
parent af24b6377e
commit 70ecea2207
9 changed files with 180 additions and 40 deletions

View File

@@ -2692,5 +2692,13 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver
android:name=".dashboard.conditional.CellularDataCondition$Receiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.ANY_DATA_STATE" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>

View File

@@ -0,0 +1,41 @@
<!--
Copyright (C) 2014 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:autoMirrored="true"
android:width="17dp"
android:height="17dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@android:color/white"
android:pathData="M8,9
l-2.87,-2.87
-2.87,2.87
-1.13,-1.13
2.87,-2.87
-2.87,-2.87
1.13,-1.13
2.87,2.87
2.87,-2.87
1.13,1.13,
-2.87,2.87
2.87,2.87
L8,9
z" />
<path
android:fillColor="@android:color/white"
android:pathData="M2.000000,22.000000l20.000000,0.000000 0.000000,-20.000000z"/>
</vector>

View File

@@ -6693,6 +6693,9 @@
<!-- Turn off a conditional state of the device (e.g. airplane mode, or hotspot) [CHAR LIMIT=30] --> <!-- Turn off a conditional state of the device (e.g. airplane mode, or hotspot) [CHAR LIMIT=30] -->
<string name="condition_turn_off">Turn off</string> <string name="condition_turn_off">Turn off</string>
<!-- Turn on a conditional state of the device (e.g. cellular data) [CHAR LIMIT=30] -->
<string name="condition_turn_on">Turn on</string>
<!-- Title of condition that hotspot is on [CHAR LIMIT=30] --> <!-- Title of condition that hotspot is on [CHAR LIMIT=30] -->
<string name="condition_hotspot_title">Hotspot is on</string> <string name="condition_hotspot_title">Hotspot is on</string>
@@ -6714,4 +6717,10 @@
<!-- Summary of condition that battery saver is on [CHAR LIMIT=NONE] --> <!-- Summary of condition that battery saver is on [CHAR LIMIT=NONE] -->
<string name="condition_battery_summary">Performance is reduced. Location services and background data are turned off.</string> <string name="condition_battery_summary">Performance is reduced. Location services and background data are turned off.</string>
<!-- Title of condition that cellular data is off [CHAR LIMIT=30] -->
<string name="condition_cellular_title">Cellular data is off</string>
<!-- Summary of condition that cellular data is off [CHAR LIMIT=NONE] -->
<string name="condition_cellular_summary">Internet is available only via Wi-Fi</string>
</resources> </resources>

View File

@@ -16,10 +16,8 @@
package com.android.settings.dashboard.conditional; package com.android.settings.dashboard.conditional;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import com.android.settings.R; import com.android.settings.R;
@@ -38,13 +36,8 @@ public class AirplaneModeCondition extends Condition {
} }
@Override @Override
protected void onSilenceChanged(boolean silenced) { protected Class<?> getReceiverClass() {
// Only need to listen for airplane mode changes when its been silenced. return Receiver.class;
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);
} }
@Override @Override

View File

@@ -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();
}
}
}
}

View File

@@ -16,9 +16,10 @@
package com.android.settings.dashboard.conditional; package com.android.settings.dashboard.conditional;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.util.Log;
public abstract class Condition { public abstract class Condition {
@@ -81,8 +82,21 @@ public abstract class Condition {
} }
} }
protected void onSilenceChanged(boolean state) { private void onSilenceChanged(boolean silenced) {
// Optional enable/disable receivers based on silence state. 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() { public boolean shouldShow() {

View File

@@ -27,8 +27,6 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@@ -132,6 +130,7 @@ public class ConditionManager {
addIfMissing(HotspotCondition.class); addIfMissing(HotspotCondition.class);
addIfMissing(DndCondition.class); addIfMissing(DndCondition.class);
addIfMissing(BatterySaverCondition.class); addIfMissing(BatterySaverCondition.class);
addIfMissing(CellularDataCondition.class);
} }
private void addIfMissing(Class<? extends Condition> clz) { private void addIfMissing(Class<? extends Condition> clz) {
@@ -150,14 +149,10 @@ public class ConditionManager {
return new DndCondition(this); return new DndCondition(this);
} else if (BatterySaverCondition.class == clz) { } else if (BatterySaverCondition.class == clz) {
return new BatterySaverCondition(this); return new BatterySaverCondition(this);
} else if (CellularDataCondition.class == clz) {
return new CellularDataCondition(this);
} }
try { throw new RuntimeException("Unexpected Condition " + clz);
Constructor<?> constructor = clz.getConstructor(ConditionManager.class);
return (Condition) constructor.newInstance(this);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException
| InvocationTargetException e) {
}
return null;
} }
Context getContext() { Context getContext() {

View File

@@ -19,13 +19,10 @@ import android.app.ActivityManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.StatusBarManager; import android.app.StatusBarManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.provider.Settings; import android.provider.Settings;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import com.android.settings.R; import com.android.settings.R;
@@ -55,13 +52,8 @@ public class DndCondition extends Condition {
} }
@Override @Override
protected void onSilenceChanged(boolean silenced) { protected Class<?> getReceiverClass() {
// Only need to listen for dnd mode changes when its been silenced. return Receiver.class;
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);
} }
private CharSequence getZenState() { private CharSequence getZenState() {

View File

@@ -16,10 +16,8 @@
package com.android.settings.dashboard.conditional; package com.android.settings.dashboard.conditional;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
@@ -44,13 +42,8 @@ public class HotspotCondition extends Condition {
} }
@Override @Override
protected void onSilenceChanged(boolean silenced) { protected Class<?> getReceiverClass() {
// Only need to listen for hotspot changes when hotspot has been silenced. return Receiver.class;
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);
} }
@Override @Override