blob: 1bd2be8f0783c5251664c59479eea6f2c43ecf63 [file] [log] [blame]
Fan Zhang667d4272017-05-16 20:51:36 +00001/*
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
15package com.android.settings.datausage;
16
Fan Zhang31b21002019-01-16 13:49:47 -080017import android.app.settings.SettingsEnums;
Fan Zhang667d4272017-05-16 20:51:36 +000018import android.content.Context;
19import android.content.Intent;
Fan Zhang667d4272017-05-16 20:51:36 +000020import android.net.NetworkTemplate;
21import android.os.Bundle;
22import android.os.RemoteException;
Bonian Chen7e530dd2022-07-29 07:03:38 +000023import android.telephony.TelephonyManager;
Bonian Chen8f677012020-04-19 21:19:18 +080024import android.telephony.data.ApnSetting;
Fan Zhang667d4272017-05-16 20:51:36 +000025import android.util.AttributeSet;
26
Fan Zhang23f8d592018-08-28 15:11:40 -070027import androidx.preference.Preference;
28
Fan Zhang667d4272017-05-16 20:51:36 +000029import com.android.settings.R;
Fan Zhang1f6d24a2018-02-19 14:31:50 -080030import com.android.settings.core.SubSettingLauncher;
Bonian Chen5e65da02019-11-08 07:40:35 +080031import com.android.settings.network.MobileDataEnabledListener;
Fan Zhang667d4272017-05-16 20:51:36 +000032
Bonian Chen5e65da02019-11-08 07:40:35 +080033/**
34 * Preference which displays billing cycle of subscription
35 */
36public class BillingCyclePreference extends Preference
37 implements TemplatePreference, MobileDataEnabledListener.Client {
Fan Zhang667d4272017-05-16 20:51:36 +000038
39 private NetworkTemplate mTemplate;
40 private NetworkServices mServices;
Fan Zhang667d4272017-05-16 20:51:36 +000041 private int mSubId;
Bonian Chen5e65da02019-11-08 07:40:35 +080042 private MobileDataEnabledListener mListener;
Fan Zhang667d4272017-05-16 20:51:36 +000043
Bonian Chen5e65da02019-11-08 07:40:35 +080044 /**
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 Zhang667d4272017-05-16 20:51:36 +000050 public BillingCyclePreference(Context context, AttributeSet attrs) {
51 super(context, attrs);
Bonian Chen5e65da02019-11-08 07:40:35 +080052 mListener = new MobileDataEnabledListener(context, this);
Fan Zhang667d4272017-05-16 20:51:36 +000053 }
54
55 @Override
56 public void onAttached() {
57 super.onAttached();
Bonian Chen5e65da02019-11-08 07:40:35 +080058 mListener.start(mSubId);
Fan Zhang667d4272017-05-16 20:51:36 +000059 }
60
61 @Override
62 public void onDetached() {
Bonian Chen5e65da02019-11-08 07:40:35 +080063 mListener.stop();
Fan Zhang667d4272017-05-16 20:51:36 +000064 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 Zhangcd21cb82018-06-11 09:28:02 -070073 setSummary(null);
74
Fan Zhang667d4272017-05-16 20:51:36 +000075 setIntent(getIntent());
76 }
77
78 private void updateEnabled() {
79 try {
Jeff Sharkey47f51fa2017-07-13 16:47:51 -060080 setEnabled(mServices.mNetworkService.isBandwidthControlEnabled()
Bonian Chen8f677012020-04-19 21:19:18 +080081 && mServices.mTelephonyManager.createForSubscriptionId(mSubId)
Bonian Chen7e530dd2022-07-29 07:03:38 +000082 .isDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER)
Fan Zhang667d4272017-05-16 20:51:36 +000083 && mServices.mUserManager.isAdminUser());
84 } catch (RemoteException e) {
85 setEnabled(false);
86 }
87 }
88
89 @Override
90 public Intent getIntent() {
Bonian Chen5e65da02019-11-08 07:40:35 +080091 final Bundle args = new Bundle();
Doris Ling282a0d92018-11-30 16:20:32 -080092 args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate);
Fan Zhang1f6d24a2018-02-19 14:31:50 -080093 return new SubSettingLauncher(getContext())
94 .setDestination(BillingCycleSettings.class.getName())
95 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +080096 .setTitleRes(R.string.billing_cycle)
Fan Zhang31b21002019-01-16 13:49:47 -080097 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
Fan Zhang1f6d24a2018-02-19 14:31:50 -080098 .toIntent();
Fan Zhang667d4272017-05-16 20:51:36 +000099 }
100
Bonian Chen5e65da02019-11-08 07:40:35 +0800101 /**
Bonian Chen90cb45e2020-01-08 17:29:36 +0800102 * Implementation of {@code MobileDataEnabledListener.Client}
Bonian Chen5e65da02019-11-08 07:40:35 +0800103 */
104 public void onMobileDataEnabledChange() {
105 updateEnabled();
106 }
Fan Zhang667d4272017-05-16 20:51:36 +0000107}