Search in sources :

Example 21 with DataUsageController

use of com.android.settingslib.net.DataUsageController in project android_packages_apps_Settings by omnirom.

the class SettingsDumpService method dumpDataUsage.

private JSONObject dumpDataUsage() throws JSONException {
    JSONObject obj = new JSONObject();
    DataUsageController controller = new DataUsageController(this);
    SubscriptionManager manager = this.getSystemService(SubscriptionManager.class);
    TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class);
    final PackageManager packageManager = this.getPackageManager();
    if (telephonyManager.isDataCapable()) {
        JSONArray array = new JSONArray();
        for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
            telephonyManager = telephonyManager.createForSubscriptionId(info.getSubscriptionId());
            String subscriberId = telephonyManager.getSubscriberId();
            // The null subscriberId means that no any mobile/carrier network will be matched.
            // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE.
            NetworkTemplate template = subscriberId != null ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId) : NetworkTemplate.buildTemplateMobileAll(subscriberId);
            final JSONObject usage = dumpDataUsage(template, controller);
            usage.put("subId", info.getSubscriptionId());
            array.put(usage);
        }
        obj.put("cell", array);
    }
    if (packageManager.hasSystemFeature(FEATURE_WIFI)) {
        obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifi(NetworkTemplate.WIFI_NETWORKID_ALL, null), controller));
    }
    if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) {
        obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
    }
    return obj;
}
Also used : NetworkTemplate(android.net.NetworkTemplate) JSONObject(org.json.JSONObject) PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) DataUsageController(com.android.settingslib.net.DataUsageController) JSONArray(org.json.JSONArray) SubscriptionInfo(android.telephony.SubscriptionInfo) SubscriptionManager(android.telephony.SubscriptionManager)

Example 22 with DataUsageController

use of com.android.settingslib.net.DataUsageController in project android_packages_apps_Settings by omnirom.

the class DataUsagePreferenceController method getDataUsageSummary.

private CharSequence getDataUsageSummary(Context context, int subId) {
    final DataUsageController controller = new DataUsageController(context);
    controller.setSubscriptionId(subId);
    mHistoricalUsageLevel = ThreadUtils.postOnBackgroundThread(() -> controller.getHistoricalUsageLevel(getNetworkTemplate()));
    final DataUsageController.DataUsageInfo usageInfo = getDataUsageInfo(controller);
    long usageLevel = usageInfo.usageLevel;
    if (usageLevel <= 0L) {
        try {
            usageLevel = mHistoricalUsageLevel.get();
        } catch (Exception exception) {
        }
    }
    if (usageLevel <= 0L) {
        return null;
    }
    return context.getString(R.string.data_usage_template, DataUsageUtils.formatDataUsage(context, usageLevel), usageInfo.period);
}
Also used : DataUsageController(com.android.settingslib.net.DataUsageController) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with DataUsageController

use of com.android.settingslib.net.DataUsageController in project android_packages_apps_Settings by LineageOS.

the class SettingsDumpService method dumpDataUsage.

private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller) throws JSONException {
    JSONObject obj = new JSONObject();
    DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
    obj.put("carrier", usage.carrier);
    obj.put("start", usage.startDate);
    obj.put("usage", usage.usageLevel);
    obj.put("warning", usage.warningLevel);
    obj.put("limit", usage.limitLevel);
    return obj;
}
Also used : JSONObject(org.json.JSONObject) DataUsageController(com.android.settingslib.net.DataUsageController)

Example 24 with DataUsageController

use of com.android.settingslib.net.DataUsageController in project android_packages_apps_Settings by LineageOS.

the class BillingCycleSettings method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    mDataUsageController = new DataUsageController(getContext());
    Bundle args = getArguments();
    mNetworkTemplate = args.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE);
    addPreferencesFromResource(R.xml.billing_cycle);
    mBillingCycle = findPreference(KEY_BILLING_CYCLE);
    mEnableDataWarning = (SwitchPreference) findPreference(KEY_SET_DATA_WARNING);
    mEnableDataWarning.setOnPreferenceChangeListener(this);
    mDataWarning = findPreference(KEY_DATA_WARNING);
    mEnableDataLimit = (SwitchPreference) findPreference(KEY_SET_DATA_LIMIT);
    mEnableDataLimit.setOnPreferenceChangeListener(this);
    mDataLimit = findPreference(KEY_DATA_LIMIT);
}
Also used : Bundle(android.os.Bundle) DataUsageController(com.android.settingslib.net.DataUsageController)

Example 25 with DataUsageController

use of com.android.settingslib.net.DataUsageController in project android_packages_apps_Settings by LineageOS.

the class DataUsagePreference method setTemplate.

@Override
public void setTemplate(NetworkTemplate template, int subId, NetworkServices services) {
    mTemplate = template;
    mSubId = subId;
    DataUsageController controller = new DataUsageController(getContext());
    DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
    setSummary(getContext().getString(R.string.data_usage_template, Formatter.formatFileSize(getContext(), usageInfo.usageLevel), usageInfo.period));
    setIntent(getIntent());
}
Also used : DataUsageController(com.android.settingslib.net.DataUsageController)

Aggregations

DataUsageController (com.android.settingslib.net.DataUsageController)44 JSONObject (org.json.JSONObject)14 Context (android.content.Context)13 NetworkPolicyManager (android.net.NetworkPolicyManager)12 SubscriptionInfo (android.telephony.SubscriptionInfo)12 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 SubscriptionManager (android.telephony.SubscriptionManager)8 NetworkTemplate (android.net.NetworkTemplate)7 Bundle (android.os.Bundle)7 TelephonyManager (android.telephony.TelephonyManager)7 JSONArray (org.json.JSONArray)7 ConnectivityManager (android.net.ConnectivityManager)6 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 INetworkPolicyManager (android.net.INetworkPolicyManager)1 SpannableString (android.text.SpannableString)1 IconCompat (androidx.core.graphics.drawable.IconCompat)1 ListBuilder (androidx.slice.builders.ListBuilder)1 SliceAction (androidx.slice.builders.SliceAction)1 ExecutionException (java.util.concurrent.ExecutionException)1