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 mCarrierInfoText; 37 private Intent mManageSubscriptionIntent; 38 @StyleRes 39 private int mCarrierInfoTextStyle = R.style.DataUsageSummaryCarrierInfoTextAppearance; 40 DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)41 public DataUsageSummaryPreference(Context context, AttributeSet attrs, 42 int defStyleAttr, int defStyleRes) { 43 super(context, attrs, defStyleAttr, defStyleRes); 44 init(); 45 } 46 DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr)47 public DataUsageSummaryPreference(Context context, AttributeSet attrs, int defStyleAttr) { 48 super(context, attrs, defStyleAttr); 49 init(); 50 } 51 DataUsageSummaryPreference(Context context, AttributeSet attrs)52 public DataUsageSummaryPreference(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 init(); 55 } 56 DataUsageSummaryPreference(Context context)57 public DataUsageSummaryPreference(Context context) { 58 super(context); 59 init(); 60 } 61 init()62 private void init() { 63 setLayoutResource(R.layout.data_usage_summary_preference); 64 } 65 66 /** Sets the carrier info text. */ setCarrierInfoText(CharSequence text)67 public void setCarrierInfoText(CharSequence text) { 68 if (!TextUtils.equals(mCarrierInfoText, text)) { 69 mCarrierInfoText = text; 70 notifyChanged(); 71 } 72 } 73 74 /** Gets the carrier info text. */ getCarrierInfoText()75 public CharSequence getCarrierInfoText() { 76 return mCarrierInfoText; 77 } 78 79 /** Sets the carrier info text style. */ setCarrierInfoTextStyle(@tyleRes int styleId)80 public void setCarrierInfoTextStyle(@StyleRes int styleId) { 81 if (mCarrierInfoTextStyle != styleId) { 82 mCarrierInfoTextStyle = styleId; 83 notifyChanged(); 84 } 85 } 86 87 /** Gets the carrier info text style. */ 88 @StyleRes getCarrierInfoTextStyle()89 public int getCarrierInfoTextStyle() { 90 return mCarrierInfoTextStyle; 91 } 92 93 /** Sets the manage subscription intent. */ setManageSubscriptionIntent(Intent intent)94 public void setManageSubscriptionIntent(Intent intent) { 95 mManageSubscriptionIntent = intent; 96 notifyChanged(); 97 } 98 99 /** Gets the manage subscription intent. */ getManageSubscriptionIntent()100 public Intent getManageSubscriptionIntent() { 101 return mManageSubscriptionIntent; 102 } 103 104 @Override onBindViewHolder(PreferenceViewHolder view)105 public void onBindViewHolder(PreferenceViewHolder view) { 106 super.onBindViewHolder(view); 107 108 TextView carrierInfo = (TextView) view.findViewById(R.id.carrier_info_text); 109 setTextAndVisibility(carrierInfo, mCarrierInfoText); 110 carrierInfo.setTextAppearance(mCarrierInfoTextStyle); 111 112 Button button = (Button) view.findViewById(R.id.manage_subscription_button); 113 if (mManageSubscriptionIntent != null) { 114 button.setVisibility(View.VISIBLE); 115 button.setOnClickListener(v -> getContext().startActivity(mManageSubscriptionIntent)); 116 } else { 117 button.setVisibility(View.GONE); 118 } 119 } 120 setTextAndVisibility(TextView textView, CharSequence value)121 private void setTextAndVisibility(TextView textView, CharSequence value) { 122 if (!TextUtils.isEmpty(value)) { 123 textView.setText(value); 124 textView.setVisibility(View.VISIBLE); 125 } else { 126 textView.setVisibility(View.GONE); 127 } 128 } 129 } 130