introduced expert options to set status to away and xa if screen is off or if phone is silenced

This commit is contained in:
Daniel Gultsch 2015-10-08 00:35:04 +02:00
parent 043e19dd65
commit 52a5e72b02
7 changed files with 122 additions and 28 deletions

View File

@ -30,6 +30,7 @@
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" /> <action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter> </intent-filter>
</receiver> </receiver>

View File

@ -2,6 +2,7 @@ package eu.siacs.conversations.generator;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
@ -36,8 +37,22 @@ public class PresenceGenerator extends AbstractGenerator {
return subscription("subscribed", contact); return subscription("subscribed", contact);
} }
public PresencePacket sendPresence(Account account) { public PresencePacket selfPresence(Account account, int presence) {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
switch(presence) {
case Presences.AWAY:
packet.addChild("show").setContent("away");
break;
case Presences.XA:
packet.addChild("show").setContent("xa");
break;
case Presences.CHAT:
packet.addChild("show").setContent("chat");
break;
case Presences.DND:
packet.addChild("show").setContent("dnd");
break;
}
packet.setFrom(account.getJid()); packet.setFrom(account.getJid());
String sig = account.getPgpSignature(); String sig = account.getPgpSignature();
if (sig != null) { if (sig != null) {

View File

@ -115,29 +115,13 @@ public class NotificationService {
return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false); return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false);
} }
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private boolean isInteractive() {
final PowerManager pm = (PowerManager) mXmppConnectionService
.getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
return isScreenOn;
}
public void push(final Message message) { public void push(final Message message) {
mXmppConnectionService.updateUnreadCountBadge(); mXmppConnectionService.updateUnreadCountBadge();
if (!notify(message)) { if (!notify(message)) {
return; return;
} }
final boolean isScreenOn = isInteractive(); final boolean isScreenOn = mXmppConnectionService.isInteractive();
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) { if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) {
return; return;

View File

@ -6,13 +6,16 @@ import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.media.AudioManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.FileObserver; import android.os.FileObserver;
import android.os.IBinder; import android.os.IBinder;
@ -61,6 +64,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener; import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.entities.TransferablePlaceholder;
import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.generator.IqGenerator;
@ -313,6 +317,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private PowerManager pm; private PowerManager pm;
private LruCache<String, Bitmap> mBitmapCache; private LruCache<String, Bitmap> mBitmapCache;
private Thread mPhoneContactMergerThread; private Thread mPhoneContactMergerThread;
private EventReceiver mEventReceiver = new EventReceiver();
private boolean mRestoredFromDatabase = false; private boolean mRestoredFromDatabase = false;
public boolean areMessagesInitialized() { public boolean areMessagesInitialized() {
@ -444,6 +449,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final String action = intent == null ? null : intent.getAction(); final String action = intent == null ? null : intent.getAction();
boolean interactive = false; boolean interactive = false;
if (action != null) { if (action != null) {
Log.d(Config.LOGTAG,"action: "+action);
switch (action) { switch (action) {
case ConnectivityManager.CONNECTIVITY_ACTION: case ConnectivityManager.CONNECTIVITY_ACTION:
if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) { if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
@ -483,6 +489,17 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
break; break;
} }
break; break;
case AudioManager.RINGER_MODE_CHANGED_ACTION:
if (xaOnSilentMode()) {
refreshAllPresences();
}
break;
case Intent.ACTION_SCREEN_OFF:
case Intent.ACTION_SCREEN_ON:
if (awayWhenScreenOff()) {
refreshAllPresences();
}
break;
} }
} }
this.wakeLock.acquire(); this.wakeLock.acquire();
@ -544,10 +561,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
} }
/*PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (!pm.isScreenOn()) {
removeStaleListeners();
}*/
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
try { try {
wakeLock.release(); wakeLock.release();
@ -557,6 +570,43 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return START_STICKY; return START_STICKY;
} }
private boolean xaOnSilentMode() {
return getPreferences().getBoolean("xa_on_silent_mode", false);
}
private boolean awayWhenScreenOff() {
return getPreferences().getBoolean("away_when_screen_off", false);
}
private int getTargetPresence() {
if (xaOnSilentMode() && isPhoneSilenced()) {
return Presences.XA;
} else if (awayWhenScreenOff() && !isInteractive()) {
return Presences.AWAY;
} else {
return Presences.ONLINE;
}
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public boolean isInteractive() {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
return isScreenOn;
}
private boolean isPhoneSilenced() {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
}
private void resetAllAttemptCounts(boolean reallyAll) { private void resetAllAttemptCounts(boolean reallyAll) {
Log.d(Config.LOGTAG,"resetting all attepmt counts"); Log.d(Config.LOGTAG,"resetting all attepmt counts");
for(Account account : accounts) { for(Account account : accounts) {
@ -599,6 +649,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
this.fileObserver.startWatching(); this.fileObserver.startWatching();
this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain"); this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService(); this.pgpServiceConnection.bindToService();
@ -606,6 +657,23 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"XmppConnectionService"); this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"XmppConnectionService");
toggleForegroundService(); toggleForegroundService();
updateUnreadCountBadge(); updateUnreadCountBadge();
toggleScreenEventReceiver();
}
@Override
public void onDestroy() {
unregisterReceiver(this.mEventReceiver);
super.onDestroy();
}
public void toggleScreenEventReceiver() {
if (awayWhenScreenOff()) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(this.mEventReceiver, filter);
} else {
unregisterReceiver(this.mEventReceiver);
}
} }
public void toggleForegroundService() { public void toggleForegroundService() {
@ -2502,7 +2570,15 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void sendPresence(final Account account) { public void sendPresence(final Account account) {
sendPresencePacket(account, mPresenceGenerator.sendPresence(account)); sendPresencePacket(account, mPresenceGenerator.selfPresence(account, getTargetPresence()));
}
public void refreshAllPresences() {
for (Account account : getAccounts()) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
sendPresence(account);
}
}
} }
public void sendOfflinePresence(final Account account) { public void sendOfflinePresence(final Account account) {

View File

@ -148,13 +148,14 @@ public class SettingsActivity extends XmppActivity implements
} }
} else if (name.equals("keep_foreground_service")) { } else if (name.equals("keep_foreground_service")) {
xmppConnectionService.toggleForegroundService(); xmppConnectionService.toggleForegroundService();
} else if (name.equals("confirm_messages")) { } else if (name.equals("confirm_messages")
|| name.equals("xa_on_silent_mode")
|| name.equals("away_when_screen_off")) {
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
for (Account account : xmppConnectionService.getAccounts()) { if (name.equals("away_when_screen_off")) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) { xmppConnectionService.toggleScreenEventReceiver();
xmppConnectionService.sendPresence(account);
}
} }
xmppConnectionService.refreshAllPresences();
} }
} else if (name.equals("dont_trust_system_cas")) { } else if (name.equals("dont_trust_system_cas")) {
xmppConnectionService.updateMemorizingTrustmanager(); xmppConnectionService.updateMemorizingTrustmanager();

View File

@ -519,4 +519,9 @@
<string name="pref_use_white_background_summary">Show received messages as black text on a white background</string> <string name="pref_use_white_background_summary">Show received messages as black text on a white background</string>
<string name="account_status_dns_timeout">Timeout in DNS</string> <string name="account_status_dns_timeout">Timeout in DNS</string>
<string name="server_info_broken">Broken</string> <string name="server_info_broken">Broken</string>
<string name="pref_presence_settings">Presence settings</string>
<string name="pref_away_when_screen_off">Away when screen is off</string>
<string name="pref_away_when_screen_off_summary">Marks your resource as away when the screen is turned off</string>
<string name="pref_xa_on_silent_mode">Not available in silent mode</string>
<string name="pref_xa_on_silent_mode_summary">Marks your resource as not available when phone is in silent mode</string>
</resources> </resources>

View File

@ -159,6 +159,18 @@
android:summary="@string/pref_display_enter_key_summary" android:summary="@string/pref_display_enter_key_summary"
android:title="@string/pref_display_enter_key"/> android:title="@string/pref_display_enter_key"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_presence_settings">
<CheckBoxPreference
android:defaultValue="false"
android:key="away_when_screen_off"
android:summary="@string/pref_away_when_screen_off_summary"
android:title="@string/pref_away_when_screen_off"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="xa_on_silent_mode"
android:summary="@string/pref_xa_on_silent_mode_summary"
android:title="@string/pref_xa_on_silent_mode"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_expert_options_other"> <PreferenceCategory android:title="@string/pref_expert_options_other">
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"