下面列出了android.media.AudioManager#ADJUST_LOWER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onResume() {
super.onResume();
Intent sipServiceIntent = new Intent(SipManager.INTENT_SIP_SERVICE);
// Optional, but here we bundle so just ensure we are using csipsimple package
sipServiceIntent.setPackage(getPackageName());
bindService(sipServiceIntent , sipConnection, BIND_AUTO_CREATE);
int direction = getIntent().getIntExtra(Intent.EXTRA_KEY_EVENT, 0);
if(direction == AudioManager.ADJUST_LOWER || direction == AudioManager.ADJUST_RAISE) {
isAutoClose = true;
okBar.setVisibility(View.GONE);
delayedQuit(AUTO_QUIT_DELAY);
}else {
okBar.setVisibility(View.VISIBLE);
isAutoClose = false;
}
registerReceiver(callStateReceiver, new IntentFilter(SipManager.ACTION_SIP_CALL_CHANGED));
if (monitorThread == null) {
monitorThread = new MonitorThread();
monitorThread.start();
}
}
/**
* Set the volume_3 of a calibrated stream.
* @see {@link #setStreamVolume(int, float)}
* @throws RemoteException
*/
public int adjustStreamVolume(int stream, int direction, int index, int max) throws RemoteException {
LogUtils.LOGI("AudioFlingerProxy", "adjustStreamVolume(" + stream + ", " + direction + ")");
if (null == mAudioFlinger || TextUtils.isEmpty(mInterfaceDescriptor) || !isCalibrated(stream)) {
return BAD_VALUE;
}
float value = getStreamVolume(stream);
float increment = getStreamIncrement(stream);
float newValue = value;
switch (direction) {
case AudioManager.ADJUST_LOWER:
newValue -= increment;
case AudioManager.ADJUST_RAISE:
newValue += increment;
}
newValue = Math.max(0, Math.min(newValue, max * increment));
LogUtils.LOGI("AudioFlingerProxy", "adjustStreamVolume() increment = " + increment + ", newVolume = " + newValue);
return setStreamVolume(stream, newValue);
}
/**
* Set the volume_3 of a calibrated stream.
* @see {@link #setStreamVolume(int, float)}
* @throws RemoteException
*/
public int adjustStreamVolume(int stream, int direction, int index, int max) throws RemoteException {
LogUtils.LOGI("AudioFlingerProxy", "adjustStreamVolume(" + stream + ", " + direction + ")");
if (null == mAudioFlinger || TextUtils.isEmpty(mInterfaceDescriptor) || !isCalibrated(stream)) {
return BAD_VALUE;
}
float value = getStreamVolume(stream);
float increment = getStreamIncrement(stream);
float newValue = value;
switch (direction) {
case AudioManager.ADJUST_LOWER:
newValue -= increment;
case AudioManager.ADJUST_RAISE:
newValue += increment;
}
newValue = Math.max(0, Math.min(newValue, max * increment));
LogUtils.LOGI("AudioFlingerProxy", "adjustStreamVolume() increment = " + increment + ", newVolume = " + newValue);
return setStreamVolume(stream, newValue);
}
private void adjustVolumeFromKeyEvent(int button) {
final int direction =
((button == VolumeButtonPatternDetector.VOLUME_UP)
? AudioManager.ADJUST_RAISE
: AudioManager.ADJUST_LOWER);
// While continuous reading is active, we do not want to show the UI and interrupt continuous
// reading.
if (touchingScreen || fullScreenReadController.isActive()) {
AudioManagerCompatUtils.adjustStreamVolume(
audioManager,
STREAM_TALKBACK_AUDIO,
direction,
DEFAULT_FLAGS_TOUCHING_SCREEN,
getClass().getName());
} else if (actorState.getSpeechState().isSpeakingOrSpeechQueued()) {
AudioManagerCompatUtils.adjustStreamVolume(
audioManager,
STREAM_TALKBACK_AUDIO,
direction,
DEFAULT_FLAGS_NOT_TOUCHING_SCREEN,
getClass().getName());
} else {
// Attempt to adjust the suggested stream, but let the system
// override in special situations like during voice calls, when an
// application has locked the volume control stream, or when music
// is playing.
audioManager.adjustSuggestedStreamVolume(
direction, STREAM_DEFAULT, DEFAULT_FLAGS_NOT_TOUCHING_SCREEN);
}
}
public static int adjustIndex(final int index, final int max, final int direction) {
switch (direction) {
case AudioManager.ADJUST_LOWER:
return Math.max(index - 1, 0);
case AudioManager.ADJUST_RAISE:
return Math.min(index + 1, max);
case AudioManager.ADJUST_SAME:
return index;
default:
return 0;
}
}
public static int adjustIndex(final int index, final int max, final int direction) {
switch (direction) {
case AudioManager.ADJUST_LOWER:
return Math.max(index - 1, 0);
case AudioManager.ADJUST_RAISE:
return Math.min(index + 1, max);
case AudioManager.ADJUST_SAME:
return index;
default:
return 0;
}
}
private void dispatchVolumeKeyEventLocked(String packageName, int pid, int uid,
boolean asSystemService, KeyEvent keyEvent, int stream, boolean musicOnly) {
boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
int direction = 0;
boolean isMute = false;
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
direction = AudioManager.ADJUST_RAISE;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
direction = AudioManager.ADJUST_LOWER;
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
isMute = true;
break;
}
if (down || up) {
int flags = AudioManager.FLAG_FROM_KEY;
if (musicOnly) {
// This flag is used when the screen is off to only affect active media.
flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
} else {
// These flags are consistent with the home screen
if (up) {
flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
} else {
flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
}
}
if (direction != 0) {
// If this is action up we want to send a beep for non-music events
if (up) {
direction = 0;
}
dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
direction, flags);
} else if (isMute) {
if (down && keyEvent.getRepeatCount() == 0) {
dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
AudioManager.ADJUST_TOGGLE_MUTE, flags);
}
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(THIS_FILE, "Key down : " + keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
//
// Volume has been adjusted by the user.
//
Log.d(THIS_FILE, "onKeyDown: Volume button pressed");
int action = AudioManager.ADJUST_RAISE;
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
action = AudioManager.ADJUST_LOWER;
}
// Detect if ringing
SipCallSession currentCallInfo = getActiveCallInfo();
// If not any active call active
if (currentCallInfo == null && serviceConnected) {
break;
}
if (service != null) {
try {
service.adjustVolume(currentCallInfo, action, AudioManager.FLAG_SHOW_UI);
} catch (RemoteException e) {
Log.e(THIS_FILE, "Can't adjust volume", e);
}
}
return true;
case KeyEvent.KEYCODE_CALL:
case KeyEvent.KEYCODE_ENDCALL:
return inCallAnswerControls.onKeyDown(keyCode, event);
case KeyEvent.KEYCODE_SEARCH:
// Prevent search
return true;
default:
// Nothing to do
}
return super.onKeyDown(keyCode, event);
}
/** Set the volume of all public AudioManager streams. */
public void adjustVolumeSync(final int direction) {
// Find the stream with the smallest max volume.
LOGD(TAG, "adjustVolumeSync(" + direction + ")");
Pair<Integer, Integer> lowestStream = null;
for (final int stream : STREAMS) {
Pair<Integer, Integer> streamPair = STREAM_VOLUMES.get(stream);
if (null != streamPair) {
if (null == lowestStream || streamPair.second < lowestStream.second)
lowestStream = streamPair;
if (streamPair.second == lowestStream.second && streamPair.first < lowestStream.first)
lowestStream = streamPair;
}
}
// Raise or lower all streams based on this one.
if (null == lowestStream) return;
LOGD(TAG, "Lowest stream (" + lowestStream.first + '/' + lowestStream.second + ')');
// There's an odd exception where volumes can be set higher than their maximum. To avoid
// this strange issue we use the smallest of the volume and and maximum.
int newVolume = Math.min(lowestStream.first, lowestStream.second);
switch (direction) {
case AudioManager.ADJUST_RAISE:
// Lower the volume but bound to max.
newVolume = Math.min(++newVolume, lowestStream.second);
break;
case AudioManager.ADJUST_LOWER:
// Lower the volume but bound to zero.
newVolume = Math.max(--newVolume, 0);
break;
}
// Don't bother adjusting the volume if there's no change
// (and this wasn't the intent).
if (direction != AudioManager.ADJUST_SAME && lowestStream.first == newVolume) {
LOGD(TAG, "adjustVolumeSync(" + direction + ") ignored, no volume change.");
return;
}
setVolumeSync(newVolume, lowestStream.second);
}
@Override
public boolean onKey(View v, final int keyCode, KeyEvent event) {
LOGI("VolumePanel", "onKey(" + keyCode + ")");
// Don't handle ANYTHING when a call is in progress!
if (mCallState != TelephonyManager.CALL_STATE_IDLE)
return super.onKey(v, keyCode, event);
switch (keyCode) {
// Handle the DOWN + MULTIPLE action (holding down).
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
final int adjust = ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) ?
AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER);
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
// If another key was pressed while holding on to
// one volume key, we'll need to abort mission.
if (mKeyCodeDown != 0) {
mIgnoreNextKeyUp = true;
mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
return super.onKey(v, keyCode, event);
}
mKeyCodeDown = event.getKeyCode();
mKeyTimeDown = System.currentTimeMillis();
event.startTracking();
// NOTE: we'll allow long press events if we've set an action.
boolean callIdle = (mCallState == TelephonyManager.CALL_STATE_IDLE);
if (!noLongPress || hasLongPressAction(keyCode)) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(
MSG_VOLUME_LONG_PRESS, event), ((callIdle && hasLongPressAction(keyCode)) ?
mLongPressTimeout : mLongPressTimeout / 2));
}
break;
case KeyEvent.ACTION_UP:
case KeyEvent.ACTION_MULTIPLE:
boolean hasLongPress = mHandler.hasMessages(MSG_VOLUME_LONG_PRESS);
mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
boolean ignoreNextKeyUp = mIgnoreNextKeyUp;
mIgnoreNextKeyUp = false;
mKeyCodeDown = 0;
// We've been told to let this one go.
if (ignoreNextKeyUp || event.isCanceled()) {
mKeyTimeDown = 0;
return true;
}
if ((hasLongPress || noLongPress) && (System.currentTimeMillis() -
mKeyTimeDown) < mLongPressTimeout) {
mVolumeDirty = true;
mKeyTimeDown = 0;
if (!firstReveal || (firstReveal && isShowing())) {
adjustVolume(adjust);
show();
} else {
reveal();
}
}
break;
}
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
switch (event.getAction()) {
case KeyEvent.ACTION_UP:
boolean mute = isMuted(STREAM_RING);
mAudioManager.setStreamMute(STREAM_RING, !mute);
mAudioManager.setStreamMute(STREAM_NOTIFICATION, !mute);
mVolumeDirty = true;
show();
break;
}
break;
}
return super.onKey(v, keyCode, event);
}
/** A Volume {@link android.view.KeyEvent} has been long pressed. */
public void onVolumeLongPress(KeyEvent event) {
if (null == event) return;
// Set the action based on the KeyEvent.
String longPressAction = null;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
longPressAction = longPressActionUp;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
longPressAction = longPressActionDown;
break;
}
// If the event was volume up/ down, handle it. If the user is currently
// in the middle of a call, only handle volume up/ down events.
LOGI("VolumePanel", "onVolumeLongPress(" + event.getKeyCode() + ") action=" + longPressAction);
boolean callIdle = (mCallState == TelephonyManager.CALL_STATE_IDLE);
if (!TextUtils.isEmpty(longPressAction) && callIdle) {
try {
Intent action = Intent.parseUri(longPressAction, Intent.URI_INTENT_SCHEME);
startActivity(action);
} catch (URISyntaxException e) {
LOGE("VolumePanel", "Error parsing long press action as an Intent.", e);
}
} else {
// If we don't have a long press event, use this timeout as
// a key timeout for volume manipulation.
final int adjust = ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) ?
AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER);
int flags = getFlags(lastStreamType);
flags &= ~FLAG_PLAY_SOUND;
flags &= ~FLAG_VIBRATE;
// Adjust stream volume, but avoid making noises continuously.
int stream = lastStreamType;
if (hasDefaultStream()) stream = defaultStream;
boolean ringerAffected = mAudioHelper.isStreamAffectedByRingerMode(stream);
// Ringer mode transition for long-press actions
/*if (ringerAffected) {
int volume = getStreamVolume(stream);
// If we're already at silent, stop listening.
if ((adjust == ADJUST_LOWER) && (volume == 0) &&
(mRingerMode == AudioManager.RINGER_MODE_SILENT)) {
return;
} else if (adjust == ADJUST_RAISE && volume == 0) {
int nextRinger = Utils.nextRingerMode(adjust, mRingerMode);
mAudioManager.setRingerMode(nextRinger);
}
}*/
LOGI("VolumePanel", "[stream=" + stream + ", lastStream=" + lastStreamType +
", ringerAffected=" + ringerAffected + ']');
adjustSuggestedStreamVolume(adjust, stream, flags);
mVolumeDirty = true; // This is needed to show our volume change!
mIgnoreNextKeyUp = false; // This is key to avoid infinite loops!
if (!noLongPress || hasLongPressAction(event.getKeyCode())) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(
MSG_VOLUME_LONG_PRESS, event), (mLongPressTimeout / 3));
}
show();
}
}
/** Set the volume of all public AudioManager streams. */
public void adjustVolumeSync(final int direction) {
// Find the stream with the smallest max volume.
LOGD(TAG, "adjustVolumeSync(" + direction + ")");
Pair<Integer, Integer> lowestStream = null;
for (final int stream : STREAMS) {
Pair<Integer, Integer> streamPair = STREAM_VOLUMES.get(stream);
if (null != streamPair) {
if (null == lowestStream || streamPair.second < lowestStream.second)
lowestStream = streamPair;
if (streamPair.second == lowestStream.second && streamPair.first < lowestStream.first)
lowestStream = streamPair;
}
}
// Raise or lower all streams based on this one.
if (null == lowestStream) return;
LOGD(TAG, "Lowest stream (" + lowestStream.first + '/' + lowestStream.second + ')');
// There's an odd exception where volumes can be set higher than their maximum. To avoid
// this strange issue we use the smallest of the volume and and maximum.
int newVolume = Math.min(lowestStream.first, lowestStream.second);
switch (direction) {
case AudioManager.ADJUST_RAISE:
// Lower the volume but bound to max.
newVolume = Math.min(++newVolume, lowestStream.second);
break;
case AudioManager.ADJUST_LOWER:
// Lower the volume but bound to zero.
newVolume = Math.max(--newVolume, 0);
break;
}
// Don't bother adjusting the volume if there's no change
// (and this wasn't the intent).
if (direction != AudioManager.ADJUST_SAME && lowestStream.first == newVolume) {
LOGD(TAG, "adjustVolumeSync(" + direction + ") ignored, no volume change.");
return;
}
setVolumeSync(newVolume, lowestStream.second);
}
@Override
public boolean onKey(View v, final int keyCode, KeyEvent event) {
LOGI("VolumePanel", "onKey(" + keyCode + ")");
// Don't handle ANYTHING when a call is in progress!
if (mCallState != TelephonyManager.CALL_STATE_IDLE)
return super.onKey(v, keyCode, event);
switch (keyCode) {
// Handle the DOWN + MULTIPLE action (holding down).
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
final int adjust = ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) ?
AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER);
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
// If another key was pressed while holding on to
// one volume key, we'll need to abort mission.
if (mKeyCodeDown != 0) {
mIgnoreNextKeyUp = true;
mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
return super.onKey(v, keyCode, event);
}
mKeyCodeDown = event.getKeyCode();
mKeyTimeDown = System.currentTimeMillis();
event.startTracking();
// NOTE: we'll allow long press events if we've set an action.
boolean callIdle = (mCallState == TelephonyManager.CALL_STATE_IDLE);
if (!noLongPress || hasLongPressAction(keyCode)) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(
MSG_VOLUME_LONG_PRESS, event), ((callIdle && hasLongPressAction(keyCode)) ?
mLongPressTimeout : mLongPressTimeout / 2));
}
break;
case KeyEvent.ACTION_UP:
case KeyEvent.ACTION_MULTIPLE:
boolean hasLongPress = mHandler.hasMessages(MSG_VOLUME_LONG_PRESS);
mHandler.removeMessages(MSG_VOLUME_LONG_PRESS);
boolean ignoreNextKeyUp = mIgnoreNextKeyUp;
mIgnoreNextKeyUp = false;
mKeyCodeDown = 0;
// We've been told to let this one go.
if (ignoreNextKeyUp || event.isCanceled()) {
mKeyTimeDown = 0;
return true;
}
if ((hasLongPress || noLongPress) && (System.currentTimeMillis() -
mKeyTimeDown) < mLongPressTimeout) {
mVolumeDirty = true;
mKeyTimeDown = 0;
if (!firstReveal || (firstReveal && isShowing())) {
adjustVolume(adjust);
show();
} else {
reveal();
}
}
break;
}
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
switch (event.getAction()) {
case KeyEvent.ACTION_UP:
boolean mute = isMuted(STREAM_RING);
mAudioManager.setStreamMute(STREAM_RING, !mute);
mAudioManager.setStreamMute(STREAM_NOTIFICATION, !mute);
mVolumeDirty = true;
show();
break;
}
break;
}
return super.onKey(v, keyCode, event);
}
/** A Volume {@link android.view.KeyEvent} has been long pressed. */
public void onVolumeLongPress(KeyEvent event) {
if (null == event) return;
// Set the action based on the KeyEvent.
String longPressAction = null;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
longPressAction = longPressActionUp;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
longPressAction = longPressActionDown;
break;
}
// If the event was volume up/ down, handle it. If the user is currently
// in the middle of a call, only handle volume up/ down events.
LOGI("VolumePanel", "onVolumeLongPress(" + event.getKeyCode() + ") action=" + longPressAction);
boolean callIdle = (mCallState == TelephonyManager.CALL_STATE_IDLE);
if (!TextUtils.isEmpty(longPressAction) && callIdle) {
try {
Intent action = Intent.parseUri(longPressAction, Intent.URI_INTENT_SCHEME);
startActivity(action);
} catch (URISyntaxException e) {
LOGE("VolumePanel", "Error parsing long press action as an Intent.", e);
}
} else {
// If we don't have a long press event, use this timeout as
// a key timeout for volume manipulation.
final int adjust = ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) ?
AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER);
int flags = getFlags(lastStreamType);
flags &= ~FLAG_PLAY_SOUND;
flags &= ~FLAG_VIBRATE;
// Adjust stream volume, but avoid making noises continuously.
int stream = lastStreamType;
if (hasDefaultStream()) stream = defaultStream;
boolean ringerAffected = mAudioHelper.isStreamAffectedByRingerMode(stream);
// Ringer mode transition for long-press actions
/*if (ringerAffected) {
int volume = getStreamVolume(stream);
// If we're already at silent, stop listening.
if ((adjust == ADJUST_LOWER) && (volume == 0) &&
(mRingerMode == AudioManager.RINGER_MODE_SILENT)) {
return;
} else if (adjust == ADJUST_RAISE && volume == 0) {
int nextRinger = Utils.nextRingerMode(adjust, mRingerMode);
mAudioManager.setRingerMode(nextRinger);
}
}*/
LOGI("VolumePanel", "[stream=" + stream + ", lastStream=" + lastStreamType +
", ringerAffected=" + ringerAffected + ']');
adjustSuggestedStreamVolume(adjust, stream, flags);
mVolumeDirty = true; // This is needed to show our volume change!
mIgnoreNextKeyUp = false; // This is key to avoid infinite loops!
if (!noLongPress || hasLongPressAction(event.getKeyCode())) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(
MSG_VOLUME_LONG_PRESS, event), (mLongPressTimeout / 3));
}
show();
}
}