-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathVolumeSlider.java
More file actions
executable file
·149 lines (132 loc) · 5.54 KB
/
VolumeSlider.java
File metadata and controls
executable file
·149 lines (132 loc) · 5.54 KB
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
package org.devgeeks.volumeslider;
import android.content.Context;
import android.media.AudioManager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONException;
import android.util.Log;
public class VolumeSlider extends CordovaPlugin {
private SeekBar volumeSeekBar;
private AudioManager audioManager;
private static final String TAG = "volume_slider";
double current_volume = 0.2;
private int cssToViewUnit(double size) {
return (int)Math.abs(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)size,
cordova.getActivity().getResources().getDisplayMetrics()));
}
private double cssToViewUnitDouble(double size) {
return TypedValue.applyDimension(TypedValue.TYPE_NULL, (float)size,
cordova.getActivity().getResources().getDisplayMetrics());
}
@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if (action == null)
return false;
if (action.equals("createVolumeSlider")) {
final int x = cssToViewUnit(args.getDouble(0));
final int y = cssToViewUnit(args.getDouble(1));
final int width = cssToViewUnit(args.getDouble(2));
final int height = cssToViewUnit(args.getDouble(3));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
createVolumeSlider(x, y, width, height);
}
});
return true;
}
if (action.equals("showVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showVolumeSlider();
}
});
return true;
}
if (action.equals("hideVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
hideVolumeSlider();
}
});
return true;
}
if (action.equals("setVolumeSlider")) {
final double volume = cssToViewUnitDouble(args.getDouble(0));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setVolumeSlider(volume);
}
});
return true;
}
if (action.equals("resetVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
resetVolumeSlider();
}
});
return true;
}
return false;
}
private void bindVolumeSeekBarToAudioManager(SeekBar volumeSeekBar, final Context context)
{
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volumeSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
private void createVolumeSlider(int x, int y, int width, int height) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
Context context = cordova.getActivity();
volumeSeekBar = new SeekBar(context);
bindVolumeSeekBarToAudioManager(volumeSeekBar, context);
// Record current media volume to revert to with resetVolumeSlider()
current_volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG, "CURRENT VOLUME = " + current_volume);
hideVolumeSlider();
RelativeLayout layout = new RelativeLayout(context);
layout.addView(volumeSeekBar, params);
ViewGroup container = (ViewGroup)cordova.getActivity().findViewById(android.R.id.content);
container.addView(layout);
}
private void showVolumeSlider() {
volumeSeekBar.setVisibility(View.VISIBLE);
}
private void hideVolumeSlider() {
volumeSeekBar.setVisibility(View.INVISIBLE);
}
private void setVolumeSlider(double volume) {
// We map volume to a range of 0.0 - 1.0
Log.d(TAG, "SET VOLUME TO: " + volume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(volume * audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)), 0);
}
private void resetVolumeSlider() {
// Resetting back to initial volume
Log.d(TAG, "RESET VOLUME TO: " + current_volume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(current_volume), 0);
}
}