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