Permalink
Browse files

SystemUI: add volume tones back

During the move from VolumePanel to the new VolumeController/Dialog
setup, the volume tone logic got removed.

Ref: CYNGNOS-1891

Change-Id: I2860819e83507ef01948d1adb53840feecfa622d
Signed-off-by: Roman Birg <roman@cyngn.com>
  • Loading branch information...
romanbb authored and akhilnarang committed Feb 5, 2016
1 parent 861e445 commit 8a6266fb1e8672f135d604d512c68b65b831e609
Showing with 86 additions and 0 deletions.
  1. +86 −0 packages/SystemUI/src/com/android/systemui/volume/VolumeDialogController.java
@@ -29,6 +29,7 @@
import android.media.AudioManager;
import android.media.AudioSystem;
import android.media.IVolumeController;
import android.media.ToneGenerator;
import android.media.VolumePolicy;
import android.media.session.MediaController.PlaybackInfo;
import android.media.session.MediaSession.Token;
@@ -66,6 +67,9 @@
private static final int DYNAMIC_STREAM_START_INDEX = 100;
private static final int VIBRATE_HINT_DURATION = 50;
private static final int FREE_DELAY = 10000;
private static final int BEEP_DURATION = 150;
private static final int[] STREAMS = {
AudioSystem.STREAM_ALARM,
AudioSystem.STREAM_BLUETOOTH_SCO,
@@ -101,10 +105,13 @@
private VolumePolicy mVolumePolicy;
private boolean mShowDndTile = true;
private ToneGenerator mToneGenerators[];
public VolumeDialogController(Context context, ComponentName component) {
mContext = context.getApplicationContext();
Events.writeEvent(mContext, Events.EVENT_COLLECTION_STARTED);
mComponent = component;
mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
mWorkerThread = new HandlerThread(VolumeDialogController.class.getSimpleName());
mWorkerThread.start();
mWorker = new W(mWorkerThread.getLooper());
@@ -284,6 +291,7 @@ private boolean onVolumeChangedW(int stream, int flags) {
final boolean fromKey = (flags & AudioManager.FLAG_FROM_KEY) != 0;
final boolean showVibrateHint = (flags & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0;
final boolean showSilentHint = (flags & AudioManager.FLAG_SHOW_SILENT_HINT) != 0;
final boolean playSound = (flags & AudioManager.FLAG_PLAY_SOUND) != 0;
boolean changed = false;
if (showUI) {
changed |= updateActiveStreamW(stream);
@@ -303,6 +311,19 @@ private boolean onVolumeChangedW(int stream, int flags) {
if (showSilentHint) {
mCallbacks.onShowSilentHint();
}
if (playSound) {
if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0) {
mWorker.removeMessages(W.PLAY_SOUND);
mWorker.sendMessageDelayed(mWorker.obtainMessage(W.PLAY_SOUND, stream, flags),
AudioSystem.PLAY_SOUND_DELAY);
}
if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
mWorker.removeMessages(W.PLAY_SOUND);
onStopSoundsW();
}
}
if (changed && fromKey) {
Events.writeEvent(mContext, Events.EVENT_KEY, stream, lastAudibleStreamVolume);
}
@@ -545,6 +566,9 @@ public void dismiss() throws RemoteException {
private static final int NOTIFY_VISIBLE = 12;
private static final int USER_ACTIVITY = 13;
private static final int SHOW_SAFETY_WARNING = 14;
private static final int PLAY_SOUND = 15;
private static final int STOP_SOUNDS = 16;
private static final int FREE_RESOURCES = 17;
W(Looper looper) {
super(looper);
@@ -567,6 +591,9 @@ public void handleMessage(Message msg) {
case NOTIFY_VISIBLE: onNotifyVisibleW(msg.arg1 != 0); break;
case USER_ACTIVITY: onUserActivityW(); break;
case SHOW_SAFETY_WARNING: onShowSafetyWarningW(msg.arg1); break;
case PLAY_SOUND: onPlaySoundW(msg.arg1, msg.arg2); break;
case STOP_SOUNDS: onStopSoundsW(); break;
case FREE_RESOURCES: onFreeResourcesW(); break;
}
}
}
@@ -695,6 +722,65 @@ public void run() {
}
}
protected void onPlaySoundW(int streamType, int flags) {
// If preference is no sound - just exit here
if (Settings.System.getInt(mContext.getContentResolver(),
Settings.System.VOLUME_ADJUST_SOUNDS_ENABLED, 1) == 0) {
return;
}
if (mWorker.hasMessages(W.STOP_SOUNDS)) {
mWorker.removeMessages(W.STOP_SOUNDS);
// Force stop right now
onStopSoundsW();
}
ToneGenerator toneGen = getOrCreateToneGeneratorW(streamType);
if (toneGen != null) {
toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
mWorker.sendMessageDelayed(mWorker.obtainMessage(W.STOP_SOUNDS), BEEP_DURATION);
}
mWorker.removeMessages(W.FREE_RESOURCES);
mWorker.sendMessageDelayed(mWorker.obtainMessage(W.FREE_RESOURCES), FREE_DELAY);
}
protected void onStopSoundsW() {
int numStreamTypes = AudioSystem.getNumStreamTypes();
for (int i = numStreamTypes - 1; i >= 0; i--) {
ToneGenerator toneGen = mToneGenerators[i];
if (toneGen != null) {
toneGen.stopTone();
}
}
}
private ToneGenerator getOrCreateToneGeneratorW(int streamType) {
if (mToneGenerators[streamType] == null) {
try {
mToneGenerators[streamType] = new ToneGenerator(streamType,
ToneGenerator.MAX_VOLUME);
} catch (RuntimeException e) {
if (false) {
Log.d(TAG, "ToneGenerator constructor failed with "
+ "RuntimeException: " + e);
}
}
}
return mToneGenerators[streamType];
}
protected void onFreeResourcesW() {
synchronized (this) {
for (int i = mToneGenerators.length - 1; i >= 0; i--) {
if (mToneGenerators[i] != null) {
mToneGenerators[i].release();
}
mToneGenerators[i] = null;
}
}
}
private final class SettingObserver extends ContentObserver {
private final Uri SERVICE_URI = Settings.Secure.getUriFor(

0 comments on commit 8a6266f

Please sign in to comment.