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.car.settings.datausage;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.TextView;
26 
27 import androidx.annotation.StyleRes;
28 import androidx.preference.PreferenceViewHolder;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.common.ProgressBarPreference;
32 
33 /** Extends {@link ProgressBarPreference} in order to support multiple text fields. */
34 public class DataUsageSummaryPreference extends ProgressBarPreference {
35 
36     private CharSequence mDataLimitText;
37     private CharSequence mRemainingBillingCycleText;
38     private CharSequence mCarrierInfoText;
39     private Intent mManageSubscriptionIntent;
40     @StyleRes
41     private int mCarrierInfoTextStyle = R.style.DataUsageSummaryCarrierInfoTextAppearance;
42 
DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)43     public DataUsageSummaryPreference(Context context, AttributeSet attrs,
44             int defStyleAttr, int defStyleRes) {
45         super(context, attrs, defStyleAttr, defStyleRes);
46         init();
47     }
48 
DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr)49     public DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr) {
50         super(context, attrs, defStyleAttr);
51         init();
52     }
53 
DataUsageSummaryPreference(Context context, AttributeSet attrs)54     public DataUsageSummaryPreference(Context context, AttributeSet attrs) {
55         super(context, attrs);
56         init();
57     }
58 
DataUsageSummaryPreference(Context context)59     public DataUsageSummaryPreference(Context context) {
60         super(context);
61         init();
62     }
63 
init()64     private void init() {
65         setLayoutResource(R.layout.data_usage_summary_preference);
66     }
67 
68     /** Sets the data limit text. */
setDataLimitText(CharSequence text)69     public void setDataLimitText(CharSequence text) {
70         if (!TextUtils.equals(mDataLimitText, text)) {
71             mDataLimitText = text;
72             notifyChanged();
73         }
74     }
75 
76     /** Gets the data limit text. */
getDataLimitText()77     public CharSequence getDataLimitText() {
78         return mDataLimitText;
79     }
80 
81     /** Sets the remaining billing cycle description. */
setRemainingBillingCycleText(CharSequence text)82     public void setRemainingBillingCycleText(CharSequence text) {
83         if (!TextUtils.equals(mRemainingBillingCycleText, text)) {
84             mRemainingBillingCycleText = text;
85             notifyChanged();
86         }
87     }
88 
89     /** Gets the remaining billing cycle description. */
getRemainingBillingCycleText()90     public CharSequence getRemainingBillingCycleText() {
91         return mRemainingBillingCycleText;
92     }
93 
94     /** Sets the carrier info text. */
setCarrierInfoText(CharSequence text)95     public void setCarrierInfoText(CharSequence text) {
96         if (!TextUtils.equals(mCarrierInfoText, text)) {
97             mCarrierInfoText = text;
98             notifyChanged();
99         }
100     }
101 
102     /** Gets the carrier info text. */
getCarrierInfoText()103     public CharSequence getCarrierInfoText() {
104         return mCarrierInfoText;
105     }
106 
107     /** Sets the carrier info text style. */
setCarrierInfoTextStyle(@tyleRes int styleId)108     public void setCarrierInfoTextStyle(@StyleRes int styleId) {
109         if (mCarrierInfoTextStyle != styleId) {
110             mCarrierInfoTextStyle = styleId;
111             notifyChanged();
112         }
113     }
114 
115     /** Gets the carrier info text style. */
116     @StyleRes
getCarrierInfoTextStyle()117     public int getCarrierInfoTextStyle() {
118         return mCarrierInfoTextStyle;
119     }
120 
121     /** Sets the manage subscription intent. */
setManageSubscriptionIntent(Intent intent)122     public void setManageSubscriptionIntent(Intent intent) {
123         mManageSubscriptionIntent = intent;
124         notifyChanged();
125     }
126 
127     /** Gets the manage subscription intent. */
getManageSubscriptionIntent()128     public Intent getManageSubscriptionIntent() {
129         return mManageSubscriptionIntent;
130     }
131 
132     @Override
onBindViewHolder(PreferenceViewHolder view)133     public void onBindViewHolder(PreferenceViewHolder view) {
134         super.onBindViewHolder(view);
135 
136         setTextAndVisibility((TextView) view.findViewById(R.id.data_limit_text), mDataLimitText);
137         setTextAndVisibility((TextView) view.findViewById(R.id.remaining_billing_cycle_time_text),
138                 mRemainingBillingCycleText);
139         TextView carrierInfo = (TextView) view.findViewById(R.id.carrier_info_text);
140         setTextAndVisibility(carrierInfo, mCarrierInfoText);
141         carrierInfo.setTextAppearance(mCarrierInfoTextStyle);
142 
143         Button button = (Button) view.findViewById(R.id.manage_subscription_button);
144         if (mManageSubscriptionIntent != null) {
145             button.setVisibility(View.VISIBLE);
146             button.setOnClickListener(v -> getContext().startActivity(mManageSubscriptionIntent));
147         } else {
148             button.setVisibility(View.GONE);
149         }
150     }
151 
setTextAndVisibility(TextView textView, CharSequence value)152     private void setTextAndVisibility(TextView textView, CharSequence value) {
153         if (!TextUtils.isEmpty(value)) {
154             textView.setText(value);
155             textView.setVisibility(View.VISIBLE);
156         } else {
157             textView.setVisibility(View.GONE);
158         }
159     }
160 }
161