1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.datausage; 16 17 import static android.net.ConnectivityManager.TYPE_ETHERNET; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.INetworkStatsService; 22 import android.net.INetworkStatsSession; 23 import android.net.NetworkPolicy; 24 import android.net.NetworkPolicyManager; 25 import android.net.NetworkTemplate; 26 import android.net.TrafficStats; 27 import android.os.Bundle; 28 import android.os.INetworkManagementService; 29 import android.os.RemoteException; 30 import android.os.ServiceManager; 31 import android.os.SystemProperties; 32 import android.os.UserManager; 33 import android.telephony.SubscriptionManager; 34 import android.telephony.TelephonyManager; 35 import android.util.Log; 36 import com.android.settings.SettingsPreferenceFragment; 37 import com.android.settingslib.NetworkPolicyEditor; 38 39 /** 40 * @deprecated please use {@link DataUsageBaseFragment} instead. 41 */ 42 @Deprecated 43 public abstract class DataUsageBase extends SettingsPreferenceFragment { 44 private static final String TAG = "DataUsageBase"; 45 private static final String ETHERNET = "ethernet"; 46 47 protected final TemplatePreference.NetworkServices services = 48 new TemplatePreference.NetworkServices(); 49 50 @Override onCreate(Bundle icicle)51 public void onCreate(Bundle icicle) { 52 super.onCreate(icicle); 53 final Context context = getActivity(); 54 55 services.mNetworkService = INetworkManagementService.Stub.asInterface( 56 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); 57 services.mStatsService = INetworkStatsService.Stub.asInterface( 58 ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); 59 services.mPolicyManager = NetworkPolicyManager.from(context); 60 61 services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager); 62 63 services.mTelephonyManager = TelephonyManager.from(context); 64 services.mSubscriptionManager = SubscriptionManager.from(context); 65 services.mUserManager = UserManager.get(context); 66 } 67 68 @Override onResume()69 public void onResume() { 70 super.onResume(); 71 services.mPolicyEditor.read(); 72 } 73 isAdmin()74 protected boolean isAdmin() { 75 return services.mUserManager.isAdminUser(); 76 } 77 isMobileDataAvailable(int subId)78 protected boolean isMobileDataAvailable(int subId) { 79 return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null; 80 } 81 isNetworkPolicyModifiable(NetworkPolicy policy, int subId)82 protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) { 83 return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser() 84 && isDataEnabled(subId); 85 } 86 isDataEnabled(int subId)87 private boolean isDataEnabled(int subId) { 88 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 89 return true; 90 } 91 return services.mTelephonyManager.getDataEnabled(subId); 92 } 93 isBandwidthControlEnabled()94 protected boolean isBandwidthControlEnabled() { 95 try { 96 return services.mNetworkService.isBandwidthControlEnabled(); 97 } catch (RemoteException e) { 98 Log.w(TAG, "problem talking with INetworkManagementService: ", e); 99 return false; 100 } 101 } 102 103 /** 104 * Test if device has an ethernet network connection. 105 */ hasEthernet(Context context)106 public boolean hasEthernet(Context context) { 107 if (DataUsageUtils.TEST_RADIOS) { 108 return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET); 109 } 110 111 final ConnectivityManager conn = ConnectivityManager.from(context); 112 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET); 113 114 final long ethernetBytes; 115 try { 116 INetworkStatsSession statsSession = services.mStatsService.openSession(); 117 if (statsSession != null) { 118 ethernetBytes = statsSession.getSummaryForNetwork( 119 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE) 120 .getTotalBytes(); 121 TrafficStats.closeQuietly(statsSession); 122 } else { 123 ethernetBytes = 0; 124 } 125 } catch (RemoteException e) { 126 throw new RuntimeException(e); 127 } 128 129 // only show ethernet when both hardware present and traffic has occurred 130 return hasEthernet && ethernetBytes > 0; 131 } 132 } 133