1 /*
2  * Copyright (C) 2016 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 com.android.settings.SettingsPreferenceFragment;
18 import com.android.settingslib.NetworkPolicyEditor;
19 
20 import android.content.Context;
21 import android.net.INetworkStatsService;
22 import android.net.NetworkPolicy;
23 import android.net.NetworkPolicyManager;
24 import android.os.Bundle;
25 import android.os.INetworkManagementService;
26 import android.os.RemoteException;
27 import android.os.ServiceManager;
28 import android.os.UserManager;
29 import android.telephony.SubscriptionManager;
30 import android.telephony.TelephonyManager;
31 import android.util.Log;
32 
33 public abstract class DataUsageBase extends SettingsPreferenceFragment {
34 
35     private static final String TAG = "DataUsageBase";
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         final Context context = getActivity();
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.from(context);
50 
51         services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);
52 
53         services.mTelephonyManager = TelephonyManager.from(context);
54         services.mSubscriptionManager = SubscriptionManager.from(context);
55         services.mUserManager = UserManager.get(context);
56     }
57 
58     @Override
onResume()59     public void onResume() {
60         super.onResume();
61         services.mPolicyEditor.read();
62     }
63 
isAdmin()64     protected boolean isAdmin() {
65         return services.mUserManager.isAdminUser();
66     }
67 
isMobileDataAvailable(int subId)68     protected boolean isMobileDataAvailable(int subId) {
69         return services.mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
70     }
71 
isNetworkPolicyModifiable(NetworkPolicy policy, int subId)72     protected boolean isNetworkPolicyModifiable(NetworkPolicy policy, int subId) {
73         return policy != null && isBandwidthControlEnabled() && services.mUserManager.isAdminUser()
74                 && isDataEnabled(subId);
75     }
76 
isDataEnabled(int subId)77     private boolean isDataEnabled(int subId) {
78         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
79             return true;
80         }
81         return services.mTelephonyManager.getDataEnabled(subId);
82     }
83 
isBandwidthControlEnabled()84     protected boolean isBandwidthControlEnabled() {
85         try {
86             return services.mNetworkService.isBandwidthControlEnabled();
87         } catch (RemoteException e) {
88             Log.w(TAG, "problem talking with INetworkManagementService: " + e);
89             return false;
90         }
91     }
92 }
93