Commit 4b73b403 authored by Grigory Fedorov's avatar Grigory Fedorov

appcompat-v7, single dark theme.

All activities uses support action bars. BaseSettingsActivity and ManagedPreferenceActivity removed - deprecated PreferenceActivity replaced by PreferenceFragment.
parent 0de94da4
......@@ -22,5 +22,5 @@ android {
dependencies {
compile files('libs/otr4j.jar')
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
}
......@@ -37,7 +37,7 @@
android:label="@string/application_name"
android:icon="@drawable/ic_launcher"
android:name="com.xabber.android.data.Application"
android:theme="@style/Theme.Light"
android:theme="@style/Theme"
android:allowBackup="true">
<activity
android:label="@string/application_name"
......@@ -210,7 +210,7 @@
<activity
android:label="@string/chat_settings"
android:name="com.xabber.android.ui.ChatEditor"
android:name="com.xabber.android.ui.preferences.ChatEditor"
android:parentActivityName="com.xabber.android.ui.ChatViewer"
>
......@@ -222,7 +222,7 @@
<activity
android:label="@string/contact_viewer"
android:name="com.xabber.android.ui.ContactViewer"
android:name="com.xabber.android.ui.preferences.ContactViewer"
android:icon="@drawable/ic_information"
android:parentActivityName="com.xabber.android.ui.ContactList"
>
......
......@@ -14,24 +14,21 @@
*/
package com.xabber.android.data;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.WeakHashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.Toast;
import com.xabber.android.data.SettingsManager.InterfaceTheme;
import com.xabber.android.ui.ContactList;
import com.xabber.android.ui.LoadActivity;
import com.xabber.android.ui.preferences.PreferenceEditor;
import com.xabber.androiddev.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.WeakHashMap;
/**
* Activity stack manager.
*
......@@ -123,28 +120,14 @@ public class ActivityManager implements OnUnloadListener {
return false;
}
/**
* Apply theme settings.
*
* @param activity
*/
private void applyTheme(Activity activity) {
TypedArray title = activity.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.windowNoTitle,
android.R.attr.windowIsFloating });
boolean noTitle = title.getBoolean(0, false);
boolean isFloating = title.getBoolean(1, false);
title.recycle();
if (isFloating)
return;
InterfaceTheme theme = SettingsManager.interfaceTheme();
if (theme == SettingsManager.InterfaceTheme.light)
activity.setTheme(noTitle ? R.style.Theme_Light_NoTitleBar
: R.style.Theme_Light);
else if (theme == SettingsManager.InterfaceTheme.dark)
activity.setTheme(noTitle ? R.style.Theme_Dark_NoTitleBar
: R.style.Theme_Dark);
}
/**
* Apply theme settings.
*
* @param activity
*/
private void applyTheme(Activity activity) {
activity.setTheme(R.style.Theme);
}
/**
* Push activity to stack.
......@@ -210,9 +193,9 @@ public class ActivityManager implements OnUnloadListener {
LogManager.i(this, "Wait for loading");
activity.startActivity(LoadActivity.createIntent(activity));
}
if (onErrorListener != null)
application
.removeUIListener(OnErrorListener.class, onErrorListener);
if (onErrorListener != null) {
application.removeUIListener(OnErrorListener.class, onErrorListener);
}
onErrorListener = new OnErrorListener() {
@Override
public void onError(final int resourceId) {
......
......@@ -91,7 +91,7 @@ public class AccountAdd extends ManagedActivity implements
inputManager.hideSoftInputFromWindow(findViewById(R.id.ok)
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.ArchiveMode;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.ui.helper.BaseSettingsActivity;
import com.xabber.androiddev.R;
public class ChatEditor extends BaseSettingsActivity {
private String account;
private String user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onInflate(Bundle savedInstanceState) {
account = getAccount(getIntent());
user = getUser(getIntent());
AccountItem accountItem = AccountManager.getInstance().getAccount(
account);
if (accountItem == null || user == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
finish();
return;
}
addPreferencesFromResource(R.xml.chat_editor);
if (accountItem.getArchiveMode() == ArchiveMode.server
|| accountItem.getArchiveMode() == ArchiveMode.dontStore)
getPreferenceScreen().removePreference(
getPreferenceScreen().findPreference(
getString(R.string.chat_save_history_key)));
}
@Override
protected Map<String, Object> getValues() {
Map<String, Object> map = new HashMap<String, Object>();
putValue(map, R.string.chat_save_history_key, ChatManager.getInstance()
.isSaveMessages(account, user));
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));
putValue(map, R.string.chat_events_vibro_key, ChatManager.getInstance()
.isMakeVibro(account, user));
putValue(map, R.string.chat_events_sound_key, ChatManager.getInstance()
.getSound(account, user));
return map;
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
if (hasChanges(source, result, R.string.chat_save_history_key))
ChatManager.getInstance().setSaveMessages(account, user,
getBoolean(result, R.string.chat_save_history_key));
if (hasChanges(source, result, R.string.chat_events_visible_chat_key))
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))
ChatManager.getInstance().setShowText(account, user,
getBoolean(result, R.string.chat_events_show_text_key));
if (hasChanges(source, result, R.string.chat_events_vibro_key))
ChatManager.getInstance().setMakeVibro(account, user,
getBoolean(result, R.string.chat_events_vibro_key));
if (hasChanges(source, result, R.string.chat_events_sound_key))
ChatManager.getInstance().setSound(account, user,
getUri(result, R.string.chat_events_sound_key));
return true;
}
public static Intent createIntent(Context context, String account,
String user) {
return new EntityIntentBuilder(context, ChatEditor.class)
.setAccount(account).setUser(user).build();
}
private static String getAccount(Intent intent) {
return EntityIntentBuilder.getAccount(intent);
}
private static String getUser(Intent intent) {
return EntityIntentBuilder.getUser(intent);
}
}
......@@ -50,7 +50,7 @@ public class ChatList extends ManagedListActivity implements
setListAdapter(listAdapter);
getListView().setOnItemClickListener(this);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
......@@ -115,7 +115,7 @@ public class ChatViewer extends ManagedActivity implements OnSelectListener,
if (actionWithUser == null)
actionWithUser = user;
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
selectChat(actionWithAccount, actionWithUser);
}
......@@ -179,7 +179,6 @@ public class ChatViewer extends ManagedActivity implements OnSelectListener,
String account = getAccount(intent);
String user = getUser(intent);
if (account == null || user == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
return;
}
if (hasAttention(intent))
......
......@@ -57,6 +57,7 @@ import com.xabber.android.ui.adapter.ChatMessageAdapter;
import com.xabber.android.ui.dialog.ChatExportDialogFragment;
import com.xabber.android.ui.helper.AbstractAvatarInflaterHelper;
import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.preferences.ChatEditor;
import com.xabber.android.ui.widget.PageSwitcher;
import com.xabber.androiddev.R;
......
......@@ -62,7 +62,7 @@ public class ContactAdd extends GroupListActivity implements
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
......@@ -47,7 +47,7 @@ public class ContactEditor extends GroupListActivity implements
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.capability.CapabilitiesManager;
import com.xabber.android.data.extension.capability.ClientInfo;
import com.xabber.android.data.extension.vcard.OnVCardListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.android.data.intent.AccountIntentBuilder;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.ResourceItem;
import com.xabber.android.data.roster.RosterContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.helper.ManagedPreferenceActivity;
import com.xabber.android.ui.widget.StatusPreference;
import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.vcard.Address;
import com.xabber.xmpp.vcard.AddressProperty;
import com.xabber.xmpp.vcard.AddressType;
import com.xabber.xmpp.vcard.Email;
import com.xabber.xmpp.vcard.EmailType;
import com.xabber.xmpp.vcard.NameProperty;
import com.xabber.xmpp.vcard.Organization;
import com.xabber.xmpp.vcard.Telephone;
import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.VCardProperty;
import com.xabber.xmpp.vcard.VCardProvider;
public class ContactViewer extends ManagedPreferenceActivity implements
OnVCardListener, OnContactChangedListener, OnAccountChangedListener {
private static final String SAVED_VCARD = "com.xabber.android.ui.ContactViewer.SAVED_VCARD";
private static final String SAVED_VCARD_ERROR = "com.xabber.android.ui.ContactViewer.SAVED_VCARD_ERROR";
private String account;
private String bareAddress;
private VCard vCard;
private boolean vCardError;
private List<PreferenceCategory> addresses;
private List<PreferenceCategory> telephones;
private List<PreferenceCategory> emails;
private static final Map<AddressType, Integer> ADDRESS_TYPE_MAP = new HashMap<AddressType, Integer>();
private static final Map<AddressProperty, Integer> ADDRESS_PROPERTY_MAP = new HashMap<AddressProperty, Integer>();
private static final Map<TelephoneType, Integer> TELEPHONE_TYPE_MAP = new HashMap<TelephoneType, Integer>();
private static final Map<EmailType, Integer> EMAIL_TYPE_MAP = new HashMap<EmailType, Integer>();
static {
ADDRESS_TYPE_MAP.put(AddressType.DOM, R.string.vcard_type_dom);
ADDRESS_TYPE_MAP.put(AddressType.HOME, R.string.vcard_type_home);
ADDRESS_TYPE_MAP.put(AddressType.INTL, R.string.vcard_type_intl);
ADDRESS_TYPE_MAP.put(AddressType.PARCEL, R.string.vcard_type_parcel);
ADDRESS_TYPE_MAP.put(AddressType.POSTAL, R.string.vcard_type_postal);
ADDRESS_TYPE_MAP.put(AddressType.PREF, R.string.vcard_type_pref);
ADDRESS_TYPE_MAP.put(AddressType.WORK, R.string.vcard_type_work);
if (ADDRESS_TYPE_MAP.size() != AddressType.values().length)
throw new IllegalStateException();
ADDRESS_PROPERTY_MAP.put(AddressProperty.CTRY,
R.string.vcard_address_ctry);
ADDRESS_PROPERTY_MAP.put(AddressProperty.EXTADR,
R.string.vcard_address_extadr);
ADDRESS_PROPERTY_MAP.put(AddressProperty.LOCALITY,
R.string.vcard_address_locality);
ADDRESS_PROPERTY_MAP.put(AddressProperty.PCODE,
R.string.vcard_address_pcode);
ADDRESS_PROPERTY_MAP.put(AddressProperty.POBOX,
R.string.vcard_address_pobox);
ADDRESS_PROPERTY_MAP.put(AddressProperty.REGION,
R.string.vcard_address_region);
ADDRESS_PROPERTY_MAP.put(AddressProperty.STREET,
R.string.vcard_address_street);
if (ADDRESS_PROPERTY_MAP.size() != AddressProperty.values().length)
throw new IllegalStateException();
TELEPHONE_TYPE_MAP.put(TelephoneType.BBS, R.string.vcard_type_bbs);
TELEPHONE_TYPE_MAP.put(TelephoneType.CELL, R.string.vcard_type_cell);
TELEPHONE_TYPE_MAP.put(TelephoneType.FAX, R.string.vcard_type_fax);
TELEPHONE_TYPE_MAP.put(TelephoneType.HOME, R.string.vcard_type_home);
TELEPHONE_TYPE_MAP.put(TelephoneType.ISDN, R.string.vcard_type_isdn);
TELEPHONE_TYPE_MAP.put(TelephoneType.MODEM, R.string.vcard_type_modem);
TELEPHONE_TYPE_MAP.put(TelephoneType.MSG, R.string.vcard_type_msg);
TELEPHONE_TYPE_MAP.put(TelephoneType.PAGER, R.string.vcard_type_pager);
TELEPHONE_TYPE_MAP.put(TelephoneType.PCS, R.string.vcard_type_pcs);
TELEPHONE_TYPE_MAP.put(TelephoneType.PREF, R.string.vcard_type_pref);
TELEPHONE_TYPE_MAP.put(TelephoneType.VIDEO, R.string.vcard_type_video);
TELEPHONE_TYPE_MAP.put(TelephoneType.VOICE, R.string.vcard_type_voice);
TELEPHONE_TYPE_MAP.put(TelephoneType.WORK, R.string.vcard_type_work);
if (TELEPHONE_TYPE_MAP.size() != TelephoneType.values().length)
throw new IllegalStateException();
EMAIL_TYPE_MAP.put(EmailType.HOME, R.string.vcard_type_home);
EMAIL_TYPE_MAP.put(EmailType.INTERNET, R.string.vcard_type_internet);
EMAIL_TYPE_MAP.put(EmailType.PREF, R.string.vcard_type_pref);
EMAIL_TYPE_MAP.put(EmailType.WORK, R.string.vcard_type_work);
EMAIL_TYPE_MAP.put(EmailType.X400, R.string.vcard_type_x400);
if (EMAIL_TYPE_MAP.size() != EmailType.values().length)
throw new IllegalStateException();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.contact_viewer);
addresses = new ArrayList<PreferenceCategory>();
telephones = new ArrayList<PreferenceCategory>();
emails = new ArrayList<PreferenceCategory>();
if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
// View information about contact from system contact list
Uri data = getIntent().getData();
if (data != null && "content".equals(data.getScheme())) {
List<String> segments = data.getPathSegments();
if (segments.size() == 2 && "data".equals(segments.get(0))) {
Long id;
try {
id = Long.valueOf(segments.get(1));
} catch (NumberFormatException e) {
id = null;
}
if (id != null)
// FIXME: Will be empty while application is loading
for (RosterContact rosterContact : RosterManager
.getInstance().getContacts())
if (id.equals(rosterContact.getViewId())) {
account = rosterContact.getAccount();
bareAddress = rosterContact.getUser();
break;
}
}
}
} else {
account = getAccount(getIntent());
bareAddress = Jid.getBareAddress(getUser(getIntent()));
}
if (account == null || bareAddress == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
finish();
return;
}
vCard = null;
vCardError = false;
if (savedInstanceState != null) {
vCardError = savedInstanceState
.getBoolean(SAVED_VCARD_ERROR, false);
String xml = savedInstanceState.getString(SAVED_VCARD);
if (xml != null)
try {
XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
true);
parser.setInput(new StringReader(xml));
int eventType = parser.next();
if (eventType != XmlPullParser.START_TAG)
throw new IllegalStateException(
String.valueOf(eventType));
if (!VCard.ELEMENT_NAME.equals(parser.getName()))
throw new IllegalStateException(parser.getName());
if (!VCard.NAMESPACE.equals(parser.getNamespace()))
throw new IllegalStateException(parser.getNamespace());
vCard = (VCard) (new VCardProvider()).parseIQ(parser);
} catch (Exception e) {
LogManager.exception(this, e);
}
}
setTitle(getString(R.string.contact_viewer_for, bareAddress));
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnVCardListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class,
this);
Application.getInstance().addUIListener(OnAccountChangedListener.class,
this);
if (vCard == null && !vCardError)
VCardManager.getInstance().request(account, bareAddress, null);
updateContact();
updateVCard();
}
@Override
protected void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(
OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(
OnAccountChangedListener.class, this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_VCARD_ERROR, vCardError);
if (vCard != null)
outState.putString(SAVED_VCARD, vCard.getChildElementXML());
}
@Override
public void onVCardReceived(String account, String bareAddress, VCard vCard) {
if (!this.account.equals(account)
|| !this.bareAddress.equals(bareAddress))
return;
this.vCard = vCard;
this.vCardError = false;
updateVCard();
}
@Override
public void onVCardFailed(String account, String bareAddress) {
if (!this.account.equals(account)
|| !this.bareAddress.equals(bareAddress))
return;
this.vCard = null;
this.vCardError = true;
updateVCard();
Application.getInstance().onError(R.string.XMPP_EXCEPTION);
}
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
for (BaseEntity entity : entities)
if (entity.equals(account, bareAddress)) {
updateContact();
break;
}
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
if (accounts.contains(account))
updateContact();
}
/**
* Sets value for the preference by its id.
*
* @param resourceId
* @param value
*/
private void setValue(int resourceId, String value) {
if (value == null)
value = "";
findPreference(getString(resourceId)).setSummary(value);
}
/**
* @param source
* @param value
* @param splitter
* @return Concatenated source and value with splitter if necessary.
*/
private String addString(String source, String value, String splitter) {
if (value == null || "".equals(value))
return source;
if (source == null || "".equals(source))
return value;
return source + splitter + value;
}
private void updateContact() {
setValue(R.string.contact_viewer_jid, bareAddress);
RosterContact rosterContact = RosterManager.getInstance()
.getRosterContact(account, bareAddress);
setValue(R.string.contact_viewer_name, rosterContact == null ? null
: rosterContact.getRealName());
PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference(getString(R.string.contact_viewer_resources));
preferenceCategory.removeAll();
if (rosterContact != null && rosterContact.isConnected())
for (ResourceItem resourceItem : PresenceManager.getInstance()
.getResourceItems(account, bareAddress)) {
StatusPreference preference = new StatusPreference(this);
preference.setLayoutResource(R.layout.info_preference);
preference.setStatusMode(resourceItem.getStatusMode());
String user = resourceItem.getUser(bareAddress);
ClientInfo clientInfo = CapabilitiesManager.getInstance()
.getClientInfo(account, user);
String client;
if (clientInfo == null) {
CapabilitiesManager.getInstance().request(account, user);
client = getString(R.string.please_wait);
} else if (clientInfo == CapabilitiesManager.INVALID_CLIENT_INFO) {
client = getString(R.string.unknown);
} else {
String name = clientInfo.getName();
if (name == null)
name = getString(R.string.unknown);
String type = clientInfo.getType();
if (type == null)
type = getString(R.string.unknown);
client = getString(R.string.contact_viewer_client_info,
name, type);
}
preference.setTitle(getString(
R.string.contact_viewer_resource_summary,
resourceItem.getVerbose(), resourceItem.getPriority(),
client));
preference.setSummary(resourceItem.getStatusText());
preferenceCategory.addPreference(preference);
}
}
private void updateVCard() {
if (vCard == null)
return;
setValue(R.string.vcard_nick_name,
vCard.getField(VCardProperty.NICKNAME));
setValue(R.string.vcard_formatted_name, vCard.getFormattedName());
setValue(R.string.vcard_prefix_name,
vCard.getField(NameProperty.PREFIX));
setValue(R.string.vcard_given_name, vCard.getField(NameProperty.GIVEN));
setValue(R.string.vcard_middle_name,
vCard.getField(NameProperty.MIDDLE));
setValue(R.string.vcard_family_name,
vCard.getField(NameProperty.FAMILY));
setValue(R.string.vcard_suffix_name,
vCard.getField(NameProperty.SUFFIX));
setValue(R.string.vcard_birth_date, vCard.getField(VCardProperty.BDAY));
setValue(R.string.vcard_title, vCard.getField(VCardProperty.TITLE));
setValue(R.string.vcard_role, vCard.getField(VCardProperty.ROLE));
List<Organization> organizations = vCard.getOrganizations();
String organization;
if (organizations.isEmpty())
organization = null;
else {
organization = organizations.get(0).getName();
for (String unit : organizations.get(0).getUnits())
organization = addString(organization, unit, "\n");
}
setValue(R.string.vcard_organization, organization);
setValue(R.string.vcard_url, vCard.getField(VCardProperty.URL));
String categories = null;
for (String category : vCard.getCategories())
categories = addString(categories, category, "\n");
setValue(R.string.vcard_categories, categories);
setValue(R.string.vcard_note, vCard.getField(VCardProperty.NOTE));
setValue(R.string.vcard_decsription, vCard.getField(VCardProperty.DESC));
PreferenceScreen screen = getPreferenceScreen();
for (PreferenceCategory category : addresses)
screen.removePreference(category);
for (PreferenceCategory category : telephones)
screen.removePreference(category);
for (PreferenceCategory category : emails)
screen.removePreference(category);
for (Address address : vCard.getAddresses()) {
String types = null;
for (AddressType type : address.getTypes())
types = addString(types, getString(ADDRESS_TYPE_MAP.get(type)),
", ");
String value = null;
for (AddressProperty property : AddressProperty.values())
value = addString(value, address.getProperties().get(property),
"\n");
PreferenceScreen addressScreen = createTypedCategory(
R.string.vcard_address, types, value);
for (AddressProperty property : AddressProperty.values())
addPreferenceScreen(addressScreen,
ADDRESS_PROPERTY_MAP.get(property), address
.getProperties().get(property));
}
for (Telephone telephone : vCard.getTelephones()) {
String types = null;
for (TelephoneType type : telephone.getTypes())
types = addString(types,
getString(TELEPHONE_TYPE_MAP.get(type)), ", ");
createTypedCategory(R.string.vcard_telephone, types,
telephone.getValue());
}
for (Email email : vCard.getEmails()) {
String types = null;
for (EmailType type : email.getTypes())
types = addString(types, getString(EMAIL_TYPE_MAP.get(type)),
", ");
createTypedCategory(R.string.vcard_email, types, email.getValue());
}
}
private PreferenceScreen createTypedCategory(int title, String types,
String value) {
PreferenceCategory preferenceCategory = new PreferenceCategory(this);
preferenceCategory.setTitle(title);
getPreferenceScreen().addPreference(preferenceCategory);
addPreferenceScreen(preferenceCategory, R.string.vcard_type, types);
return addPreferenceScreen(preferenceCategory, title, value);
}
private PreferenceScreen addPreferenceScreen(PreferenceGroup container,
int title, String summary) {
PreferenceScreen preference = getPreferenceManager()
.createPreferenceScreen(this);
preference.setLayoutResource(R.layout.info_preference);
preference.setTitle(title);
preference.setSummary(summary == null ? "" : summary);
container.addPreference(preference);
return preference;
}
public static Intent createIntent(Context context, String account,
String user) {
return new EntityIntentBuilder(context, ContactViewer.class)
.setAccount(account).setUser(user).build();
}
private static String getAccount(Intent intent) {
return AccountIntentBuilder.getAccount(intent);
}
private static String getUser(Intent intent) {
return EntityIntentBuilder.getUser(intent);
}
}
......@@ -120,7 +120,7 @@ public class FingerprintViewer extends ManagedActivity implements
copyView.setOnClickListener(this);
isUpdating = false;
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
......@@ -72,7 +72,7 @@ public class MUCEditor extends ManagedActivity implements View.OnClickListener,
setContentView(R.layout.muc_editor);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
accountView = (Spinner) findViewById(R.id.contact_account);
serverView = (EditText) findViewById(R.id.muc_server);
......
......@@ -99,7 +99,7 @@ public class QuestionViewer extends ManagedActivity implements
findViewById(R.id.cancel).setOnClickListener(this);
findViewById(R.id.send).setOnClickListener(this);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
......@@ -72,7 +72,7 @@ public class StatusEditor extends ManagedListActivity implements
setContentView(R.layout.status_editor);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
account = StatusEditor.getAccount(intent);
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.helper;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import com.xabber.android.ui.widget.RingtonePreference;
import com.xabber.androiddev.R;
/**
* Provide possibility to edit, apply and discard settings.
*
* String resource id is used to identify preferences.
*
* @author alexander.ivanov
*
* @param <T>
*/
public abstract class BaseSettingsActivity extends ManagedPreferenceActivity
implements Preference.OnPreferenceChangeListener {
private static final int MENU_SAVE = Menu.FIRST;
private static final int MENU_CANCEL = Menu.FIRST + 1;
private static final int CONFIRM_DIALOG_ID = 0;
/**
* Set of initial values is in progress.
*/
private boolean initialChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFinishing())
return;
onInflate(savedInstanceState);
if (isFinishing())
return;
PreferenceSummaryHelper.updateSummary(getPreferenceScreen());
if (savedInstanceState == null)
operation(Operation.read);
PreferenceScreen preferenceScreen = getPreferenceScreen();
initialChange = true;
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
preference.setOnPreferenceChangeListener(this);
if (preference instanceof EditTextPreference)
onPreferenceChange(preference,
((EditTextPreference) preference).getText());
else if (preference instanceof CheckBoxPreference)
onPreferenceChange(preference,
((CheckBoxPreference) preference).isChecked());
else if (preference instanceof ListPreference)
onPreferenceChange(preference,
((ListPreference) preference).getValue());
}
initialChange = false;
}
protected boolean isInitialChange() {
return initialChange;
}
/**
* Inflates layout.
*
* @param savedInstanceState
*/
protected abstract void onInflate(Bundle savedInstanceState);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_SAVE, 0, android.R.string.ok).setIcon(
android.R.drawable.ic_menu_save);
menu.add(0, MENU_CANCEL, 0, android.R.string.cancel).setIcon(
android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SAVE:
if (operation(Operation.save))
finish();
return true;
case MENU_CANCEL:
if (operation(Operation.discard))
finish();
else
showDialog(CONFIRM_DIALOG_ID);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof EditTextPreference)
preference.setSummary((String) newValue);
return true;
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == CONFIRM_DIALOG_ID) {
return new AlertDialog.Builder(this)
.setTitle(android.R.string.dialog_alert_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.confirm_cancellation)
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int w) {
finish();
}
}).setNegativeButton(android.R.string.no, null)
.create();
}
return super.onCreateDialog(id);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (operation(Operation.save))
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Possible operations.
*/
private static enum Operation {
save, discard, read
};
protected void putValue(Map<String, Object> map, int resoureId, Object value) {
map.put(getString(resoureId), value);
}
protected String getString(Map<String, Object> map, int resoureId) {
return (String) map.get(getString(resoureId));
}
protected int getInt(Map<String, Object> map, int resoureId) {
return (Integer) map.get(getString(resoureId));
}
protected boolean getBoolean(Map<String, Object> map, int resoureId) {
return (Boolean) map.get(getString(resoureId));
}
protected Uri getUri(Map<String, Object> map, int resoureId) {
return (Uri) map.get(getString(resoureId));
}
/**
* @param intent
* @return Whether an operation succeed.
*/
private boolean operation(Operation selected) {
Map<String, Object> source = getValues();
if (selected == Operation.read)
setPreferences(source);
else {
Map<String, Object> result = getPreferences(source);
for (Entry<String, Object> entry : source.entrySet())
if (!result.containsKey(entry.getKey()))
result.put(entry.getKey(), entry.getValue());
if (selected == Operation.save)
return setValues(source, result);
else if (selected == Operation.discard) {
for (String key : source.keySet())
if (hasChanges(source, result, key))
return false;
} else
throw new IllegalStateException();
}
return true;
}
/**
* @param source
* @param result
* @param key
* @return Whether value has been changed.
*/
protected boolean hasChanges(Map<String, Object> source,
Map<String, Object> result, String key) {
Object sourceValue = source.get(key);
Object targetValue = result.get(key);
return (sourceValue == null && targetValue != null)
|| (sourceValue != null && !sourceValue.equals(targetValue));
}
/**
* @param source
* @param result
* @param resourceId
* @return Whether value has been changed.
*/
protected boolean hasChanges(Map<String, Object> source,
Map<String, Object> result, int resourceId) {
return hasChanges(source, result, getString(resourceId));
}
/**
* @return Map with source values.
*/
protected abstract Map<String, Object> getValues();
/**
* Set values to the UI elements.
*
* @param source
*/
protected void setPreferences(Map<String, Object> source) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
Object value = source.get(preference.getKey());
setPreference(preference, value);
}
}
/**
* Set value to the UI element.
*
* @param preference
* @param value
*/
protected void setPreference(Preference preference, Object value) {
if (preference instanceof EditTextPreference)
((EditTextPreference) preference)
.setText(value instanceof Integer ? String.valueOf(value)
: (String) value);
else if (preference instanceof CheckBoxPreference)
((CheckBoxPreference) preference).setChecked((Boolean) value);
else if (preference instanceof ListPreference)
((ListPreference) preference).setValueIndex((Integer) value);
else if (preference instanceof RingtonePreference)
((RingtonePreference) preference).setUri((Uri) value);
}
/**
* Get values from the UI elements.
*
* @param source
* @return
*/
protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = new HashMap<String, Object>();
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
result.put(preference.getKey(), getPrefecence(preference, source));
}
return result;
}
/**
* Get value from the UI element.
*
* @param preference
* @param source
* @return
*/
protected Object getPrefecence(Preference preference,
Map<String, Object> source) {
if (preference instanceof PreferenceScreen)
return null;
else if (preference instanceof EditTextPreference) {
String value = ((EditTextPreference) preference).getText();
if (source.get(preference.getKey()) instanceof Integer)
try {
return Integer.parseInt(value);
} catch (Exception NumberFormatException) {
return null;
}
else
return value;
} else if (preference instanceof CheckBoxPreference)
return ((CheckBoxPreference) preference).isChecked();
else if (preference instanceof ListPreference)
return Integer
.valueOf(((ListPreference) preference)
.findIndexOfValue(((ListPreference) preference)
.getValue()));
else if (preference instanceof RingtonePreference)
return ((RingtonePreference) preference).getUri();
throw new IllegalStateException();
}
/**
* Apply result values.
*
* @param source
* @param result
* @return Whether operation succeed.
*/
protected abstract boolean setValues(Map<String, Object> source,
Map<String, Object> result);
}
......@@ -37,7 +37,7 @@ import com.xabber.android.ui.preferences.AccountEditor;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.ContactAdd;
import com.xabber.android.ui.ContactEditor;
import com.xabber.android.ui.ContactViewer;
import com.xabber.android.ui.preferences.ContactViewer;
import com.xabber.android.ui.MUCEditor;
import com.xabber.android.ui.StatusEditor;
import com.xabber.android.ui.adapter.UpdatableAdapter;
......
......@@ -16,7 +16,7 @@ package com.xabber.android.ui.helper;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import com.xabber.android.data.ActivityManager;
......@@ -28,7 +28,7 @@ import com.xabber.android.data.ActivityManager;
* @author alexander.ivanov
*
*/
public abstract class ManagedActivity extends FragmentActivity {
public abstract class ManagedActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
......
......@@ -16,7 +16,7 @@ package com.xabber.android.ui.helper;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListAdapter;
import android.widget.ListView;
......@@ -30,7 +30,7 @@ import com.xabber.android.data.ActivityManager;
* @author alexander.ivanov
*
*/
public abstract class ManagedListActivity extends FragmentActivity {
public abstract class ManagedListActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.helper;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import com.xabber.android.data.ActivityManager;
/**
* Base class for all PreferenceActivities.
*
* Adds custom activity logic.
*
* @author alexander.ivanov
*
*/
public abstract class ManagedPreferenceActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ActivityManager.getInstance().onCreate(this);
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
ActivityManager.getInstance().onResume(this);
super.onResume();
}
@Override
protected void onPause() {
ActivityManager.getInstance().onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
ActivityManager.getInstance().onDestroy(this);
super.onDestroy();
}
@Override
protected void onNewIntent(Intent intent) {
ActivityManager.getInstance().onNewIntent(this, intent);
super.onNewIntent(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ActivityManager.getInstance().onActivityResult(this, requestCode,
resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void startActivity(Intent intent) {
ActivityManager.getInstance().updateIntent(this, intent);
super.startActivity(intent);
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
ActivityManager.getInstance().updateIntent(this, intent);
super.startActivityForResult(intent, requestCode);
}
}
......@@ -39,7 +39,7 @@ public class AboutViewer extends ManagedActivity {
((TextView) findViewById(R.id.about_license))
.setMovementMethod(LinkMovementMethod.getInstance());
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private String getVersionName() {
......
......@@ -14,294 +14,151 @@
*/
package com.xabber.android.ui.preferences;
import java.util.HashMap;
import java.util.Map;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.AccountProtocol;
import com.xabber.android.data.account.ArchiveMode;
import com.xabber.android.data.connection.ProxyType;
import com.xabber.android.data.connection.TLSMode;
import com.xabber.android.data.intent.AccountIntentBuilder;
import com.xabber.android.ui.OAuthActivity;
import com.xabber.android.ui.dialog.OrbotInstallerDialogBuilder;
import com.xabber.android.ui.helper.BaseSettingsActivity;
import com.xabber.android.ui.helper.OrbotHelper;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
public class AccountEditor extends BaseSettingsActivity implements
OnPreferenceClickListener {
private static final int OAUTH_WML_REQUEST_CODE = 1;
public class AccountEditor extends ManagedActivity implements
OnPreferenceClickListener, AccountEditorFragment.AccountEditorFragmentInteractionListener {
private static final String SAVED_TOKEN = "com.xabber.android.ui.preferences.AccountEditor.TOKEN";
private static final int OAUTH_WML_REQUEST_CODE = 1;
private static final String INVALIDATED_TOKEN = "com.xabber.android.ui.preferences.AccountEditor.INVALIDATED";
private static final String SAVED_TOKEN = "com.xabber.android.ui.preferences.AccountEditor.TOKEN";
private static final int ORBOT_DIALOG_ID = 9050;
public static final String INVALIDATED_TOKEN = "com.xabber.android.ui.preferences.AccountEditor.INVALIDATED";
private String account;
private AccountItem accountItem;
private static final int ORBOT_DIALOG_ID = 9050;
private String token;
private String account;
private AccountItem accountItem;
private Preference oauthPreference;
private String token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
account = AccountEditor.getAccount(getIntent());
if (account == null) {
finish();
return;
}
accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null) {
Application.getInstance().onError(R.string.NO_SUCH_ACCOUNT);
finish();
return;
}
if (savedInstanceState == null) {
token = accountItem.getConnectionSettings().getPassword();
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container, new AccountEditorFragment()).commit();
} else {
token = savedInstanceState.getString(SAVED_TOKEN);
}
setTitle(AccountManager.getInstance().getVerboseName(account));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_preferences);
}
@Override
protected void onInflate(Bundle savedInstanceState) {
account = AccountEditor.getAccount(getIntent());
if (account == null) {
finish();
return;
}
accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null) {
Application.getInstance().onError(R.string.NO_SUCH_ACCOUNT);
finish();
return;
}
AccountProtocol protocol = accountItem.getConnectionSettings()
.getProtocol();
if (protocol == AccountProtocol.xmpp)
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.gtalk)
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.wlm)
addPreferencesFromResource(R.xml.account_editor_oauth);
else
throw new IllegalStateException();
if (!Application.getInstance().isContactsSupported())
getPreferenceScreen().removePreference(
findPreference(getString(R.string.account_syncable_key)));
setTitle(AccountManager.getInstance().getVerboseName(account));
if (savedInstanceState == null)
token = accountItem.getConnectionSettings().getPassword();
else
token = savedInstanceState.getString(SAVED_TOKEN);
oauthPreference = findPreference(getString(R.string.account_oauth_key));
if (oauthPreference != null)
oauthPreference.setOnPreferenceClickListener(this);
onOAuthChange();
AccountManager.getInstance().removeAuthorizationError(account);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SAVED_TOKEN, token);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SAVED_TOKEN, token);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OAUTH_WML_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (OAuthActivity.isInvalidated(data)) {
token = INVALIDATED_TOKEN;
} else {
String value = OAuthActivity.getToken(data);
if (value == null)
Application.getInstance().onError(
R.string.AUTHENTICATION_FAILED);
else
token = value;
}
onOAuthChange();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OAUTH_WML_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (OAuthActivity.isInvalidated(data)) {
token = INVALIDATED_TOKEN;
} else {
String value = OAuthActivity.getToken(data);
if (value == null)
Application.getInstance().onError(
R.string.AUTHENTICATION_FAILED);
else
token = value;
}
((AccountEditorFragment)getFragmentManager().findFragmentById(
R.id.preferences_activity_container)).onOAuthChange();
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (getString(R.string.account_port_key).equals(preference.getKey()))
try {
Integer.parseInt((String) newValue);
} catch (NumberFormatException e) {
Toast.makeText(this, getString(R.string.account_invalid_port),
Toast.LENGTH_LONG).show();
return false;
}
if (getString(R.string.account_tls_mode_key)
.equals(preference.getKey())
|| getString(R.string.account_archive_mode_key).equals(
preference.getKey())
|| getString(R.string.account_proxy_type_key).equals(
preference.getKey()))
preference.setSummary((String) newValue);
else if (!getString(R.string.account_password_key).equals(
preference.getKey())
&& !getString(R.string.account_proxy_password_key).equals(
preference.getKey())
&& !getString(R.string.account_priority_key).equals(
preference.getKey()))
super.onPreferenceChange(preference, newValue);
if (getString(R.string.account_proxy_type_key).equals(
preference.getKey())) {
boolean enabled = !getString(R.string.account_proxy_type_none)
.equals(newValue)
&& !getString(R.string.account_proxy_type_orbot).equals(
newValue);
for (int id : new Integer[] { R.string.account_proxy_host_key,
R.string.account_proxy_port_key,
R.string.account_proxy_user_key,
R.string.account_proxy_password_key, }) {
Preference proxyPreference = findPreference(getString(id));
if (proxyPreference != null)
proxyPreference.setEnabled(enabled);
}
}
return true;
}
private void onOAuthChange() {
if (oauthPreference == null)
return;
if (INVALIDATED_TOKEN.equals(token))
oauthPreference.setSummary(R.string.account_oauth_invalidated);
else
oauthPreference.setSummary(R.string.account_oauth_summary);
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (getString(R.string.account_oauth_key).equals(preference.getKey())) {
startActivityForResult(OAuthActivity.createIntent(this, accountItem
.getConnectionSettings().getProtocol()),
OAUTH_WML_REQUEST_CODE);
return true;
}
return false;
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (getString(R.string.account_oauth_key).equals(preference.getKey())) {
startActivityForResult(OAuthActivity.createIntent(this, accountItem
.getConnectionSettings().getProtocol()),
OAUTH_WML_REQUEST_CODE);
return true;
}
return false;
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == ORBOT_DIALOG_ID) {
return new OrbotInstallerDialogBuilder(this, ORBOT_DIALOG_ID)
.create();
}
return super.onCreateDialog(id);
}
@Override
protected Map<String, Object> getValues() {
Map<String, Object> source = new HashMap<String, Object>();
putValue(source, R.string.account_custom_key, accountItem
.getConnectionSettings().isCustom());
putValue(source, R.string.account_host_key, accountItem
.getConnectionSettings().getHost());
putValue(source, R.string.account_port_key, accountItem
.getConnectionSettings().getPort());
putValue(source, R.string.account_server_key, accountItem
.getConnectionSettings().getServerName());
putValue(source, R.string.account_username_key, accountItem
.getConnectionSettings().getUserName());
putValue(source, R.string.account_store_password_key,
accountItem.isStorePassword());
putValue(source, R.string.account_password_key, accountItem
.getConnectionSettings().getPassword());
putValue(source, R.string.account_resource_key, accountItem
.getConnectionSettings().getResource());
putValue(source, R.string.account_priority_key,
accountItem.getPriority());
putValue(source, R.string.account_enabled_key, accountItem.isEnabled());
putValue(source, R.string.account_sasl_key, accountItem
.getConnectionSettings().isSaslEnabled());
putValue(
source,
R.string.account_tls_mode_key,
Integer.valueOf(accountItem.getConnectionSettings()
.getTlsMode().ordinal()));
putValue(source, R.string.account_compression_key, accountItem
.getConnectionSettings().useCompression());
putValue(
source,
R.string.account_proxy_type_key,
Integer.valueOf(accountItem.getConnectionSettings()
.getProxyType().ordinal()));
putValue(source, R.string.account_proxy_host_key, accountItem
.getConnectionSettings().getProxyHost());
putValue(source, R.string.account_proxy_port_key, accountItem
.getConnectionSettings().getProxyPort());
putValue(source, R.string.account_proxy_user_key, accountItem
.getConnectionSettings().getProxyUser());
putValue(source, R.string.account_proxy_password_key, accountItem
.getConnectionSettings().getProxyPassword());
putValue(source, R.string.account_syncable_key,
accountItem.isSyncable());
putValue(source, R.string.account_archive_mode_key,
Integer.valueOf(accountItem.getArchiveMode().ordinal()));
return source;
}
private static String getAccount(Intent intent) {
return AccountIntentBuilder.getAccount(intent);
}
@Override
protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = super.getPreferences(source);
if (oauthPreference != null)
putValue(result, R.string.account_password_key, token);
return result;
}
public static Intent createIntent(Context context, String account) {
return new AccountIntentBuilder(context, AccountEditor.class)
.setAccount(account).build();
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
ProxyType proxyType = ProxyType.values()[getInt(result,
R.string.account_proxy_type_key)];
if (proxyType == ProxyType.orbot && !OrbotHelper.isOrbotInstalled()) {
showDialog(ORBOT_DIALOG_ID);
return false;
}
AccountManager
.getInstance()
.updateAccount(
account,
getBoolean(result, R.string.account_custom_key),
getString(result, R.string.account_host_key),
getInt(result, R.string.account_port_key),
getString(result, R.string.account_server_key),
getString(result, R.string.account_username_key),
getBoolean(result, R.string.account_store_password_key),
getString(result, R.string.account_password_key),
getString(result, R.string.account_resource_key),
getInt(result, R.string.account_priority_key),
getBoolean(result, R.string.account_enabled_key),
getBoolean(result, R.string.account_sasl_key),
TLSMode.values()[getInt(result,
R.string.account_tls_mode_key)],
getBoolean(result, R.string.account_compression_key),
proxyType,
getString(result, R.string.account_proxy_host_key),
getInt(result, R.string.account_proxy_port_key),
getString(result, R.string.account_proxy_user_key),
getString(result, R.string.account_proxy_password_key),
getBoolean(result, R.string.account_syncable_key),
ArchiveMode.values()[getInt(result,
R.string.account_archive_mode_key)]);
return true;
}
@Override
public String getAccount() {
return account;
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == ORBOT_DIALOG_ID) {
return new OrbotInstallerDialogBuilder(this, ORBOT_DIALOG_ID)
.create();
}
return super.onCreateDialog(id);
}
@Override
public AccountItem getAccountItem() {
return accountItem;
}
private static String getAccount(Intent intent) {
return AccountIntentBuilder.getAccount(intent);
}
@Override
public String getToken() {
return token;
}
public static Intent createIntent(Context context, String account) {
return new AccountIntentBuilder(context, AccountEditor.class)
.setAccount(account).build();
}
@Override
public void onOAuthClick() {
startActivityForResult(OAuthActivity.createIntent(this, accountItem
.getConnectionSettings().getProtocol()),
OAUTH_WML_REQUEST_CODE);
}
@Override
public void showOrbotDialog() {
showDialog(ORBOT_DIALOG_ID);
}
}
package com.xabber.android.ui.preferences;
import android.app.Activity;
import android.os.Bundle;
import android.preference.Preference;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.AccountProtocol;
import com.xabber.android.data.account.ArchiveMode;
import com.xabber.android.data.connection.ProxyType;
import com.xabber.android.data.connection.TLSMode;
import com.xabber.android.ui.helper.OrbotHelper;
import com.xabber.androiddev.R;
import java.util.HashMap;
import java.util.Map;
public class AccountEditorFragment extends BaseSettingsFragment
implements Preference.OnPreferenceClickListener {
private AccountEditorFragmentInteractionListener mListener;
private Preference oauthPreference;
@Override
protected void onInflate(Bundle savedInstanceState) {
AccountProtocol protocol = mListener.getAccountItem().getConnectionSettings()
.getProtocol();
if (protocol == AccountProtocol.xmpp)
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.gtalk)
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.wlm)
addPreferencesFromResource(R.xml.account_editor_oauth);
else
throw new IllegalStateException();
if (!Application.getInstance().isContactsSupported())
getPreferenceScreen().removePreference(
findPreference(getString(R.string.account_syncable_key)));
oauthPreference = findPreference(getString(R.string.account_oauth_key));
if (oauthPreference != null) {
oauthPreference.setOnPreferenceClickListener(this);
}
onOAuthChange();
AccountManager.getInstance().removeAuthorizationError(mListener.getAccount());
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (getString(R.string.account_port_key).equals(preference.getKey()))
try {
Integer.parseInt((String) newValue);
} catch (NumberFormatException e) {
Toast.makeText(getActivity(), getString(R.string.account_invalid_port),
Toast.LENGTH_LONG).show();
return false;
}
if (getString(R.string.account_tls_mode_key)
.equals(preference.getKey())
|| getString(R.string.account_archive_mode_key).equals(
preference.getKey())
|| getString(R.string.account_proxy_type_key).equals(
preference.getKey()))
preference.setSummary((String) newValue);
else if (!getString(R.string.account_password_key).equals(
preference.getKey())
&& !getString(R.string.account_proxy_password_key).equals(
preference.getKey())
&& !getString(R.string.account_priority_key).equals(
preference.getKey()))
super.onPreferenceChange(preference, newValue);
if (getString(R.string.account_proxy_type_key).equals(
preference.getKey())) {
boolean enabled = !getString(R.string.account_proxy_type_none)
.equals(newValue)
&& !getString(R.string.account_proxy_type_orbot).equals(
newValue);
for (int id : new Integer[] { R.string.account_proxy_host_key,
R.string.account_proxy_port_key,
R.string.account_proxy_user_key,
R.string.account_proxy_password_key, }) {
Preference proxyPreference = findPreference(getString(id));
if (proxyPreference != null)
proxyPreference.setEnabled(enabled);
}
}
return true;
}
public void onOAuthChange() {
if (oauthPreference == null)
return;
if (AccountEditor.INVALIDATED_TOKEN.equals(mListener.getToken()))
oauthPreference.setSummary(R.string.account_oauth_invalidated);
else
oauthPreference.setSummary(R.string.account_oauth_summary);
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (getString(R.string.account_oauth_key).equals(preference.getKey())) {
mListener.onOAuthClick();
return true;
}
return false;
}
@Override
protected Map<String, Object> getValues() {
Map<String, Object> source = new HashMap<>();
AccountItem accountItem = mListener.getAccountItem();
putValue(source, R.string.account_custom_key, accountItem
.getConnectionSettings().isCustom());
putValue(source, R.string.account_host_key, accountItem
.getConnectionSettings().getHost());
putValue(source, R.string.account_port_key, accountItem
.getConnectionSettings().getPort());
putValue(source, R.string.account_server_key, accountItem
.getConnectionSettings().getServerName());
putValue(source, R.string.account_username_key, accountItem
.getConnectionSettings().getUserName());
putValue(source, R.string.account_store_password_key, accountItem.isStorePassword());
putValue(source, R.string.account_password_key, accountItem
.getConnectionSettings().getPassword());
putValue(source, R.string.account_resource_key, accountItem
.getConnectionSettings().getResource());
putValue(source, R.string.account_priority_key, accountItem.getPriority());
putValue(source, R.string.account_enabled_key, accountItem.isEnabled());
putValue(source, R.string.account_sasl_key, accountItem
.getConnectionSettings().isSaslEnabled());
putValue(source, R.string.account_tls_mode_key,
accountItem.getConnectionSettings().getTlsMode().ordinal());
putValue(source, R.string.account_compression_key, accountItem
.getConnectionSettings().useCompression());
putValue(source, R.string.account_proxy_type_key,
accountItem.getConnectionSettings().getProxyType().ordinal());
putValue(source, R.string.account_proxy_host_key, accountItem
.getConnectionSettings().getProxyHost());
putValue(source, R.string.account_proxy_port_key, accountItem
.getConnectionSettings().getProxyPort());
putValue(source, R.string.account_proxy_user_key, accountItem
.getConnectionSettings().getProxyUser());
putValue(source, R.string.account_proxy_password_key, accountItem
.getConnectionSettings().getProxyPassword());
putValue(source, R.string.account_syncable_key,
accountItem.isSyncable());
putValue(source, R.string.account_archive_mode_key,
accountItem.getArchiveMode().ordinal());
return source;
}
@Override
protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = super.getPreferences(source);
if (oauthPreference != null)
putValue(result, R.string.account_password_key, mListener.getAccount());
return result;
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
ProxyType proxyType = ProxyType.values()[getInt(result,
R.string.account_proxy_type_key)];
if (proxyType == ProxyType.orbot && !OrbotHelper.isOrbotInstalled()) {
mListener.showOrbotDialog();
return false;
}
AccountManager.getInstance().updateAccount(
mListener.getAccount(),
getBoolean(result, R.string.account_custom_key),
getString(result, R.string.account_host_key),
getInt(result, R.string.account_port_key),
getString(result, R.string.account_server_key),
getString(result, R.string.account_username_key),
getBoolean(result, R.string.account_store_password_key),
getString(result, R.string.account_password_key),
getString(result, R.string.account_resource_key),
getInt(result, R.string.account_priority_key),
getBoolean(result, R.string.account_enabled_key),
getBoolean(result, R.string.account_sasl_key),
TLSMode.values()[getInt(result, R.string.account_tls_mode_key)],
getBoolean(result, R.string.account_compression_key),
proxyType,
getString(result, R.string.account_proxy_host_key),
getInt(result, R.string.account_proxy_port_key),
getString(result, R.string.account_proxy_user_key),
getString(result, R.string.account_proxy_password_key),
getBoolean(result, R.string.account_syncable_key),
ArchiveMode.values()[getInt(result, R.string.account_archive_mode_key)]);
return true;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AccountEditorFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement AccountEditorFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface AccountEditorFragmentInteractionListener {
public String getAccount();
public AccountItem getAccountItem();
public String getToken();
public void onOAuthClick();
public void showOrbotDialog();
}
}
......@@ -43,7 +43,7 @@ public class AccountList extends BaseListEditor<String> implements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_accounts)));
}
......
package com.xabber.android.ui.preferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import com.xabber.android.ui.helper.PreferenceSummaryHelper;
import com.xabber.android.ui.widget.RingtonePreference;
import java.util.HashMap;
import java.util.Map;
public abstract class BaseSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onInflate(savedInstanceState);
PreferenceSummaryHelper.updateSummary(getPreferenceScreen());
if (savedInstanceState == null)
operation(Operation.read);
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
preference.setOnPreferenceChangeListener(this);
if (preference instanceof EditTextPreference)
onPreferenceChange(preference,
((EditTextPreference) preference).getText());
else if (preference instanceof CheckBoxPreference)
onPreferenceChange(preference,
((CheckBoxPreference) preference).isChecked());
else if (preference instanceof ListPreference)
onPreferenceChange(preference,
((ListPreference) preference).getValue());
}
}
@Override
public void onStop() {
super.onStop();
operation(Operation.save);
}
/**
* Inflates layout.
*
* @param savedInstanceState
*/
protected abstract void onInflate(Bundle savedInstanceState);
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof EditTextPreference)
preference.setSummary((String) newValue);
return true;
}
/**
* Possible operations.
*/
private static enum Operation {
save, discard, read
}
protected void putValue(Map<String, Object> map, int resoureId, Object value) {
map.put(getString(resoureId), value);
}
protected String getString(Map<String, Object> map, int resoureId) {
return (String) map.get(getString(resoureId));
}
protected int getInt(Map<String, Object> map, int resoureId) {
return (Integer) map.get(getString(resoureId));
}
protected boolean getBoolean(Map<String, Object> map, int resoureId) {
return (Boolean) map.get(getString(resoureId));
}
protected Uri getUri(Map<String, Object> map, int resoureId) {
return (Uri) map.get(getString(resoureId));
}
/**
* @param intent
* @return Whether an operation succeed.
*/
private boolean operation(Operation selected) {
Map<String, Object> source = getValues();
if (selected == Operation.read)
setPreferences(source);
else {
Map<String, Object> result = getPreferences(source);
for (Map.Entry<String, Object> entry : source.entrySet())
if (!result.containsKey(entry.getKey()))
result.put(entry.getKey(), entry.getValue());
if (selected == Operation.save)
return setValues(source, result);
else if (selected == Operation.discard) {
for (String key : source.keySet())
if (hasChanges(source, result, key))
return false;
} else
throw new IllegalStateException();
}
return true;
}
/**
* @param source
* @param result
* @param key
* @return Whether value has been changed.
*/
protected boolean hasChanges(Map<String, Object> source,
Map<String, Object> result, String key) {
Object sourceValue = source.get(key);
Object targetValue = result.get(key);
return (sourceValue == null && targetValue != null)
|| (sourceValue != null && !sourceValue.equals(targetValue));
}
/**
* @param source
* @param result
* @param resourceId
* @return Whether value has been changed.
*/
protected boolean hasChanges(Map<String, Object> source,
Map<String, Object> result, int resourceId) {
return hasChanges(source, result, getString(resourceId));
}
/**
* @return Map with source values.
*/
protected abstract Map<String, Object> getValues();
/**
* Set values to the UI elements.
*
* @param source
*/
protected void setPreferences(Map<String, Object> source) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
Object value = source.get(preference.getKey());
setPreference(preference, value);
}
}
/**
* Set value to the UI element.
*
* @param preference
* @param value
*/
protected void setPreference(Preference preference, Object value) {
if (preference instanceof EditTextPreference)
((EditTextPreference) preference)
.setText(value instanceof Integer ? String.valueOf(value)
: (String) value);
else if (preference instanceof CheckBoxPreference)
((CheckBoxPreference) preference).setChecked((Boolean) value);
else if (preference instanceof ListPreference)
((ListPreference) preference).setValueIndex((Integer) value);
else if (preference instanceof RingtonePreference)
((RingtonePreference) preference).setUri((Uri) value);
}
/**
* Get values from the UI elements.
*
* @param source
* @return
*/
protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = new HashMap<>();
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int index = 0; index < preferenceScreen.getPreferenceCount(); index++) {
Preference preference = preferenceScreen.getPreference(index);
result.put(preference.getKey(), getPrefecence(preference, source));
}
return result;
}
/**
* Get value from the UI element.
*
* @param preference
* @param source
* @return
*/
protected Object getPrefecence(Preference preference,
Map<String, Object> source) {
if (preference instanceof PreferenceScreen)
return null;
else if (preference instanceof EditTextPreference) {
String value = ((EditTextPreference) preference).getText();
if (source.get(preference.getKey()) instanceof Integer)
try {
return Integer.parseInt(value);
} catch (Exception NumberFormatException) {
return null;
}
else
return value;
} else if (preference instanceof CheckBoxPreference)
return ((CheckBoxPreference) preference).isChecked();
else if (preference instanceof ListPreference)
return ((ListPreference) preference)
.findIndexOfValue(((ListPreference) preference).getValue());
else if (preference instanceof RingtonePreference)
return ((RingtonePreference) preference).getUri();
throw new IllegalStateException();
}
/**
* Apply result values.
*
* @param source
* @param result
* @return Whether operation succeed.
*/
protected abstract boolean setValues(Map<String, Object> source, Map<String, Object> result);
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.preferences;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
public class ChatEditor extends ManagedActivity
implements ChatEditorFragment.ChatEditorFragmentInteractionListener {
private String account;
private String user;
private AccountItem accountItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
account = getAccount(getIntent());
user = getUser(getIntent());
accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null || user == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
finish();
return;
}
setContentView(R.layout.activity_preferences);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container, new ChatEditorFragment()).commit();
}
}
public static Intent createIntent(Context context, String account,
String user) {
return new EntityIntentBuilder(context, ChatEditor.class)
.setAccount(account).setUser(user).build();
}
private static String getAccount(Intent intent) {
return EntityIntentBuilder.getAccount(intent);
}
private static String getUser(Intent intent) {
return EntityIntentBuilder.getUser(intent);
}
@Override
public String getAccount() {
return account;
}
@Override
public AccountItem getAccountItem() {
return accountItem;
}
@Override
public String getUser() {
return user;
}
}
package com.xabber.android.ui.preferences;
import android.app.Activity;
import android.os.Bundle;
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.androiddev.R;
import java.util.HashMap;
import java.util.Map;
public class ChatEditorFragment extends BaseSettingsFragment {
private ChatEditorFragmentInteractionListener mListener;
@Override
protected void onInflate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.chat_editor);
AccountItem accountItem = mListener.getAccountItem();
if (accountItem.getArchiveMode() == ArchiveMode.server
|| accountItem.getArchiveMode() == ArchiveMode.dontStore) {
getPreferenceScreen().removePreference(getPreferenceScreen()
.findPreference(getString(R.string.chat_save_history_key)));
}
}
@Override
protected Map<String, Object> getValues() {
Map<String, Object> map = new HashMap<>();
String account = mListener.getAccount();
String user = mListener.getUser();
putValue(map, R.string.chat_save_history_key, ChatManager.getInstance()
.isSaveMessages(account, user));
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));
putValue(map, R.string.chat_events_vibro_key, ChatManager.getInstance()
.isMakeVibro(account, user));
putValue(map, R.string.chat_events_sound_key, ChatManager.getInstance()
.getSound(account, user));
return map;
}
@Override
protected boolean setValues(Map<String, Object> source, Map<String, Object> result) {
String account = mListener.getAccount();
String user = mListener.getUser();
if (hasChanges(source, result, R.string.chat_save_history_key))
ChatManager.getInstance().setSaveMessages(account, user,
getBoolean(result, R.string.chat_save_history_key));
if (hasChanges(source, result, R.string.chat_events_visible_chat_key))
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))
ChatManager.getInstance().setShowText(account, user,
getBoolean(result, R.string.chat_events_show_text_key));
if (hasChanges(source, result, R.string.chat_events_vibro_key))
ChatManager.getInstance().setMakeVibro(account, user,
getBoolean(result, R.string.chat_events_vibro_key));
if (hasChanges(source, result, R.string.chat_events_sound_key))
ChatManager.getInstance().setSound(account, user,
getUri(result, R.string.chat_events_sound_key));
return true;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (ChatEditorFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ChatEditorFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface ChatEditorFragmentInteractionListener {
public String getAccount();
public AccountItem getAccountItem();
public String getUser();
}
}
......@@ -18,7 +18,7 @@ public class ChatSettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.chat_viewer)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
......@@ -18,7 +18,7 @@ public class ConnectionSettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_connection)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
......@@ -17,7 +17,7 @@ public class ContactListSettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_contacts)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.preferences;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.vcard.OnVCardListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.android.data.intent.AccountIntentBuilder;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.data.roster.RosterContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.vcard.AddressProperty;
import com.xabber.xmpp.vcard.AddressType;
import com.xabber.xmpp.vcard.EmailType;
import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.VCardProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ContactViewer extends ManagedActivity implements
OnVCardListener, OnContactChangedListener, OnAccountChangedListener {
private static final String SAVED_VCARD = "com.xabber.android.ui.preferences.ContactViewer.SAVED_VCARD";
private static final String SAVED_VCARD_ERROR = "com.xabber.android.ui.preferences.ContactViewer.SAVED_VCARD_ERROR";
private String account;
private String bareAddress;
private VCard vCard;
private boolean vCardError;
private static final Map<AddressType, Integer> ADDRESS_TYPE_MAP = new HashMap<>();
private static final Map<AddressProperty, Integer> ADDRESS_PROPERTY_MAP = new HashMap<>();
private static final Map<TelephoneType, Integer> TELEPHONE_TYPE_MAP = new HashMap<>();
private static final Map<EmailType, Integer> EMAIL_TYPE_MAP = new HashMap<>();
static {
ADDRESS_TYPE_MAP.put(AddressType.DOM, R.string.vcard_type_dom);
ADDRESS_TYPE_MAP.put(AddressType.HOME, R.string.vcard_type_home);
ADDRESS_TYPE_MAP.put(AddressType.INTL, R.string.vcard_type_intl);
ADDRESS_TYPE_MAP.put(AddressType.PARCEL, R.string.vcard_type_parcel);
ADDRESS_TYPE_MAP.put(AddressType.POSTAL, R.string.vcard_type_postal);
ADDRESS_TYPE_MAP.put(AddressType.PREF, R.string.vcard_type_pref);
ADDRESS_TYPE_MAP.put(AddressType.WORK, R.string.vcard_type_work);
if (ADDRESS_TYPE_MAP.size() != AddressType.values().length)
throw new IllegalStateException();
ADDRESS_PROPERTY_MAP.put(AddressProperty.CTRY,
R.string.vcard_address_ctry);
ADDRESS_PROPERTY_MAP.put(AddressProperty.EXTADR,
R.string.vcard_address_extadr);
ADDRESS_PROPERTY_MAP.put(AddressProperty.LOCALITY,
R.string.vcard_address_locality);
ADDRESS_PROPERTY_MAP.put(AddressProperty.PCODE,
R.string.vcard_address_pcode);
ADDRESS_PROPERTY_MAP.put(AddressProperty.POBOX,
R.string.vcard_address_pobox);
ADDRESS_PROPERTY_MAP.put(AddressProperty.REGION,
R.string.vcard_address_region);
ADDRESS_PROPERTY_MAP.put(AddressProperty.STREET,
R.string.vcard_address_street);
if (ADDRESS_PROPERTY_MAP.size() != AddressProperty.values().length)
throw new IllegalStateException();
TELEPHONE_TYPE_MAP.put(TelephoneType.BBS, R.string.vcard_type_bbs);
TELEPHONE_TYPE_MAP.put(TelephoneType.CELL, R.string.vcard_type_cell);
TELEPHONE_TYPE_MAP.put(TelephoneType.FAX, R.string.vcard_type_fax);
TELEPHONE_TYPE_MAP.put(TelephoneType.HOME, R.string.vcard_type_home);
TELEPHONE_TYPE_MAP.put(TelephoneType.ISDN, R.string.vcard_type_isdn);
TELEPHONE_TYPE_MAP.put(TelephoneType.MODEM, R.string.vcard_type_modem);
TELEPHONE_TYPE_MAP.put(TelephoneType.MSG, R.string.vcard_type_msg);
TELEPHONE_TYPE_MAP.put(TelephoneType.PAGER, R.string.vcard_type_pager);
TELEPHONE_TYPE_MAP.put(TelephoneType.PCS, R.string.vcard_type_pcs);
TELEPHONE_TYPE_MAP.put(TelephoneType.PREF, R.string.vcard_type_pref);
TELEPHONE_TYPE_MAP.put(TelephoneType.VIDEO, R.string.vcard_type_video);
TELEPHONE_TYPE_MAP.put(TelephoneType.VOICE, R.string.vcard_type_voice);
TELEPHONE_TYPE_MAP.put(TelephoneType.WORK, R.string.vcard_type_work);
if (TELEPHONE_TYPE_MAP.size() != TelephoneType.values().length)
throw new IllegalStateException();
EMAIL_TYPE_MAP.put(EmailType.HOME, R.string.vcard_type_home);
EMAIL_TYPE_MAP.put(EmailType.INTERNET, R.string.vcard_type_internet);
EMAIL_TYPE_MAP.put(EmailType.PREF, R.string.vcard_type_pref);
EMAIL_TYPE_MAP.put(EmailType.WORK, R.string.vcard_type_work);
EMAIL_TYPE_MAP.put(EmailType.X400, R.string.vcard_type_x400);
if (EMAIL_TYPE_MAP.size() != EmailType.values().length)
throw new IllegalStateException();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
// View information about contact from system contact list
Uri data = getIntent().getData();
if (data != null && "content".equals(data.getScheme())) {
List<String> segments = data.getPathSegments();
if (segments.size() == 2 && "data".equals(segments.get(0))) {
Long id;
try {
id = Long.valueOf(segments.get(1));
} catch (NumberFormatException e) {
id = null;
}
if (id != null)
// FIXME: Will be empty while application is loading
for (RosterContact rosterContact : RosterManager
.getInstance().getContacts())
if (id.equals(rosterContact.getViewId())) {
account = rosterContact.getAccount();
bareAddress = rosterContact.getUser();
break;
}
}
}
} else {
account = getAccount(getIntent());
bareAddress = Jid.getBareAddress(getUser(getIntent()));
}
if (account == null || bareAddress == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
finish();
return;
}
vCard = null;
vCardError = false;
if (savedInstanceState != null) {
vCardError = savedInstanceState
.getBoolean(SAVED_VCARD_ERROR, false);
String xml = savedInstanceState.getString(SAVED_VCARD);
if (xml != null)
try {
XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
true);
parser.setInput(new StringReader(xml));
int eventType = parser.next();
if (eventType != XmlPullParser.START_TAG)
throw new IllegalStateException(
String.valueOf(eventType));
if (!VCard.ELEMENT_NAME.equals(parser.getName()))
throw new IllegalStateException(parser.getName());
if (!VCard.NAMESPACE.equals(parser.getNamespace()))
throw new IllegalStateException(parser.getNamespace());
vCard = (VCard) (new VCardProvider()).parseIQ(parser);
} catch (Exception e) {
LogManager.exception(this, e);
}
}
setTitle(getString(R.string.contact_viewer));
setContentView(R.layout.activity_preferences);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container, new ContactViewerFragment()).commit();
}
}
@Override
protected void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnVCardListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class,
this);
Application.getInstance().addUIListener(OnAccountChangedListener.class,
this);
if (vCard == null && !vCardError)
VCardManager.getInstance().request(account, bareAddress, null);
ContactViewerFragment contactViewerFragment = getFragment();
contactViewerFragment.updateContact(account, bareAddress);
contactViewerFragment.updateVCard(vCard);
}
private ContactViewerFragment getFragment() {
return (ContactViewerFragment) getFragmentManager()
.findFragmentById(R.id.preferences_activity_container);
}
@Override
protected void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(
OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(
OnAccountChangedListener.class, this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_VCARD_ERROR, vCardError);
if (vCard != null)
outState.putString(SAVED_VCARD, vCard.getChildElementXML());
}
@Override
public void onVCardReceived(String account, String bareAddress, VCard vCard) {
if (!this.account.equals(account)
|| !this.bareAddress.equals(bareAddress))
return;
this.vCard = vCard;
this.vCardError = false;
getFragment().updateVCard(vCard);
}
@Override
public void onVCardFailed(String account, String bareAddress) {
if (!this.account.equals(account)
|| !this.bareAddress.equals(bareAddress))
return;
this.vCard = null;
this.vCardError = true;
getFragment().updateVCard(vCard);
Application.getInstance().onError(R.string.XMPP_EXCEPTION);
}
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
for (BaseEntity entity : entities)
if (entity.equals(account, bareAddress)) {
getFragment().updateContact(account, bareAddress);
break;
}
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
if (accounts.contains(account))
getFragment().updateContact(account, bareAddress);
}
public static Intent createIntent(Context context, String account,
String user) {
return new EntityIntentBuilder(context, ContactViewer.class)
.setAccount(account).setUser(user).build();
}
private static String getAccount(Intent intent) {
return AccountIntentBuilder.getAccount(intent);
}
private static String getUser(Intent intent) {
return EntityIntentBuilder.getUser(intent);
}
public static Map<AddressType, Integer> getAddressTypeMap() {
return ADDRESS_TYPE_MAP;
}
public static Map<AddressProperty, Integer> getAddressPropertyMap() {
return ADDRESS_PROPERTY_MAP;
}
public static Map<TelephoneType, Integer> getTelephoneTypeMap() {
return TELEPHONE_TYPE_MAP;
}
public static Map<EmailType, Integer> getEmailTypeMap() {
return EMAIL_TYPE_MAP;
}
}
package com.xabber.android.ui.preferences;
import android.os.Bundle;
import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import com.xabber.android.data.extension.capability.CapabilitiesManager;
import com.xabber.android.data.extension.capability.ClientInfo;
import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.ResourceItem;
import com.xabber.android.data.roster.RosterContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.helper.PreferenceSummaryHelper;
import com.xabber.android.ui.widget.StatusPreference;
import com.xabber.androiddev.R;
import com.xabber.xmpp.vcard.Address;
import com.xabber.xmpp.vcard.AddressProperty;
import com.xabber.xmpp.vcard.AddressType;
import com.xabber.xmpp.vcard.Email;
import com.xabber.xmpp.vcard.EmailType;
import com.xabber.xmpp.vcard.NameProperty;
import com.xabber.xmpp.vcard.Organization;
import com.xabber.xmpp.vcard.Telephone;
import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.VCardProperty;
import java.util.ArrayList;
import java.util.List;
public class ContactViewerFragment extends android.preference.PreferenceFragment {
private List<PreferenceCategory> addresses;
private List<PreferenceCategory> telephones;
private List<PreferenceCategory> emails;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addresses = new ArrayList<>();
telephones = new ArrayList<>();
emails = new ArrayList<>();
addPreferencesFromResource(R.xml.contact_viewer);
PreferenceSummaryHelper.updateSummary(getPreferenceScreen());
}
/**
* Sets value for the preference by its id.
*
* @param resourceId
* @param value
*/
private void setValue(int resourceId, String value) {
if (value == null)
value = "";
findPreference(getString(resourceId)).setSummary(value);
}
/**
* @param source
* @param value
* @param splitter
* @return Concatenated source and value with splitter if necessary.
*/
private String addString(String source, String value, String splitter) {
if (value == null || "".equals(value))
return source;
if (source == null || "".equals(source))
return value;
return source + splitter + value;
}
public void updateContact(String account, String bareAddress) {
setValue(R.string.contact_viewer_jid, bareAddress);
RosterContact rosterContact = RosterManager.getInstance()
.getRosterContact(account, bareAddress);
setValue(R.string.contact_viewer_name, rosterContact == null ? null
: rosterContact.getRealName());
PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference(getString(R.string.contact_viewer_resources));
preferenceCategory.removeAll();
if (rosterContact != null && rosterContact.isConnected())
for (ResourceItem resourceItem : PresenceManager.getInstance()
.getResourceItems(account, bareAddress)) {
StatusPreference preference = new StatusPreference(getActivity());
preference.setLayoutResource(R.layout.info_preference);
preference.setStatusMode(resourceItem.getStatusMode());
String user = resourceItem.getUser(bareAddress);
ClientInfo clientInfo = CapabilitiesManager.getInstance()
.getClientInfo(account, user);
String client;
if (clientInfo == null) {
CapabilitiesManager.getInstance().request(account, user);
client = getString(R.string.please_wait);
} else if (clientInfo == CapabilitiesManager.INVALID_CLIENT_INFO) {
client = getString(R.string.unknown);
} else {
String name = clientInfo.getName();
if (name == null)
name = getString(R.string.unknown);
String type = clientInfo.getType();
if (type == null)
type = getString(R.string.unknown);
client = getString(R.string.contact_viewer_client_info,
name, type);
}
preference.setTitle(getString(
R.string.contact_viewer_resource_summary,
resourceItem.getVerbose(), resourceItem.getPriority(),
client));
preference.setSummary(resourceItem.getStatusText());
preferenceCategory.addPreference(preference);
}
}
public void updateVCard(VCard vCard) {
if (vCard == null)
return;
setValue(R.string.vcard_nick_name,
vCard.getField(VCardProperty.NICKNAME));
setValue(R.string.vcard_formatted_name, vCard.getFormattedName());
setValue(R.string.vcard_prefix_name,
vCard.getField(NameProperty.PREFIX));
setValue(R.string.vcard_given_name, vCard.getField(NameProperty.GIVEN));
setValue(R.string.vcard_middle_name,
vCard.getField(NameProperty.MIDDLE));
setValue(R.string.vcard_family_name,
vCard.getField(NameProperty.FAMILY));
setValue(R.string.vcard_suffix_name,
vCard.getField(NameProperty.SUFFIX));
setValue(R.string.vcard_birth_date, vCard.getField(VCardProperty.BDAY));
setValue(R.string.vcard_title, vCard.getField(VCardProperty.TITLE));
setValue(R.string.vcard_role, vCard.getField(VCardProperty.ROLE));
List<Organization> organizations = vCard.getOrganizations();
String organization;
if (organizations.isEmpty())
organization = null;
else {
organization = organizations.get(0).getName();
for (String unit : organizations.get(0).getUnits())
organization = addString(organization, unit, "\n");
}
setValue(R.string.vcard_organization, organization);
setValue(R.string.vcard_url, vCard.getField(VCardProperty.URL));
String categories = null;
for (String category : vCard.getCategories())
categories = addString(categories, category, "\n");
setValue(R.string.vcard_categories, categories);
setValue(R.string.vcard_note, vCard.getField(VCardProperty.NOTE));
setValue(R.string.vcard_decsription, vCard.getField(VCardProperty.DESC));
PreferenceScreen screen = getPreferenceScreen();
for (PreferenceCategory category : addresses)
screen.removePreference(category);
for (PreferenceCategory category : telephones)
screen.removePreference(category);
for (PreferenceCategory category : emails)
screen.removePreference(category);
for (Address address : vCard.getAddresses()) {
String types = null;
for (AddressType type : address.getTypes())
types = addString(types, getString(ContactViewer.getAddressTypeMap().get(type)),
", ");
String value = null;
for (AddressProperty property : AddressProperty.values())
value = addString(value, address.getProperties().get(property),
"\n");
PreferenceScreen addressScreen = createTypedCategory(
R.string.vcard_address, types, value);
for (AddressProperty property : AddressProperty.values())
addPreferenceScreen(addressScreen,
ContactViewer.getAddressPropertyMap().get(property), address
.getProperties().get(property));
}
for (Telephone telephone : vCard.getTelephones()) {
String types = null;
for (TelephoneType type : telephone.getTypes())
types = addString(types,
getString(ContactViewer.getTelephoneTypeMap().get(type)), ", ");
createTypedCategory(R.string.vcard_telephone, types,
telephone.getValue());
}
for (Email email : vCard.getEmails()) {
String types = null;
for (EmailType type : email.getTypes())
types = addString(types, getString(ContactViewer.getEmailTypeMap().get(type)),
", ");
createTypedCategory(R.string.vcard_email, types, email.getValue());
}
}
private PreferenceScreen createTypedCategory(int title, String types,
String value) {
PreferenceCategory preferenceCategory = new PreferenceCategory(getActivity());
preferenceCategory.setTitle(title);
getPreferenceScreen().addPreference(preferenceCategory);
addPreferenceScreen(preferenceCategory, R.string.vcard_type, types);
return addPreferenceScreen(preferenceCategory, title, value);
}
private PreferenceScreen addPreferenceScreen(PreferenceGroup container,
int title, String summary) {
PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity());
preference.setLayoutResource(R.layout.info_preference);
preference.setTitle(title);
preference.setSummary(summary == null ? "" : summary);
container.addPreference(preference);
return preference;
}
}
......@@ -18,7 +18,7 @@ public class DebugSettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_debug)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
......@@ -19,7 +19,7 @@ public class NotificationsSettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_events)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
......@@ -14,25 +14,19 @@
*/
package com.xabber.android.ui.preferences;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.PatternSyntaxException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.intent.SegmentIntentBuilder;
import com.xabber.android.data.message.phrase.Phrase;
import com.xabber.android.data.message.phrase.PhraseManager;
import com.xabber.android.ui.helper.BaseSettingsActivity;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
public class PhraseEditor extends BaseSettingsActivity {
public class PhraseEditor extends ManagedActivity
implements PhraseEditorFragment.OnPhraseEditorFragmentInteractionListener {
private Phrase phrase;
......@@ -40,90 +34,63 @@ public class PhraseEditor extends BaseSettingsActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@Override
protected void onInflate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.phrase_editor);
Integer index = getPhraseIndex(getIntent());
if (index == null) {
phrase = null;
setTitle(R.string.phrase_add);
} else {
phrase = PhraseManager.getInstance().getPhrase(index);
if (phrase == null) {
finish();
return;
}
String title = phrase.getText();
if ("".equals(title))
title = Application.getInstance().getString(
R.string.phrase_empty);
setTitle(title);
}
}
Integer index = getPhraseIndex(getIntent());
if (index == null) {
phrase = null;
setTitle(R.string.phrase_add);
} else {
phrase = PhraseManager.getInstance().getPhrase(index);
if (phrase == null) {
finish();
return;
}
String title = phrase.getText();
if ("".equals(title))
title = Application.getInstance().getString(
R.string.phrase_empty);
setTitle(title);
}
@Override
protected Map<String, Object> getValues() {
Map<String, Object> source = new HashMap<String, Object>();
putValue(source, R.string.phrase_text_key,
phrase == null ? "" : phrase.getText());
putValue(source, R.string.phrase_user_key,
phrase == null ? "" : phrase.getUser());
putValue(source, R.string.phrase_group_key, phrase == null ? ""
: phrase.getGroup());
putValue(source, R.string.phrase_regexp_key, phrase == null ? false
: phrase.isRegexp());
putValue(source, R.string.phrase_sound_key,
phrase == null ? Settings.System.DEFAULT_NOTIFICATION_URI
: phrase.getSound());
return source;
}
setContentView(R.layout.activity_preferences);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container, new PhraseEditorFragment()).commit();
}
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
String text = getString(result, R.string.phrase_text_key);
String user = getString(result, R.string.phrase_user_key);
String group = getString(result, R.string.phrase_group_key);
boolean regexp = getBoolean(result, R.string.phrase_regexp_key);
Uri sound = getUri(result, R.string.phrase_sound_key);
if (regexp)
try {
Phrase.compile(text);
Phrase.compile(user);
Phrase.compile(group);
} catch (PatternSyntaxException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return false;
}
if (phrase == null && "".equals(text) && "".equals(user)
&& "".equals(group))
return true;
PhraseManager.getInstance().updateOrCreatePhrase(phrase, text, user,
group, regexp, sound);
return true;
}
public static Intent createIntent(Context context) {
public static Intent createIntent(Context context) {
return createIntent(context, null);
}
public static Intent createIntent(Context context, Integer phraseIndex) {
SegmentIntentBuilder<?> builder = new SegmentIntentBuilder<SegmentIntentBuilder<?>>(
context, PhraseEditor.class);
if (phraseIndex != null)
builder.addSegment(phraseIndex.toString());
return builder.build();
}
public static Intent createIntent(Context context, Integer phraseIndex) {
SegmentIntentBuilder<?> builder = new SegmentIntentBuilder<>(
context, PhraseEditor.class);
if (phraseIndex != null)
builder.addSegment(phraseIndex.toString());
return builder.build();
}
private Integer getPhraseIndex(Intent intent) {
String value = SegmentIntentBuilder.getSegment(intent, 0);
if (value == null)
return null;
else
return Integer.valueOf(value);
}
private Integer getPhraseIndex(Intent intent) {
String value = SegmentIntentBuilder.getSegment(intent, 0);
if (value == null)
return null;
else
return Integer.valueOf(value);
}
@Override
public Phrase getPhrase() {
return phrase;
}
@Override
public void setPhrase(Phrase phrase) {
this.phrase = phrase;
}
}
package com.xabber.android.ui.preferences;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import com.xabber.android.data.message.phrase.Phrase;
import com.xabber.android.data.message.phrase.PhraseManager;
import com.xabber.androiddev.R;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.PatternSyntaxException;
public class PhraseEditorFragment extends BaseSettingsFragment {
private OnPhraseEditorFragmentInteractionListener mListener;
@Override
protected void onInflate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.phrase_editor);
}
@Override
protected Map<String, Object> getValues() {
Phrase phrase = mListener.getPhrase();
Map<String, Object> source = new HashMap<>();
putValue(source, R.string.phrase_text_key,
phrase == null ? "" : phrase.getText());
putValue(source, R.string.phrase_user_key,
phrase == null ? "" : phrase.getUser());
putValue(source, R.string.phrase_group_key, phrase == null ? ""
: phrase.getGroup());
putValue(source, R.string.phrase_regexp_key, phrase != null && phrase.isRegexp());
putValue(source, R.string.phrase_sound_key,
phrase == null ? Settings.System.DEFAULT_NOTIFICATION_URI
: phrase.getSound());
return source;
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
String text = getString(result, R.string.phrase_text_key);
String user = getString(result, R.string.phrase_user_key);
String group = getString(result, R.string.phrase_group_key);
boolean regexp = getBoolean(result, R.string.phrase_regexp_key);
Uri sound = getUri(result, R.string.phrase_sound_key);
Log.i("PhraseEditorFragment", "setValues. text: " + text);
if (regexp) {
try {
Phrase.compile(text);
Phrase.compile(user);
Phrase.compile(group);
} catch (PatternSyntaxException e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
return false;
}
}
Phrase phrase = mListener.getPhrase();
if (phrase == null && "".equals(text) && "".equals(user) && "".equals(group)) {
return true;
}
Log.i("PhraseEditorFragment", "updateOrCreatePhrase");
PhraseManager.getInstance().updateOrCreatePhrase(phrase, text, user, group, regexp, sound);
mListener.setPhrase(phrase);
return true;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnPhraseEditorFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnThemeSettingsFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnPhraseEditorFragmentInteractionListener {
public Phrase getPhrase();
public void setPhrase(Phrase phrase);
}
}
......@@ -31,7 +31,7 @@ public class PhraseList extends BaseListEditor<Integer> {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
......
......@@ -39,7 +39,7 @@ public class PreferenceEditor extends ManagedActivity
.add(R.id.preferences_activity_container, new PreferencesFragment()).commit();
}
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Force request sound. This will set default value if not specified.
SettingsManager.eventsSound();
......
......@@ -18,7 +18,7 @@ public class SecuritySettings extends ManagedActivity {
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_security)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
......
......@@ -19,7 +19,7 @@ public class ThemeSettings extends ManagedActivity
setContentView(R.layout.activity_preferences);
getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(PreferenceSummaryHelper.getPreferenceTitle(getString(R.string.preference_interface)));
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2013, Redsolution LTD. All rights reserved.
This file is part of Xabber project; you can redistribute it and/or
modify it under the terms of the GNU General Public License, Version 3.
Xabber is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License,
along with this program. If not, see http://www.gnu.org/licenses/.
-->
<resources>
<style name="Theme" parent="@android:style/Theme.DeviceDefault">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.NoTitleBar" parent="@android:style/Theme.DeviceDefault.NoActionBar">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Dark" parent="@android:style/Theme.DeviceDefault">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_dark</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Dark.NoTitleBar" parent="@android:style/Theme.DeviceDefault.NoActionBar">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_dark</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Light" parent="@android:style/Theme.DeviceDefault.Light">
<item name="expanderBackground">@drawable/expander_background_light</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimary</item>
<item name="expanderIndicator">@drawable/expander_indicator_light</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_light</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_light</item>
</style>
<style name="Theme.Light.NoTitleBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="expanderBackground">@drawable/expander_background_light</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimary</item>
<item name="expanderIndicator">@drawable/expander_indicator_light</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_light</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_light</item>
</style>
</resources>
\ No newline at end of file
......@@ -13,30 +13,7 @@
along with this program. If not, see http://www.gnu.org/licenses/.
-->
<resources>
<style name="Theme" parent="@android:style/Theme">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.NoTitleBar" parent="@android:style/Theme.NoTitleBar">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Dark" parent="@android:style/Theme.Black">
<style name="Theme" parent="Theme.AppCompat">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
......@@ -46,38 +23,4 @@
<item name="titleAccountBackground">@drawable/title_account_background_dark</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Dark.NoTitleBar" parent="@android:style/Theme.Black.NoTitleBar">
<item name="expanderBackground">@drawable/expander_background_dark</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimaryInverse</item>
<item name="expanderIndicator">@drawable/expander_indicator_dark</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_dark</item>
<item name="titleAccountBackground">@drawable/title_account_background_dark</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_dark</item>
</style>
<style name="Theme.Light" parent="@android:style/Theme.Light">
<item name="expanderBackground">@drawable/expander_background_light</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimary</item>
<item name="expanderIndicator">@drawable/expander_indicator_light</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_light</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_light</item>
</style>
<style name="Theme.Light.NoTitleBar" parent="@android:style/Theme.Light.NoTitleBar">
<item name="expanderBackground">@drawable/expander_background_light</item>
<item name="expanderGroupColor">?android:attr/textColorPrimary</item>
<item name="expanderAccountColor">?android:attr/textColorPrimary</item>
<item name="expanderIndicator">@drawable/expander_indicator_light</item>
<item name="accountBackground">@drawable/account_background</item>
<item name="titleMainBackground">@drawable/title_main_background_light</item>
<item name="titleAccountBackground">@drawable/title_account_background_light</item>
<item name="textColorPrimaryNoSelected">@color/bright_foreground_light</item>
</style>
</resources>
\ No newline at end of file
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