Json RPC server address field in Setting + method to get value from PrefUtil

This commit is contained in:
Yaroslav Berezanskyi 2015-08-15 12:12:23 +03:00
parent cb796f87b5
commit 30b2ade2f4
5 changed files with 91 additions and 60 deletions

View File

@ -38,9 +38,8 @@ public class SettingsActivity extends PreferenceActivity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
@ -59,7 +58,8 @@ public class SettingsActivity extends PreferenceActivity {
// Add 'general' preferences.
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference("runningMode"));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_json_rpc_server_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_running_mode_key)));
/*
// Add 'notifications' preferences, and a corresponding header.
PreferenceCategory fakeHeader = new PreferenceCategory(this);
@ -83,6 +83,45 @@ public class SettingsActivity extends PreferenceActivity {
*/
}
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(mBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
mBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
private Preference.OnPreferenceChangeListener mBindPreferenceSummaryToValueListener =
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
String key = preference.getKey();
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 (key.equals(getString(R.string.pref_json_rpc_server_key))) {
preference.setSummary("Current server address is " + stringValue);
} else {
preference.setSummary(stringValue);
}
return true;
}
};
/**
* {@inheritDoc}
*/
@ -126,30 +165,7 @@ public class SettingsActivity extends PreferenceActivity {
* 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
@ -158,19 +174,9 @@ public class SettingsActivity extends PreferenceActivity {
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
* @see #mBindPreferenceSummaryToValueListener
*/
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

View File

@ -3,7 +3,9 @@ package io.syng.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import java.util.ArrayList;
@ -13,18 +15,18 @@ import io.syng.entity.Profile;
public final class PrefsUtil {
private final static String SHARED_PREFERENCES_FILE = "test";
private static final String PROFILES_KEY = "pref_profile_key";
private static final String FIRST_LAUNCH_KEY = "first_launch_key";
private static final String CURRENT_PROFILE_KEY = "current_profile";
private static PrefsUtil sInstance;
private final Context mContext;
private SharedPreferences mPreferences;
private PrefsUtil(Context context) {
mPreferences = context.getSharedPreferences(SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mContext = context;
}
public static void initialize(Context context) {
@ -49,6 +51,10 @@ public final class PrefsUtil {
return getInstance().mPreferences;
}
private static String getString(@StringRes int resourceId) {
return getInstance().mContext.getString(resourceId);
}
private static void saveProfiles(ArrayList<Profile> profiles) {
try {
getEditor().putString(PROFILES_KEY, ObjectSerializer.serialize(profiles)).commit();
@ -119,4 +125,8 @@ public final class PrefsUtil {
return getPrefs().getInt(profileId, R.drawable.bg0_resized);
}
public static String getJsonRPCServerAddress() {
return getPrefs().getString(getString(R.string.pref_json_rpc_server_key), getString(R.string.pref_json_rpc_server_default));
}
}

View File

@ -1,9 +1,9 @@
<resources>
<string name="app_name">Syng</string>
<string name="app_name">Syng</string>
<string name="drawer_open">Open Navigation Drawer</string>
<string name="drawer_close">Close Navigation Drawer</string>
<string name="title_activity_profile_manager">ProfileManagerActivity</string>
<string name="add_profile">Add Profile</string>
<string name="profile_manager_title">Profiles</string>
@ -16,17 +16,14 @@
<string name="save">Save</string>
<string name="cancel">Cancel</string>
<string name="sImport">Import</string>
<string name="manage_profiles">Manage Profiles</string>
<string name="title_activity_spash_screen">Syng</string>
<string name="wallet_title">Import Wallet</string>
<string name="ok">OK</string>
<string name="json_wallet">Import json wallet</string>
<string name="hex_private_key">Import hex encoded private key</string>
<string name="file_not_found">The specified file was not found.</string>
<string name="error_reading_file">Error reading specified file.</string>
<string name="invalid_wallet_password">Invalid wallet or password.</string>
<string name="file_not_found">The specified file was not found</string>
<string name="error_reading_file">Error reading specified file</string>
<string name="invalid_wallet_password">Invalid wallet or password</string>
<string name="invalid_url">Invalid url</string>

View File

@ -7,22 +7,22 @@
<string name="pref_header_general">General</string>
<string name="pref_title_sync_in_background">Sync in background</string>
<string name="pref_description_sync_in_background">Enable syncing blockchain in background
</string>
<string name="pref_description_sync_in_background">Enable syncing blockchain in background</string>
<string name="pref_title_sync_only_on_wifi">Sync only on wifi</string>
<string name="pref_description_sync_only_on_wifi">Sync blockchain only on wifi
</string>
<string name="pref_description_sync_only_on_wifi">Sync blockchain only on wifi</string>
<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>
<string name="pref_title_running_mode">Running Mode</string>
<string-array name="pref_running_modes_titles">
<item>Full Client</item>
<item>Light Client</item>
<item>Json RPC Client</item>
</string-array>
<string-array name="pref_running_modes_values">
<item>1</item>
<item>2</item>
@ -41,6 +41,7 @@
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
@ -49,4 +50,12 @@
<item>360</item>
<item>-1</item>
</string-array>
<string name="pref_json_rpc_server_default" translate="false">default address</string>
<string name="pref_json_rpc_server_key" translate="false">pref_json_rpc_server_key</string>
<string name="pref_json_rpc_server_title" translate="false">JSON-RPC server address</string>
<string name="pref_running_mode_key" translate="false">runningMode</string>
</resources>

View File

@ -1,24 +1,33 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:defaultValue="@string/pref_json_rpc_server_default"
android:inputType="text"
android:key="@string/pref_json_rpc_server_key"
android:singleLine="true"
android:title="@string/pref_json_rpc_server_title"/>
<ListPreference
android:key="runningMode"
android:title="@string/pref_title_running_mode"
android:defaultValue="3"
android:entries="@array/pref_running_modes_titles"
android:entryValues="@array/pref_running_modes_values"
android:key="@string/pref_running_mode_key"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
android:positiveButtonText="@null"
android:title="@string/pref_title_running_mode"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="syncInBackground"
android:title="@string/pref_title_sync_in_background"
android:summary="@string/pref_description_sync_in_background"
android:defaultValue="true" />
android:title="@string/pref_title_sync_in_background"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="syncOnlyWhenWifi"
android:title="@string/pref_title_sync_only_on_wifi"
android:summary="@string/pref_description_sync_only_on_wifi"
android:defaultValue="true" />
android:title="@string/pref_title_sync_only_on_wifi"/>
</PreferenceScreen>