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 android.content.Context; 18 import android.net.INetworkStatsService; 19 import android.net.NetworkPolicy; 20 import android.net.NetworkPolicyManager; 21 import android.os.Bundle; 22 import android.os.INetworkManagementService; 23 import android.os.RemoteException; 24 import android.os.ServiceManager; 25 import android.os.UserManager; 26 import android.telephony.SubscriptionManager; 27 import android.telephony.TelephonyManager; 28 import android.util.Log; 29 30 import com.android.settings.dashboard.DashboardFragment; 31 import com.android.settingslib.NetworkPolicyEditor; 32 33 public abstract class DataUsageBaseFragment extends DashboardFragment { 34 private static final String TAG = "DataUsageBase"; 35 private static final String ETHERNET = "ethernet"; 36 37 protected final TemplatePreference.NetworkServices services = 38 new TemplatePreference.NetworkServices(); 39 40 @Override onCreate(Bundle icicle)41 public void onCreate(Bundle icicle) { 42 super.onCreate(icicle); 43 Context context = getContext(); 44 45 services.mNetworkService = INetworkManagementService.Stub.asInterface( 46 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); 47 services.mStatsService = INetworkStatsService.Stub.asInterface( 48 ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); 49 services.mPolicyManager = (NetworkPolicyManager) context 50 .getSystemService(Context.NETWORK_POLICY_SERVICE); 51 52 services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager); 53 54 services.mTelephonyManager = TelephonyManager.from(context); 55 services.mSubscriptionManager = SubscriptionManager.from(context); 56 services.mUserManager = UserManager.get(context); 57 } 58 59 @Override onResume()60 public void onResume() { 61 super.onResume(); 62 services.mPolicyEditor.read(); 63 } 64 isAdmin()65 protected boolean isAdmin() { 66 return services.mUserManager.isAdminUser(); 67 } 68 isMobileDataAvailable(int subId)69 protected boolean isMobileDataAvailable(int subId) { 70 return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null; 71 } 72 isNetworkPolicyModifiable(NetworkPolicy policy, int subId)73 protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) { 74 return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser() 75 && isDataEnabled(subId); 76 } 77 isDataEnabled(int subId)78 private boolean isDataEnabled(int subId) { 79 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 80 return true; 81 } 82 return services.mTelephonyManager.getDataEnabled(subId); 83 } 84 isBandwidthControlEnabled()85 protected boolean isBandwidthControlEnabled() { 86 try { 87 return services.mNetworkService.isBandwidthControlEnabled(); 88 } catch (RemoteException e) { 89 Log.w(TAG, "problem talking with INetworkManagementService: ", e); 90 return false; 91 } 92 } 93 } 94