Commit e9a90014 authored by Grigory Fedorov's avatar Grigory Fedorov

Implementation of a tri-state option for contact specific notifications #344

New ShowMessageTextInNotification enum. Database migration was not done. Enum stored as an integer instead of boolean.
parent d83d20c5
......@@ -14,9 +14,6 @@
*/
package com.xabber.android.data.message.chat;
import java.util.HashSet;
import java.util.Set;
import android.database.Cursor;
import android.net.Uri;
......@@ -28,6 +25,9 @@ import com.xabber.android.data.account.OnAccountRemovedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.entity.NestedMap;
import java.util.HashSet;
import java.util.Set;
/**
* Manage chat specific options.
*
......@@ -39,63 +39,57 @@ public class ChatManager implements OnLoadListener, OnAccountRemovedListener {
.parse("com.xabber.android.data.message.ChatManager.EMPTY_SOUND");
private static final Object PRIVATE_CHAT = new Object();
private final static ChatManager instance;
static {
instance = new ChatManager();
Application.getInstance().addManager(instance);
}
/**
* Stored input for user in account.
*/
private final NestedMap<ChatInput> chatInputs;
/**
* List of chats whose messages mustn't be saved for user in account.
*/
private final NestedMap<Object> privateChats;
/**
* Whether notification in visible chat should be used for user in account.
*/
private final NestedMap<Boolean> notifyVisible;
/**
* Whether text of incoming message should be shown in notification bar for
* user in account.
*/
private final NestedMap<Boolean> showText;
private final NestedMap<ShowMessageTextInNotification> showText;
/**
* Whether vibro notification should be used for user in account.
*/
private final NestedMap<Boolean> makeVibro;
/**
* Sound, associated with chat for user in account.
*/
private final NestedMap<Uri> sounds;
private final static ChatManager instance;
static {
instance = new ChatManager();
Application.getInstance().addManager(instance);
}
public static ChatManager getInstance() {
return instance;
}
private ChatManager() {
chatInputs = new NestedMap<ChatInput>();
privateChats = new NestedMap<Object>();
sounds = new NestedMap<Uri>();
showText = new NestedMap<Boolean>();
showText = new NestedMap<>();
makeVibro = new NestedMap<Boolean>();
notifyVisible = new NestedMap<Boolean>();
}
public static ChatManager getInstance() {
return instance;
}
@Override
public void onLoad() {
final Set<BaseEntity> privateChats = new HashSet<BaseEntity>();
final NestedMap<Boolean> notifyVisible = new NestedMap<Boolean>();
final NestedMap<Boolean> showText = new NestedMap<Boolean>();
final NestedMap<ShowMessageTextInNotification> showText = new NestedMap<>();
final NestedMap<Boolean> makeVibro = new NestedMap<Boolean>();
final NestedMap<Uri> sounds = new NestedMap<Uri>();
Cursor cursor;
......@@ -174,7 +168,7 @@ public class ChatManager implements OnLoadListener, OnAccountRemovedListener {
}
private void onLoaded(Set<BaseEntity> privateChats,
NestedMap<Boolean> notifyVisible, NestedMap<Boolean> showText,
NestedMap<Boolean> notifyVisible, NestedMap<ShowMessageTextInNotification> showText,
NestedMap<Boolean> vibro, NestedMap<Uri> sounds) {
for (BaseEntity baseEntity : privateChats)
this.privateChats.put(baseEntity.getAccount(),
......@@ -316,14 +310,27 @@ public class ChatManager implements OnLoadListener, OnAccountRemovedListener {
* Common value if there is no user specific value.
*/
public boolean isShowText(String account, String user) {
Boolean value = showText.get(account, user);
if (value == null)
return SettingsManager.eventsShowText();
return value;
switch (getShowText(account, user)) {
case show:
return true;
case hide:
return false;
case default_settings:
default:
return SettingsManager.eventsShowText();
}
}
public ShowMessageTextInNotification getShowText(String account, String user) {
ShowMessageTextInNotification showMessageTextInNotification = showText.get(account, user);
if (showMessageTextInNotification == null) {
return ShowMessageTextInNotification.default_settings;
} else {
return showMessageTextInNotification;
}
}
public void setShowText(final String account, final String user,
final boolean value) {
public void setShowText(final String account, final String user, final ShowMessageTextInNotification value) {
showText.put(account, user, value);
Application.getInstance().runInBackground(new Runnable() {
@Override
......
package com.xabber.android.data.message.chat;
public enum ShowMessageTextInNotification {
/**
* Show message text in notifications according to global settings.
*/
default_settings,
/**
* Always show message text in notifications.
*/
show,
/**
* Never show message text in notifications.
*/
hide;
public static ShowMessageTextInNotification fromInteger(int x) {
switch (x) {
case 0:
return default_settings;
case 1:
return show;
case 2:
return hide;
default:
return default_settings;
}
}
}
......@@ -25,7 +25,7 @@ import com.xabber.android.data.DatabaseManager;
*
* @author alexander.ivanov
*/
class ShowTextTable extends AbstractChatPropertyTable<Boolean> {
class ShowTextTable extends AbstractChatPropertyTable<ShowMessageTextInNotification> {
static final String NAME = "chat_show_text";
......@@ -36,12 +36,16 @@ class ShowTextTable extends AbstractChatPropertyTable<Boolean> {
DatabaseManager.getInstance().addTable(instance);
}
private ShowTextTable(DatabaseManager databaseManager) {
super(databaseManager);
}
public static ShowTextTable getInstance() {
return instance;
}
private ShowTextTable(DatabaseManager databaseManager) {
super(databaseManager);
static ShowMessageTextInNotification getValue(Cursor cursor) {
return ShowMessageTextInNotification.fromInteger((int) cursor.getLong(cursor.getColumnIndex(Fields.VALUE)));
}
@Override
......@@ -55,8 +59,8 @@ class ShowTextTable extends AbstractChatPropertyTable<Boolean> {
}
@Override
void bindValue(SQLiteStatement writeStatement, Boolean value) {
writeStatement.bindLong(3, value ? 1 : 0);
void bindValue(SQLiteStatement writeStatement, ShowMessageTextInNotification showMessageTextInNotification) {
writeStatement.bindLong(3, showMessageTextInNotification.ordinal());
}
@Override
......@@ -71,8 +75,4 @@ class ShowTextTable extends AbstractChatPropertyTable<Boolean> {
}
}
static boolean getValue(Cursor cursor) {
return cursor.getLong(cursor.getColumnIndex(Fields.VALUE)) != 0;
}
}
......@@ -8,6 +8,7 @@ import com.xabber.android.R;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.ArchiveMode;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.data.message.chat.ShowMessageTextInNotification;
import java.util.HashMap;
import java.util.Map;
......@@ -46,7 +47,8 @@ public class ChatContactSettingsFragment extends BaseSettingsFragment {
putValue(map, R.string.chat_events_visible_chat_key, ChatManager
.getInstance().isNotifyVisible(account, user));
putValue(map, R.string.chat_events_show_text_key, ChatManager
.getInstance().isShowText(account, user));
.getInstance().getShowText(account, user).ordinal());
putValue(map, R.string.chat_events_vibro_key, ChatManager.getInstance()
.isMakeVibro(account, user));
putValue(map, R.string.chat_events_sound_key, ChatManager.getInstance()
......@@ -67,9 +69,10 @@ public class ChatContactSettingsFragment extends BaseSettingsFragment {
ChatManager.getInstance().setNotifyVisible(account, user,
getBoolean(result, R.string.chat_events_visible_chat_key));
if (hasChanges(source, result, R.string.chat_events_show_text_key))
if (hasChanges(source, result, R.string.chat_events_show_text_key)) {
ChatManager.getInstance().setShowText(account, user,
getBoolean(result, R.string.chat_events_show_text_key));
ShowMessageTextInNotification.fromInteger(getInt(result, R.string.chat_events_show_text_key)));
}
if (hasChanges(source, result, R.string.chat_events_vibro_key))
ChatManager.getInstance().setMakeVibro(account, user,
......
......@@ -152,4 +152,8 @@
<string name="under_construction">В разработке</string>
<string name="under_construction_message">Этот экран находится в разработке</string>
<string name="chat_events_show_text_global">Использовать глобальные настройки</string>
<string name="chat_events_show_text_always">Отображать текст сообщения</string>
<string name="chat_events_show_text_never">Скрывать текст сообщения</string>
</resources>
\ No newline at end of file
......@@ -152,4 +152,9 @@
<string name="under_construction">Under construction</string>
<string name="under_construction_message">This screen is under construction</string>
<string name="chat_events_show_text_global">Use global settings</string>
<string name="chat_events_show_text_always">Show message text</string>
<string name="chat_events_show_text_never">Hide message text</string>
</resources>
\ No newline at end of file
......@@ -485,7 +485,25 @@
<!-- preference_chat -->
<string name="chat_save_history_key">chat_save_history</string>
<string name="chat_events_sound_key">chat_events_sound</string>
<string name="chat_events_show_text_key">chat_events_show_text</string>
<string name="chat_events_show_text_key">chat_events_show_message_text</string>
<string-array name="chat_events_show_text_entries">
<item>@string/chat_events_show_text_global</item>
<item>@string/chat_events_show_text_always</item>
<item>@string/chat_events_show_text_never</item>
</string-array>
<string-array name="chat_events_show_text_entry_values">
<item>@string/chat_events_show_text_global_value</item>
<item>@string/chat_events_show_text_always_value</item>
<item>@string/chat_events_show_text_never_value</item>
</string-array>
<string name="chat_events_show_text_global_value">default</string>
<string name="chat_events_show_text_always_value">always</string>
<string name="chat_events_show_text_never_value">never</string>
<string name="chat_events_show_text_default">@string/chat_events_show_text_global_value</string>
<string name="chat_events_vibro_key">chat_events_vibro</string>
<string name="chat_events_visible_chat_key">chat_events_visible_chat</string>
......
......@@ -24,7 +24,11 @@
android:showDefault="true"
android:showSilent="true"
android:title="@string/events_sound" />
<CheckBoxPreference
<ListPreference
android:defaultValue="@string/chat_events_show_text_default"
android:entries="@array/chat_events_show_text_entries"
android:entryValues="@array/chat_events_show_text_entry_values"
android:key="@string/chat_events_show_text_key"
android:title="@string/events_show_text"
/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment