If you think the Android project Tacere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (c) 2014 Jonathan Nelson//www.java2s.com
* Released under the BSD license. For details see the COPYING file.
*/package org.ciasaboark.tacere.manager;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import org.ciasaboark.tacere.prefs.Prefs;
publicclass VolumesManager {
privatestaticfinalint MAX_MEDIA_VOLUME = 15;
privatestaticfinalint MAX_ALARM_VOLUME = 7;
privatestaticfinal String TAG = "VolumesManager";
private Context context;
private Prefs prefs;
public VolumesManager(Context ctx) {
this.context = ctx;
this.prefs = new Prefs(ctx);
}
publicstaticint getMaxMediaVolume() {
return MAX_MEDIA_VOLUME;
}
publicstaticint getMaxAlarmVolume() {
return MAX_ALARM_VOLUME;
}
publicvoid restoreVolumes() {
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
restoreAlarmVolume();
restoreMediaVolume();
clearStoredVolumes();
}
privatevoid restoreAlarmVolume() {
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int storedAlarmVolume = prefs.getStoredAlarmVolume();
if (!hasAlarmVolumeBeenStored()) {
Log.w(TAG, "asked to restore alarm volume when none has been previously stored");
} else {
audio.setStreamVolume(AudioManager.STREAM_ALARM, storedAlarmVolume, 0);
}
}
privatevoid restoreMediaVolume() {
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int storedMediaVolume = prefs.getStoredMediaVolume();
if (!hasMediaVolumeBeenStored()) {
Log.w(TAG, "asked to restore media volume when none has been previously stored");
} else {
audio.setStreamVolume(AudioManager.STREAM_MUSIC, storedMediaVolume, 0);
}
}
publicvoid clearStoredVolumes() {
prefs.storeAlarmVolume(-1);
prefs.storeMediaVolume(-1);
}
privateboolean hasAlarmVolumeBeenStored() {
return getStoredAlarmVolume() != -1;
}
privateboolean hasMediaVolumeBeenStored() {
return getStoredMediaVolume() != -1;
}
privateint getStoredAlarmVolume() {
return prefs.getStoredAlarmVolume();
}
privateint getStoredMediaVolume() {
return prefs.getStoredMediaVolume();
}
publicvoid silenceMediaAndAlarmVolumesIfNeeded() {
// change media volume, and alarm volume
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (prefs.shouldMediaVolumeBeSilenced()) {
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
} else {
restoreMediaVolume();
}
if (prefs.shouldAlarmVolumeBeSilenced()) {
audio.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
} else {
restoreAlarmVolume();
}
}
publicvoid storeVolumesIfNeeded() {
//store the current media and alarm volumes if none are already set
if (!hasMediaVolumeBeenStored() && prefs.shouldMediaVolumeBeSilenced()) {
storeMediaVolume();
}
if (!hasAlarmVolumeBeenStored() && prefs.shouldAlarmVolumeBeSilenced()) {
storeAlarmVolume();
}
}
privatevoid storeMediaVolume() {
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int mediaVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
prefs.storeMediaVolume(mediaVolume);
}
privatevoid storeAlarmVolume() {
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int alarmVolume = audio.getStreamVolume(AudioManager.STREAM_ALARM);
prefs.storeAlarmVolume(alarmVolume);
}
}