← Back to team overview

openlp-android team mailing list archive

[Merge] lp:~johanmynhardt/openlp/android into lp:openlp/android

 

Johan Mynhardt has proposed merging lp:~johanmynhardt/openlp/android into lp:openlp/android.

Requested reviews:
  Tim Bentley (trb143)

For more details, see:
https://code.launchpad.net/~johanmynhardt/openlp/android/+merge/106879

Connection profile management - the ability to add and remove server configurations.
-- 
https://code.launchpad.net/~johanmynhardt/openlp/android/+merge/106879
Your team OpenLP Android Developers is subscribed to branch lp:openlp/android.
=== modified file 'AndroidManifest.xml'
--- AndroidManifest.xml	2012-05-12 19:40:12 +0000
+++ AndroidManifest.xml	2012-05-22 19:15:38 +0000
@@ -32,6 +32,7 @@
         </activity>
 
 		<activity android:name=".activity.Preferences" android:label="@string/preferences"/>
+		<activity android:name=".activity.ConnectionPreferenceActivity"/>
 		<activity android:name=".activity.PagerActivity"/>
 
         <service android:name=".service.PingIntent"/>

=== modified file 'OpenLP.apk'
Binary files OpenLP.apk	2012-05-18 17:55:42 +0000 and OpenLP.apk	2012-05-22 19:15:38 +0000 differ
=== added file 'res/menu/connections_menu.xml'
--- res/menu/connections_menu.xml	1970-01-01 00:00:00 +0000
+++ res/menu/connections_menu.xml	2012-05-22 19:15:38 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android";>
+	<item
+		android:id="@+id/menuAddHost"
+		android:enabled="true"
+		android:title="Add Host"
+		android:titleCondensed="Add"/>
+</menu>

=== modified file 'res/values/strings.xml'
--- res/values/strings.xml	2012-05-18 18:06:34 +0000
+++ res/values/strings.xml	2012-05-22 19:15:38 +0000
@@ -72,4 +72,13 @@
 	<string name="requestFailed">Request Failed</string>
 	<string name="couldNotHandleLiveItems">Could not handle live items</string>
 	<string name="couldNotHandleServiceItems">Could not handle service items</string>
+	<string name="connection.profiles">Connection Profiles</string>
+	<string name="connection.profiles.manage">Manage Profiles</string>
+	<string name="connection.profile.active">Active Profile</string>
+	<string name="connection.profile.summary.activate">Set this profile as the active profile.</string>
+	<string name="connection.profile.activate">Activate</string>
+	<string name="connection.profile.server">Server</string>
+	<string name="connection.profile.remove">Remove</string>
+	<string name="connection.profile.remove.summary">Remove this configuration</string>
+	<string name="notSet">Not set</string>
 </resources>

=== added file 'res/xml/connection_preferences.xml'
--- res/xml/connection_preferences.xml	1970-01-01 00:00:00 +0000
+++ res/xml/connection_preferences.xml	2012-05-22 19:15:38 +0000
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen
+        xmlns:android="http://schemas.android.com/apk/res/android";>
+
+</PreferenceScreen>

=== modified file 'res/xml/preferences.xml'
--- res/xml/preferences.xml	2012-05-18 17:55:42 +0000
+++ res/xml/preferences.xml	2012-05-22 19:15:38 +0000
@@ -22,7 +22,7 @@
                 android:defaultValue="@string/displayTypeValue"/>  
     </PreferenceCategory>
     <PreferenceCategory
-            android:title="@string/preferenceCategoryTitleServer">
+            android:title="@string/preferenceCategoryTitleServer" android:key="category.server">
         <EditTextPreference
                 android:title="@string/url"
                 android:key="@string/keyHost"

=== added file 'src/org/openlp/android/activity/ConnectionPreferenceActivity.java'
--- src/org/openlp/android/activity/ConnectionPreferenceActivity.java	1970-01-01 00:00:00 +0000
+++ src/org/openlp/android/activity/ConnectionPreferenceActivity.java	2012-05-22 19:15:38 +0000
@@ -0,0 +1,248 @@
+/******************************************************************************
+ * OpenLP - Open Source Lyrics Projection                                      *
+ * --------------------------------------------------------------------------- *
+ * Copyright (c) 2011-2012 Raoul Snyman                                        *
+ * Portions copyright (c) 2011-2012 Tim Bentley, Johan Mynhardt, Samuel        *
+ * Sjöbergsson                                                                 *
+ * --------------------------------------------------------------------------- *
+ * 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.android.activity;
+
+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.EditTextPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceCategory;
+import android.preference.PreferenceScreen;
+import android.text.InputType;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.widget.Toast;
+import org.openlp.android.R;
+
+public class ConnectionPreferenceActivity extends PreferenceActivity {
+	private final String LOG_TAG = ConnectionPreferenceActivity.class.getName();
+	private PreferenceScreen preferenceScreen = null;
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		addPreferencesFromResource(R.xml.connection_preferences);
+		getPreferenceManager().setSharedPreferencesName(getString(R.string.keySharedPreferences));
+		preferenceScreen = getPreferenceScreen() == null
+				? getPreferenceManager().createPreferenceScreen(this)
+				: getPreferenceScreen();
+
+		constructPreferencesScreen();
+	}
+
+	private void constructPreferencesScreen() {
+		getPreferenceScreen().removeAll();
+		List<HostConfig> hostConfigs = hostConfigsFromPreferences(
+				getHostConfigMap(
+						getPreferenceManager().getSharedPreferences().getAll()
+				)
+		);
+		for (HostConfig config : hostConfigs) {
+			addPreferenceCategory(preferenceScreen, config);
+		}
+	}
+
+	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) {
+			HostConfig hostConfig = new HostConfig(getString(R.string.connection_profile_server), id);
+			Object hostValueObject = preferences.get(hostConfig.hostAddress.getKey());
+			String hostValue = hostValueObject == null ? "" : hostValueObject.toString();
+			hostConfig.hostAddress.setText(hostValue);
+			hostConfig.hostAddress.setSummary(hostValue);
+			Object portValueObject = preferences.get(hostConfig.hostPort.getKey());
+			String portValue = portValueObject == null ? "" : portValueObject.toString();
+			hostConfig.hostPort.setText(portValue);
+			hostConfig.hostPort.setSummary(portValue);
+			hostConfigs.add(hostConfig);
+		}
+		return hostConfigs;
+	}
+
+	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(this);
+		preferenceCategory.setTitle(hostConfig.title);
+		preferenceScreen.addPreference(preferenceCategory);
+		preferenceCategory.addPreference(hostConfig.hostAddress);
+		preferenceCategory.addPreference(hostConfig.hostPort);
+		preferenceCategory.addPreference(hostConfig.remove);
+		preferenceCategory.addPreference(hostConfig.activate);
+		return true;
+	}
+
+	@Override
+	public boolean onCreateOptionsMenu(Menu menu) {
+		MenuInflater inflater = getMenuInflater();
+		inflater.inflate(R.menu.connections_menu, menu);
+		return true;
+	}
+
+	@Override
+	public boolean onOptionsItemSelected(MenuItem item) {
+		switch (item.getItemId()) {
+			case R.id.preferences:
+				startActivity(new Intent(this, Preferences.class));
+				return true;
+			case R.id.menuAddHost:
+				int nextId = getNextPrefId(
+						getHostConfigMap(
+								getPreferenceManager().getSharedPreferences().getAll()
+						)
+				);
+				addPreferenceCategory(preferenceScreen,
+						new HostConfig(
+								getString(R.string.connection_profile_server),
+								nextId
+						)
+				);
+				return true;
+			default:
+				return super.onOptionsItemSelected(item);
+		}
+	}
+
+	private class HostConfig {
+		static final String KEY_PREFIX = "host.config.";
+		final int id;
+		final String title;
+		final EditTextPreference hostAddress;
+		final EditTextPreference hostPort;
+		final Preference remove;
+		final Preference activate;
+
+		HostConfig(String title, int id) {
+			this.title = title + " " + id;
+			this.id = id;
+			hostAddress = new EditTextPreference(ConnectionPreferenceActivity.this);
+			hostAddress.setTitle(getString(R.string.urlHint));
+			hostAddress.setKey(KEY_PREFIX + id + ".address");
+			hostAddress.setSummary(getString(R.string.urlHint));
+			hostAddress.setDialogTitle(getString(R.string.urlHint));
+			hostAddress.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
+			hostPort = new EditTextPreference(ConnectionPreferenceActivity.this);
+			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));
+			remove = new Preference(ConnectionPreferenceActivity.this);
+			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.commit();
+					ConnectionPreferenceActivity.this.constructPreferencesScreen();
+					return false;
+				}
+			});
+
+			activate = new Preference(ConnectionPreferenceActivity.this);
+			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);
+					editor.commit();
+					Toast.makeText(ConnectionPreferenceActivity.this,
+							String.format(
+									"%s: %s [%s:%s]",
+									getString(R.string.connection_profile_active),
+									HostConfig.this.title,
+									host,
+									port
+							),
+							Toast.LENGTH_SHORT).show();
+					return false;
+				}
+			});
+		}
+
+		@Override
+		public String toString() {
+			return "HostConfig{" +
+					"id=" + id +
+					", title='" + title + '\'' +
+					", hostAddress=" + hostAddress.getKey() +
+					", hostPort=" + hostPort.getKey() +
+					'}';
+		}
+	}
+}

=== modified file 'src/org/openlp/android/activity/Preferences.java'
--- src/org/openlp/android/activity/Preferences.java	2012-05-18 17:55:42 +0000
+++ src/org/openlp/android/activity/Preferences.java	2012-05-22 19:15:38 +0000
@@ -20,19 +20,25 @@
  *******************************************************************************/
 package org.openlp.android.activity;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Bundle;
-import android.preference.*;
+import android.preference.DialogPreference;
+import android.preference.EditTextPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceCategory;
+import android.preference.PreferenceScreen;
 import android.util.Log;
 import org.openlp.android.R;
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
 /**
  * Credits: http://www.kaloer.com/android-preferences
  * http://androidpartaker.wordpress.com/2010/07/11/android-preferences/
@@ -45,8 +51,16 @@
         getPreferenceManager().setSharedPreferencesName(
                 getString(R.string.keySharedPreferences));
         addPreferencesFromResource(R.xml.preferences);
-
-        PackageManager manager = this.getPackageManager();
+		SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
+
+		findPreference(getString(R.string.keyHost)).setSummary(
+				sharedPreferences.getString(getString(R.string.keyHost), getString(R.string.notSet))
+		);
+		findPreference(getString(R.string.keyPort)).setSummary(
+				sharedPreferences.getString(getString(R.string.keyPort), getString(R.string.notSet))
+		);
+
+		PackageManager manager = this.getPackageManager();
         String version = "";
         try {
             PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
@@ -54,29 +68,31 @@
         } catch (PackageManager.NameNotFoundException e) {
             // Nop as version defined
         }
-        Date date = new Date();
-        SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
+
+        String date = new SimpleDateFormat("yyyy").format(new Date());
 
         /* Preferences time! (we build the preferences) */
-        Preference about = getPreference(getResources().getString(R.string.about_text), version, null);
-        Preference openlpLink = getPreference(getResources().getString(R.string.about_display_3), getResources().getString(R.string.about_display_1), new Intent(Intent.ACTION_VIEW, Uri.parse("http://openlp.org/";)));
+        Preference about = getPreference(getString(R.string.about_text), version, null);
+        Preference openlpLink = getPreference(getString(R.string.about_display_3), getString(R.string.about_display_1), new Intent(Intent.ACTION_VIEW, Uri.parse("http://openlp.org/";)));
 
-        StringBuffer html = new StringBuffer();
-        html.append(getResources().getString(R.string.about_display_4));
-        html.append(" © 2004-");
-        html.append(simpleDateformat.format(date));
-        html.append(" Raoul Snyman\n");
-        html.append(getResources().getString(R.string.about_display_5));
-        html.append(" © 2004-");
-        html.append(simpleDateformat.format(date));
+        StringBuilder html = new StringBuilder();
+        html.append(getString(R.string.about_display_4));
+        html.append(" © 2004-").append(date).append(" Raoul Snyman\n");
+        html.append(getString(R.string.about_display_5));
+        html.append(" © 2004-").append(date);
         html.append("\nTim Bentley, Johan Mynhardt, Samuel Sjöbergsson");
-        Preference copyright = getPreference(getResources().getString(R.string.about_display_4), html.toString(), null);
-
-        DialogPreference license = new MyDialogPreference(this, getResources().getString(R.string.about_display_6), getResources().getString(R.string.about_display_7) + "\n" + getResources().getString(R.string.about_display_8));
+        Preference copyright = getPreference(getString(R.string.about_display_4), html.toString(), null);
+
+        DialogPreference license = new MyDialogPreference(this, getString(R.string.about_display_6), getString(R.string.about_display_7) + "\n" + getString(R.string.about_display_8));
+
 
         PreferenceScreen preferenceScreen = getPreferenceScreen();
-        addPreferenceCategory(preferenceScreen, getResources().getString(R.string.about), about, openlpLink, copyright, license);
-        this.setPreferenceScreen(preferenceScreen);
+        addPreferenceCategory(preferenceScreen, getString(R.string.about), about, openlpLink, copyright, license);
+
+		Preference preference = new Preference(this);
+		preference.setTitle(getString(R.string.connection_profiles_manage));
+		preference.setIntent(new Intent(this, ConnectionPreferenceActivity.class));
+		((PreferenceCategory) findPreference("category.server")).addPreference(preference);
     }
 
     private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,


Follow ups