Defer persisting master data to avoid excessive database writes
Bug: 5705192
Signed-off-by: Mike Lockwood <lockwood@google.com>
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java
index e41f7fd..74cad6f 100644
--- a/media/java/android/media/AudioService.java
+++ b/media/java/android/media/AudioService.java
@@ -112,6 +112,7 @@
// AudioHandler message.whats
private static final int MSG_SET_DEVICE_VOLUME = 0;
private static final int MSG_PERSIST_VOLUME = 1;
+ private static final int MSG_PERSIST_MASTER_VOLUME = 2;
private static final int MSG_PERSIST_RINGER_MODE = 3;
private static final int MSG_PERSIST_VIBRATE_SETTING = 4;
private static final int MSG_MEDIA_SERVER_DIED = 5;
@@ -638,9 +639,9 @@
if (volume < 0.0f) volume = 0.0f;
}
AudioSystem.setMasterVolume(volume);
- long origCallerIdentityToken = Binder.clearCallingIdentity();
- Settings.System.putFloat(mContentResolver, Settings.System.VOLUME_MASTER, volume);
- Binder.restoreCallingIdentity(origCallerIdentityToken);
+ // Post a persist master volume msg
+ sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME, 0, SENDMSG_REPLACE,
+ Math.round(volume * (float)1000.0), 0, null, PERSIST_DELAY);
sendMasterVolumeUpdate(flags, oldVolume, getMasterVolume());
}
}
@@ -813,7 +814,23 @@
}
public void setMasterVolume(int volume, int flags) {
- AudioSystem.setMasterVolume((float)volume / MAX_MASTER_VOLUME);
+ doSetMasterVolume((float)volume / MAX_MASTER_VOLUME, flags);
+ }
+
+ private void doSetMasterVolume(float volume, int flags) {
+ // don't allow changing master volume when muted
+ if (!AudioSystem.getMasterMute()) {
+ int oldVolume = getMasterVolume();
+ AudioSystem.setMasterVolume(volume);
+
+ int newVolume = getMasterVolume();
+ if (newVolume != oldVolume) {
+ // Post a persist master volume msg
+ sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME, SENDMSG_REPLACE,
+ Math.round(volume * (float)1000.0), 0, null, PERSIST_DELAY);
+ sendMasterVolumeUpdate(flags, oldVolume, newVolume);
+ }
+ }
}
/** @see AudioManager#getStreamMaxVolume(int) */
@@ -2447,6 +2464,11 @@
persistVolume((VolumeStreamState) msg.obj, msg.arg1, msg.arg2);
break;
+ case MSG_PERSIST_MASTER_VOLUME:
+ Settings.System.putFloat(mContentResolver, Settings.System.VOLUME_MASTER,
+ (float)msg.arg1 / (float)1000.0);
+ break;
+
case MSG_PERSIST_RINGER_MODE:
// note that the value persisted is the current ringer mode, not the
// value of ringer mode as of the time the request was made to persist