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