Eric Kok
12 years ago
18 changed files with 482 additions and 166 deletions
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
@ -0,0 +1,5 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<rotate xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
android:drawable="@drawable/ic_empty_details" |
||||||
|
android:pivotX="50%" |
||||||
|
android:pivotY="50%" /> |
@ -0,0 +1,191 @@ |
|||||||
|
package org.transdroid.core.gui.settings; |
||||||
|
|
||||||
|
import org.androidannotations.annotations.EActivity; |
||||||
|
import org.androidannotations.annotations.Extra; |
||||||
|
|
||||||
|
import android.content.SharedPreferences; |
||||||
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
||||||
|
import android.preference.CheckBoxPreference; |
||||||
|
import android.preference.EditTextPreference; |
||||||
|
import android.preference.ListPreference; |
||||||
|
import android.preference.PreferenceManager; |
||||||
|
import android.preference.PreferenceScreen; |
||||||
|
|
||||||
|
import com.actionbarsherlock.app.SherlockPreferenceActivity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract activity that helps implement a preference screen for key-bound settings, i.e. settings of which there can |
||||||
|
* be multiple and which are identified by an ascending order number/unique key. A typical implementation calls |
||||||
|
* {@link #init(int, int)} during the {@link #onCreate(android.os.Bundle)} (but after calling super.onCreate(Bundle)) |
||||||
|
* and then call initXPreference for each contained preference. {@link #onPreferencesChanged()} can be overridden to |
||||||
|
* react to preference changes, e.g. when field availability should be updated (and where preference dependency isn't |
||||||
|
* enough). |
||||||
|
* @author Eric Kok |
||||||
|
*/ |
||||||
|
@EActivity |
||||||
|
public abstract class KeyBoundPreferencesActivity extends SherlockPreferenceActivity { |
||||||
|
|
||||||
|
@Extra |
||||||
|
protected int key = -1; |
||||||
|
|
||||||
|
private SharedPreferences sharedPrefs; |
||||||
|
|
||||||
|
/** |
||||||
|
* Should be called during the activity {@link #onCreate(android.os.Bundle)} (but after super.onCreate(Bundle)) to |
||||||
|
* load the preferences for this screen from an XML resource. |
||||||
|
* @param preferencesResId The XML resource to read preferences from, which may contain embedded |
||||||
|
* {@link PreferenceScreen} objects |
||||||
|
* @param currentMaxKey The value of what is currently the last defined settings object, or -1 of no settings were |
||||||
|
* defined so far at all |
||||||
|
*/ |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
protected final void init(int preferencesResId, int currentMaxKey) { |
||||||
|
|
||||||
|
// Load the raw preferences to show in this screen
|
||||||
|
addPreferencesFromResource(preferencesResId); |
||||||
|
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); |
||||||
|
|
||||||
|
// If no key was supplied (in the extra bundle) then use a new key instead
|
||||||
|
if (key < 0) { |
||||||
|
key = currentMaxKey + 1; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
protected void onResume() { |
||||||
|
super.onResume(); |
||||||
|
// Monitor preference changes
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener( |
||||||
|
onPreferenceChangeListener); |
||||||
|
}; |
||||||
|
|
||||||
|
protected void onPause() { |
||||||
|
super.onPause(); |
||||||
|
// Stop monitoring preference changes
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener( |
||||||
|
onPreferenceChangeListener); |
||||||
|
}; |
||||||
|
|
||||||
|
private OnSharedPreferenceChangeListener onPreferenceChangeListener = new OnSharedPreferenceChangeListener() { |
||||||
|
@Override |
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { |
||||||
|
onPreferencesChanged(); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Key-bound preference activities may override this method if they want to react to preference changes. |
||||||
|
*/ |
||||||
|
protected void onPreferencesChanged() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that allows for text entry via a dialog. This is used for both string and integer values. No |
||||||
|
* default value will be shown. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @return The concrete {@link EditTextPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
protected final EditTextPreference initTextPreference(String baseName) { |
||||||
|
return initTextPreference(baseName, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that allows for text entry via a dialog. This is used for both string and integer values. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @param defValue The default value for this preference, as shown when no value was yet stored |
||||||
|
* @return The concrete {@link EditTextPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
protected final EditTextPreference initTextPreference(String baseName, String defValue) { |
||||||
|
return initTextPreference(baseName, defValue, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference (including dependency) that allows for text entry via a dialog. This is used for both string |
||||||
|
* and integer values. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @param defValue The default value for this preference, as shown when no value was yet stored |
||||||
|
* @param dependency The base name of the preference to which this preference depends |
||||||
|
* @return The concrete {@link EditTextPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
protected final EditTextPreference initTextPreference(String baseName, String defValue, String dependency) { |
||||||
|
// Update the loaded Preference with the actual preference key to load/store with
|
||||||
|
EditTextPreference pref = (EditTextPreference) findPreference(baseName); |
||||||
|
pref.setKey(baseName + "_" + key); |
||||||
|
pref.setDependency(dependency == null? null: dependency + "_" + key); |
||||||
|
// Update the Preference by loading the current stored value into the EditText, if it exists
|
||||||
|
pref.setText(sharedPrefs.getString(baseName + "_" + key, defValue)); |
||||||
|
return pref; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that simply shows a check box. No default value will be shown. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @return The concrete {@link CheckBoxPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
protected final CheckBoxPreference initBooleanPreference(String baseName) { |
||||||
|
return initBooleanPreference(baseName, false); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that simply shows a check box. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @param defValue The default value for this preference, as shown when no value was yet stored |
||||||
|
* @return The concrete {@link CheckBoxPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
protected final CheckBoxPreference initBooleanPreference(String baseName, boolean defValue) { |
||||||
|
return initBooleanPreference(baseName, defValue, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference (including dependency) that simply shows a check box. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @param defValue The default value for this preference, as shown when no value was yet stored |
||||||
|
* @param dependency The base name of the preference to which this preference depends |
||||||
|
* @return The concrete {@link CheckBoxPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
protected final CheckBoxPreference initBooleanPreference(String baseName, boolean defValue, String dependency) { |
||||||
|
// Update the loaded Preference with the actual preference key to load/store with
|
||||||
|
CheckBoxPreference pref = (CheckBoxPreference) findPreference(baseName); |
||||||
|
pref.setKey(baseName + "_" + key); |
||||||
|
pref.setDependency(dependency == null? null: dependency + "_" + key); |
||||||
|
// Update the Preference by loading the current stored value into the Checkbox, if it exists
|
||||||
|
pref.setChecked(sharedPrefs.getBoolean(baseName + "_" + key, defValue)); |
||||||
|
return pref; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that allows picking an item from a list. No default value will be shown. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @return The concrete {@link ListPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
protected final ListPreference initListPreference(String baseName) { |
||||||
|
return initListPreference(baseName, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates a preference that allows picking an item from a list. |
||||||
|
* @param baseName The base name of the stored preference, e.g. item_name, which will then actually be stored under |
||||||
|
* item_name_[key] |
||||||
|
* @param defValue The default value for this preference, as shown when no value was yet stored |
||||||
|
* @return The concrete {@link ListPreference} that is bound to this preference |
||||||
|
*/ |
||||||
|
@SuppressWarnings("deprecation") |
||||||
|
protected final ListPreference initListPreference(String baseName, String defValue) { |
||||||
|
// Update the loaded Preference with the actual preference key to load/store with
|
||||||
|
ListPreference pref = (ListPreference) findPreference(baseName); |
||||||
|
pref.setKey(baseName + "_" + key); |
||||||
|
// Update the Preference by selecting the current stored value in the list, if it exists
|
||||||
|
pref.setValue(sharedPrefs.getString(baseName + "_" + key, defValue)); |
||||||
|
return pref; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,99 +1,77 @@ |
|||||||
package org.transdroid.core.gui.settings; |
package org.transdroid.core.gui.settings; |
||||||
|
|
||||||
import org.androidannotations.annotations.Bean; |
|
||||||
import org.androidannotations.annotations.EActivity; |
import org.androidannotations.annotations.EActivity; |
||||||
import org.androidannotations.annotations.Extra; |
import org.androidannotations.annotations.OptionsItem; |
||||||
import org.androidannotations.annotations.OptionsMenu; |
import org.androidannotations.annotations.OptionsMenu; |
||||||
import org.transdroid.daemon.Daemon; |
|
||||||
import org.transdroid.core.R; |
import org.transdroid.core.R; |
||||||
import org.transdroid.core.app.settings.ApplicationSettings; |
import org.transdroid.core.app.settings.ApplicationSettings_; |
||||||
|
import org.transdroid.daemon.Daemon; |
||||||
|
|
||||||
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
||||||
import android.os.Bundle; |
import android.os.Bundle; |
||||||
import android.preference.Preference; |
import android.preference.EditTextPreference; |
||||||
import android.preference.Preference.OnPreferenceChangeListener; |
|
||||||
import android.preference.PreferenceManager; |
import android.preference.PreferenceManager; |
||||||
|
|
||||||
import com.actionbarsherlock.app.SherlockPreferenceActivity; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Activity that allows for a configuration of a server. The key can be supplied to update an existing server setting |
* Activity that allows for a configuration of a server. The key can be supplied to update an existing server setting |
||||||
* instead of creating a new one. |
* instead of creating a new one. |
||||||
* @author Eric Kok |
* @author Eric Kok |
||||||
*/ |
*/ |
||||||
@EActivity |
@EActivity |
||||||
@OptionsMenu(resName="activity_deleteableprefs") |
@OptionsMenu(resName = "activity_deleteableprefs") |
||||||
public class ServerSettingsActivity extends SherlockPreferenceActivity { |
public class ServerSettingsActivity extends KeyBoundPreferencesActivity { |
||||||
|
|
||||||
@Extra |
private EditTextPreference extraPass, folder, downloadDir; |
||||||
protected int key = -1; |
|
||||||
|
|
||||||
@Bean |
|
||||||
protected ApplicationSettings applicationSettings; |
|
||||||
|
|
||||||
@SuppressWarnings("deprecation") |
|
||||||
@Override |
@Override |
||||||
protected void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
||||||
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
||||||
|
|
||||||
// Load the raw preferences to show in this screen
|
// Load the raw preferences to show in this screen
|
||||||
addPreferencesFromResource(R.xml.pref_server); |
init(R.xml.pref_server, ApplicationSettings_.getInstance_(this).getMaxServer()); |
||||||
|
initTextPreference("server_name"); |
||||||
// Bind the preferences to the correct storage key, e.g. the first server setting stores its address in the
|
initListPreference("server_type"); |
||||||
// 'server_address_0' shared preferences field
|
initTextPreference("server_address"); |
||||||
if (key < 0) { |
initTextPreference("server_port"); |
||||||
key = applicationSettings.getMaxWebsearch() + 1; |
initTextPreference("server_user"); |
||||||
} |
initTextPreference("server_pass"); |
||||||
findPreference("server_name").setKey("server_name_" + key); |
extraPass = initTextPreference("server_extrapass"); |
||||||
findPreference("server_type").setKey("server_type_" + key); |
initTextPreference("server_localaddress"); |
||||||
findPreference("server_address").setKey("server_address_" + key); |
initTextPreference("server_localnetwork"); |
||||||
findPreference("server_port").setKey("server_port_" + key); |
folder = initTextPreference("server_folder"); |
||||||
findPreference("server_user").setKey("server_user_" + key); |
initTextPreference("server_timeout", "8"); |
||||||
findPreference("server_pass").setKey("server_pass_" + key); |
initBooleanPreference("server_alarmfinished", true); |
||||||
findPreference("server_extrapass").setKey("server_extrapass_" + key); |
initBooleanPreference("server_alarmnew"); |
||||||
findPreference("server_localaddress").setKey("server_localaddress_" + key); |
initListPreference("server_os", "type_linux"); |
||||||
findPreference("server_localnetwork").setKey("server_localnetwork_" + key); |
downloadDir = initTextPreference("server_downloaddir"); |
||||||
findPreference("server_folder").setKey("server_folder_" + key); |
initTextPreference("server_ftpurl"); |
||||||
findPreference("server_timeout").setKey("server_timeout_" + key); |
initTextPreference("server_ftppass"); |
||||||
findPreference("server_alarmfinished").setKey("server_alarmfinished_" + key); |
initBooleanPreference("server_sslenabled"); |
||||||
findPreference("server_alarmnew").setKey("server_alarmnew_" + key); |
initBooleanPreference("server_ssltrustall", false, "server_sslenabled"); |
||||||
findPreference("server_os").setKey("server_os_" + key); |
initTextPreference("server_ssltrustkey", null, "server_sslenabled"); |
||||||
findPreference("server_downloaddir").setKey("server_downloaddir_" + key); |
|
||||||
findPreference("server_ftpurl").setKey("server_ftpurl_" + key); |
|
||||||
findPreference("server_ftppass").setKey("server_ftppass_" + key); |
|
||||||
findPreference("server_sslenabled").setKey("server_sslenabled_" + key); |
|
||||||
findPreference("server_ssltrustall").setKey("server_ssltrustall_" + key); |
|
||||||
findPreference("server_ssltrustall_" + key).setDependency("server_sslenabled_" + key); |
|
||||||
findPreference("server_ssltrustkey").setKey("server_ssltrustkey_" + key); |
|
||||||
findPreference("server_ssltrustkey_" + key).setDependency("server_sslenabled_" + key); |
|
||||||
|
|
||||||
// Monitor preference changes
|
|
||||||
getPreferenceScreen().setOnPreferenceChangeListener(onPreferenceChangeListener); |
|
||||||
} |
} |
||||||
|
|
||||||
private OnPreferenceChangeListener onPreferenceChangeListener = new OnPreferenceChangeListener() { |
@OptionsItem(resName = "action_removesettings") |
||||||
@Override |
protected void removeSettings() { |
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) { |
ApplicationSettings_.getInstance_(this).removeServerSettings(key); |
||||||
// TODO: This doesn't get called
|
finish(); |
||||||
updatePreferenceAvailability(); |
} |
||||||
return true; |
|
||||||
} |
@Override |
||||||
}; |
protected void onPreferencesChanged() { |
||||||
|
|
||||||
@SuppressWarnings("deprecation") |
|
||||||
private void updatePreferenceAvailability() { |
|
||||||
|
|
||||||
// Use daemon factory to see if the newly selected daemon supports the feature
|
// Use daemon factory to see if the newly selected daemon supports the feature
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
||||||
Daemon daemonType = Daemon.fromCode(prefs.getString("server_type_" + key, null)); |
Daemon daemonType = Daemon.fromCode(prefs.getString("server_type_" + key, null)); |
||||||
findPreference("server_extrapass_" + key).setEnabled(Daemon.supportsExtraPassword(daemonType)); |
extraPass.setEnabled(Daemon.supportsExtraPassword(daemonType)); |
||||||
findPreference("server_folder_" + key).setEnabled(daemonType == null? false: Daemon.supportsCustomFolder(daemonType)); |
folder.setEnabled(daemonType == null ? false : Daemon.supportsCustomFolder(daemonType)); |
||||||
findPreference("server_downloaddir_" + key).setEnabled(daemonType == null? false: Daemon.needsManualPathSpecified(daemonType)); |
downloadDir.setEnabled(daemonType == null ? false : Daemon.needsManualPathSpecified(daemonType)); |
||||||
//findPreference("server_ssltrustkey_" + key).setEnabled(sslValue && !sslTAValue);
|
// sslTrustKey.setEnabled(sslValue && !sslTAValue);
|
||||||
|
|
||||||
// Adjust title texts accordingly
|
// Adjust title texts accordingly
|
||||||
findPreference("server_folder_" + key).setTitle(daemonType == Daemon.rTorrent? R.string.pref_scgifolder: R.string.pref_folder); |
folder.setTitle(daemonType == Daemon.rTorrent ? R.string.pref_scgifolder : R.string.pref_folder); |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue