修改默认StreamType简单记录
2021-03-22 本文已影响0人
梧叶已秋声
首先,修改返回的StreamType
。默认的是DEFAULT_VOL_STREAM_NO_PLAYBACK
,这里定义为AudioSystem.STREAM_MUSIC
,可以直接修改DEFAULT_VOL_STREAM_NO_PLAYBACK
的定义,也可以直接返回AudioSystem.STREAM_RING
。
//frameworks/base/services/core/java/com/android/server/audio/AudioService.java
private int getActiveStreamType(int suggestedStreamType) {
...........
} else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {
...........
return AudioSystem.STREAM_RING;//DEFAULT_VOL_STREAM_NO_PLAYBACK;
...........
}
当声音为0时,system ui 的bar上添加一个铃声为0的图标。
AndroidManifest中添加定义。
//frameworks/base/core/res/AndroidManifest.xml
<protected-broadcast android:name="android.intent.action.SILENT" />
<protected-broadcast android:name="android.intent.action.NORMAL" />
PhoneStatusBarPolicy中监听。
//vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
public void init() {
// listen for broadcasts
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SILENT");
filter.addAction("android.intent.action.NORMAL");
filter.addAction(AudioManager.ACTION_HEADSET_PLUG);
filter.addAction(AudioManager.ACTION_BASEMCU_PLUG);
...........
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case "android.intent.action.SILENT":
Log.d(TAG,"SILENT");
mIconController.setIcon(mSlotVolume, R.drawable.stat_sys_ringer_silent, null);
mIconController.setIconVisibility(mSlotVolume, true);
break;
case "android.intent.action.NORMAL":
Log.d(TAG,"NORMAL");
mIconController.setIconVisibility(mSlotVolume, false);
break;
case Intent.ACTION_SIM_STATE_CHANGED:
// Avoid rebroadcast because SysUI is direct boot aware.
if (intent.getBooleanExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
break;
...........
}
按下声音按键 后,会交过VolumeDialogImpl
去处理。把STREAM_MUSIC
的row
换成STREAM_RING
。
//vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
addRow(AudioManager.STREAM_MUSIC,
R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true, true);
if (!AudioSystem.isSingleVolume(mContext)) {
addRow(AudioManager.STREAM_RING,
R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true, false);
改成
addRow(AudioManager.STREAM_RING,
R.drawable.ic_volume_ringer, R.drawable.ic_volume_ringer_mute, true, true);
if (!AudioSystem.isSingleVolume(mContext)) {
addRow(AudioManager.STREAM_MUSIC,
R.drawable.ic_volume_media, R.drawable.ic_volume_media_mute, true, false);
private VolumeRow getActiveRow() {
for (VolumeRow row : mRows) {
if (row.stream == mActiveStream) {
return row;
}
}
for (VolumeRow row : mRows) {
if (row.stream == STREAM_MUSIC) {
return row;
}
}
return mRows.get(0);
}
STREAM_MUSIC改成STREAM_RING。具体如下。
if (row.stream == STREAM_RING/*STREAM_MUSIC*/) {
最后,当音量为0时发送广播。
//vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
private void updateVolumeRowH(VolumeRow row) {
.................
updateVolumeRowSliderH(row, enableSlider, vlevel);
if (isRingStream){
if(vlevel == 0){
Intent intent = new Intent();
intent.setAction("android.intent.action.SILENT");
mContext.sendBroadcast(intent);
}else {
Intent intent = new Intent();
intent.setAction("android.intent.action.NORMAL");
mContext.sendBroadcast(intent);
}
}
}