diff options
Diffstat (limited to 'src/org/cyanogenmod/providers/datausage')
4 files changed, 75 insertions, 3 deletions
diff --git a/src/org/cyanogenmod/providers/datausage/BootReceiver.java b/src/org/cyanogenmod/providers/datausage/BootReceiver.java index 75e8e52..73e2dfb 100644 --- a/src/org/cyanogenmod/providers/datausage/BootReceiver.java +++ b/src/org/cyanogenmod/providers/datausage/BootReceiver.java @@ -19,14 +19,20 @@ package org.cyanogenmod.providers.datausage; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.util.Log; public class BootReceiver extends BroadcastReceiver { private static final String TAG = BootReceiver.class.getSimpleName(); + private static final boolean DEBUG = true; @Override public void onReceive(Context context, Intent intent) { - DataUsageUtils.startDataUsageServiceIfEnabled(context); + boolean qualified = DataUsageUtils.isDataUsageQualified(context); + + // start DataUsage service once the device boots up, but only if + // on qualified network and the service itself is enabled from the Settings->DataUsage + if (qualified) { + DataUsageUtils.startDataUsageServiceIfEnabled(context); + } } } diff --git a/src/org/cyanogenmod/providers/datausage/DataUsageConnectivityReceiver.java b/src/org/cyanogenmod/providers/datausage/DataUsageConnectivityReceiver.java new file mode 100644 index 0000000..375b995 --- /dev/null +++ b/src/org/cyanogenmod/providers/datausage/DataUsageConnectivityReceiver.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 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 org.cyanogenmod.providers.datausage; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + + +public class DataUsageConnectivityReceiver extends BroadcastReceiver { + private static final String TAG = DataUsageConnectivityReceiver.class.getSimpleName(); + private static final boolean DEBUG = true; + @Override + public void onReceive(Context context, Intent intent) { + // datausage service is only run for metered mobile connections + boolean qualified = DataUsageUtils.isDataUsageQualified(context); + if (DEBUG) { + Log.v(TAG, "onReceived: qualified: " + qualified); + } + if (qualified) { + // start DataUsage service, but only if enabled + DataUsageUtils.startDataUsageServiceIfEnabled(context); + } else { + // stop DataUsage service + DataUsageUtils.enbDataUsageService(context, false); + } + } +} diff --git a/src/org/cyanogenmod/providers/datausage/DataUsageService.java b/src/org/cyanogenmod/providers/datausage/DataUsageService.java index ce328cc..e59874e 100644 --- a/src/org/cyanogenmod/providers/datausage/DataUsageService.java +++ b/src/org/cyanogenmod/providers/datausage/DataUsageService.java @@ -27,6 +27,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.net.ConnectivityManager; import android.net.Uri; import android.os.Handler; import android.os.IBinder; diff --git a/src/org/cyanogenmod/providers/datausage/DataUsageUtils.java b/src/org/cyanogenmod/providers/datausage/DataUsageUtils.java index 5b5ce36..2159760 100644 --- a/src/org/cyanogenmod/providers/datausage/DataUsageUtils.java +++ b/src/org/cyanogenmod/providers/datausage/DataUsageUtils.java @@ -24,6 +24,8 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.UserInfo; import android.database.Cursor; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.util.Log; import org.cyanogenmod.providers.datausage.R; @@ -141,7 +143,12 @@ public final class DataUsageUtils { public static void enbDataUsageService(Context context, boolean enb) { SharedPreferences prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE); prefs.edit().putBoolean(PREF_ENB_DATA_USAGE_NOTIFY, enb).apply(); - startDataUsageService(context, enb); + // start DataUsage service, but only if on qualified mobile network + // if the service is not started now, it will be started when the network state changes + // and the connected network is qualified for the DataUsage service + if (isDataUsageQualified(context)) { + startDataUsageService(context, enb); + } } public static void startDataUsageService(Context context, boolean enb) { @@ -174,6 +181,21 @@ public final class DataUsageUtils { if (DEBUG) { Log.v(TAG, "startDataUsageServiceIfEnabled: enb: " + enb); } + } + // determine if the currently connected network qualified for the DataUsage service + public static boolean isDataUsageQualified(Context context) { + // only perform DataUsage collection for metered networks + ConnectivityManager connectivityManager = + (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + if (activeNetworkInfo != null) { + boolean isConnected = activeNetworkInfo.isConnected(); + boolean isMobile = activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE; + boolean qualified = isConnected && isMobile && connectivityManager.isActiveNetworkMetered(); + return qualified; + } else { + return false; + } } } |