android/app/src/main/java/org/openlp/android2/activities/ConnectionActivity.java

430 lines
21 KiB
Java

/******************************************************************************
* OpenLP - Open Source Lyrics Projection *
* --------------------------------------------------------------------------- *
* Copyright (c) 2011-2014 Raoul Snyman *
* Portions copyright (c) 2011-2014 Tim Bentley, Johan Mynhardt *
* *
* --------------------------------------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the Free *
* Software Foundation; version 2 of the License. *
* *
* This program 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, write to the Free Software Foundation, Inc., 59 *
* Temple Place, Suite 330, Boston, MA 02111-1307 USA *
*******************************************************************************/
package org.openlp.android2.activities;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.*;
import android.text.InputType;
import android.util.Log;
import android.widget.Toast;
import org.openlp.android2.R;
public class ConnectionActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new ConnectionFragment())
.commit();
}
public static class ConnectionFragment extends PreferenceFragment {
private final String KEY_PREFERENCE_DISPLAY = "preferenceDisplay";
private final String KEY_SERVER_ID = "keyServerId";
private final String PREFERENCE_DISPLAY_SERVER = "displayServer";
private final String LOG_TAG = ConnectionFragment.class.getName();
private PreferenceScreen preferenceScreen = null;
private boolean resume = true;
private static String getHostConfigTitleKey(int id) {
return HostConfig.KEY_PREFIX + id + ".title";
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.empty_preferences);
getPreferenceManager()
.setSharedPreferencesName(getString(R.string.keySharedPreferences));
preferenceScreen = getPreferenceScreen() == null
? getPreferenceManager().createPreferenceScreen(getActivity())
: getPreferenceScreen();
preferenceScreen.removeAll();
String preferenceDisplay = getActivity().getIntent().getStringExtra(KEY_PREFERENCE_DISPLAY);
if (preferenceDisplay != null
&& preferenceDisplay.equalsIgnoreCase(PREFERENCE_DISPLAY_SERVER)) {
constructServerView(getActivity().getIntent().getIntExtra(KEY_SERVER_ID, 1));
} else {
resume = false;
constructOverviewScreen();
}
}
private void constructOverviewScreen() {
getPreferenceScreen().removeAll();
Log.i(LOG_TAG, "constructOverviewScreen");
Preference configPref = new Preference(getActivity());
configPref.setTitle(getString(R.string.connection_available_configurations));
configPref.setSummary(getString(R.string.connection_add_by_menu));
configPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int nextId = getNextPrefId(
getHostConfigMap(
getPreferenceManager()
.getSharedPreferences().getAll()
)
);
Preference newPreference = simpleClickablePreferenceFromHostConfig(
new HostConfig(
getString(R.string.connection_profile_server),
nextId));
newPreference.setTitle(getString(R.string.connection_profile_new_server));
newPreference.setSummary(getString(R.string.connection_profile_not_saved));
preferenceScreen.addPreference(newPreference);
return true;
}
});
preferenceScreen.addPreference(configPref);
List<HostConfig> hostConfigs = getHostConfigs();
if (!hostConfigs.isEmpty()) {
for (final HostConfig config : getHostConfigs()) {
preferenceScreen.addPreference(
simpleClickablePreferenceFromHostConfig(config));
}
}
}
private void constructServerView(int hostId) {
HostConfig hostConfig = hostConfigFromPreferencesForHostId(
hostId,
getHostConfigMap(
getPreferenceManager()
.getSharedPreferences()
.getAll()
)
);
addPreferenceCategory(preferenceScreen, hostConfig);
}
private Preference simpleClickablePreferenceFromHostConfig(final HostConfig config) {
final Preference serverConfig = new Preference(getActivity());
serverConfig.setTitle(config.title.getSummary());
Boolean useSsl = getPreferenceManager()
.getSharedPreferences()
.getBoolean(config.useSsl.getKey(), false);
serverConfig.setSummary(String.format(
"%s:%s %s",
config.hostAddress.getText(),
config.hostPort.getText(),
useSsl ? "(SSL)" : "")
);
serverConfig.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent serverConfigIntent = new Intent(
getActivity(),
ConnectionActivity.class
);
serverConfigIntent.putExtra(KEY_PREFERENCE_DISPLAY,
PREFERENCE_DISPLAY_SERVER);
serverConfigIntent.putExtra(KEY_SERVER_ID, config.id);
startActivity(serverConfigIntent);
return true;
}
});
return serverConfig;
}
private List<HostConfig> getHostConfigs() {
return hostConfigsFromPreferences(
getHostConfigMap(
getPreferenceManager()
.getSharedPreferences()
.getAll()
)
);
}
private Map<String, ?> getHostConfigMap(Map<String, ?> preferences) {
Map<String, Object> configMap = new TreeMap<String, Object>();
List<String> sortedKeys = new ArrayList<String>(preferences.keySet());
Collections.sort(sortedKeys);
for (String key : sortedKeys) {
if (key.startsWith(HostConfig.KEY_PREFIX)) {
configMap.put(key, preferences.get(key));
}
}
return configMap;
}
private List<HostConfig> hostConfigsFromPreferences(Map<String, ?> preferences) {
List<Integer> hostIds = getHostIds(preferences);
List<HostConfig> hostConfigs = new ArrayList<HostConfig>();
for (Integer id : hostIds) {
hostConfigs.add(hostConfigFromPreferencesForHostId(id, preferences));
}
return hostConfigs;
}
private HostConfig hostConfigFromPreferencesForHostId(int hostId, Map<String, ?> preferences) {
Object titleValue = preferences.get(getHostConfigTitleKey(hostId));
String hostTitle = titleValue == null ? getString(R.string.connection_profile_server) : titleValue.toString();
HostConfig hostConfig = new HostConfig(
hostTitle, hostId
);
Object hostValueObject = preferences.get(hostConfig.hostAddress.getKey());
String hostValue = hostValueObject == null
? getString(R.string.hostDefaultValue) : hostValueObject.toString();
hostConfig.hostAddress.setText(hostValue);
hostConfig.hostAddress.setSummary(hostValue);
Object portValueObject = preferences.get(hostConfig.hostPort.getKey());
String portValue = portValueObject == null
? getString(R.string.portDefaultValue) : portValueObject.toString();
hostConfig.hostPort.setText(portValue);
hostConfig.hostPort.setSummary(portValue);
Object useridValueObject = preferences.get(hostConfig.userid.getKey());
String useridValue = useridValueObject == null
? getString(R.string.useridDefaultValue) : useridValueObject.toString();
hostConfig.userid.setText(useridValue);
hostConfig.userid.setSummary(useridValue);
Object passwordValueObject = preferences.get(hostConfig.password.getKey());
String passwordValue = passwordValueObject == null
? getString(R.string.passwordDefaultValue) : passwordValueObject.toString();
hostConfig.password.setText(passwordValue);
hostConfig.password.setSummary(passwordValue);
return hostConfig;
}
private int getNextPrefId(Map<String, ?> preferences) {
int max = 0;
for (String key : preferences.keySet()) {
int prefValue = Integer.valueOf(key.split("\\.")[2]);
max = prefValue > max ? prefValue : max;
}
return max + 1;
}
private List<Integer> getHostIds(Map<String, ?> preferences) {
Set<Integer> hashSet = new HashSet<Integer>();
for (String key : preferences.keySet()) {
hashSet.add(Integer.valueOf(key.split("\\.")[2]));
}
List<Integer> hostIds = new ArrayList<Integer>(hashSet);
Collections.sort(hostIds);
Log.i(LOG_TAG, "Got HostIds: " + hostIds);
return hostIds;
}
private boolean addPreferenceCategory(PreferenceScreen preferenceScreen, HostConfig hostConfig) {
PreferenceCategory preferenceCategory = new PreferenceCategory(getActivity());
preferenceCategory.setTitle(hostConfig.title.getSummary());
preferenceCategory.setKey("key.preference.category");
preferenceScreen.addPreference(preferenceCategory);
preferenceCategory.addPreference(hostConfig.title);
preferenceCategory.addPreference(hostConfig.hostAddress);
preferenceCategory.addPreference(hostConfig.hostPort);
preferenceCategory.addPreference(hostConfig.useSsl);
preferenceCategory.addPreference(hostConfig.userid);
preferenceCategory.addPreference(hostConfig.password);
preferenceCategory.addPreference(hostConfig.remove);
preferenceCategory.addPreference(hostConfig.activate);
return true;
}
@Override
public void onResume() {
super.onResume();
if (resume) {
Log.i(LOG_TAG, "Resuming...");
} else {
constructOverviewScreen();
Log.i(LOG_TAG, "Not resuming...");
}
}
private class HostConfig {
static final String KEY_PREFIX = "host.config.";
final int id;
final EditTextPreference title;
final EditTextPreference hostAddress;
final EditTextPreference hostPort;
final CheckBoxPreference useSsl;
final EditTextPreference userid;
final EditTextPreference password;
final Preference remove;
final Preference activate;
Preference.OnPreferenceChangeListener onPreferenceChangeListener =
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
preference.setSummary("" + o);
if (preference.getKey().endsWith(".title")) {
preferenceScreen
.findPreference("key.preference.category")
.setTitle("" + o);
}
return true;
}
};
HostConfig(String sTitle, int id) {
this.id = id;
title = new EditTextPreference(getActivity());
title.setSummary(getString(R.string.url));
title.getEditText().setHint(getString(R.string.url));
title.setTitle(getString(R.string.connection_profile_title));
title.setKey(KEY_PREFIX + id + ".title");
title.setDefaultValue(getString(R.string.url));
title.setSummary(sTitle);
title.setDialogTitle(getString(R.string.connection_profile_title));
title.setOnPreferenceChangeListener(onPreferenceChangeListener);
hostAddress = new EditTextPreference(getActivity());
hostAddress.setTitle(getString(R.string.urlHint));
hostAddress.setKey(KEY_PREFIX + id + ".address");
hostAddress.getEditText().setHint(R.string.urlHint);
hostAddress.setSummary(getString(R.string.urlHint));
hostAddress.setDialogTitle(getString(R.string.urlHint));
hostAddress.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
hostAddress.setOnPreferenceChangeListener(onPreferenceChangeListener);
hostPort = new EditTextPreference(getActivity());
hostPort.setTitle(getString(R.string.port));
hostPort.setKey(KEY_PREFIX + id + ".port");
hostPort.setSummary(getString(R.string.port));
hostPort.setDialogTitle(getString(R.string.port));
hostPort.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
hostPort.setDefaultValue(getString(R.string.portDefaultValue));
hostPort.setOnPreferenceChangeListener(onPreferenceChangeListener);
useSsl = new CheckBoxPreference(getActivity());
useSsl.setTitle(getString(R.string.connection_profile_ssl_use));
useSsl.setSummary(getString(R.string.connection_profile_ssl_summary));
useSsl.setKey(KEY_PREFIX + id + ".usessl");
userid = new EditTextPreference(getActivity());
userid.setSummary(getString(R.string.connection_userid));
userid.getEditText().setHint(getString(R.string.connection_userid));
userid.setTitle(getString(R.string.connection_userid));
userid.setKey(KEY_PREFIX + id + ".userid");
userid.setDefaultValue(getString(R.string.useridDefaultValue));
userid.setSummary(getString(R.string.useridDefaultValue));
userid.setDialogTitle(getString(R.string.connection_userid));
userid.setOnPreferenceChangeListener(onPreferenceChangeListener);
password = new EditTextPreference(getActivity());
password.setSummary(getString(R.string.connection_password));
password.getEditText().setHint(getString(R.string.connection_password));
password.setTitle(getString(R.string.connection_password));
password.setKey(KEY_PREFIX + id + ".password");
password.setDefaultValue(getString(R.string.passwordDefaultValue));
password.setSummary(getString(R.string.passwordDefaultValue));
password.setDialogTitle(getString(R.string.connection_password));
password.setOnPreferenceChangeListener(onPreferenceChangeListener);
remove = new Preference(getActivity());
remove.setTitle(getString(R.string.connection_profile_remove));
remove.setSummary(getString(R.string.connection_profile_remove_summary));
remove.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.remove(hostAddress.getKey());
editor.remove(hostPort.getKey());
editor.remove(title.getKey());
editor.remove(useSsl.getKey());
editor.remove(userid.getKey());
editor.remove(password.getKey());
editor.commit();
getActivity().onBackPressed();
return false;
}
});
activate = new Preference(getActivity());
activate.setTitle(getString(R.string.connection_profile_activate));
activate.setSummary(getString(R.string.connection_profile_summary_activate));
activate.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
SharedPreferences.Editor editor = preferences.edit();
String host = preferences.getString(hostAddress.getKey(),
getString(R.string.hostDefaultValue));
editor.putString(getString(R.string.keyHost), host);
String port = preferences.getString(hostPort.getKey(),
getString(R.string.portDefaultValue));
editor.putString(getString(R.string.keyPort), port);
Boolean bUseSsl = preferences.getBoolean(useSsl.getKey(), false);
editor.putBoolean(getString(R.string.key_ssl_use), bUseSsl);
String suserid = preferences.getString(userid.getKey(), getString(R.string.useridDefaultValue));
editor.putString(getString(R.string.key_userid), suserid);
String spassword = preferences.getString(password.getKey(),
getString(R.string.passwordDefaultValue));
editor.putString(getString(R.string.key_password), spassword);
editor.putString(
getString(R.string.key_profile_selected_title),
preferences.getString(
title.getKey(),
getString(R.string.url)));
editor.commit();
Toast.makeText(getActivity(),
String.format(
"%s: %s\n[%s:%s] %s",
getString(R.string.connection_profile_active_toast),
HostConfig.this.title.getText(),
host,
port,
useSsl.isChecked() ? "(SSL)" : ""
),
Toast.LENGTH_LONG).show();
return false;
}
});
}
@Override
public String toString() {
return "HostConfig{" +
"id=" + id +
", title='" + title + '\'' +
", hostAddress=" + hostAddress.getKey() +
", hostPort=" + hostPort.getKey() +
'}';
}
}
}
}