Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 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 | |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 17 | import android.app.settings.SettingsEnums; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.content.Intent; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 20 | import android.net.NetworkTemplate; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.RemoteException; |
Bonian Chen | 7e530dd | 2022-07-29 07:03:38 +0000 | [diff] [blame] | 23 | import android.telephony.TelephonyManager; |
Bonian Chen | 8f67701 | 2020-04-19 21:19:18 +0800 | [diff] [blame] | 24 | import android.telephony.data.ApnSetting; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.preference.Preference; |
| 28 | |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 29 | import com.android.settings.R; |
Fan Zhang | 1f6d24a | 2018-02-19 14:31:50 -0800 | [diff] [blame] | 30 | import com.android.settings.core.SubSettingLauncher; |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 31 | import com.android.settings.network.MobileDataEnabledListener; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 32 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 33 | /** |
| 34 | * Preference which displays billing cycle of subscription |
| 35 | */ |
| 36 | public class BillingCyclePreference extends Preference |
| 37 | implements TemplatePreference, MobileDataEnabledListener.Client { |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 38 | |
| 39 | private NetworkTemplate mTemplate; |
| 40 | private NetworkServices mServices; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 41 | private int mSubId; |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 42 | private MobileDataEnabledListener mListener; |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 43 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 44 | /** |
| 45 | * Preference constructor |
| 46 | * |
| 47 | * @param context Context of preference |
| 48 | * @param arrts The attributes of the XML tag that is inflating the preference |
| 49 | */ |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 50 | public BillingCyclePreference(Context context, AttributeSet attrs) { |
| 51 | super(context, attrs); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 52 | mListener = new MobileDataEnabledListener(context, this); |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onAttached() { |
| 57 | super.onAttached(); |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 58 | mListener.start(mSubId); |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void onDetached() { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 63 | mListener.stop(); |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 64 | super.onDetached(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void setTemplate(NetworkTemplate template, int subId, |
| 69 | NetworkServices services) { |
| 70 | mTemplate = template; |
| 71 | mSubId = subId; |
| 72 | mServices = services; |
Fan Zhang | cd21cb8 | 2018-06-11 09:28:02 -0700 | [diff] [blame] | 73 | setSummary(null); |
| 74 | |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 75 | setIntent(getIntent()); |
| 76 | } |
| 77 | |
| 78 | private void updateEnabled() { |
| 79 | try { |
Jeff Sharkey | 47f51fa | 2017-07-13 16:47:51 -0600 | [diff] [blame] | 80 | setEnabled(mServices.mNetworkService.isBandwidthControlEnabled() |
Bonian Chen | 8f67701 | 2020-04-19 21:19:18 +0800 | [diff] [blame] | 81 | && mServices.mTelephonyManager.createForSubscriptionId(mSubId) |
Bonian Chen | 7e530dd | 2022-07-29 07:03:38 +0000 | [diff] [blame] | 82 | .isDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER) |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 83 | && mServices.mUserManager.isAdminUser()); |
| 84 | } catch (RemoteException e) { |
| 85 | setEnabled(false); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Intent getIntent() { |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 91 | final Bundle args = new Bundle(); |
Doris Ling | 282a0d9 | 2018-11-30 16:20:32 -0800 | [diff] [blame] | 92 | args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); |
Fan Zhang | 1f6d24a | 2018-02-19 14:31:50 -0800 | [diff] [blame] | 93 | return new SubSettingLauncher(getContext()) |
| 94 | .setDestination(BillingCycleSettings.class.getName()) |
| 95 | .setArguments(args) |
hjchangliao | e86eec0 | 2018-05-02 13:01:07 +0800 | [diff] [blame] | 96 | .setTitleRes(R.string.billing_cycle) |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 97 | .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN) |
Fan Zhang | 1f6d24a | 2018-02-19 14:31:50 -0800 | [diff] [blame] | 98 | .toIntent(); |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 101 | /** |
Bonian Chen | 90cb45e | 2020-01-08 17:29:36 +0800 | [diff] [blame] | 102 | * Implementation of {@code MobileDataEnabledListener.Client} |
Bonian Chen | 5e65da0 | 2019-11-08 07:40:35 +0800 | [diff] [blame] | 103 | */ |
| 104 | public void onMobileDataEnabledChange() { |
| 105 | updateEnabled(); |
| 106 | } |
Fan Zhang | 667d427 | 2017-05-16 20:51:36 +0000 | [diff] [blame] | 107 | } |