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 | |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 17 | import android.content.Context; |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 18 | import android.net.INetworkStatsService; |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 19 | import android.net.NetworkPolicy; |
| 20 | import android.net.NetworkPolicyManager; |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
| 22 | import android.os.INetworkManagementService; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 25 | import android.os.UserManager; |
| 26 | import android.telephony.SubscriptionManager; |
| 27 | import android.telephony.TelephonyManager; |
| 28 | import android.util.Log; |
| 29 | |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 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 |
| 41 | public void onCreate(Bundle icicle) { |
| 42 | super.onCreate(icicle); |
Jan Nordqvist | 9eb43dd | 2018-03-26 15:29:44 -0700 | [diff] [blame] | 43 | Context context = getContext(); |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 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)); |
Fan Zhang | cd21cb8 | 2018-06-11 09:28:02 -0700 | [diff] [blame] | 49 | services.mPolicyManager = (NetworkPolicyManager) context |
Jan Nordqvist | 9eb43dd | 2018-03-26 15:29:44 -0700 | [diff] [blame] | 50 | .getSystemService(Context.NETWORK_POLICY_SERVICE); |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 51 | |
| 52 | services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager); |
| 53 | |
Bonian Chen | 228dd34 | 2019-12-23 04:54:51 +0800 | [diff] [blame] | 54 | services.mTelephonyManager = context.getSystemService(TelephonyManager.class); |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 55 | services.mSubscriptionManager = SubscriptionManager.from(context); |
| 56 | services.mUserManager = UserManager.get(context); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void onResume() { |
| 61 | super.onResume(); |
| 62 | services.mPolicyEditor.read(); |
| 63 | } |
| 64 | |
| 65 | protected boolean isAdmin() { |
| 66 | return services.mUserManager.isAdminUser(); |
| 67 | } |
| 68 | |
| 69 | protected boolean isMobileDataAvailable(int subId) { |
| 70 | return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null; |
| 71 | } |
| 72 | |
| 73 | protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) { |
| 74 | return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser() |
| 75 | && isDataEnabled(subId); |
| 76 | } |
| 77 | |
| 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 | |
| 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 | } |
jackqdyulei | 137ff28 | 2018-01-25 10:55:59 -0800 | [diff] [blame] | 93 | } |