Search in sources :

Example 16 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project android_frameworks_base by crdroidandroid.

the class SettingsBackupAgent method restoreNetworkPolicies.

private void restoreNetworkPolicies(byte[] data) {
    NetworkPolicyManager networkPolicyManager = (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
    if (data != null && data.length != 0) {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
        try {
            int version = in.readInt();
            if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
                throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
            }
            int length = in.readInt();
            NetworkPolicy[] policies = new NetworkPolicy[length];
            for (int i = 0; i < length; i++) {
                byte isNull = in.readByte();
                if (isNull == BackupUtils.NULL)
                    continue;
                int byteLength = in.readInt();
                byte[] policyData = new byte[byteLength];
                in.read(policyData, 0, byteLength);
                policies[i] = NetworkPolicy.getNetworkPolicyFromBackup(new DataInputStream(new ByteArrayInputStream(policyData)));
            }
            // Only set the policies if there was no error in the restore operation
            networkPolicyManager.setNetworkPolicies(policies);
        } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
            // NPE can be thrown when trying to instantiate a NetworkPolicy
            Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager(android.net.NetworkPolicyManager) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 17 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project android_packages_apps_Settings by LineageOS.

the class DataUsageSummary method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    final Context context = getContext();
    NetworkPolicyManager policyManager = NetworkPolicyManager.from(context);
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mPolicyEditor = new NetworkPolicyEditor(policyManager);
    boolean hasMobileData = DataUsageUtils.hasMobileData(context);
    mDataUsageController = new DataUsageController(context);
    mDataInfoController = new DataUsageInfoController();
    addPreferencesFromResource(R.xml.data_usage);
    int defaultSubId = DataUsageUtils.getDefaultSubscriptionId(context);
    if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        hasMobileData = false;
    }
    mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
    mSummaryPreference = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
    if (!hasMobileData || !isAdmin()) {
        removePreference(KEY_RESTRICT_BACKGROUND);
    }
    if (hasMobileData) {
        mLimitPreference = findPreference(KEY_LIMIT_SUMMARY);
        List<SubscriptionInfo> subscriptions = services.mSubscriptionManager.getActiveSubscriptionInfoList();
        if (subscriptions == null || subscriptions.size() == 0) {
            addMobileSection(defaultSubId);
        }
        for (int i = 0; subscriptions != null && i < subscriptions.size(); i++) {
            SubscriptionInfo subInfo = subscriptions.get(i);
            if (subscriptions.size() > 1) {
                addMobileSection(subInfo.getSubscriptionId(), subInfo);
            } else {
                addMobileSection(subInfo.getSubscriptionId());
            }
        }
        mSummaryPreference.setSelectable(true);
    } else {
        removePreference(KEY_LIMIT_SUMMARY);
        mSummaryPreference.setSelectable(false);
    }
    boolean hasWifiRadio = DataUsageUtils.hasWifiRadio(context);
    if (hasWifiRadio) {
        addWifiSection();
    }
    if (hasEthernet(context)) {
        addEthernetSection();
    }
    mDataUsageTemplate = hasMobileData ? R.string.cell_data_template : hasWifiRadio ? R.string.wifi_data_template : R.string.ethernet_data_template;
    setHasOptionsMenu(true);
}
Also used : Context(android.content.Context) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) NetworkPolicyManager(android.net.NetworkPolicyManager) DataUsageController(com.android.settingslib.net.DataUsageController) SubscriptionInfo(android.telephony.SubscriptionInfo)

Example 18 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project android_packages_apps_Settings by crdroidandroid.

the class DataPlanUsageSummary method onCreate.

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    final Context context = getContext();
    NetworkPolicyManager policyManager = NetworkPolicyManager.from(context);
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mPolicyEditor = new NetworkPolicyEditor(policyManager);
    mDataUsageController = new DataUsageController(context);
    mDataInfoController = new DataUsageInfoController();
    int defaultSubId = DataUsageUtils.getDefaultSubscriptionId(context);
    boolean hasMobileData = DataUsageUtils.hasMobileData(context);
    if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        hasMobileData = false;
    }
    mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
    if (hasMobileData) {
        addDataPlanSection(defaultSubId);
    }
    if (DataUsageUtils.hasWifiRadio(context)) {
        addWifiSection();
    }
    if (hasEthernet(context)) {
        addEthernetSection();
    }
    setHasOptionsMenu(true);
}
Also used : Context(android.content.Context) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) NetworkPolicyManager(android.net.NetworkPolicyManager) DataUsageController(com.android.settingslib.net.DataUsageController)

Example 19 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project android_packages_apps_Settings by SudaMod.

the class DataPlanUsageSummary method onCreate.

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    final Context context = getContext();
    NetworkPolicyManager policyManager = NetworkPolicyManager.from(context);
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mPolicyEditor = new NetworkPolicyEditor(policyManager);
    mDataUsageController = new DataUsageController(context);
    mDataInfoController = new DataUsageInfoController();
    int defaultSubId = DataUsageUtils.getDefaultSubscriptionId(context);
    boolean hasMobileData = DataUsageUtils.hasMobileData(context);
    if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        hasMobileData = false;
    }
    mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
    if (hasMobileData) {
        addDataPlanSection(defaultSubId);
    }
    if (DataUsageUtils.hasWifiRadio(context)) {
        addWifiSection();
    }
    if (hasEthernet(context)) {
        addEthernetSection();
    }
    setHasOptionsMenu(true);
}
Also used : Context(android.content.Context) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) NetworkPolicyManager(android.net.NetworkPolicyManager) DataUsageController(com.android.settingslib.net.DataUsageController)

Example 20 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project platform_packages_apps_Settings by BlissRoms.

the class DataUsageSummary method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    final Context context = getContext();
    NetworkPolicyManager policyManager = NetworkPolicyManager.from(context);
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mPolicyEditor = new NetworkPolicyEditor(policyManager);
    boolean hasMobileData = DataUsageUtils.hasMobileData(context);
    mDataUsageController = new DataUsageController(context);
    mDataInfoController = new DataUsageInfoController();
    addPreferencesFromResource(R.xml.data_usage);
    int defaultSubId = DataUsageUtils.getDefaultSubscriptionId(context);
    if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        hasMobileData = false;
    }
    mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
    mSummaryPreference = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
    if (!hasMobileData || !isAdmin()) {
        removePreference(KEY_RESTRICT_BACKGROUND);
    }
    if (hasMobileData) {
        mLimitPreference = findPreference(KEY_LIMIT_SUMMARY);
        List<SubscriptionInfo> subscriptions = services.mSubscriptionManager.getActiveSubscriptionInfoList();
        if (subscriptions == null || subscriptions.size() == 0) {
            addMobileSection(defaultSubId);
        }
        for (int i = 0; subscriptions != null && i < subscriptions.size(); i++) {
            SubscriptionInfo subInfo = subscriptions.get(i);
            if (subscriptions.size() > 1) {
                addMobileSection(subInfo.getSubscriptionId(), subInfo);
            } else {
                addMobileSection(subInfo.getSubscriptionId());
            }
        }
        mSummaryPreference.setSelectable(true);
    } else {
        removePreference(KEY_LIMIT_SUMMARY);
        mSummaryPreference.setSelectable(false);
    }
    boolean hasWifiRadio = DataUsageUtils.hasWifiRadio(context);
    if (hasWifiRadio) {
        addWifiSection();
    }
    if (hasEthernet(context)) {
        addEthernetSection();
    }
    mDataUsageTemplate = hasMobileData ? R.string.cell_data_template : hasWifiRadio ? R.string.wifi_data_template : R.string.ethernet_data_template;
    setHasOptionsMenu(true);
}
Also used : Context(android.content.Context) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) NetworkPolicyManager(android.net.NetworkPolicyManager) DataUsageController(com.android.settingslib.net.DataUsageController) SubscriptionInfo(android.telephony.SubscriptionInfo)

Aggregations

NetworkPolicyManager (android.net.NetworkPolicyManager)24 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 DataUsageController (com.android.settingslib.net.DataUsageController)12 Context (android.content.Context)11 NetworkPolicy (android.net.NetworkPolicy)10 IOException (java.io.IOException)10 SubscriptionInfo (android.telephony.SubscriptionInfo)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 DataOutputStream (java.io.DataOutputStream)5 AppItem (com.android.settingslib.AppItem)2 EnforcedAdmin (com.android.settingslib.RestrictedLockUtils.EnforcedAdmin)2 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)2 Test (org.junit.Test)2 INetworkPolicyManager (android.net.INetworkPolicyManager)1