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