@ -15,15 +15,26 @@
@@ -15,15 +15,26 @@
* along with Transdroid . If not , see < http : //www.gnu.org/licenses/>.
*
* /
package com.xirvik.transdroid.preferences ;
package com.xirvik.transdroid.preferences ;
import java.io.IOException ;
import java.io.InputStream ;
import org.apache.http.HttpResponse ;
import org.apache.http.client.ClientProtocolException ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.impl.client.DefaultHttpClient ;
import org.transdroid.R ;
import org.transdroid.daemon.DaemonException ;
import org.transdroid.daemon.util.HttpHelper ;
import org.transdroid.preferences.Preferences ;
import org.transdroid.preferences.TransdroidCheckBoxPreference ;
import org.transdroid.preferences.TransdroidEditTextPreference ;
import org.transdroid.preferences.TransdroidListPreference ;
import android.content.SharedPreferences ;
import android.content.SharedPreferences.Editor ;
import android.os.AsyncTask ;
import android.os.Bundle ;
import android.preference.Preference ;
import android.preference.PreferenceActivity ;
@ -39,7 +50,7 @@ import android.widget.Toast;
@@ -39,7 +50,7 @@ import android.widget.Toast;
public class PreferencesXirvikServer extends PreferenceActivity {
public static final String PREFERENCES_XSERVER_KEY = "PREFERENCES_XSERVER_POSTFIX" ;
/*public static final String[] validAddressStart = { "dedi", "semi" };*/
/* public static final String[] validAddressStart = { "dedi", "semi" }; */
public static final String [ ] validAddressEnding = { ".xirvik.com" , ".xirvik.net" } ;
private String serverPostfix ;
@ -47,6 +58,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
@@ -47,6 +58,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private TransdroidEditTextPreference name ;
private TransdroidListPreference type ;
private TransdroidEditTextPreference server ;
private TransdroidEditTextPreference folder ;
private TransdroidEditTextPreference user ;
private TransdroidEditTextPreference pass ;
private TransdroidCheckBoxPreference alarmFinished ;
@ -55,94 +67,104 @@ public class PreferencesXirvikServer extends PreferenceActivity {
@@ -55,94 +67,104 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private String nameValue = null ;
private String typeValue = null ;
private String serverValue = null ;
private String folderValue = null ;
private String userValue = null ;
//private String passValue = null;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
// For which server?
serverPostfix = getIntent ( ) . getStringExtra ( PREFERENCES_XSERVER_KEY ) ;
// Create the preferences screen here: this takes care of saving/loading, but also contains the ListView adapter, etc.
setPreferenceScreen ( getPreferenceManager ( ) . createPreferenceScreen ( this ) ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
nameValue = prefs . getString ( Preferences . KEY_PREF_XNAME + serverPostfix , null ) ;
typeValue = prefs . getString ( Preferences . KEY_PREF_XTYPE + serverPostfix , null ) ;
serverValue = prefs . getString ( Preferences . KEY_PREF_XSERVER + serverPostfix , null ) ;
userValue = prefs . getString ( Preferences . KEY_PREF_XUSER + serverPostfix , null ) ;
//passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null);
// Create preference objects
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
// Name
name = new TransdroidEditTextPreference ( this ) ;
name . setTitle ( R . string . pref_name ) ;
name . setKey ( Preferences . KEY_PREF_XNAME + serverPostfix ) ;
name . getEditText ( ) . setSingleLine ( ) ;
name . setDialogTitle ( R . string . pref_name ) ;
name . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( name ) ;
// Type
type = new TransdroidListPreference ( this ) ;
type . setTitle ( R . string . xirvik_pref_type ) ;
type . setKey ( Preferences . KEY_PREF_XTYPE + serverPostfix ) ;
type . setEntries ( R . array . pref_xirvik_types ) ;
type . setEntryValues ( R . array . pref_xirvik_values ) ;
type . setDialogTitle ( R . string . xirvik_pref_type ) ;
type . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( type ) ;
// Server
server = new TransdroidEditTextPreference ( this ) ;
server . setTitle ( R . string . xirvik_pref_server ) ;
server . setKey ( Preferences . KEY_PREF_XSERVER + serverPostfix ) ;
server . getEditText ( ) . setSingleLine ( ) ;
server . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_URI ) ;
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
server . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( server ) ;
// User
user = new TransdroidEditTextPreference ( this ) ;
user . setTitle ( R . string . pref_user ) ;
user . setKey ( Preferences . KEY_PREF_XUSER + serverPostfix ) ;
user . getEditText ( ) . setSingleLine ( ) ;
user . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_FILTER ) ;
user . setDialogTitle ( R . string . pref_user ) ;
user . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( user ) ;
// Pass
pass = new TransdroidEditTextPreference ( this ) ;
pass . setTitle ( R . string . pref_pass ) ;
pass . setKey ( Preferences . KEY_PREF_XPASS + serverPostfix ) ;
pass . getEditText ( ) . setSingleLine ( ) ;
pass . getEditText ( ) . setInputType ( EditorInfo . TYPE_TEXT_VARIATION_PASSWORD ) ;
pass . getEditText ( ) . setTransformationMethod ( new PasswordTransformationMethod ( ) ) ;
pass . setDialogTitle ( R . string . pref_pass ) ;
pass . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( pass ) ;
// AlertFinished
alarmFinished = new TransdroidCheckBoxPreference ( this ) ;
alarmFinished . setDefaultValue ( true ) ;
alarmFinished . setTitle ( R . string . pref_alarmfinished ) ;
alarmFinished . setSummary ( R . string . pref_alarmfinished_info ) ;
alarmFinished . setKey ( Preferences . KEY_PREF_XALARMFINISHED + serverPostfix ) ;
alarmFinished . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmFinished ) ;
// AlertNew
alarmNew = new TransdroidCheckBoxPreference ( this ) ;
alarmNew . setTitle ( R . string . pref_alarmnew ) ;
alarmNew . setSummary ( R . string . pref_alarmnew_info ) ;
alarmNew . setKey ( Preferences . KEY_PREF_XALARMNEW + serverPostfix ) ;
alarmNew . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmNew ) ;
updateDescriptionTexts ( ) ;
}
private OnPreferenceChangeListener updateHandler = new OnPreferenceChangeListener ( ) {
private String passValue = null ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
// For which server?
serverPostfix = getIntent ( ) . getStringExtra ( PREFERENCES_XSERVER_KEY ) ;
// Create the preferences screen here: this takes care of saving/loading, but also contains the
// ListView adapter, etc.
setPreferenceScreen ( getPreferenceManager ( ) . createPreferenceScreen ( this ) ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
nameValue = prefs . getString ( Preferences . KEY_PREF_XNAME + serverPostfix , null ) ;
typeValue = prefs . getString ( Preferences . KEY_PREF_XTYPE + serverPostfix , null ) ;
serverValue = prefs . getString ( Preferences . KEY_PREF_XSERVER + serverPostfix , null ) ;
folderValue = prefs . getString ( Preferences . KEY_PREF_XFOLDER + serverPostfix , null ) ;
userValue = prefs . getString ( Preferences . KEY_PREF_XUSER + serverPostfix , null ) ;
passValue = prefs . getString ( Preferences . KEY_PREF_XPASS + serverPostfix , null ) ;
// Create preference objects
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
// Name
name = new TransdroidEditTextPreference ( this ) ;
name . setTitle ( R . string . pref_name ) ;
name . setKey ( Preferences . KEY_PREF_XNAME + serverPostfix ) ;
name . getEditText ( ) . setSingleLine ( ) ;
name . setDialogTitle ( R . string . pref_name ) ;
name . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( name ) ;
// Type
type = new TransdroidListPreference ( this ) ;
type . setTitle ( R . string . xirvik_pref_type ) ;
type . setKey ( Preferences . KEY_PREF_XTYPE + serverPostfix ) ;
type . setEntries ( R . array . pref_xirvik_types ) ;
type . setEntryValues ( R . array . pref_xirvik_values ) ;
type . setDialogTitle ( R . string . xirvik_pref_type ) ;
type . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( type ) ;
// Server
server = new TransdroidEditTextPreference ( this ) ;
server . setTitle ( R . string . xirvik_pref_server ) ;
server . setKey ( Preferences . KEY_PREF_XSERVER + serverPostfix ) ;
server . getEditText ( ) . setSingleLine ( ) ;
server . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_URI ) ;
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
server . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( server ) ;
// Folder
folder = new TransdroidEditTextPreference ( this ) ;
folder . setTitle ( R . string . xirvik_pref_folder ) ;
folder . setKey ( Preferences . KEY_PREF_XFOLDER + serverPostfix ) ;
folder . setEnabled ( false ) ;
folder . setSummary ( R . string . xirvik_pref_setautomatically ) ;
getPreferenceScreen ( ) . addItemFromInflater ( folder ) ;
// User
user = new TransdroidEditTextPreference ( this ) ;
user . setTitle ( R . string . pref_user ) ;
user . setKey ( Preferences . KEY_PREF_XUSER + serverPostfix ) ;
user . getEditText ( ) . setSingleLine ( ) ;
user . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_FILTER ) ;
user . setDialogTitle ( R . string . pref_user ) ;
user . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( user ) ;
// Pass
pass = new TransdroidEditTextPreference ( this ) ;
pass . setTitle ( R . string . pref_pass ) ;
pass . setKey ( Preferences . KEY_PREF_XPASS + serverPostfix ) ;
pass . getEditText ( ) . setSingleLine ( ) ;
pass . getEditText ( ) . setInputType ( EditorInfo . TYPE_TEXT_VARIATION_PASSWORD ) ;
pass . getEditText ( ) . setTransformationMethod ( new PasswordTransformationMethod ( ) ) ;
pass . setDialogTitle ( R . string . pref_pass ) ;
pass . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( pass ) ;
// AlertFinished
alarmFinished = new TransdroidCheckBoxPreference ( this ) ;
alarmFinished . setDefaultValue ( true ) ;
alarmFinished . setTitle ( R . string . pref_alarmfinished ) ;
alarmFinished . setSummary ( R . string . pref_alarmfinished_info ) ;
alarmFinished . setKey ( Preferences . KEY_PREF_XALARMFINISHED + serverPostfix ) ;
alarmFinished . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmFinished ) ;
// AlertNew
alarmNew = new TransdroidCheckBoxPreference ( this ) ;
alarmNew . setTitle ( R . string . pref_alarmnew ) ;
alarmNew . setSummary ( R . string . pref_alarmnew_info ) ;
alarmNew . setKey ( Preferences . KEY_PREF_XALARMNEW + serverPostfix ) ;
alarmNew . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmNew ) ;
updateDescriptionTexts ( ) ;
}
private OnPreferenceChangeListener updateHandler = new OnPreferenceChangeListener ( ) {
@Override
public boolean onPreferenceChange ( Preference preference , Object newValue ) {
if ( preference = = name ) {
@ -158,48 +180,107 @@ public class PreferencesXirvikServer extends PreferenceActivity {
@@ -158,48 +180,107 @@ public class PreferencesXirvikServer extends PreferenceActivity {
validEnd | = newServer . endsWith ( validAddressEnding [ i ] ) ;
}
if ( ! valid | | ! validEnd ) {
Toast . makeText ( getApplicationContext ( ) , R . string . xirvik_error_invalid_servername , Toast . LENGTH_LONG ) . show ( ) ;
Toast
. makeText ( getApplicationContext ( ) , R . string . xirvik_error_invalid_servername , Toast . LENGTH_LONG )
. show ( ) ;
return false ;
}
serverValue = newServer ;
} else if ( preference = = user ) {
userValue = ( String ) newValue ;
} else if ( preference = = pass ) {
//passValue = (String) newValue;
passValue = ( String ) newValue ;
}
updateDescriptionTexts ( ) ;
updateScgiMountFolder ( ) ;
// Set the value as usual
return true ;
}
} ;
} ;
@Override
protected void onListItemClick ( ListView l , View v , int position , long id ) {
@Override
protected void onListItemClick ( ListView l , View v , int position , long id ) {
// Perform click action, which always is a Preference
Preference item = ( Preference ) getListAdapter ( ) . getItem ( position ) ;
// Perform click action, which always is a Preference
Preference item = ( Preference ) getListAdapter ( ) . getItem ( position ) ;
// Let the Preference open the right dialog
if ( item instanceof TransdroidListPreference ) {
( ( TransdroidListPreference ) item ) . click ( ) ;
( ( TransdroidListPreference ) item ) . click ( ) ;
} else if ( item instanceof TransdroidCheckBoxPreference ) {
( ( TransdroidCheckBoxPreference ) item ) . click ( ) ;
( ( TransdroidCheckBoxPreference ) item ) . click ( ) ;
} else if ( item instanceof TransdroidEditTextPreference ) {
( ( TransdroidEditTextPreference ) item ) . click ( ) ;
if ( ( ( TransdroidEditTextPreference ) item ) . isEnabled ( ) ) {
( ( TransdroidEditTextPreference ) item ) . click ( ) ;
}
}
}
private void updateScgiMountFolder ( ) {
if ( typeValue ! = null & & XirvikServerType . fromCode ( typeValue ) = = XirvikServerType . SharedRtorrent ) {
new AsyncTask < Void , Void , String > ( ) {
@Override
protected String doInBackground ( Void . . . params ) {
try {
// Get, from the server, the RPC SCGI mount address
DefaultHttpClient httpclient = HttpHelper . createStandardHttpClient ( true , userValue , passValue ,
true , null , HttpHelper . DEFAULT_CONNECTION_TIMEOUT , serverValue , 443 ) ;
String url = "https://" + serverValue + ":443/browsers_addons/transdroid_autoconf.txt" ;
HttpResponse request = httpclient . execute ( new HttpGet ( url ) ) ;
InputStream stream = request . getEntity ( ) . getContent ( ) ;
String folderVal = HttpHelper . ConvertStreamToString ( stream ) . trim ( ) ;
if ( folderVal . startsWith ( "<?xml" ) ) {
folderVal = null ;
}
stream . close ( ) ;
return folderVal ;
} catch ( DaemonException e ) {
} catch ( ClientProtocolException e ) {
} catch ( IOException e ) {
}
return null ;
}
@Override
protected void onPostExecute ( String result ) {
storeScgiMountFolder ( result ) ;
}
} . execute ( ) ;
} else {
// No need to retrieve this value
storeScgiMountFolder ( XirvikSettings . RTORRENT_FOLDER ) ;
}
}
protected void storeScgiMountFolder ( String result ) {
if ( result = = null ) {
// The RPC SCGI mount folder address couldn't be retrieved, so we cannot continue: show an error
Toast . makeText ( getApplicationContext ( ) , R . string . xirvik_error_nofolder , Toast . LENGTH_LONG ) . show ( ) ;
folder . setSummary ( R . string . xirvik_error_nofolder ) ;
result = "" ;
}
}
// Store the new folder setting
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( getApplicationContext ( ) ) ;
Editor edit = prefs . edit ( ) ;
edit . putString ( Preferences . KEY_PREF_XFOLDER + serverPostfix , result ) ;
edit . commit ( ) ;
folderValue = result ;
updateDescriptionTexts ( ) ;
}
private void updateDescriptionTexts ( ) {
private void updateDescriptionTexts ( ) {
// Update the 'summary' labels of all preferences to show their current value
XirvikServerType typeType = XirvikServerType . fromCode ( typeValue ) ;
// Update the 'summary' labels of all preferences to show their current value
XirvikServerType typeType = XirvikServerType . fromCode ( typeValue ) ;
name . setSummary ( nameValue = = null ? getText ( R . string . pref_name_info ) : nameValue ) ;
type . setSummary ( typeType = = null ? getText ( R . string . xirvik_pref_type_info ) : typeType . toString ( ) ) ;
server . setSummary ( serverValue = = null ? getText ( R . string . xirvik_pref_server_info ) : serverValue ) ;
user . setSummary ( userValue = = null ? "" : userValue ) ;
name . setSummary ( nameValue = = null ? getText ( R . string . pref_name_info ) : nameValue ) ;
type . setSummary ( typeType = = null ? getText ( R . string . xirvik_pref_type_info ) : typeType . toString ( ) ) ;
server . setSummary ( serverValue = = null ? getText ( R . string . xirvik_pref_server_info ) : serverValue ) ;
user . setSummary ( userValue = = null ? "" : userValue ) ;
folder . setSummary ( folderValue = = null ? "" : folderValue ) ;
}
}
}