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 public abstract class DataUsageBase extends SettingsPreferenceFragment { 40 private static final String TAG = "DataUsageBase"; 41 private static final String ETHERNET = "ethernet"; 42 43 protected final TemplatePreference.NetworkServices services = 44 new TemplatePreference.NetworkServices(); 45 46 @Override 47 public void onCreate(Bundle icicle) { 48 super.onCreate(icicle); 49 final Context context = getActivity(); 50 51 services.mNetworkService = INetworkManagementService.Stub.asInterface( 52 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); 53 services.mStatsService = INetworkStatsService.Stub.asInterface( 54 ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); 55 services.mPolicyManager = NetworkPolicyManager.from(context); 56 57 services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager); 58 59 services.mTelephonyManager = TelephonyManager.from(context); 60 services.mSubscriptionManager = SubscriptionManager.from(context); 61 services.mUserManager = UserManager.get(context); 62 } 63 64 @Override 65 public void onResume() { 66 super.onResume(); 67 services.mPolicyEditor.read(); 68 } 69 70 protected boolean isAdmin() { 71 return services.mUserManager.isAdminUser(); 72 } 73 74 protected boolean isMobileDataAvailable(int subId) { 75 return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null; 76 } 77 78 protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) { 79 return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser() 80 && isDataEnabled(subId); 81 } 82 83 private boolean isDataEnabled(int subId) { 84 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 85 return true; 86 } 87 return services.mTelephonyManager.getDataEnabled(subId); 88 } 89 90 protected boolean isBandwidthControlEnabled() { 91 try { 92 return services.mNetworkService.isBandwidthControlEnabled(); 93 } catch (RemoteException e) { 94 Log.w(TAG, "problem talking with INetworkManagementService: ", e); 95 return false; 96 } 97 } 98 99 /** 100 * Test if device has an ethernet network connection. 101 */ 102 public boolean hasEthernet(Context context) { 103 if (DataUsageUtils.TEST_RADIOS) { 104 return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET); 105 } 106 107 final ConnectivityManager conn = ConnectivityManager.from(context); 108 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET); 109 110 final long ethernetBytes; 111 try { 112 INetworkStatsSession statsSession = services.mStatsService.openSession(); 113 if (statsSession != null) { 114 ethernetBytes = statsSession.getSummaryForNetwork( 115 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE) 116 .getTotalBytes(); 117 TrafficStats.closeQuietly(statsSession); 118 } else { 119 ethernetBytes = 0; 120 } 121 } catch (RemoteException e) { 122 throw new RuntimeException(e); 123 } 124 125 // only show ethernet when both hardware present and traffic has occurred 126 return hasEthernet && ethernetBytes > 0; 127 } 128 } 129