Skip to content

Commit

Permalink
Added settings activity and corresponding xml documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcgaunn committed Apr 9, 2015
1 parent e32f714 commit 38dc869
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 1 deletion.
1 change: 1 addition & 0 deletions 2_DataAggregator/Code/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:support-v4:22.0.0'
}
4 changes: 4 additions & 0 deletions 2_DataAggregator/Code/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.uml.cs411;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
Expand Down Expand Up @@ -31,7 +32,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
startActivity(new Intent(this, SettingsActivity.class));
}

return super.onOptionsItemSelected(item);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package edu.uml.cs411;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
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 android.preference.RingtonePreference;
import android.text.TextUtils;


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.
* <p/>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> 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 onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

setupSimplePreferencesScreen();
}

/**
* 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);

// // Add 'notifications' preferences, and a corresponding header.
// fakeHeader = new PreferenceCategory(this);
// fakeHeader.setTitle(R.string.pref_header_notifications);
// getPreferenceScreen().addPreference(fakeHeader);
// addPreferencesFromResource(R.xml.pref_notification);
//
// // Add 'data and sync' preferences, and a corresponding header.
// fakeHeader = new PreferenceCategory(this);
// fakeHeader.setTitle(R.string.pref_header_data_sync);
// getPreferenceScreen().addPreference(fakeHeader);
// addPreferencesFromResource(R.xml.pref_data_sync);

// 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("preferences_name"));
bindPreferenceSummaryToValue(findPreference("preferences_weight"));
// bindPreferenceSummaryToValue(findPreference("example_text"));
// bindPreferenceSummaryToValue(findPreference("example_list"));
// bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
// bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}

/**
* {@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<Header> target) {
if (!isSimplePreferences(this)) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}

/**
* 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 if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);

} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));

if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}

} 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("example_text"));
bindPreferenceSummaryToValue(findPreference("example_list"));
}
}

}
6 changes: 6 additions & 0 deletions 2_DataAggregator/Code/app/src/main/res/menu/menu_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="edu.uml.cs411.SettingsActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
2 changes: 2 additions & 0 deletions 2_DataAggregator/Code/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="abvFeet">ft.</string>
<string name="abvInches">in.</string>
<string name="done">Done</string>
<string name="settings_prompt">Change Settings Here</string>

<string-array name="feet">
<item>2</item>
Expand Down Expand Up @@ -43,5 +44,6 @@
<item>Male</item>
<item>Female</item>
</string-array>
<string name="hello_world">Hello world!</string>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<resources>
<string name="title_activity_settings">Settings</string>

<!-- Strings related to Settings -->

<!-- Example General settings -->
<string name="pref_header_general">General</string>

<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>

<string name="pref_title_user_weight">Weight (lbs)</string>
<string name="pref_default_user_weight">150</string>

<string name="pref_title_add_friends_to_messages">Add friends to messages</string>
<string-array name="pref_example_list_titles">
<item>Always</item>
<item>When possible</item>
<item>Never</item>
</string-array>
<string-array name="pref_example_list_values">
<item>1</item>
<item>0</item>
<item>-1</item>
</string-array>

<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>

<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>

<string name="pref_title_system_sync_settings">System sync settings</string>

<!-- Example settings for Notifications -->
<string name="pref_header_notifications">Notifications</string>

<string name="pref_title_new_message_notifications">New message notifications</string>

<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>

<string name="pref_title_vibrate">Vibrate</string>
</resources>
32 changes: 32 additions & 0 deletions 2_DataAggregator/Code/app/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
android:title="General"
android:key="pref_key_general">

<EditTextPreference
android:key="preferences_name"
android:title="@string/pref_title_display_name"
android:defaultValue="@string/pref_default_display_name"
android:selectAllOnFocus="true"
android:inputType="textCapWords"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />

<EditTextPreference
android:key="preferences_weight"
android:title="@string/pref_title_user_weight"
android:defaultValue="@string/pref_default_user_weight"
android:selectAllOnFocus="true"
android:inputType="number"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>

<PreferenceCategory
android:title="Network"
android:key="pref_key_network">
</PreferenceCategory>
</PreferenceScreen>
8 changes: 8 additions & 0 deletions 2_DataAggregator/Code/app/src/main/res/xml/pref_headers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

<!-- These settings headers are only used on tablets. -->

<header android:fragment="edu.uml.cs411.SettingsActivity$GeneralPreferenceFragment"
android:title="@string/pref_header_general" />

</preference-headers>

1 comment on commit 38dc869

@andylincoln
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, just a comment on excess code:

Lines like @TargetApi(Build.VERSION_CODES.HONEYCOMB) are basically useless code because of the lack of Honeycomb devices out there. While it's not going to break anything it's just extra stuff to sift through.
https://developer.android.com/about/dashboards/index.html?utm_source=suzunone

Please sign in to comment.