Merge "Add "vibrate when silent" mode to notifications"

This commit is contained in:
Jim Shuma 2010-03-18 16:30:33 -07:00 committed by Android (Google) Code Review
commit f7da371234
12 changed files with 125 additions and 35 deletions

View File

@ -81,6 +81,23 @@
<item>5</item>
</string-array>
<!-- The vibrate notification modes -->
<string-array name="account_settings_vibrate_when_entries">
<!-- Always -->
<item>@string/account_settings_vibrate_when_always</item>
<!-- Only when the phone is in Silent mode -->
<item>@string/account_settings_vibrate_when_silent</item>
<!-- Never -->
<item>@string/account_settings_vibrate_when_never</item>
</string-array>
<!-- The vibrate notification values -->
<string-array translatable="false" name="account_settings_vibrate_when_values">
<item>always</item>
<item>silent</item>
<item>never</item>
</string-array>
<!-- Arrays "mailbox_display_names" and "mailbox_display_icons" MUST match the order
of the types of mailboxes defined in EmailContent -->
<string-array name="mailbox_display_names" translatable="false">

View File

@ -580,10 +580,20 @@
<string name="account_settings_sync_calendar_summary">Also sync calendar from this account
</string>
<!-- On Settings screen, setting check box label -->
<string name="account_settings_vibrate_enable">Vibrate</string>
<!-- On Settings screen, setting summary text -->
<string name="account_settings_vibrate_summary">Also vibrate when email arrives</string>
<!-- On Settings screen, vibrate pop-up menu label -->
<string name="account_settings_vibrate_when_label">Vibrate</string>
<!-- On Settings screen, vibrate pop-up menu summary text -->
<string name="account_settings_vibrate_when_summary">Also vibrate when email arrives</string>
<!-- On Settings screen, vibrate pop-up menu option "always" text -->
<string name="account_settings_vibrate_when_always">Always</string>
<!-- On Settings screen, vibrate pop-up menu option "silent" text -->
<string name="account_settings_vibrate_when_silent">Only when silent</string>
<!-- On Settings screen, vibrate pop-up menu option "never" text -->
<string name="account_settings_vibrate_when_never">Never</string>
<!-- Dialog title for the Vibrate dialog -->
<string name="account_settings_vibrate_when_dlg_title">Vibrate</string>
<!-- On Settings screen, setting option name -->
<string name="account_settings_ringtone">Select ringtone</string>
<!-- On Settings screen, section heading -->

View File

@ -74,13 +74,16 @@
android:ringtoneType="notification"
android:defaultValue="content://settings/system/notification_sound" />
<CheckBoxPreference
<ListPreference
android:layout="?android:attr/preferenceLayoutChild"
android:dependency="account_notify"
android:key="account_vibrate"
android:defaultValue="false"
android:title="@string/account_settings_vibrate_enable"
android:summary="@string/account_settings_vibrate_summary" />
android:key="account_settings_vibrate_when"
android:defaultValue="never"
android:title="@string/account_settings_vibrate_when_label"
android:summary="@string/account_settings_vibrate_when_summary"
android:entries="@array/account_settings_vibrate_when_entries"
android:entryValues="@array/account_settings_vibrate_when_values"
android:dialogTitle="@string/account_settings_vibrate_when_dlg_title" />
</PreferenceCategory>

View File

@ -70,7 +70,8 @@ public class Account {
String mTrashFolderName;
String mOutboxFolderName;
int mAccountNumber;
boolean mVibrate;
boolean mVibrate; // true: Always vibrate. false: Only when mVibrateWhenSilent.
boolean mVibrateWhenSilent; // true: Vibrate even if !mVibrate. False: Require mVibrate.
String mRingtoneUri;
int mSyncWindow;
int mBackupFlags; // for account backups only
@ -95,6 +96,7 @@ public class Account {
private final String KEY_PROTOCOL_VERSION = ".protocolVersion";
private final String KEY_SECURITY_FLAGS = ".securityFlags";
private final String KEY_SIGNATURE = ".signature";
private final String KEY_VIBRATE_WHEN_SILENT = ".vibrateWhenSilent";
public Account(Context context) {
// TODO Change local store path to something readable / recognizable
@ -104,6 +106,7 @@ public class Account {
mAccountNumber = -1;
mNotifyNewMail = true;
mVibrate = false;
mVibrateWhenSilent = false;
mRingtoneUri = "content://settings/system/notification_sound";
mSyncWindow = SYNC_WINDOW_USER; // IMAP & POP3
mBackupFlags = 0;
@ -163,6 +166,8 @@ public class Account {
"Outbox");
mAccountNumber = preferences.mSharedPreferences.getInt(mUuid + ".accountNumber", 0);
mVibrate = preferences.mSharedPreferences.getBoolean(mUuid + ".vibrate", false);
mVibrateWhenSilent = preferences.mSharedPreferences.getBoolean(mUuid +
KEY_VIBRATE_WHEN_SILENT, false);
mRingtoneUri = preferences.mSharedPreferences.getString(mUuid + ".ringtone",
"content://settings/system/notification_sound");
@ -228,6 +233,14 @@ public class Account {
mVibrate = vibrate;
}
public boolean isVibrateWhenSilent() {
return mVibrateWhenSilent;
}
public void setVibrateWhenSilent(boolean vibrateWhenSilent) {
mVibrateWhenSilent = vibrateWhenSilent;
}
public String getRingtone() {
return mRingtoneUri;
}
@ -267,6 +280,7 @@ public class Account {
editor.remove(mUuid + ".outboxFolderName");
editor.remove(mUuid + ".accountNumber");
editor.remove(mUuid + ".vibrate");
editor.remove(mUuid + KEY_VIBRATE_WHEN_SILENT);
editor.remove(mUuid + ".ringtone");
editor.remove(mUuid + KEY_SYNC_WINDOW);
editor.remove(mUuid + KEY_BACKUP_FLAGS);
@ -334,6 +348,7 @@ public class Account {
editor.putString(mUuid + ".outboxFolderName", mOutboxFolderName);
editor.putInt(mUuid + ".accountNumber", mAccountNumber);
editor.putBoolean(mUuid + ".vibrate", mVibrate);
editor.putBoolean(mUuid + KEY_VIBRATE_WHEN_SILENT, mVibrateWhenSilent);
editor.putString(mUuid + ".ringtone", mRingtoneUri);
editor.putInt(mUuid + KEY_SYNC_WINDOW, mSyncWindow);
editor.putInt(mUuid + KEY_BACKUP_FLAGS, mBackupFlags);

View File

@ -604,14 +604,17 @@ public class LegacyConversions {
// Provider Account flags, and how they are mapped.
// FLAGS_NOTIFY_NEW_MAIL -> mNotifyNewMail
// FLAGS_VIBRATE -> mVibrate
// FLAGS_VIBRATE_ALWAYS -> mVibrate
// FLAGS_VIBRATE_WHEN_SILENT -> mVibrateWhenSilent
// DELETE_POLICY_NEVER -> mDeletePolicy
// DELETE_POLICY_7DAYS
// DELETE_POLICY_ON_DELETE
result.setNotifyNewMail(0 !=
(fromAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL));
result.setVibrate(0 !=
(fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE));
(fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_ALWAYS));
result.setVibrateWhenSilent(0 !=
(fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT));
result.setDeletePolicy(fromAccount.getDeletePolicy());
result.mUuid = fromAccount.getUuid();
@ -651,7 +654,9 @@ public class LegacyConversions {
// result.mHostAuthKeySend; -- will be set when object is saved
int flags = 0;
if (fromAccount.isNotifyNewMail()) flags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
if (fromAccount.isVibrate()) flags |= EmailContent.Account.FLAGS_VIBRATE;
if (fromAccount.isVibrate()) flags |= EmailContent.Account.FLAGS_VIBRATE_ALWAYS;
if (fromAccount.isVibrateWhenSilent())
flags |= EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT;
result.setFlags(flags);
result.setDeletePolicy(fromAccount.getDeletePolicy());
// result.setDefaultAccount(); -- will be set by caller, if neededf

View File

@ -34,6 +34,7 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.util.Log;
@ -388,7 +389,8 @@ public class SecurityPolicy {
String contentText = account.getDisplayName();
String ringtoneString = account.getRingtone();
Uri ringTone = (ringtoneString == null) ? null : Uri.parse(ringtoneString);
boolean vibrate = 0 != (account.mFlags & Account.FLAGS_VIBRATE);
boolean vibrate = 0 != (account.mFlags & Account.FLAGS_VIBRATE_ALWAYS);
boolean vibrateWhenSilent = 0 != (account.mFlags & Account.FLAGS_VIBRATE_WHEN_SILENT);
Intent intent = AccountSecurity.actionUpdateSecurityIntent(mContext, accountId);
PendingIntent pending =
@ -399,8 +401,13 @@ public class SecurityPolicy {
notification.setLatestEventInfo(mContext, contentTitle, contentText, pending);
// Use the account's notification rules for sound & vibrate (but always notify)
AudioManager audioManager =
(AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
boolean nowSilent =
audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
notification.sound = ringTone;
if (vibrate) {
if (vibrate || (vibrateWhenSilent && nowSilent)) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

View File

@ -51,7 +51,7 @@ public class AccountSettings extends PreferenceActivity {
private static final String PREFERENCE_FREQUENCY = "account_check_frequency";
private static final String PREFERENCE_DEFAULT = "account_default";
private static final String PREFERENCE_NOTIFY = "account_notify";
private static final String PREFERENCE_VIBRATE = "account_vibrate";
private static final String PREFERENCE_VIBRATE_WHEN = "account_settings_vibrate_when";
private static final String PREFERENCE_RINGTONE = "account_ringtone";
private static final String PREFERENCE_SERVER_CATERGORY = "account_servers";
private static final String PREFERENCE_INCOMING = "incoming";
@ -59,6 +59,11 @@ public class AccountSettings extends PreferenceActivity {
private static final String PREFERENCE_SYNC_CONTACTS = "account_sync_contacts";
private static final String PREFERENCE_SYNC_CALENDAR = "account_sync_calendar";
// These strings must match account_settings_vibrate_when_* strings in strings.xml
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS = "always";
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_SILENT = "silent";
private static final String PREFERENCE_VALUE_VIBRATE_WHEN_NEVER = "never";
// NOTE: This string must match the one in res/xml/account_preferences.xml
public static final String ACTION_ACCOUNT_MANAGER_ENTRY =
"com.android.email.activity.setup.ACCOUNT_MANAGER_ENTRY";
@ -78,7 +83,7 @@ public class AccountSettings extends PreferenceActivity {
private ListPreference mSyncWindow;
private CheckBoxPreference mAccountDefault;
private CheckBoxPreference mAccountNotify;
private CheckBoxPreference mAccountVibrate;
private ListPreference mAccountVibrateWhen;
private RingtonePreference mAccountRingtone;
private CheckBoxPreference mSyncContacts;
private CheckBoxPreference mSyncCalendar;
@ -228,9 +233,13 @@ public class AccountSettings extends PreferenceActivity {
SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences();
prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).commit();
mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE);
mAccountVibrate.setChecked(0 !=
(mAccount.getFlags() & Account.FLAGS_VIBRATE));
mAccountVibrateWhen = (ListPreference) findPreference(PREFERENCE_VIBRATE_WHEN);
boolean flagsVibrate = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_ALWAYS);
boolean flagsVibrateSilent = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_WHEN_SILENT);
mAccountVibrateWhen.setValue(
flagsVibrate ? PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS :
flagsVibrateSilent ? PREFERENCE_VALUE_VIBRATE_WHEN_SILENT :
PREFERENCE_VALUE_VIBRATE_WHEN_NEVER);
findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
@ -327,7 +336,8 @@ public class AccountSettings extends PreferenceActivity {
private void saveSettings() {
int newFlags = mAccount.getFlags() &
~(Account.FLAGS_NOTIFY_NEW_MAIL | Account.FLAGS_VIBRATE);
~(Account.FLAGS_NOTIFY_NEW_MAIL |
Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_VIBRATE_WHEN_SILENT);
mAccount.setDefaultAccount(mAccountDefault.isChecked());
mAccount.setDisplayName(mAccountDescription.getText());
@ -338,7 +348,11 @@ public class AccountSettings extends PreferenceActivity {
if (mSyncWindow != null) {
mAccount.setSyncLookback(Integer.parseInt(mSyncWindow.getValue()));
}
newFlags |= mAccountVibrate.isChecked() ? Account.FLAGS_VIBRATE : 0;
if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS)) {
newFlags |= Account.FLAGS_VIBRATE_ALWAYS;
} else if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_SILENT)) {
newFlags |= Account.FLAGS_VIBRATE_WHEN_SILENT;
}
SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences();
mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null));
mAccount.setFlags(newFlags);

View File

@ -831,11 +831,12 @@ public abstract class EmailContent {
Uri.parse(EmailContent.CONTENT_URI + "/accountIdAddToField");
public final static int FLAGS_NOTIFY_NEW_MAIL = 1;
public final static int FLAGS_VIBRATE = 2;
public final static int FLAGS_VIBRATE_ALWAYS = 2;
public static final int FLAGS_DELETE_POLICY_MASK = 4+8;
public static final int FLAGS_DELETE_POLICY_SHIFT = 2;
public static final int FLAGS_INCOMPLETE = 16;
public static final int FLAGS_SECURITY_HOLD = 32;
public static final int FLAGS_VIBRATE_WHEN_SILENT = 64;
public static final int DELETE_POLICY_NEVER = 0;
public static final int DELETE_POLICY_7DAYS = 1; // not supported
@ -1091,7 +1092,8 @@ public abstract class EmailContent {
/**
* @return the flags for this account
* @see #FLAGS_NOTIFY_NEW_MAIL
* @see #FLAGS_VIBRATE
* @see #FLAGS_VIBRATE_ALWAYS
* @see #FLAGS_VIBRATE_WHEN_SILENT
*/
public int getFlags() {
return mFlags;
@ -1100,7 +1102,8 @@ public abstract class EmailContent {
/**
* Set the flags for this account
* @see #FLAGS_NOTIFY_NEW_MAIL
* @see #FLAGS_VIBRATE
* @see #FLAGS_VIBRATE_ALWAYS
* @see #FLAGS_VIBRATE_WHEN_SILENT
* @param newFlags the new value for the flags
*/
public void setFlags(int newFlags) {

View File

@ -36,6 +36,7 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.SystemClock;
@ -417,6 +418,7 @@ public class MailService extends Service {
int syncInterval;
boolean notify;
boolean vibrate;
boolean vibrateWhenSilent;
Uri ringtoneUri;
String displayName; // temporary, for debug logging
@ -488,7 +490,8 @@ public class MailService extends Service {
report.syncInterval = syncInterval;
report.notify = (flags & Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
report.vibrate = (flags & Account.FLAGS_VIBRATE) != 0;
report.vibrate = (flags & Account.FLAGS_VIBRATE_ALWAYS) != 0;
report.vibrateWhenSilent = (flags & Account.FLAGS_VIBRATE_WHEN_SILENT) != 0;
report.ringtoneUri = (ringtoneString == null) ? null
: Uri.parse(ringtoneString);
@ -637,6 +640,7 @@ public class MailService extends Service {
private void notifyNewMessages(long accountId) {
boolean notify = false;
boolean vibrate = false;
boolean vibrateWhenSilent = false;
Uri ringtone = null;
int accountsWithNewMessages = 0;
int numNewMessages = 0;
@ -651,6 +655,7 @@ public class MailService extends Service {
if (report.accountId == accountId) {
notify = report.notify;
vibrate = report.vibrate;
vibrateWhenSilent = report.vibrateWhenSilent;
ringtone = report.ringtoneUri;
reportName = report.displayName;
}
@ -694,8 +699,11 @@ public class MailService extends Service {
pending);
notification.sound = ringtone;
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
boolean nowSilent = audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
// Use same code here as in Gmail and GTalk for vibration
if (vibrate) {
if (vibrate || (vibrateWhenSilent && nowSilent)) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}

View File

@ -317,6 +317,7 @@ public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider>
backup.mOutboxFolderName = "outbox " + name;
backup.mAccountNumber = 300;
backup.mVibrate = true;
backup.mVibrateWhenSilent = false;
backup.mRingtoneUri = "ringtone://test/" + name;
backup.mSyncWindow = 400;
backup.mBackupFlags = Account.BACKUP_FLAGS_IS_BACKUP;

View File

@ -650,6 +650,7 @@ public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> {
mLegacyAccount.mOutboxFolderName = "outbox " + name;
mLegacyAccount.mAccountNumber = 300;
mLegacyAccount.mVibrate = true;
mLegacyAccount.mVibrateWhenSilent = false;
mLegacyAccount.mRingtoneUri = "ringtone://test/" + name;
mLegacyAccount.mSyncWindow = 400;
mLegacyAccount.mBackupFlags = 0;
@ -679,7 +680,9 @@ public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> {
// Synthesize & check flags
int expectFlags = 0;
if (expect.mNotifyNewMail) expectFlags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
if (expect.mVibrate) expectFlags |= EmailContent.Account.FLAGS_VIBRATE;
if (expect.mVibrate) expectFlags |= EmailContent.Account.FLAGS_VIBRATE_ALWAYS;
if (expect.mVibrateWhenSilent)
expectFlags |= EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT;
expectFlags |=
(expect.mDeletePolicy << EmailContent.Account.FLAGS_DELETE_POLICY_SHIFT)
& EmailContent.Account.FLAGS_DELETE_POLICY_MASK;
@ -720,8 +723,12 @@ public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> {
assertEquals(tag + " trash", null, actual.mTrashFolderName);
assertEquals(tag + " outbox", null, actual.mOutboxFolderName);
assertEquals(tag + " acct #", -1, actual.mAccountNumber);
assertEquals(tag + " vibrate", (expectFlags & EmailContent.Account.FLAGS_VIBRATE) != 0,
assertEquals(tag + " vibrate",
(expectFlags & EmailContent.Account.FLAGS_VIBRATE_ALWAYS) != 0,
actual.mVibrate);
assertEquals(tag + " vibrateSilent",
(expectFlags & EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT) != 0,
actual.mVibrateWhenSilent);
assertEquals(tag + " ", expect.getRingtone(), actual.mRingtoneUri);
assertEquals(tag + " sync window", expect.getSyncLookback(), actual.mSyncWindow);
assertEquals(tag + " backup flags", 0, actual.mBackupFlags);

View File

@ -252,7 +252,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
a1.mFlags = Account.FLAGS_NOTIFY_NEW_MAIL;
a1.save(mMockContext);
Account a2 = ProviderTestUtils.setupAccount("holdflag-2", false, mMockContext);
a2.mFlags = Account.FLAGS_VIBRATE | Account.FLAGS_SECURITY_HOLD;
a2.mFlags = Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD;
a2.save(mMockContext);
// confirm clear until set
@ -265,11 +265,11 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
// confirm set until cleared
Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId);
assertEquals(Account.FLAGS_VIBRATE | Account.FLAGS_SECURITY_HOLD, a2a.mFlags);
assertEquals(Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD, a2a.mFlags);
sp.setAccountHoldFlag(a2, false);
assertEquals(Account.FLAGS_VIBRATE, a2.mFlags);
assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2.mFlags);
Account a2b = Account.restoreAccountWithId(mMockContext, a2.mId);
assertEquals(Account.FLAGS_VIBRATE, a2b.mFlags);
assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2b.mFlags);
}
/**
@ -282,7 +282,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
a1.mFlags = Account.FLAGS_NOTIFY_NEW_MAIL;
a1.save(mMockContext);
Account a2 = ProviderTestUtils.setupAccount("holdflag-2", false, mMockContext);
a2.mFlags = Account.FLAGS_VIBRATE | Account.FLAGS_SECURITY_HOLD;
a2.mFlags = Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD;
a2.save(mMockContext);
// bulk clear
@ -292,7 +292,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
Account a1a = Account.restoreAccountWithId(mMockContext, a1.mId);
assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL, a1a.mFlags);
Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId);
assertEquals(Account.FLAGS_VIBRATE, a2a.mFlags);
assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2a.mFlags);
}
/**