1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.datausage; 18 19 import android.content.Context; 20 import android.net.NetworkPolicyManager; 21 import android.net.NetworkTemplate; 22 import android.os.INetworkManagementService; 23 import android.os.ServiceManager; 24 import android.os.UserManager; 25 import android.telephony.SubscriptionManager; 26 import android.telephony.TelephonyManager; 27 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.datausage.DataUsageUtils; 32 import com.android.settings.datausage.lib.DataUsageLib; 33 import com.android.settingslib.NetworkPolicyEditor; 34 35 public class BillingCyclePreferenceController extends BasePreferenceController { 36 private int mSubscriptionId; 37 BillingCyclePreferenceController(Context context, String preferenceKey)38 public BillingCyclePreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 } 41 init(int subscriptionId)42 public void init(int subscriptionId) { 43 mSubscriptionId = subscriptionId; 44 } 45 46 @Override displayPreference(PreferenceScreen screen)47 public void displayPreference(PreferenceScreen screen) { 48 super.displayPreference(screen); 49 BillingCyclePreference preference = screen.findPreference(getPreferenceKey()); 50 51 TemplatePreference.NetworkServices services = new TemplatePreference.NetworkServices(); 52 services.mNetworkService = INetworkManagementService.Stub.asInterface( 53 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); 54 services.mPolicyManager = mContext.getSystemService(NetworkPolicyManager.class); 55 services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager); 56 services.mTelephonyManager = mContext.getSystemService(TelephonyManager.class); 57 services.mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class); 58 services.mUserManager = mContext.getSystemService(UserManager.class); 59 60 NetworkTemplate template = DataUsageLib.getMobileTemplate(mContext, mSubscriptionId); 61 62 preference.setTemplate(template, mSubscriptionId, services); 63 } 64 65 @Override getAvailabilityStatus()66 public int getAvailabilityStatus() { 67 return DataUsageUtils.hasMobileData(mContext) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 68 } 69 } 70