Home | History | Annotate | Download | only in datausage
      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 
     37 import com.android.settings.SettingsPreferenceFragment;
     38 import com.android.settings.dashboard.DashboardFragment;
     39 import com.android.settingslib.NetworkPolicyEditor;
     40 
     41 public abstract class DataUsageBaseFragment extends DashboardFragment {
     42     private static final String TAG = "DataUsageBase";
     43     private static final String ETHERNET = "ethernet";
     44 
     45     protected final TemplatePreference.NetworkServices services =
     46             new TemplatePreference.NetworkServices();
     47 
     48     @Override
     49     public void onCreate(Bundle icicle) {
     50         super.onCreate(icicle);
     51         Context context = getContext();
     52 
     53         services.mNetworkService = INetworkManagementService.Stub.asInterface(
     54                 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
     55         services.mStatsService = INetworkStatsService.Stub.asInterface(
     56                 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
     57         services.mPolicyManager = (NetworkPolicyManager)context
     58                 .getSystemService(Context.NETWORK_POLICY_SERVICE);
     59 
     60         services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);
     61 
     62         services.mTelephonyManager = TelephonyManager.from(context);
     63         services.mSubscriptionManager = SubscriptionManager.from(context);
     64         services.mUserManager = UserManager.get(context);
     65     }
     66 
     67     @Override
     68     public void onResume() {
     69         super.onResume();
     70         services.mPolicyEditor.read();
     71     }
     72 
     73     protected boolean isAdmin() {
     74         return services.mUserManager.isAdminUser();
     75     }
     76 
     77     protected boolean isMobileDataAvailable(int subId) {
     78         return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
     79     }
     80 
     81     protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) {
     82         return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser()
     83                 && isDataEnabled(subId);
     84     }
     85 
     86     private boolean isDataEnabled(int subId) {
     87         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
     88             return true;
     89         }
     90         return services.mTelephonyManager.getDataEnabled(subId);
     91     }
     92 
     93     protected boolean isBandwidthControlEnabled() {
     94         try {
     95             return services.mNetworkService.isBandwidthControlEnabled();
     96         } catch (RemoteException e) {
     97             Log.w(TAG, "problem talking with INetworkManagementService: ", e);
     98             return false;
     99         }
    100     }
    101 
    102     /**
    103      * Test if device has an ethernet network connection.
    104      * TODO(b/77590489): Remove this method when DataUsageSummaryLegacy is deprecated.
    105      */
    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