@ -17,13 +17,24 @@
* /
* /
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 ;
@ -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,8 +67,9 @@ 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 ) {
@ -64,15 +77,17 @@ public class PreferencesXirvikServer extends PreferenceActivity {
// 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
// ListView adapter, etc.
setPreferenceScreen ( getPreferenceManager ( ) . createPreferenceScreen ( this ) ) ;
setPreferenceScreen ( getPreferenceManager ( ) . createPreferenceScreen ( this ) ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
nameValue = prefs . getString ( Preferences . KEY_PREF_XNAME + serverPostfix , null ) ;
nameValue = prefs . getString ( Preferences . KEY_PREF_XNAME + serverPostfix , null ) ;
typeValue = prefs . getString ( Preferences . KEY_PREF_XTYPE + serverPostfix , null ) ;
typeValue = prefs . getString ( Preferences . KEY_PREF_XTYPE + serverPostfix , null ) ;
serverValue = prefs . getString ( Preferences . KEY_PREF_XSERVER + 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 ) ;
userValue = prefs . getString ( Preferences . KEY_PREF_XUSER + serverPostfix , null ) ;
//passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null);
passValue = prefs . getString ( Preferences . KEY_PREF_XPASS + serverPostfix , null ) ;
// Create preference objects
// Create preference objects
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
getPreferenceScreen ( ) . setTitle ( R . string . xirvik_pref_title ) ;
@ -102,6 +117,13 @@ public class PreferencesXirvikServer extends PreferenceActivity {
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
server . setDialogTitle ( R . string . xirvik_pref_server ) ;
server . setOnPreferenceChangeListener ( updateHandler ) ;
server . setOnPreferenceChangeListener ( updateHandler ) ;
getPreferenceScreen ( ) . addItemFromInflater ( server ) ;
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
user = new TransdroidEditTextPreference ( this ) ;
user = new TransdroidEditTextPreference ( this ) ;
user . setTitle ( R . string . pref_user ) ;
user . setTitle ( R . string . pref_user ) ;
@ -158,16 +180,20 @@ 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 ;
}
}
@ -185,9 +211,63 @@ public class PreferencesXirvikServer extends PreferenceActivity {
} 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 ) {
if ( ( ( TransdroidEditTextPreference ) item ) . isEnabled ( ) ) {
( ( TransdroidEditTextPreference ) item ) . click ( ) ;
( ( 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 ( ) {
@ -199,6 +279,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
type . setSummary ( typeType = = null ? getText ( R . string . xirvik_pref_type_info ) : typeType . toString ( ) ) ;
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 ) ;
server . setSummary ( serverValue = = null ? getText ( R . string . xirvik_pref_server_info ) : serverValue ) ;
user . setSummary ( userValue = = null ? "" : userValue ) ;
user . setSummary ( userValue = = null ? "" : userValue ) ;
folder . setSummary ( folderValue = = null ? "" : folderValue ) ;
}
}