1 /* 2 * Copyright (C) 2017 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 package com.android.statsd.loadtest; 17 18 import android.text.Editable; 19 import android.text.TextWatcher; 20 import android.util.Log; 21 import android.widget.TextView; 22 23 public abstract class NumericalWatcher implements TextWatcher { 24 25 private static final String TAG = "loadtest.NumericalWatcher"; 26 27 private final TextView mTextView; 28 private final int mMin; 29 private final int mMax; 30 private int currentValue = -1; 31 32 public NumericalWatcher(TextView textView, int min, int max) { 33 mTextView = textView; 34 mMin = min; 35 mMax = max; 36 } 37 38 public abstract void onNewValue(int newValue); 39 40 @Override 41 final public void afterTextChanged(Editable editable) { 42 String s = mTextView.getText().toString(); 43 if (s.isEmpty()) { 44 return; 45 } 46 int unsanitized = Integer.parseInt(s); 47 int newValue = sanitize(unsanitized); 48 if (currentValue != newValue || unsanitized != newValue) { 49 currentValue = newValue; 50 editable.clear(); 51 editable.append(newValue + ""); 52 } 53 onNewValue(newValue); 54 } 55 56 @Override 57 final public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 58 59 @Override 60 final public void onTextChanged(CharSequence s, int start, int before, int count) {} 61 62 private int sanitize(int val) { 63 if (val > mMax) { 64 val = mMax; 65 } else if (val < mMin) { 66 val = mMin; 67 } 68 return val; 69 } 70 } 71