Browse Source

Added default server feature, with option to always ask when adding a torrent (fixes #150). Default to last used server (old behaviour).

pull/173/head
Eric Kok 10 years ago
parent
commit
5e39a67393
  1. 4
      core/res/values/strings.xml
  2. 7
      core/res/xml/pref_main.xml
  3. 60
      core/src/org/transdroid/core/app/settings/ApplicationSettings.java
  4. 41
      core/src/org/transdroid/core/gui/ServerPickerDialog.java
  5. 46
      core/src/org/transdroid/core/gui/TorrentsActivity.java
  6. 21
      core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java

4
core/res/values/strings.xml

@ -90,6 +90,7 @@ @@ -90,6 +90,7 @@
<string name="navigation_selectall">Select all</string>
<string name="navigation_selectfinished">Select finished</string>
<string name="navigation_invertselection">Invert selection</string>
<string name="navigation_pickserver">Add torrent to&#8230;</string>
<string name="status_status">STATUS: %1$s</string>
<string name="status_waiting">Waiting to check&#8230;</string>
@ -216,6 +217,9 @@ @@ -216,6 +217,9 @@
<string name="pref_servers">Servers</string>
<string name="pref_addserver">Add new server</string>
<string name="pref_addseedbox">Add seedbox</string>
<string name="pref_defaultserver">Default server</string>
<string name="pref_defaultserver_lastused">Last used</string>
<string name="pref_defaultserver_askonadd">Ask when adding torrent</string>
<string name="pref_searchsites">Search sites</string>
<string name="pref_setsearchsite">Set default site</string>
<string name="pref_addwebsearch">Add web search site</string>

7
core/res/xml/pref_main.xml

@ -24,7 +24,12 @@ @@ -24,7 +24,12 @@
<org.transdroid.core.gui.settings.OverflowPreference
android:key="header_addserver"
android:title="@string/pref_addserver"
android:order="99" />
android:order="98" />
<ListPreference
android:key="header_defaultserver"
android:title="@string/pref_defaultserver"
android:order="99"
android:defaultValue="-2" />
</PreferenceCategory>
<PreferenceCategory

60
core/src/org/transdroid/core/app/settings/ApplicationSettings.java

@ -51,6 +51,9 @@ import android.preference.PreferenceManager; @@ -51,6 +51,9 @@ import android.preference.PreferenceManager;
@EBean(scope = Scope.Singleton)
public class ApplicationSettings {
public static final int DEFAULTSERVER_LASTUSED = -2;
public static final int DEFAULTSERVER_ASKONADD = -1;
@RootContext
protected Context context;
private SharedPreferences prefs;
@ -234,10 +237,67 @@ public class ApplicationSettings { @@ -234,10 +237,67 @@ public class ApplicationSettings {
edit.remove("server_timeout_" + max);
edit.remove("server_alarmfinished_" + max);
edit.remove("server_alarmfinished_" + max);
// Perhaps we should also update the default server to match the server's new id or remove the default selection
// in case it was this server that was removed
int defaultServer = getDefaultServerKey();
if (defaultServer == order) {
edit.remove("header_defaultserver");
} else if (defaultServer > order) {
// Move 'up' one place to account for the removed server setting
edit.putInt("header_defaultserver", --order);
}
edit.commit();
}
/**
* Returns the settings of the server that was explicitly selected by the user to select as default or, when no
* specific default server was selected, the last used server settings. As opposed to getDefaultServerKey(int), this
* method checks whether the particular server still exists (and returns the first server if not). If no servers are
* configured, null is returned.
* @return A server settings object of the server to use by default, or null if no server is yet configured
*/
public ServerSetting getDefaultServer() {
int defaultServer = getDefaultServerKey();
if (defaultServer == DEFAULTSERVER_LASTUSED || defaultServer == DEFAULTSERVER_ASKONADD) {
return getLastUsedServer();
}
// Use the explicitly selected default server
int max = getMaxOfAllServers(); // Zero-based index, so with max == 0 there is 1 server
if (max < 0) {
// No servers configured
return null;
}
if (defaultServer < 0 || defaultServer > max) {
// Last server was never set or no longer exists
return getServerSetting(0);
}
return getServerSetting(defaultServer);
}
/**
* Returns the unique key of the server setting that the user selected as their default server, or code indicating
* that the last used server should be selected by default; use with getDefaultServer directly. WARNING: the
* returned string may no longer refer to a known server setting key.
* @return An integer; if it is 0 or higher it represents the unique key of a configured server setting, -2 means
* the last used server should be selected as default instead and -1 means the last used server should be
* selected by default for viewing yet it should always ask when adding a new torrent
*/
public int getDefaultServerKey() {
String defaultServer = prefs.getString("header_defaultserver", Integer.toString(DEFAULTSERVER_LASTUSED));
try {
return Integer.parseInt(defaultServer);
} catch (NumberFormatException e) {
// This should NEVER happen but if the setting somehow is not a number, return the default
return DEFAULTSERVER_LASTUSED;
}
}
/**
* Returns the settings of the server that was last used by the user. As opposed to getLastUsedServerKey(int), this
* method checks whether a server was already registered as being last used and check whether the server still

41
core/src/org/transdroid/core/gui/ServerPickerDialog.java

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
package org.transdroid.core.gui;
import java.util.List;
import org.transdroid.core.R;
import org.transdroid.core.app.settings.ServerSetting;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class ServerPickerDialog {
/**
* Opens a dialog that allows the selection of a configured server (manual or seedbox). The calling activity will
* receive a callback on its switchServerAndAddFromIntent(int) method.
* @param activity The torrents activity from which the picker is started (and which received the callback)
* @param serverSettings The list of all available servers, of which their names will be offered to the user to pick
* from (and its position in the list is returned to the activity)
*/
public static void startServerPicker(final TorrentsActivity activity, List<ServerSetting> serverSettings) {
final String[] serverNames = new String[serverSettings.size()];
for (int i = 0; i < serverSettings.size(); i++) {
serverNames[i] = serverSettings.get(i).getName();
}
new DialogFragment() {
public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
return new AlertDialog.Builder(activity).setTitle(R.string.navigation_pickserver)
.setItems(serverNames, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (activity != null)
activity.switchServerAndAddFromIntent(which);
}
}).create();
};
}.show(activity.getFragmentManager(), "serverpicker");
}
}

46
core/src/org/transdroid/core/gui/TorrentsActivity.java

@ -51,11 +51,6 @@ import org.transdroid.core.gui.lists.NoProgressHeaderTransformer; @@ -51,11 +51,6 @@ import org.transdroid.core.gui.lists.NoProgressHeaderTransformer;
import org.transdroid.core.gui.lists.SimpleListItem;
import org.transdroid.core.gui.log.*;
import org.transdroid.core.gui.navigation.*;
import org.transdroid.core.gui.navigation.Label;
import org.transdroid.core.gui.navigation.NavigationFilter;
import org.transdroid.core.gui.navigation.NavigationHelper;
import org.transdroid.core.gui.navigation.RefreshableActivity;
import org.transdroid.core.gui.navigation.StatusType;
import org.transdroid.core.gui.rss.*;
import org.transdroid.core.gui.search.BarcodeHelper;
import org.transdroid.core.gui.search.FilePickerHelper;
@ -236,10 +231,10 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, @@ -236,10 +231,10 @@ public class TorrentsActivity extends Activity implements OnNavigationListener,
// Log messages from the server daemons using our singleton logger
DLog.setLogger(Log_.getInstance_(this));
// Load the last used server or a server that was explicitly supplied in the starting intent
ServerSetting lastUsed = applicationSettings.getLastUsedServer();
if (lastUsed == null) {
// No server settings yet;
// Load the default server or a server that was explicitly supplied in the starting intent
ServerSetting defaultServer = applicationSettings.getDefaultServer();
if (defaultServer == null) {
// No server settings yet
return;
}
Torrent openTorrent = null;
@ -251,7 +246,7 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, @@ -251,7 +246,7 @@ public class TorrentsActivity extends Activity implements OnNavigationListener,
Log.e(this, "Tried to start with " + ListWidgetProvider.EXTRA_SERVER + " intent but " + serverId
+ " is not an existing server order id");
} else {
lastUsed = applicationSettings.getServerSetting(serverId);
defaultServer = applicationSettings.getServerSetting(serverId);
if (getIntent().hasExtra(ListWidgetProvider.EXTRA_TORRENT))
openTorrent = getIntent().getParcelableExtra(ListWidgetProvider.EXTRA_TORRENT);
}
@ -259,12 +254,12 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, @@ -259,12 +254,12 @@ public class TorrentsActivity extends Activity implements OnNavigationListener,
// Set this as selection in the action bar spinner; we can use the server setting key since we have stable ids
// Note: skipNextOnNavigationItemSelectedCalls is used to prevent this event from triggering filterSelected
actionBar.setSelectedNavigationItem(lastUsed.getOrder() + 1);
actionBar.setSelectedNavigationItem(defaultServer.getOrder() + 1);
// Connect to the last used server or a server that was explicitly supplied in the starting intent
if (firstStart) {
// Force first torrents refresh
filterSelected(lastUsed, true);
filterSelected(defaultServer, true);
// Perhaps we can select the last used navigation filter, but only after a first refresh was completed
preselectNavigationFilter = applicationSettings.getLastUsedNavigationFilter();
// Handle any start up intents
@ -276,6 +271,7 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, @@ -276,6 +271,7 @@ public class TorrentsActivity extends Activity implements OnNavigationListener,
}
} else {
// Resume after instead of fully loading the torrents list; create connection and set action bar title
ServerSetting lastUsed = applicationSettings.getLastUsedServer();
currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
navigationSpinnerAdapter.updateCurrentServer(currentConnection);
navigationSpinnerAdapter.updateCurrentFilter(currentFilter);
@ -556,11 +552,31 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, @@ -556,11 +552,31 @@ public class TorrentsActivity extends Activity implements OnNavigationListener,
handleStartIntent();
}
/**
* If required, add torrents, switch to a specific server, etc.
*/
protected void handleStartIntent() {
// For intents that come from out of the application, perhaps we can not directly add them
if (applicationSettings.getDefaultServerKey() == ApplicationSettings.DEFAULTSERVER_ASKONADD
&& getIntent().getData() != null) {
// First ask which server to use before adding any intent from the extras
ServerPickerDialog.startServerPicker(this, applicationSettings.getAllServerSettings());
return;
}
addFromIntent();
}
public void switchServerAndAddFromIntent(int position) {
// Callback from the ServerPickerDialog; force a connection before selecting it (in the navigation)
// Note: we can just use the list position as we have stable server setting ids
ServerSetting selectedServer = applicationSettings.getAllServerSettings().get(position);
filterSelected(selectedServer, false);
addFromIntent();
skipNextOnNavigationItemSelectedCalls++; // Prevent this selection from launching filterSelected() again
getActionBar().setSelectedNavigationItem(position + 1);
}
/**
* If required, add torrents from the supplied intent extras.
*/
protected void addFromIntent() {
Intent intent = getIntent();
Uri dataUri = intent.getData();
String data = intent.getDataString();

21
core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java

@ -110,10 +110,22 @@ public class MainSettingsActivity extends PreferenceActivity { @@ -110,10 +110,22 @@ public class MainSettingsActivity extends PreferenceActivity {
// Add existing servers
List<ServerSetting> servers = applicationSettings.getNormalServerSettings();
String[] serverCodes = new String[servers.size() + 2];
String[] serverNames = new String[servers.size() + 2];
serverCodes[0] = Integer.toString(ApplicationSettings.DEFAULTSERVER_LASTUSED); // Last used
serverNames[0] = getString(R.string.pref_defaultserver_lastused);
serverCodes[1] = Integer.toString(ApplicationSettings.DEFAULTSERVER_ASKONADD); // Ask when adding
serverNames[1] = getString(R.string.pref_defaultserver_askonadd);
int s = 2;
for (ServerSetting serverSetting : servers) {
getPreferenceScreen().addPreference(
new ServerPreference(this).setServerSetting(serverSetting).setOnServerClickedListener(
onServerClicked));
if (serverSetting.getUniqueIdentifier() != null) {
serverCodes[s] = Integer.toString(serverSetting.getOrder());
serverNames[s] = serverSetting.getName();
s++;
}
}
// Add seedboxes; serversOffset keeps an int to have all ServerSettings with unique ids, seedboxOffset is unique
// only per seedbox type
@ -126,8 +138,17 @@ public class MainSettingsActivity extends PreferenceActivity { @@ -126,8 +138,17 @@ public class MainSettingsActivity extends PreferenceActivity {
.setOnSeedboxClickedListener(onSeedboxClicked, seedboxOffset));
orderOffset++;
seedboxOffset++;
if (seedbox.getUniqueIdentifier() != null) {
serverCodes[s] = Integer.toString(seedbox.getOrder());
serverNames[s] = seedbox.getName();
s++;
}
}
}
// Allow selection of the default server
ListPreference defaultServerPreference = (ListPreference) findPreference("header_defaultserver");
defaultServerPreference.setEntries(serverNames);
defaultServerPreference.setEntryValues(serverCodes);
// Add existing RSS feeds
if (!enableRssUi) {

Loading…
Cancel
Save