blob: 512b358d6828f79ae8fd0a28f0e70f68dc603acc [file] [log] [blame]
jackqdyulei137ff282018-01-25 10:55:59 -08001/*
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
15package com.android.settings.datausage;
16
17import static android.net.ConnectivityManager.TYPE_ETHERNET;
18
19import android.content.Context;
20import android.net.ConnectivityManager;
21import android.net.INetworkStatsService;
22import android.net.INetworkStatsSession;
23import android.net.NetworkPolicy;
24import android.net.NetworkPolicyManager;
25import android.net.NetworkTemplate;
26import android.net.TrafficStats;
27import android.os.Bundle;
28import android.os.INetworkManagementService;
29import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.os.SystemProperties;
32import android.os.UserManager;
33import android.telephony.SubscriptionManager;
34import android.telephony.TelephonyManager;
35import android.util.Log;
36
jackqdyulei137ff282018-01-25 10:55:59 -080037import com.android.settings.dashboard.DashboardFragment;
38import com.android.settingslib.NetworkPolicyEditor;
39
40public 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 Nordqvistcd3f9e22018-03-26 15:29:44 -070050 Context context = getContext();
jackqdyulei137ff282018-01-25 10:55:59 -080051
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 Nordqvistcd3f9e22018-03-26 15:29:44 -070056 services.mPolicyManager = (NetworkPolicyManager)context
57 .getSystemService(Context.NETWORK_POLICY_SERVICE);
jackqdyulei137ff282018-01-25 10:55:59 -080058
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 Nordqvistcd3f9e22018-03-26 15:29:44 -0700103 * TODO(b/77590489): Remove this method when DataUsageSummaryLegacy is deprecated.
jackqdyulei137ff282018-01-25 10:55:59 -0800104 */
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}