1 /* 2 * Copyright 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.mobileer.oboetester; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.util.Log; 22 import android.view.LayoutInflater; 23 import android.widget.LinearLayout; 24 import android.widget.SeekBar; 25 import android.widget.TextView; 26 27 import java.util.Locale; 28 29 public class WorkloadView extends LinearLayout { 30 31 protected static final int FADER_PROGRESS_MAX = 1000; // must match layout 32 protected TextView mTextView; 33 protected SeekBar mSeekBar; 34 35 private String mLabel = "Workload"; 36 protected ExponentialTaper mExponentialTaper; 37 38 public interface WorkloadReceiver { setWorkload(int workload)39 void setWorkload(int workload); 40 } 41 42 WorkloadReceiver mWorkloadReceiver; 43 44 private SeekBar.OnSeekBarChangeListener mChangeListener = new SeekBar.OnSeekBarChangeListener() { 45 @Override 46 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 47 setValueByPosition(progress); 48 } 49 50 @Override 51 public void onStartTrackingTouch(SeekBar seekBar) { 52 } 53 54 @Override 55 public void onStopTrackingTouch(SeekBar seekBar) { 56 } 57 }; 58 WorkloadView(Context context)59 public WorkloadView(Context context) { 60 super(context); 61 initializeViews(context); 62 } 63 WorkloadView(Context context, AttributeSet attrs)64 public WorkloadView(Context context, AttributeSet attrs) { 65 super(context, attrs); 66 initializeViews(context); 67 } 68 WorkloadView(Context context, AttributeSet attrs, int defStyle)69 public WorkloadView(Context context, 70 AttributeSet attrs, 71 int defStyle) { 72 super(context, attrs, defStyle); 73 initializeViews(context); 74 } 75 setWorkloadReceiver(WorkloadReceiver workloadReceiver)76 public void setWorkloadReceiver(WorkloadReceiver workloadReceiver) { 77 mWorkloadReceiver = workloadReceiver; 78 } 79 setFaderNormalizedProgress(double fraction)80 void setFaderNormalizedProgress(double fraction) { 81 mSeekBar.setProgress((int)(fraction * FADER_PROGRESS_MAX)); 82 } 83 84 /** 85 * Inflates the views in the layout. 86 * 87 * @param context 88 * the current context for the view. 89 */ initializeViews(Context context)90 private void initializeViews(Context context) { 91 LayoutInflater inflater = (LayoutInflater) context 92 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 93 inflater.inflate(R.layout.workload_view, this); 94 95 mTextView = (TextView) findViewById(R.id.textWorkload); 96 mSeekBar = (SeekBar) findViewById(R.id.faderWorkload); 97 mSeekBar.setOnSeekBarChangeListener(mChangeListener); 98 setRange(0.0, 100.0); 99 //mSeekBar.setProgress(0); 100 } 101 setRange(double dMin, double dMax)102 void setRange(double dMin, double dMax) { 103 mExponentialTaper = new ExponentialTaper(dMin, dMax, 10.0); 104 } 105 setValueByPosition(int progress)106 private void setValueByPosition(int progress) { 107 int workload = (int) mExponentialTaper.linearToExponential( 108 ((double)progress) / FADER_PROGRESS_MAX); 109 if (mWorkloadReceiver != null) { 110 mWorkloadReceiver.setWorkload(workload); 111 } 112 mTextView.setText(getLabel() + " = " + String.format(Locale.getDefault(), "%3d", workload)); 113 } 114 115 @Override setEnabled(boolean enabled)116 public void setEnabled(boolean enabled) { 117 super.setEnabled(enabled); 118 mSeekBar.setEnabled(enabled); 119 } 120 getLabel()121 public String getLabel() { 122 return mLabel; 123 } 124 setLabel(String label)125 public void setLabel(String label) { 126 this.mLabel = label; 127 } 128 } 129