summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/audiofx/audiofx/eq/EqBarView.java
blob: df697ddc8fa475a10533a4442867c6398ae4547a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.cyngn.audiofx.eq;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.cyngn.audiofx.R;
import com.cyngn.audiofx.activity.EqualizerManager;
import com.cyngn.audiofx.activity.MasterConfigControl;
import com.cyngn.audiofx.activity.StateCallbacks;

public class EqBarView extends FrameLayout implements StateCallbacks.EqUpdatedCallback {

    private static final String TAG = EqBarView.class.getSimpleName();
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private EqualizerManager mEqManager;

    private float mNormalWidth;
    private float mParentHeight = -1;
    private float mLastTouchX;
    private float mLastTouchY;
    private float mPosX;
    private float mPosY = -1;
    private boolean mUserInteracting;
    private int mParentTop;
    private Integer mIndex;
    private float mInitialLevel;

    public EqBarView(Context context) {
        super(context);
        init();
    }

    public EqBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EqBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mEqManager = MasterConfigControl.getInstance(mContext).getEqualizerManager();
        mNormalWidth = getResources().getDimension(R.dimen.eq_bar_width);
    }

    @Override
    public boolean hasOverlappingRendering() {
        return false;
    }

    private EqContainerView.EqBandInfo getInfo() {
        return (EqContainerView.EqBandInfo) getTag();
    }

    public void setParentHeight(float h, int top) {
        mParentHeight = h;
        mParentTop = top;
        updateHeight();
    }

    void updateHeight() {
        if (DEBUG) Log.d(TAG, "updateHeight()");

        if (getInfo() != null) {
            float level = mEqManager.getLevel(getIndex());
            float yProjection = 1 - mEqManager.projectY(level);
            float height = (yProjection * (mParentHeight));
            mPosY = height;

            if (DEBUG) {
                Log.d(TAG, getIndex() + "level: " + level + ", yProjection: "
                        + yProjection + ", mPosY: " + mPosY);
            }
            updateHeight((int) mPosY);
        } else {
            if (DEBUG) Log.d(TAG, "could not updateHeight()");
        }
    }

    public int getIndex() {
        if (mIndex == null) {
            mIndex = (getInfo()).mIndex;
        }
        return mIndex;
    }

    public boolean isUserInteracting() {
        return mUserInteracting;
    }

    /* package */ void startInteraction(float x, float y) {

        mLastTouchX = x;
        mLastTouchY = y;
        mUserInteracting = true;

        if (DEBUG) Log.d(TAG, "initial level: " + mInitialLevel);
        mInitialLevel = (1 - (mPosY / mParentHeight)) * (mEqManager.getMinDB() - mEqManager.getMaxDB())
                - mEqManager.getMinDB();

        updateWidth((int) (mNormalWidth * 2));
    }

    /* package */ void endInteraction() {
        mUserInteracting = false;

        updateWidth((int) mNormalWidth);
    }

    private void updateHeight(int h) {
        if (!isInLayout()) {
            final ViewGroup.LayoutParams params = getLayoutParams();
            params.height = h;
            setLayoutParams(params);
        }
    }

    private void updateWidth(int w) {
        final ViewGroup.LayoutParams params = getLayoutParams();
        params.width = w;
        setLayoutParams(params);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mEqManager.isEqualizerLocked()) {
            return false;
        }

        final float x = event.getRawX();
        final float y = event.getRawY() - mParentTop;

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                startInteraction(x, y);
                break;

            case MotionEvent.ACTION_MOVE:
                // Calculate the distance moved
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY -= dy;

                // Remember this touch position for the next move event
                mLastTouchX = x;
                mLastTouchY = y;

                int wy = (int) mParentHeight;
                float level = (1 - (mPosY / wy)) * (mEqManager.getMinDB() - mEqManager.getMaxDB())
                        - mEqManager.getMinDB();

                if (DEBUG) Log.d(TAG, "new level: " + level);
                if (level < mEqManager.getMinDB()) {
                    level = mEqManager.getMinDB();
                } else if (level > mEqManager.getMaxDB()) {
                    level = mEqManager.getMaxDB();
                }

                if (mInitialLevel != level) {
                    mEqManager.setLevel(getInfo().mIndex, level, false);
                } else {
                    updateHeight();
                }

                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                endInteraction();
                break;

        }

        return true;
    }

    public float getPosY() {
        return mPosY;
    }

    @Override
    public void onBandLevelChange(int band, float dB, boolean fromSystem) {
        if (getInfo().mIndex != band) {
            return;
        }

        updateHeight();
    }

    @Override
    public void onPresetChanged(int newPresetIndex) {

    }

    @Override
    public void onPresetsChanged() {

    }
}