From 47164004b100684e16a2339b7fb46ff8ef0beb9a Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Fri, 23 Jan 2015 17:53:00 +0000 Subject: [PATCH] Clean up and settings fix --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 13 +- .../main/java/org/openlp/android2/OpenLP.java | 24 +- .../activities/ConnectionActivity.java | 427 ------------------ .../android2/activities/SettingsActivity.java | 258 ++++++++--- .../android2/common/OpenLPHttpClient.java | 59 +-- .../android2/fragments/AboutFragment.java | 3 +- .../android2/fragments/HomeFragment.java | 3 +- .../android2/fragments/LiveListFragment.java | 42 +- .../android2/fragments/LiveWebFragment.java | 3 +- .../fragments/NavigationDrawerFragment.java | 7 +- .../fragments/ServiceListFragment.java | 4 +- .../android2/fragments/StageWebFragment.java | 3 +- app/src/main/res/layout/activity_main.xml | 19 +- .../main/res/layout/alert_display_dialog.xml | 5 +- .../main/res/layout/blank_display_dialog.xml | 3 +- app/src/main/res/menu/main.xml | 8 - app/src/main/res/menu/open_l.xml | 8 +- app/src/main/res/values/defaultValues.xml | 9 +- app/src/main/res/values/keyStrings.xml | 23 +- app/src/main/res/values/strings.xml | 23 +- .../res/values/strings_activity_settings.xml | 61 --- app/src/main/res/xml/empty_preferences.xml | 6 - app/src/main/res/xml/pref_data_sync.xml | 21 - app/src/main/res/xml/pref_general.xml | 46 +- app/src/main/res/xml/pref_headers.xml | 11 +- app/src/main/res/xml/pref_notification.xml | 74 ++- 27 files changed, 368 insertions(+), 797 deletions(-) delete mode 100644 app/src/main/java/org/openlp/android2/activities/ConnectionActivity.java delete mode 100644 app/src/main/res/menu/main.xml delete mode 100644 app/src/main/res/values/strings_activity_settings.xml delete mode 100755 app/src/main/res/xml/empty_preferences.xml delete mode 100644 app/src/main/res/xml/pref_data_sync.xml diff --git a/app/build.gradle b/app/build.gradle index 3a0608c..f81293b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,7 @@ apply plugin: 'com.android.application' +project.archivesBaseName = 'OpenLP' + android { compileSdkVersion 21 buildToolsVersion "21.1.2" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8209cf4..6e0cd60 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,10 +20,6 @@ - - - - - + diff --git a/app/src/main/java/org/openlp/android2/OpenLP.java b/app/src/main/java/org/openlp/android2/OpenLP.java index 76834d4..0a639c7 100644 --- a/app/src/main/java/org/openlp/android2/OpenLP.java +++ b/app/src/main/java/org/openlp/android2/OpenLP.java @@ -20,12 +20,15 @@ package org.openlp.android2; import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.app.ActionBar; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -57,6 +60,7 @@ public class OpenLP extends ActionBarActivity /** * Used to store the last screen title. For use in {@link #restoreActionBar()}. */ + private final String LOG_TAG = OpenLP.class.getName(); private CharSequence mTitle; private boolean backArrowActive = false; private Menu dropdownMenu; @@ -66,6 +70,8 @@ public class OpenLP extends ActionBarActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + doPreferenceCheck(); + mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); @@ -76,6 +82,19 @@ public class OpenLP extends ActionBarActivity (DrawerLayout) findViewById(R.id.drawer_layout)); } + private void doPreferenceCheck() { + SharedPreferences sharedPrefs = PreferenceManager + .getDefaultSharedPreferences(this); + if (sharedPrefs.getString(getString(R.string.key_host), "NONE").equals("NONE") + || sharedPrefs.getString(getString(R.string.key_host), null).equals(null)) { + Log.d(LOG_TAG, + "URL preference not set. Starting preference activity..."); + Intent preferenceIntent = new Intent(this, SettingsActivity.class); + startActivity(preferenceIntent); + } + } + + @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments @@ -123,7 +142,7 @@ public class OpenLP extends ActionBarActivity actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } catch (Exception e) { - e.printStackTrace(); + //noop } } @@ -226,8 +245,7 @@ public class OpenLP extends ActionBarActivity @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_main, container, false); - return rootView; + return inflater.inflate(R.layout.fragment_main, container, false); } @Override diff --git a/app/src/main/java/org/openlp/android2/activities/ConnectionActivity.java b/app/src/main/java/org/openlp/android2/activities/ConnectionActivity.java deleted file mode 100644 index 38df396..0000000 --- a/app/src/main/java/org/openlp/android2/activities/ConnectionActivity.java +++ /dev/null @@ -1,427 +0,0 @@ -/****************************************************************************** - * OpenLP - Open Source Lyrics Projection * - * --------------------------------------------------------------------------- * - * Copyright (c) 2011-2015 OpenLP Android Developers * - * --------------------------------------------------------------------------- * - * 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 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 getHostConfigs() { - return hostConfigsFromPreferences( - getHostConfigMap( - getPreferenceManager() - .getSharedPreferences() - .getAll() - ) - ); - } - - private Map getHostConfigMap(Map preferences) { - Map configMap = new TreeMap(); - - List sortedKeys = new ArrayList(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 hostConfigsFromPreferences(Map preferences) { - List hostIds = getHostIds(preferences); - List hostConfigs = new ArrayList(); - for (Integer id : hostIds) { - hostConfigs.add(hostConfigFromPreferencesForHostId(id, preferences)); - } - return hostConfigs; - } - - private HostConfig hostConfigFromPreferencesForHostId(int hostId, Map 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 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 getHostIds(Map preferences) { - Set hashSet = new HashSet(); - for (String key : preferences.keySet()) { - hashSet.add(Integer.valueOf(key.split("\\.")[2])); - } - List hostIds = new ArrayList(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.url_hint)); - hostAddress.setKey(KEY_PREFIX + id + ".address"); - hostAddress.getEditText().setHint(R.string.url_hint); - hostAddress.setSummary(getString(R.string.url_hint)); - hostAddress.setDialogTitle(getString(R.string.url_hint)); - 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() + - '}'; - } - } - } -} - diff --git a/app/src/main/java/org/openlp/android2/activities/SettingsActivity.java b/app/src/main/java/org/openlp/android2/activities/SettingsActivity.java index 4751060..a078c91 100644 --- a/app/src/main/java/org/openlp/android2/activities/SettingsActivity.java +++ b/app/src/main/java/org/openlp/android2/activities/SettingsActivity.java @@ -1,88 +1,220 @@ -/****************************************************************************** - * OpenLP - Open Source Lyrics Projection * - * --------------------------------------------------------------------------- * - * Copyright (c) 2011-2015 OpenLP Android Developers * - * --------------------------------------------------------------------------- * - * 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 android.content.Intent; -import android.content.SharedPreferences; +import android.annotation.TargetApi; +import android.content.Context; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; +import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; + import org.openlp.android2.R; +import java.util.List; +/** + * A {@link PreferenceActivity} that presents a set of application settings. On + * handset devices, settings are presented as a single list. On tablets, + * settings are split by category, with category headers shown to the left of + * the list of settings. + *

+ * See + * Android Design: Settings for design guidelines and the Settings + * API Guide for more information on developing a Settings UI. + */ public class SettingsActivity extends PreferenceActivity { + /** + * Determines whether to always show the simplified settings UI, where + * settings are presented in a single list. When false, settings are shown + * as a master/detail two-pane view on tablets. When true, a single pane is + * shown on tablets. + */ + private static final boolean ALWAYS_SIMPLE_PREFS = false; + @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); - //todo fix this code to work with fragments but not now! - //getFragmentManager().beginTransaction() - // .replace(android.R.id.content, new SettingsFragment()) - // .commit(); + setupSimplePreferencesScreen(); + } - getPreferenceManager() - .setSharedPreferencesName(getString(R.string.keySharedPreferences)); + /** + * Shows the simplified settings UI if the device configuration if the + * device configuration dictates that a simplified, single-pane UI should be + * shown. + */ + private void setupSimplePreferencesScreen() { + if (!isSimplePreferences(this)) { + return; + } + // In the simplified UI, fragments are not used at all and we instead + // use the older PreferenceActivity APIs. + + // Add 'general' preferences. addPreferencesFromResource(R.xml.pref_general); - final SharedPreferences sharedPreferences = getPreferenceManager() - .getSharedPreferences(); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(sharedPreferences.getString( - getString(R.string.keyHost), - getString(R.string.not_set))); - stringBuilder.append(":"); - - Boolean useSsl = sharedPreferences.getBoolean( - getString(R.string.key_ssl_use), false); - stringBuilder.append( - String.format("%s %s", - sharedPreferences.getString( - getString(R.string.keyPort), - getString(R.string.not_set)), useSsl ? "(SSL)" : "")); - - final Preference hostPreference = findPreference(getString(R.string.keyHost)); - hostPreference.setTitle( - getPreferenceManager().getSharedPreferences() - .getString( - getString(R.string.key_profile_selected_title), - getString(R.string.url) - ) - ); - hostPreference.setSummary(stringBuilder.toString()); - - Preference preference = findPreference(getString(R.string.keyHost)); - preference.setIntent(new Intent(this, ConnectionActivity.class)); + // Add 'notifications' preferences, and a corresponding header. + PreferenceCategory fakeHeader = new PreferenceCategory(this); + fakeHeader.setTitle(R.string.connection_configuration); + getPreferenceScreen().addPreference(fakeHeader); + addPreferencesFromResource(R.xml.pref_notification); + // Bind the summaries of EditText/List/Dialog/Ringtone preferences to + // their values. When their values change, their summaries are updated + // to reflect the new value, per the Android Design guidelines. + bindPreferenceSummaryToValue(findPreference("key_text_size")); + bindPreferenceSummaryToValue(findPreference("key_connection_timeout")); + bindPreferenceSummaryToValue(findPreference("key_host")); + bindPreferenceSummaryToValue(findPreference("key_port")); + bindPreferenceSummaryToValue(findPreference("key_userid")); + bindPreferenceSummaryToValue(findPreference("key_password")); } - public static class SettingsFragment extends PreferenceFragment { - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getPreferenceManager().setSharedPreferencesName(getString(R.string.keySharedPreferences)); - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.pref_general); + /** + * {@inheritDoc} + */ + @Override + public boolean onIsMultiPane() { + return isXLargeTablet(this) && !isSimplePreferences(this); + } + + /** + * Helper method to determine if the device has an extra-large screen. For + * example, 10" tablets are extra-large. + */ + private static boolean isXLargeTablet(Context context) { + return (context.getResources().getConfiguration().screenLayout + & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; + } + + /** + * Determines whether the simplified settings UI should be shown. This is + * true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device + * doesn't have newer APIs like {@link PreferenceFragment}, or the device + * doesn't have an extra-large screen. In these cases, a single-pane + * "simplified" settings UI should be shown. + */ + private static boolean isSimplePreferences(Context context) { + return ALWAYS_SIMPLE_PREFS + || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB + || !isXLargeTablet(context); + } + + /** + * {@inheritDoc} + */ + @Override + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public void onBuildHeaders(List

target) { + if (!isSimplePreferences(this)) { + loadHeadersFromResource(R.xml.pref_headers, target); } } + @Override + protected boolean isValidFragment (String fragmentName) { + return true; + } + + /** + * A preference value change listener that updates the preference's summary + * to reflect its new value. + */ + private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + String stringValue = value.toString(); + + if (preference instanceof ListPreference) { + // For list preferences, look up the correct display value in + // the preference's 'entries' list. + ListPreference listPreference = (ListPreference) preference; + int index = listPreference.findIndexOfValue(stringValue); + + // Set the summary to reflect the new value. + preference.setSummary( + index >= 0 + ? listPreference.getEntries()[index] + : null); + + } else { + // For all other preferences, set the summary to the value's + // simple string representation. + preference.setSummary(stringValue); + } + return true; + } + }; + + /** + * Binds a preference's summary to its value. More specifically, when the + * preference's value is changed, its summary (line of text below the + * preference title) is updated to reflect the value. The summary is also + * immediately updated upon calling this method. The exact display format is + * dependent on the type of preference. + * + * @see #sBindPreferenceSummaryToValueListener + */ + private static void bindPreferenceSummaryToValue(Preference preference) { + // Set the listener to watch for value changes. + preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); + + // Trigger the listener immediately with the preference's + // current value. + sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, + PreferenceManager + .getDefaultSharedPreferences(preference.getContext()) + .getString(preference.getKey(), "")); + } + + /** + * This fragment shows general preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class GeneralPreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_general); + + // Bind the summaries of EditText/List/Dialog/Ringtone preferences + // to their values. When their values change, their summaries are + // updated to reflect the new value, per the Android Design + // guidelines. + bindPreferenceSummaryToValue(findPreference("key_text_size")); + } + + } + + /** + * This fragment shows notification preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class NotificationPreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_notification); + + // Bind the summaries of EditText/List/Dialog/Ringtone preferences + // to their values. When their values change, their summaries are + // updated to reflect the new value, per the Android Design + // guidelines. + bindPreferenceSummaryToValue(findPreference("key_connection_timeout")); + bindPreferenceSummaryToValue(findPreference("key_host")); + bindPreferenceSummaryToValue(findPreference("key_port")); + bindPreferenceSummaryToValue(findPreference("key_userid")); + bindPreferenceSummaryToValue(findPreference("key_password")); + } + } } diff --git a/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java b/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java index f2d2aa4..5b26711 100644 --- a/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java +++ b/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java @@ -20,12 +20,11 @@ package org.openlp.android2.common; import java.security.KeyStore; -import java.util.*; import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Log; import com.loopj.android.http.AsyncHttpClient; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; import org.openlp.android2.R; /** @@ -42,56 +41,29 @@ public class OpenLPHttpClient { this.context = context; } - private String getPreference(Map preferences, String key, String defaultValue) { - if (preferences.containsKey(key)) { - return preferences.get(key).toString(); - } else { - return defaultValue; - } - } - - private Integer getPreference(Map preferences, String key, Integer defaultValue) { - if (preferences.containsKey(key)) { - return Integer.valueOf(preferences.get(key).toString()); - } else { - return defaultValue; - } - } - - private Boolean getPreference(Map preferences, String key, Boolean defaultValue) { - if (preferences.containsKey(key)) { - return Boolean.valueOf(preferences.get(key).toString()); - } else { - return defaultValue; - } - } - public String getAbsoluteUrl(AsyncHttpClient client) { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + String urlBase = getBaseUrl(); - Map preferences = context.getSharedPreferences(context.getString(R.string.keySharedPreferences), - Context.MODE_PRIVATE).getAll(); + Log.d(LOG_TAG, "Base Url set to " + urlBase); - String userid = getPreference(preferences, context.getString(R.string.key_userid), "openlp"); + String userid = sharedPrefs.getString(context.getString(R.string.key_userid), "openlp"); - String password = getPreference(preferences, context.getString(R.string.key_password), "password"); + String password = sharedPrefs.getString(context.getString(R.string.key_password), "password"); Log.d(LOG_TAG, "Credentials set to " + userid + " : " + password); client.setBasicAuth(userid,password); - Credentials creds = new UsernamePasswordCredentials(userid, password); - int connectionTimeout = context.getResources().getInteger( R.integer.connectionTimeoutDefaultValue); - if (getPreference(preferences, context.getString(R.string.keyEnableCustomTimeout), false)) { + if (sharedPrefs.getBoolean(context.getString(R.string.key_enable_custom_timeout), false)) { Log.d(LOG_TAG, "Overriding Connection and Socket timeouts"); - connectionTimeout = getPreference(preferences, - context.getString(R.string.keyConnectionTimeout), - context.getResources().getInteger( - R.integer.connectionTimeoutDefaultValue) + connectionTimeout = sharedPrefs.getInt(context.getString(R.string.key_connection_timeout), + context.getResources().getInteger(R.integer.connectionTimeoutDefaultValue) ); } client.setTimeout(connectionTimeout); @@ -104,18 +76,19 @@ public class OpenLPHttpClient { client.setSSLSocketFactory(sf); } catch (Exception e){ + // } } return urlBase; } public String getBaseUrl(){ - Map preferences = context.getSharedPreferences(context.getString(R.string.keySharedPreferences), Context.MODE_PRIVATE).getAll(); + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - useSSL = getPreference(preferences, context.getString(R.string.key_ssl_use), false); - String host = getPreference(preferences, context.getString(R.string.keyHost), - context.getString(R.string.hostDefaultValue)); - String port = getPreference(preferences, context.getString(R.string.keyPort), "4316"); + useSSL =sharedPrefs.getBoolean(context.getString(R.string.key_ssl_use), false); + String host = sharedPrefs.getString(context.getString(R.string.key_host), + context.getString(R.string.host_default_value)); + String port = sharedPrefs.getString(context.getString(R.string.key_port), "4316"); return String.format("http%s://%s:%s", useSSL ? "s" : "", host, port); diff --git a/app/src/main/java/org/openlp/android2/fragments/AboutFragment.java b/app/src/main/java/org/openlp/android2/fragments/AboutFragment.java index 751fb36..d95f054 100644 --- a/app/src/main/java/org/openlp/android2/fragments/AboutFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/AboutFragment.java @@ -36,8 +36,7 @@ public class AboutFragment extends Fragment { } public static AboutFragment newInstance() { - AboutFragment fragment = new AboutFragment(); - return fragment; + return new AboutFragment(); } @Override diff --git a/app/src/main/java/org/openlp/android2/fragments/HomeFragment.java b/app/src/main/java/org/openlp/android2/fragments/HomeFragment.java index 6d8c9ab..ffd41b2 100644 --- a/app/src/main/java/org/openlp/android2/fragments/HomeFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/HomeFragment.java @@ -35,8 +35,7 @@ public class HomeFragment extends Fragment { } public static HomeFragment newInstance() { - HomeFragment fragment = new HomeFragment(); - return fragment; + return new HomeFragment(); } @Override diff --git a/app/src/main/java/org/openlp/android2/fragments/LiveListFragment.java b/app/src/main/java/org/openlp/android2/fragments/LiveListFragment.java index 708f0c7..89c144b 100644 --- a/app/src/main/java/org/openlp/android2/fragments/LiveListFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/LiveListFragment.java @@ -33,6 +33,7 @@ import android.widget.ListAdapter; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -54,8 +55,7 @@ public class LiveListFragment extends OpenLPFragment { private String LOG_TAG = LiveListFragment.class.getName(); public static LiveListFragment newInstance() { - LiveListFragment fragment = new LiveListFragment(); - return fragment; + return new LiveListFragment(); } /** @@ -84,22 +84,22 @@ public class LiveListFragment extends OpenLPFragment { List> aList = new ArrayList>(); selected = 0; - try { - JSONArray items = new JSONObject(json).getJSONObject("results").getJSONArray("slides"); - for (int i = 0; i < items.length(); ++i) { - JSONObject item = items.getJSONObject(i); + try { + JSONArray items = new JSONObject(json).getJSONObject("results").getJSONArray("slides"); + for (int i = 0; i < items.length(); ++i) { + JSONObject item = items.getJSONObject(i); - HashMap hm = new HashMap(); - hm.put("tag", item.getString("tag")); - if (item.getString("selected").equals("true")) { - selected = i; - } - hm.put("liveListNormal", Html.fromHtml(item.getString("html")).toString()); - aList.add(hm); + HashMap hm = new HashMap(); + hm.put("tag", item.getString("tag")); + if (item.getString("selected").equals("true")) { + selected = i; } - } catch (JSONException e) { - e.printStackTrace(); + hm.put("liveListNormal", Html.fromHtml(item.getString("html")).toString()); + aList.add(hm); } + } catch (JSONException e) { + e.printStackTrace(); + } // Keys used in Hashmap String[] from = {"tag", "liveListNormal", "liveListSelected"}; @@ -108,30 +108,30 @@ public class LiveListFragment extends OpenLPFragment { int[] to = {R.id.tag, R.id.liveListNormal, R.id.liveListSelected}; SharedPreferences prefs = context.getSharedPreferences( - context.getString(R.string.keySharedPreferences), + context.getString(R.string.key_shared_preferences), Context.MODE_PRIVATE); final int size = Integer.parseInt(prefs.getString( - context.getString(R.string.keyTextSize), + context.getString(R.string.key_text_size), String.valueOf(context.getResources().getInteger( R.integer.textSizeDefaultValue)))); // Instantiating an adapter to store each items ListAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, - R.layout.fragment_livelist, from, to){ + R.layout.fragment_livelist, from, to) { public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView text1 = (TextView) view.findViewById(R.id.tag); text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, size); TextView text2 = (TextView) view.findViewById(R.id.liveListNormal); text2.setTextSize(TypedValue.COMPLEX_UNIT_SP, size); - if (selected == position){ + if (selected == position) { text2.setTextColor(Color.parseColor("#000000")); text2.setTypeface(null, Typeface.BOLD_ITALIC); } return view; - }; + } }; setListAdapter(adapter); getListView().setSelection(selected - 1); @@ -140,7 +140,7 @@ public class LiveListFragment extends OpenLPFragment { @Override - public void refreshDisplay(){ + public void refreshDisplay() { Log.d(LOG_TAG, "Resuming..."); triggerTextRequest(Api.LIVE_TEXT); } diff --git a/app/src/main/java/org/openlp/android2/fragments/LiveWebFragment.java b/app/src/main/java/org/openlp/android2/fragments/LiveWebFragment.java index a080651..7ff94dc 100644 --- a/app/src/main/java/org/openlp/android2/fragments/LiveWebFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/LiveWebFragment.java @@ -27,8 +27,7 @@ public class LiveWebFragment extends WebFragment { } public static LiveWebFragment newInstance() { - LiveWebFragment fragment = new LiveWebFragment(); - return fragment; + return new LiveWebFragment(); } } diff --git a/app/src/main/java/org/openlp/android2/fragments/NavigationDrawerFragment.java b/app/src/main/java/org/openlp/android2/fragments/NavigationDrawerFragment.java index 30a5d31..a2edf98 100644 --- a/app/src/main/java/org/openlp/android2/fragments/NavigationDrawerFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/NavigationDrawerFragment.java @@ -18,6 +18,7 @@ *******************************************************************************/ package org.openlp.android2.fragments; +import android.graphics.drawable.ColorDrawable; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.support.v7.app.ActionBar; @@ -184,6 +185,7 @@ public class NavigationDrawerFragment extends Fragment { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); + actionBar.setBackgroundDrawable(new ColorDrawable(0xFF000000)); // ActionBarDrawerToggle ties together the the proper interactions // between the navigation drawer and the action bar app icon. @@ -300,11 +302,6 @@ public class NavigationDrawerFragment extends Fragment { return true; } - if (item.getItemId() == R.id.action_example) { - Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show(); - return true; - } - return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/org/openlp/android2/fragments/ServiceListFragment.java b/app/src/main/java/org/openlp/android2/fragments/ServiceListFragment.java index c4d5230..14229ec 100644 --- a/app/src/main/java/org/openlp/android2/fragments/ServiceListFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/ServiceListFragment.java @@ -161,11 +161,11 @@ public class ServiceListFragment extends OpenLPFragment { int[] to = {R.id.icon, R.id.serviceListText}; SharedPreferences prefs = context.getSharedPreferences( - context.getString(R.string.keySharedPreferences), + context.getString(R.string.key_shared_preferences), Context.MODE_PRIVATE); final int size = Integer.parseInt(prefs.getString( - context.getString(R.string.keyTextSize), + context.getString(R.string.key_text_size), String.valueOf(context.getResources().getInteger( R.integer.textSizeDefaultValue)))); diff --git a/app/src/main/java/org/openlp/android2/fragments/StageWebFragment.java b/app/src/main/java/org/openlp/android2/fragments/StageWebFragment.java index afd1db3..ede970e 100644 --- a/app/src/main/java/org/openlp/android2/fragments/StageWebFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/StageWebFragment.java @@ -29,7 +29,6 @@ public class StageWebFragment extends WebFragment { public static StageWebFragment newInstance() { - StageWebFragment fragment = new StageWebFragment(); - return fragment; + return new StageWebFragment(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5af8fd7..28033a0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,12 +1,16 @@ - - diff --git a/app/src/main/res/layout/alert_display_dialog.xml b/app/src/main/res/layout/alert_display_dialog.xml index c4cc68d..1221fbc 100644 --- a/app/src/main/res/layout/alert_display_dialog.xml +++ b/app/src/main/res/layout/alert_display_dialog.xml @@ -13,10 +13,11 @@ android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/enter_alert_text" android:id="@+id/textView" - android:autoText="true" + android:inputType="textLongMessage" android:textStyle="bold|italic" android:textSize="40px" - android:height="45dp"/> + android:height="45px"/> + + android:height="45px"/>