1 /* 2 * Copyright (C) 2021 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.util.AttributeSet; 21 import android.widget.EditText; 22 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.car.settings.R; 26 import com.android.car.ui.preference.CarUiPreference; 27 28 /** CarUiPreference that shows an edit text with numberDecimal input type */ 29 public class DataUsageEditTextPreference extends CarUiPreference { 30 31 private String mTextToSet; 32 private EditText mEditText; 33 DataUsageEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)34 public DataUsageEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, 35 int defStyleRes) { 36 super(context, attrs, defStyleAttr, defStyleRes); 37 init(); 38 } 39 DataUsageEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr)40 public DataUsageEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 41 super(context, attrs, defStyleAttr); 42 init(); 43 } 44 DataUsageEditTextPreference(Context context, AttributeSet attrs)45 public DataUsageEditTextPreference(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 init(); 48 } 49 DataUsageEditTextPreference(Context context)50 public DataUsageEditTextPreference(Context context) { 51 super(context); 52 init(); 53 } 54 init()55 private void init() { 56 setLayoutResource(R.layout.data_usage_edit_text_preference); 57 } 58 59 @Override onBindViewHolder(PreferenceViewHolder view)60 public void onBindViewHolder(PreferenceViewHolder view) { 61 super.onBindViewHolder(view); 62 63 mEditText = (EditText) view.findViewById(R.id.data_usage_edit_text); 64 65 if (mTextToSet != null) { 66 mEditText.setText(mTextToSet); 67 mTextToSet = null; 68 } 69 } 70 71 /** Sets the EditText text */ setText(String textToSet)72 public void setText(String textToSet) { 73 mTextToSet = textToSet; 74 notifyChanged(); 75 } 76 77 /** Returns the current the EditText text */ getText()78 public String getText() { 79 return mEditText.getText().toString(); 80 } 81 } 82