@ -15,15 +15,26 @@
* along with Transdroid . If not , see < http : //www.gnu.org/licenses/>.
* 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.R ;
import org.transdroid.daemon.DaemonException ;
import org.transdroid.daemon.util.HttpHelper ;
import org.transdroid.preferences.Preferences ;
import org.transdroid.preferences.Preferences ;
import org.transdroid.preferences.TransdroidCheckBoxPreference ;
import org.transdroid.preferences.TransdroidCheckBoxPreference ;
import org.transdroid.preferences.TransdroidEditTextPreference ;
import org.transdroid.preferences.TransdroidEditTextPreference ;
import org.transdroid.preferences.TransdroidListPreference ;
import org.transdroid.preferences.TransdroidListPreference ;
import android.content.SharedPreferences ;
import android.content.SharedPreferences ;
import android.content.SharedPreferences.Editor ;
import android.os.AsyncTask ;
import android.os.Bundle ;
import android.os.Bundle ;
import android.preference.Preference ;
import android.preference.Preference ;
import android.preference.PreferenceActivity ;
import android.preference.PreferenceActivity ;
@ -39,7 +50,7 @@ import android.widget.Toast;
public class PreferencesXirvikServer extends PreferenceActivity {
public class PreferencesXirvikServer extends PreferenceActivity {
public static final String PREFERENCES_XSERVER_KEY = "PREFERENCES_XSERVER_POSTFIX" ;
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" } ;
public static final String [ ] validAddressEnding = { ".xirvik.com" , ".xirvik.net" } ;
private String serverPostfix ;
private String serverPostfix ;
@ -47,6 +58,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private TransdroidEditTextPreference name ;
private TransdroidEditTextPreference name ;
private TransdroidListPreference type ;
private TransdroidListPreference type ;
private TransdroidEditTextPreference server ;
private TransdroidEditTextPreference server ;
private TransdroidEditTextPreference folder ;
private TransdroidEditTextPreference user ;
private TransdroidEditTextPreference user ;
private TransdroidEditTextPreference pass ;
private TransdroidEditTextPreference pass ;
private TransdroidCheckBoxPreference alarmFinished ;
private TransdroidCheckBoxPreference alarmFinished ;
@ -55,94 +67,104 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private String nameValue = null ;
private String nameValue = null ;
private String typeValue = null ;
private String typeValue = null ;
private String serverValue = null ;
private String serverValue = null ;
private String folderValue = null ;
private String userValue = null ;
private String userValue = null ;
//private String passValue = null;
private String passValue = null ;
@Override
@Override
protected void onCreate ( Bundle savedInstanceState ) {
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
super . onCreate ( savedInstanceState ) ;
// For which server?
// For which server?
serverPostfix = getIntent ( ) . getStringExtra ( PREFERENCES_XSERVER_KEY ) ;
serverPostfix = getIntent ( ) . getStringExtra ( PREFERENCES_XSERVER_KEY ) ;
// Create the preferences screen here: this takes care of saving/loading, but also contains the ListView adapter, etc.
// Create the preferences screen here: this takes care of saving/loading, but also contains the
setPreferenceScreen ( getPreferenceManager ( ) . createPreferenceScreen ( this ) ) ;
// ListView adapter, etc.
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
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 ) ;
nameValue = prefs . getString ( Preferences . KEY_PREF_XNAME + serverPostfix , null ) ;
serverValue = prefs . getString ( Preferences . KEY_PREF_XSERVER + serverPostfix , null ) ;
typeValue = prefs . getString ( Preferences . KEY_PREF_XTYPE + serverPostfix , null ) ;
userValue = prefs . getString ( Preferences . KEY_PREF_XUSER + serverPostfix , null ) ;
serverValue = prefs . getString ( Preferences . KEY_PREF_XSERVER + serverPostfix , null ) ;
//passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null);
folderValue = prefs . getString ( Preferences . KEY_PREF_XFOLDER + serverPostfix , null ) ;
userValue = prefs . getString ( Preferences . KEY_PREF_XUSER + serverPostfix , null ) ;
// Create preference objects
passValue = prefs . getString ( Preferences . KEY_PREF_XPASS + serverPostfix , null ) ;
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
// Name
// Create preference objects
name = new TransdroidEditTextPreference ( this ) ;
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
name . setTitle ( R . string . pref_name ) ;
// Name
name . setKey ( Preferences . KEY_PREF_XNAME + serverPostfix ) ;
name = new TransdroidEditTextPreference ( this ) ;
name . getEditText ( ) . setSingleLine ( ) ;
name . setTitle ( R . string . pref_name ) ;
name . setDialogTitle ( R . string . pref_name ) ;
name . setKey ( Preferences . KEY_PREF_XNAME + serverPostfix ) ;
name . setOnPreferenceChangeListener ( updateHandler ) ;
name . getEditText ( ) . setSingleLine ( ) ;
getPreferenceScreen ( ) . addItemFromInflater ( name ) ;
name . setDialogTitle ( R . string . pref_name ) ;
// Type
name . setOnPreferenceChangeListener ( updateHandler ) ;
type = new TransdroidListPreference ( this ) ;
getPreferenceScreen ( ) . addItemFromInflater ( name ) ;
type . setTitle ( R . string . xirvik_pref_type ) ;
// Type
type . setKey ( Preferences . KEY_PREF_XTYPE + serverPostfix ) ;
type = new TransdroidListPreference ( this ) ;
type . setEntries ( R . array . pref_xirvik_types ) ;
type . setTitle ( R . string . xirvik_pref_type ) ;
type . setEntryValues ( R . array . pref_xirvik_values ) ;
type . setKey ( Preferences . KEY_PREF_XTYPE + serverPostfix ) ;
type . setDialogTitle ( R . string . xirvik_pref_type ) ;
type . setEntries ( R . array . pref_xirvik_types ) ;
type . setOnPreferenceChangeListener ( updateHandler ) ;
type . setEntryValues ( R . array . pref_xirvik_values ) ;
getPreferenceScreen ( ) . addItemFromInflater ( type ) ;
type . setDialogTitle ( R . string . xirvik_pref_type ) ;
// Server
type . setOnPreferenceChangeListener ( updateHandler ) ;
server = new TransdroidEditTextPreference ( this ) ;
getPreferenceScreen ( ) . addItemFromInflater ( type ) ;
server . setTitle ( R . string . xirvik_pref_server ) ;
// Server
server . setKey ( Preferences . KEY_PREF_XSERVER + serverPostfix ) ;
server = new TransdroidEditTextPreference ( this ) ;
server . getEditText ( ) . setSingleLine ( ) ;
server . setTitle ( R . string . xirvik_pref_server ) ;
server . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_URI ) ;
server . setKey ( Preferences . KEY_PREF_XSERVER + serverPostfix ) ;
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
server . getEditText ( ) . setSingleLine ( ) ;
server . setOnPreferenceChangeListener ( updateHandler ) ;
server . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_URI ) ;
getPreferenceScreen ( ) . addItemFromInflater ( server ) ;
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
// User
server . setOnPreferenceChangeListener ( updateHandler ) ;
user = new TransdroidEditTextPreference ( this ) ;
getPreferenceScreen ( ) . addItemFromInflater ( server ) ;
user . setTitle ( R . string . pref_user ) ;
// Folder
user . setKey ( Preferences . KEY_PREF_XUSER + serverPostfix ) ;
folder = new TransdroidEditTextPreference ( this ) ;
user . getEditText ( ) . setSingleLine ( ) ;
folder . setTitle ( R . string . xirvik_pref_folder ) ;
user . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_FILTER ) ;
folder . setKey ( Preferences . KEY_PREF_XFOLDER + serverPostfix ) ;
user . setDialogTitle ( R . string . pref_user ) ;
folder . setEnabled ( false ) ;
user . setOnPreferenceChangeListener ( updateHandler ) ;
folder . setSummary ( R . string . xirvik_pref_setautomatically ) ;
getPreferenceScreen ( ) . addItemFromInflater ( user ) ;
getPreferenceScreen ( ) . addItemFromInflater ( folder ) ;
// Pass
// User
pass = new TransdroidEditTextPreference ( this ) ;
user = new TransdroidEditTextPreference ( this ) ;
pass . setTitle ( R . string . pref_pass ) ;
user . setTitle ( R . string . pref_user ) ;
pass . setKey ( Preferences . KEY_PREF_XPASS + serverPostfix ) ;
user . setKey ( Preferences . KEY_PREF_XUSER + serverPostfix ) ;
pass . getEditText ( ) . setSingleLine ( ) ;
user . getEditText ( ) . setSingleLine ( ) ;
pass . getEditText ( ) . setInputType ( EditorInfo . TYPE_TEXT_VARIATION_PASSWORD ) ;
user . getEditText ( ) . setInputType ( InputType . TYPE_CLASS_TEXT | InputType . TYPE_TEXT_VARIATION_FILTER ) ;
pass . getEditText ( ) . setTransformationMethod ( new PasswordTransformationMethod ( ) ) ;
user . setDialogTitle ( R . string . pref_user ) ;
pass . setDialogTitle ( R . string . pref_pass ) ;
user . setOnPreferenceChangeListener ( updateHandler ) ;
pass . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( user ) ;
getPreferenceScreen ( ) . addItemFromInflater ( pass ) ;
// Pass
pass = new TransdroidEditTextPreference ( this ) ;
// AlertFinished
pass . setTitle ( R . string . pref_pass ) ;
alarmFinished = new TransdroidCheckBoxPreference ( this ) ;
pass . setKey ( Preferences . KEY_PREF_XPASS + serverPostfix ) ;
alarmFinished . setDefaultValue ( true ) ;
pass . getEditText ( ) . setSingleLine ( ) ;
alarmFinished . setTitle ( R . string . pref_alarmfinished ) ;
pass . getEditText ( ) . setInputType ( EditorInfo . TYPE_TEXT_VARIATION_PASSWORD ) ;
alarmFinished . setSummary ( R . string . pref_alarmfinished_info ) ;
pass . getEditText ( ) . setTransformationMethod ( new PasswordTransformationMethod ( ) ) ;
alarmFinished . setKey ( Preferences . KEY_PREF_XALARMFINISHED + serverPostfix ) ;
pass . setDialogTitle ( R . string . pref_pass ) ;
alarmFinished . setOnPreferenceChangeListener ( updateHandler ) ;
pass . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmFinished ) ;
getPreferenceScreen ( ) . addItemFromInflater ( pass ) ;
// AlertNew
alarmNew = new TransdroidCheckBoxPreference ( this ) ;
// AlertFinished
alarmNew . setTitle ( R . string . pref_alarmnew ) ;
alarmFinished = new TransdroidCheckBoxPreference ( this ) ;
alarmNew . setSummary ( R . string . pref_alarmnew_info ) ;
alarmFinished . setDefaultValue ( true ) ;
alarmNew . setKey ( Preferences . KEY_PREF_XALARMNEW + serverPostfix ) ;
alarmFinished . setTitle ( R . string . pref_alarmfinished ) ;
alarmNew . setOnPreferenceChangeListener ( updateHandler ) ;
alarmFinished . setSummary ( R . string . pref_alarmfinished_info ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmNew ) ;
alarmFinished . setKey ( Preferences . KEY_PREF_XALARMFINISHED + serverPostfix ) ;
alarmFinished . setOnPreferenceChangeListener ( updateHandler ) ;
updateDescriptionTexts ( ) ;
getPreferenceScreen ( ) . addItemFromInflater ( alarmFinished ) ;
// AlertNew
}
alarmNew = new TransdroidCheckBoxPreference ( this ) ;
alarmNew . setTitle ( R . string . pref_alarmnew ) ;
private OnPreferenceChangeListener updateHandler = new OnPreferenceChangeListener ( ) {
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
@Override
public boolean onPreferenceChange ( Preference preference , Object newValue ) {
public boolean onPreferenceChange ( Preference preference , Object newValue ) {
if ( preference = = name ) {
if ( preference = = name ) {
@ -158,48 +180,107 @@ public class PreferencesXirvikServer extends PreferenceActivity {
validEnd | = newServer . endsWith ( validAddressEnding [ i ] ) ;
validEnd | = newServer . endsWith ( validAddressEnding [ i ] ) ;
}
}
if ( ! valid | | ! validEnd ) {
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 ;
return false ;
}
}
serverValue = newServer ;
serverValue = newServer ;
} else if ( preference = = user ) {
} else if ( preference = = user ) {
userValue = ( String ) newValue ;
userValue = ( String ) newValue ;
} else if ( preference = = pass ) {
} else if ( preference = = pass ) {
//passValue = (String) newValue;
passValue = ( String ) newValue ;
}
}
updateDescriptionTexts ( ) ;
updateDescriptionTexts ( ) ;
updateScgiMountFolder ( ) ;
// Set the value as usual
// Set the value as usual
return true ;
return true ;
}
}
} ;
} ;
@Override
@Override
protected void onListItemClick ( ListView l , View v , int position , long id ) {
protected void onListItemClick ( ListView l , View v , int position , long id ) {
// Perform click action, which always is a Preference
// Perform click action, which always is a Preference
Preference item = ( Preference ) getListAdapter ( ) . getItem ( position ) ;
Preference item = ( Preference ) getListAdapter ( ) . getItem ( position ) ;
// Let the Preference open the right dialog
// Let the Preference open the right dialog
if ( item instanceof TransdroidListPreference ) {
if ( item instanceof TransdroidListPreference ) {
( ( TransdroidListPreference ) item ) . click ( ) ;
( ( TransdroidListPreference ) item ) . click ( ) ;
} else if ( item instanceof TransdroidCheckBoxPreference ) {
} else if ( item instanceof TransdroidCheckBoxPreference ) {
( ( TransdroidCheckBoxPreference ) item ) . click ( ) ;
( ( TransdroidCheckBoxPreference ) item ) . click ( ) ;
} else if ( item instanceof TransdroidEditTextPreference ) {
} 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 ( ) ) ;
private void updateDescriptionTexts ( ) {
Editor edit = prefs . edit ( ) ;
edit . putString ( Preferences . KEY_PREF_XFOLDER + serverPostfix , result ) ;
// Update the 'summary' labels of all preferences to show their current value
edit . commit ( ) ;
XirvikServerType typeType = XirvikServerType . fromCode ( typeValue ) ;
folderValue = result ;
updateDescriptionTexts ( ) ;
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 ) ;
private void updateDescriptionTexts ( ) {
user . setSummary ( userValue = = null ? "" : userValue ) ;
// 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 ) ;
folder . setSummary ( folderValue = = null ? "" : folderValue ) ;
}
}
}