diff --git a/res/layout/volume_preference.xml b/res/layout/volume_preference.xml new file mode 100644 index 0000000..9d6fade --- /dev/null +++ b/res/layout/volume_preference.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/menu/profile_list_context.xml b/res/menu/profile_list_context.xml new file mode 100644 index 0000000..47f297f --- /dev/null +++ b/res/menu/profile_list_context.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 06f93d0..0aea73b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -74,6 +74,7 @@ Ringer Mode Wifi Enabled + Keep previous Normal Silent Vibrate only diff --git a/res/values/values.xml b/res/values/values.xml index 28ecaf3..c7361b5 100644 --- a/res/values/values.xml +++ b/res/values/values.xml @@ -42,6 +42,7 @@ + "-1" "2" "0" "1" diff --git a/src/com/github/nutomic/pegasus/LocationService.java b/src/com/github/nutomic/pegasus/LocationService.java index 66e9341..51b5453 100644 --- a/src/com/github/nutomic/pegasus/LocationService.java +++ b/src/com/github/nutomic/pegasus/LocationService.java @@ -252,23 +252,35 @@ public class LocationService extends Service { WifiManager wm = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); - am.setStreamVolume(AudioManager.STREAM_RING, - c.getInt(c.getColumnIndex(ProfileColumns.RINGTONE_VOLUME)), - AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + // Value smaller zero means the volume should not change. + if (c.getInt(c.getColumnIndex(ProfileColumns.RINGTONE_VOLUME)) >= 0) { + am.setStreamVolume(AudioManager.STREAM_RING, + c.getInt(c.getColumnIndex(ProfileColumns.RINGTONE_VOLUME)), + AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + } - am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, - c.getInt(c.getColumnIndex(ProfileColumns.NOTIFICATION_VOLUME)), - AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + if (c.getInt(c.getColumnIndex(ProfileColumns.NOTIFICATION_VOLUME)) >= 0) { + am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, + c.getInt(c.getColumnIndex(ProfileColumns.NOTIFICATION_VOLUME)), + AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + } - am.setStreamVolume(AudioManager.STREAM_MUSIC, - c.getInt(c.getColumnIndex(ProfileColumns.MEDIA_VOLUME)), - AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + if (c.getInt(c.getColumnIndex(ProfileColumns.MEDIA_VOLUME)) >= 0) { + am.setStreamVolume(AudioManager.STREAM_MUSIC, + c.getInt(c.getColumnIndex(ProfileColumns.MEDIA_VOLUME)), + AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + } - am.setStreamVolume(AudioManager.STREAM_ALARM, - c.getInt(c.getColumnIndex(ProfileColumns.ALARM_VOLUME)), - AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + if (c.getInt(c.getColumnIndex(ProfileColumns.ALARM_VOLUME)) >= 0) { + am.setStreamVolume(AudioManager.STREAM_ALARM, + c.getInt(c.getColumnIndex(ProfileColumns.ALARM_VOLUME)), + AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); + } - am.setRingerMode(c.getInt(c.getColumnIndex(ProfileColumns.RINGER_MODE))); + if (c.getInt(c.getColumnIndex(ProfileColumns.RINGER_MODE)) != + ProfileColumns.RINGER_MODE_KEEP) { + am.setRingerMode(c.getInt(c.getColumnIndex(ProfileColumns.RINGER_MODE))); + } wm.setWifiEnabled((c.getInt(c.getColumnIndex(ProfileColumns.WIFI_ENABLED)) == 0) ? true : false); diff --git a/src/com/github/nutomic/pegasus/VolumePreference.java b/src/com/github/nutomic/pegasus/VolumePreference.java index 8ddcb73..8dfa0e4 100644 --- a/src/com/github/nutomic/pegasus/VolumePreference.java +++ b/src/com/github/nutomic/pegasus/VolumePreference.java @@ -16,23 +16,35 @@ package com.github.nutomic.pegasus; +import android.app.AlertDialog; import android.content.Context; -import android.preference.DialogPreference; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.preference.Preference; import android.util.AttributeSet; +import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.SeekBar; +import com.github.nutomic.pegasus.content.ProfileColumns; + /** - * Preference that opens a SeekBar dialog. + * Preference that opens a SeekBar dialog and has a CheckBox, only if the + * CheckBox is checked is the value applied (but it is always saved). * * @author Felix Ableitner * */ -public class VolumePreference extends DialogPreference { +public class VolumePreference extends Preference implements + OnCheckedChangeListener, android.view.View.OnClickListener { - private int mMax; + private CheckBox mCheckBox; private int mProgress; - private SeekBar mSeekBar; + private int mMax; /** * Initialize layout, set Preference not persistent- @@ -41,20 +53,23 @@ public class VolumePreference extends DialogPreference { super(context, attrs); setPersistent(false); - setDialogLayoutResource(R.layout.seekbar_dialog); - - setPositiveButtonText(android.R.string.ok); - setNegativeButtonText(android.R.string.cancel); + setLayoutResource(R.layout.volume_preference); } - + + /** + * Initialize OnClickListeners. + */ @Override - protected void onBindDialogView(View view) { - super.onBindDialogView(view); - - mSeekBar = (SeekBar) view.findViewById(R.id.seekbar); + public View getView(View convertView, ViewGroup parent) { + View view = super.getView(convertView, parent); - mSeekBar.setProgress(mProgress); - mSeekBar.setMax(mMax); + View layout = view.findViewById(R.id.text_layout); + layout.setOnClickListener(this); + + mCheckBox = (CheckBox) view.findViewById(R.id.profile_checkbox); + mCheckBox.setOnCheckedChangeListener(this); + + return view; } /** @@ -62,30 +77,71 @@ public class VolumePreference extends DialogPreference { */ @Override public void setDefaultValue(Object defaultValue) { - mProgress = (Integer) defaultValue; - if (mSeekBar != null) { - mSeekBar.setProgress((Integer) defaultValue); - } + mProgress = (Integer) defaultValue; + if (mProgress >= 0) { + mCheckBox.setChecked(true); + } + else { + mCheckBox.setChecked(false); + // Set progress to the actual volume value. + mProgress += ProfileColumns.VOLUME_APPLY_FALSE; + } } /** * Set the maximum value for the SeekBar. */ public void setMaxValue(int max) { - mMax = max; - if (mSeekBar != null) { - mSeekBar.setMax(max); - } + mMax = max; } - - /** - * Send new value to listener (if positive button was clicked). - */ + + /** + * CheckBox value was changed. + */ @Override - protected void onDialogClosed(boolean positiveResult) { - if (positiveResult) { - mProgress = mSeekBar.getProgress(); - callChangeListener(mProgress); - } + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + callChangeListener(); } + + /** + * Calls the change listener for this preference, with VOLUME_APPLY_FALSE + * subtracted if the CheckBox is unchecked. + */ + private void callChangeListener() { + callChangeListener((mCheckBox.isChecked()) + ? mProgress + : mProgress - ProfileColumns.VOLUME_APPLY_FALSE); + } + + /** + * Left part (text) clicked, show volume dialog. + */ + @Override + public void onClick(View v) { + // Initialize dialog layout. + LayoutInflater inflater = (LayoutInflater) getContext() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.seekbar_dialog, null); + final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar); + seekBar.setMax(mMax); + seekBar.setProgress((mProgress >= 0) + ? mProgress + : mProgress + ProfileColumns.VOLUME_APPLY_FALSE); + + // Create and show dialog. + new AlertDialog.Builder(getContext()) + .setTitle(getTitle()) + .setView(view) + .setPositiveButton(android.R.string.ok, new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + mProgress = seekBar.getProgress(); + callChangeListener(); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + } + } \ No newline at end of file diff --git a/src/com/github/nutomic/pegasus/content/Database.java b/src/com/github/nutomic/pegasus/content/Database.java index aa79c3b..0007d74 100644 --- a/src/com/github/nutomic/pegasus/content/Database.java +++ b/src/com/github/nutomic/pegasus/content/Database.java @@ -75,26 +75,27 @@ public class Database extends SQLiteOpenHelper { db.execSQL(CellColumns.CREATE_TABLE); db.execSQL(CellLogColumns.CREATE_TABLE); - // Insert "Normal" profile. + // Insert "Normal" profile. Does not change any settings by default. ContentValues cv = new ContentValues(); cv.put(ProfileColumns.NAME, mContext.getResources() .getString(R.string.sql_profile_normal)); - cv.put(ProfileColumns.RINGTONE_VOLUME, 5); - cv.put(ProfileColumns.NOTIFICATION_VOLUME, 5); - cv.put(ProfileColumns.MEDIA_VOLUME, 9); - cv.put(ProfileColumns.ALARM_VOLUME, 5); + cv.put(ProfileColumns.RINGTONE_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE); + cv.put(ProfileColumns.NOTIFICATION_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE); + cv.put(ProfileColumns.MEDIA_VOLUME, 9 - ProfileColumns.VOLUME_APPLY_FALSE); + cv.put(ProfileColumns.ALARM_VOLUME, 5 - ProfileColumns.VOLUME_APPLY_FALSE); cv.put(ProfileColumns.WIFI_ENABLED, true); - cv.put(ProfileColumns.RINGER_MODE, AudioManager.RINGER_MODE_NORMAL); + cv.put(ProfileColumns.RINGER_MODE, ProfileColumns.RINGER_MODE_KEEP); long normal = db.insert(ProfileColumns.TABLE_NAME, null, cv); - // Insert "Silent" profile. + // Insert "Silent" profile. Changes ringtone and notification + // volume by default and enables vibration. cv = new ContentValues(); cv.put(ProfileColumns.NAME, mContext.getResources() .getString(R.string.sql_profile_silent)); cv.put(ProfileColumns.RINGTONE_VOLUME, 0); cv.put(ProfileColumns.NOTIFICATION_VOLUME, 0); - cv.put(ProfileColumns.MEDIA_VOLUME, 0); - cv.put(ProfileColumns.ALARM_VOLUME, 0); + cv.put(ProfileColumns.MEDIA_VOLUME, 0 - ProfileColumns.VOLUME_APPLY_FALSE); + cv.put(ProfileColumns.ALARM_VOLUME, 0 - ProfileColumns.VOLUME_APPLY_FALSE); cv.put(ProfileColumns.WIFI_ENABLED, true); cv.put(ProfileColumns.RINGER_MODE, AudioManager.RINGER_MODE_VIBRATE); long silent = db.insert(ProfileColumns.TABLE_NAME, null, cv); diff --git a/src/com/github/nutomic/pegasus/content/ProfileColumns.java b/src/com/github/nutomic/pegasus/content/ProfileColumns.java index 9742856..d737963 100644 --- a/src/com/github/nutomic/pegasus/content/ProfileColumns.java +++ b/src/com/github/nutomic/pegasus/content/ProfileColumns.java @@ -48,5 +48,17 @@ public class ProfileColumns implements BaseColumns { WIFI_ENABLED + " INTEGER," + RINGER_MODE + " INTEGER" + ");"; + + /** + * This value is subtracted from any volume value if it should not + * be applied (only saved). Generally allows for easy testing by + * checking if the value is smaller than zero. + */ + public static final int VOLUME_APPLY_FALSE = 100; + + /** + * Value to indicate that AlarmManager.setRingerMode should not be used. + */ + public static final int RINGER_MODE_KEEP = -1; }