Skip to content

Commit

Permalink
Handle unfinished mobile data changes properly.
Browse files Browse the repository at this point in the history
Change-Id: I7c7e588730024a82be8fca64d7847aac6c2e069b
  • Loading branch information
maniac103 committed Jun 6, 2012
1 parent 9d6f1e3 commit 1d85e37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Expand Up @@ -372,8 +372,10 @@ public void onReceive(Context context, Intent intent) {
logD("Received WiMAX change request");
WimaxButton.getInstance().toggleState(context);
}
} else if (MobileDataButton.MOBILE_DATA_CHANGED.equals(intent.getAction())
|| Intent.ACTION_USER_PRESENT.equals(intent.getAction())
} else if (MobileDataButton.MOBILE_DATA_CHANGED.equals(intent.getAction())) {
logD("Received mobile data mode state change");
MobileDataButton.getInstance().onReceive(context, intent);
} else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())
|| SecuritySettings.GPS_STATUS_CHANGED.equals(intent.getAction())) {
} else {
logD("Ignoring Action: " + intent.getAction());
Expand Down
19 changes: 12 additions & 7 deletions src/com/android/settings/widget/buttons/MobileDataButton.java
Expand Up @@ -6,6 +6,7 @@
import com.android.settings.widget.WidgetSettings;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.provider.Settings;
Expand All @@ -17,6 +18,7 @@ public class MobileDataButton extends WidgetButton {
static MobileDataButton ownButton = null;

static boolean stateChangeRequest = false;
static boolean intendedState = false;

public static boolean getDataRomingEnabled(Context context) {
return Settings.Secure
Expand All @@ -31,11 +33,6 @@ public static boolean getDataRomingEnabled(Context context) {
private static boolean getDataState(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
/* Make sure the state change propagates */
Thread.sleep(100);
} catch (java.lang.InterruptedException ie) {
}
return cm.getMobileDataEnabled();
}

Expand All @@ -53,6 +50,7 @@ public void toggleState(Context context) {
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (enabled) {
cm.setMobileDataEnabled(false);
intendedState = false;
if (preferences.getBoolean(WidgetSettings.AUTO_DISABLE_3G, false)) {
NetworkModeButton.getInstance().toggleState(context,
SettingsAppWidgetProvider.STATE_DISABLED);
Expand All @@ -66,14 +64,16 @@ public void toggleState(Context context) {
stateChangeRequest = true;
} else {
cm.setMobileDataEnabled(true);
intendedState = true;
}
}
}

@Override
public void updateState(Context context, SharedPreferences globalPreferences, int[] appWidgetIds) {
boolean state = getDataState(context);

if (stateChangeRequest) {
if (stateChangeRequest || state != intendedState) {
currentIcon = R.drawable.ic_appwidget_settings_data_on;
if (globalPreferences.getBoolean(WidgetSettings.MONITOR_DATA_ROAMING, true)
&& getDataRomingEnabled(context)) {
Expand All @@ -82,7 +82,7 @@ && getDataRomingEnabled(context)) {
currentState = SettingsAppWidgetProvider.STATE_INTERMEDIATE;
}

} else if (getDataState(context)) {
} else if (state) {
currentIcon = R.drawable.ic_appwidget_settings_data_on;
if (globalPreferences.getBoolean(WidgetSettings.MONITOR_DATA_ROAMING, true)
&& getDataRomingEnabled(context)) {
Expand Down Expand Up @@ -114,11 +114,16 @@ void initButton() {
preferenceName = WidgetSettings.TOGGLE_DATA;
}

public void onReceive(Context context, Intent intent) {
intendedState = intent.getBooleanExtra("enabled", false);
}

public void networkModeChanged(Context context, int networkMode) {
if (stateChangeRequest) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.setMobileDataEnabled(true);
intendedState = true;
stateChangeRequest = false;
}
}
Expand Down

0 comments on commit 1d85e37

Please sign in to comment.