blob: 299ce433f370afa0e1854eee6dadf9be7dbc011d [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
jackqdyulei137ff282018-01-25 10:55:59 -080017import android.content.Context;
jackqdyulei137ff282018-01-25 10:55:59 -080018import android.net.INetworkStatsService;
jackqdyulei137ff282018-01-25 10:55:59 -080019import android.net.NetworkPolicy;
20import android.net.NetworkPolicyManager;
jackqdyulei137ff282018-01-25 10:55:59 -080021import android.os.Bundle;
22import android.os.INetworkManagementService;
23import android.os.RemoteException;
24import android.os.ServiceManager;
jackqdyulei137ff282018-01-25 10:55:59 -080025import android.os.UserManager;
26import android.telephony.SubscriptionManager;
27import android.telephony.TelephonyManager;
28import android.util.Log;
29
jackqdyulei137ff282018-01-25 10:55:59 -080030import com.android.settings.dashboard.DashboardFragment;
31import com.android.settingslib.NetworkPolicyEditor;
32
33public 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 Nordqvist9eb43dd2018-03-26 15:29:44 -070043 Context context = getContext();
jackqdyulei137ff282018-01-25 10:55:59 -080044
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 Zhangcd21cb82018-06-11 09:28:02 -070049 services.mPolicyManager = (NetworkPolicyManager) context
Jan Nordqvist9eb43dd2018-03-26 15:29:44 -070050 .getSystemService(Context.NETWORK_POLICY_SERVICE);
jackqdyulei137ff282018-01-25 10:55:59 -080051
52 services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);
53
Bonian Chen228dd342019-12-23 04:54:51 +080054 services.mTelephonyManager = context.getSystemService(TelephonyManager.class);
jackqdyulei137ff282018-01-25 10:55:59 -080055 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 }
jackqdyulei137ff282018-01-25 10:55:59 -080093}