Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
* manage queue items
Browse files Browse the repository at this point in the history
git-svn-id: http://sabdroidplus.googlecode.com/svn/trunk@6 cd30d2d2-fef5-11de-8cb8-1d395499b42c
  • Loading branch information
chripede committed Jan 23, 2010
1 parent e8f6581 commit 0767402
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 23 deletions.
3 changes: 1 addition & 2 deletions AndroidManifest.xml
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="4" package="com.googlecode.sabdroidplus"
android:versionName="1.0.0">
package="com.googlecode.sabdroidplus" android:versionCode="1" android:versionName="1.0 Beta1">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="false">
<activity android:label="@string/app_name" android:name="SABDroidPlus">
Expand Down
Binary file modified res/drawable/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions res/layout/queue_list_item.xml
Expand Up @@ -43,4 +43,7 @@
/>

</RelativeLayout>

<TextView android:id="@+id/queueRowNzoId" android:visibility="invisible" android:layout_width="0px" android:layout_height="0px" />

</LinearLayout>
8 changes: 3 additions & 5 deletions res/xml/preferences.xml
Expand Up @@ -43,10 +43,8 @@
</PreferenceCategory>







<PreferenceCategory android:title="Connection settings"><ListPreference android:key="refresh_interval" android:title="Refresh interval" android:summary="@string/setting_refresh_interval" android:entries="@array/refreshIntervalEntries" android:dialogTitle="Refresh every" android:entryValues="@array/refreshIntervalValues"></ListPreference><CheckBoxPreference android:key="refresh_wifi_only" android:summary="Only refresh queue on wifi connection" android:title="Wifi refresh only"></CheckBoxPreference></PreferenceCategory>
<PreferenceCategory android:title="Newzbin settings"><EditTextPreference android:key="newzbin_username" android:summary="Newzbin username" android:title="Username"></EditTextPreference>
<EditTextPreference android:key="newzbin_password" android:summary="Newzbin password" android:title="Password" android:password="true"></EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>
77 changes: 76 additions & 1 deletion src/com/googlecode/sabdroidplus/SABDroidPlus.java
Expand Up @@ -16,15 +16,19 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.googlecode.sabdroidplus.activity.SettingsActivity;
import com.googlecode.sabdroidplus.activity.queue.QueueListRowAdapter;
Expand All @@ -42,11 +46,17 @@ public class SABDroidPlus extends Activity
private static final int MENU_QUIT = 3;
private static final int MENU_PLAY_PAUSE = 4;
private static final int MENU_ADD_NZB = 5;

private static final int CONTEXT_RENAME = 1;
private static final int CONTEXT_DELETE = 2;
private static final int CONTEXT_MOVEUP = 3;
private static final int CONTEXT_MOVEDOWN = 4;

final static int DIALOG_SETUP_PROMPT = 999;

private static ArrayList<String> rows = new ArrayList<String>();
private static JSONObject backupJsonObject;
private ListView listView = null;
protected boolean paused;

@SuppressWarnings("unchecked")
Expand All @@ -60,8 +70,9 @@ protected void onCreate(Bundle savedInstanceState)
SharedPreferences preferences = getSharedPreferences(SABDroidConstants.PREFERENCES_KEY, 0);
Preferences.update(preferences);

ListView listView = (ListView) findViewById(R.id.queueList);
listView = (ListView) findViewById(R.id.queueList);
listView.setAdapter(new QueueListRowAdapter(this, rows));
registerForContextMenu(listView);

// Tries to fetch recoverable data
Object data[] = (Object[]) getLastNonConfigurationInstance();
Expand Down Expand Up @@ -212,6 +223,12 @@ public void handleMessage(Message msg)
case SABnzbdController.MESSAGE_HIDE_INDERTERMINATE_PROGRESS_BAR:
setProgressBarIndeterminateVisibility(false);
break;

case SABnzbdController.MESSAGE_REMOVE_ITEM:
case SABnzbdController.MESSAGE_MOVED_ITEM:
case SABnzbdController.MESSAGE_RENAMED_ITEM:
SABnzbdController.refreshQueue(messageHandler);
break;

default:
break;
Expand Down Expand Up @@ -375,6 +392,64 @@ public Object onRetainNonConfigurationInstance()
data[1] = backupJsonObject;
return data;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);

menu.setHeaderTitle("Manage queue item");
menu.add(0, CONTEXT_RENAME, 0, "Rename");
menu.add(0, CONTEXT_DELETE, 0, "Delete");
menu.add(0, CONTEXT_MOVEUP, 0, "Move up");
menu.add(0, CONTEXT_MOVEDOWN, 0, "Move down");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

View selectedItem = listView.getAdapter().getView(info.position, null, null);
final String nzoId = ((TextView)selectedItem.findViewById(R.id.queueRowNzoId)).getText().toString();

switch(item.getItemId())
{
case CONTEXT_DELETE:
SABnzbdController.removeItem(messageHandler, nzoId);
break;
case CONTEXT_RENAME:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Rename");
alertDialog.setMessage("Enter new name");
final EditText textInput = new EditText(this);
String currentName = ((TextView)selectedItem.findViewById(R.id.queueRowLabelFilename)).getText().toString();
textInput.setText(currentName);
alertDialog.setView(textInput);
alertDialog.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newName = textInput.getText().toString();
SABnzbdController.renameItem(messageHandler, nzoId, newName);
}
});
alertDialog.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
alertDialog.show();
break;
case CONTEXT_MOVEUP:
int position = info.position == 0 ? 0 : info.position - 1;
SABnzbdController.moveItem(messageHandler, nzoId, position);
break;
case CONTEXT_MOVEDOWN:
int downPosition = info.position == (listView.getCount()-1) ? (listView.getCount()-1): info.position + 1;
SABnzbdController.moveItem(messageHandler, nzoId, downPosition);
break;
}
return true;
}

private void updateStatus(String message)
{
Expand Down
Expand Up @@ -43,6 +43,8 @@ public View getView(int position, View convertView, ViewGroup parent)
String completed = Formatter.formatShort(values[2]) + " / " + Formatter.formatShort(values[1]) + " MB";

((TextView) row.findViewById(R.id.queueRowLabelCompleted)).setText(completed);

((TextView) row.findViewById(R.id.queueRowNzoId)).setText(values[3]);

return (row);
}
Expand Down
85 changes: 78 additions & 7 deletions src/com/googlecode/sabdroidplus/sabnzbd/SABnzbdController.java
Expand Up @@ -13,7 +13,7 @@

import com.googlecode.sabdroidplus.Preferences;
import com.googlecode.sabdroidplus.util.HttpUtil;
import com.googlecode.sabdroidplus.util.HttpUtil.ServerConnectinoException;
import com.googlecode.sabdroidplus.util.HttpUtil.ServerConnectionException;

public final class SABnzbdController
{
Expand All @@ -29,6 +29,9 @@ public final class SABnzbdController
public final static int MESSAGE_LOAD_CATEGORIES = 5;
public final static int MESSAGE_ADD_NZB = 6;

public static final int MESSAGE_REMOVE_ITEM = 7;
public static final int MESSAGE_MOVED_ITEM = 8;
public static final int MESSAGE_RENAMED_ITEM = 9;

public static final String STATUS_PAUSED = "paused";

Expand Down Expand Up @@ -135,6 +138,7 @@ public void run()
String rowValues = jobs.getJSONObject(i).get("filename").toString();
rowValues = rowValues + "#" + jobs.getJSONObject(i).getDouble("mb");
rowValues = rowValues + "#" + jobs.getJSONObject(i).getDouble("mbleft");
rowValues = rowValues + "#" + jobs.getJSONObject(i).getString("id");

rows.add(rowValues);
}
Expand All @@ -151,7 +155,7 @@ public void run()
message.setData(bundle);
message.sendToTarget();
}
catch (ServerConnectinoException e)
catch (ServerConnectionException e)
{
sendUpdateMessageStatus(messageHandler, e.getMessage());
}
Expand Down Expand Up @@ -190,7 +194,7 @@ private static void sendUpdateMessageStatus(Handler messageHandler, String text)
message.sendToTarget();
}

public static String makeApiCall(String command, String xTraParams) throws ServerConnectinoException
public static String makeApiCall(String command, String xTraParams) throws ServerConnectionException
{
String url = URL_TEMPLATE;
url = url.replace("[SERVER_URL]", fixUrlFromPreferences(Preferences.get(Preferences.SERVER_URL)));
Expand Down Expand Up @@ -236,14 +240,14 @@ private static String getPreferencesParams()
return "";
}

public static String makeApiCall(String command) throws ServerConnectinoException
public static String makeApiCall(String command) throws ServerConnectionException
{
return makeApiCall(command, "");
}

public static void addNewzbinId(final Handler messageHandler, final int value)
{
if(executingCommand || !Preferences.isSet(Preferences.SERVER_URL))
if(!Preferences.isSet(Preferences.SERVER_URL))
return;

Thread thread = new Thread()
Expand All @@ -267,8 +271,7 @@ public void run()

public static void addFile(final Handler messageHandler, final String nzbUrl, final String category)
{
// Already running or settings not ready
if (executingCommand || !Preferences.isSet(Preferences.SERVER_URL))
if (!Preferences.isSet(Preferences.SERVER_URL))
return;

Thread thread = new Thread()
Expand Down Expand Up @@ -352,4 +355,72 @@ public void run()

thread.start();
}

public static void removeItem(final Handler messageHandler, final String nzoId)
{
Thread thread = new Thread()
{
@Override
public void run() {
try {
makeApiCall("queue", "name=delete&value=" + nzoId);
} catch (ServerConnectionException e) {
e.printStackTrace();
}

Message msg = new Message();
msg.setTarget(messageHandler);
msg.what = MESSAGE_REMOVE_ITEM;
msg.sendToTarget();
}
};

thread.start();
}

public static void moveItem(final Handler messageHandler, final String nzoId, final int position)
{
Thread thread = new Thread()
{
@Override
public void run()
{
try {
makeApiCall("switch", "value=" + nzoId + "&value2=" + position);
} catch (ServerConnectionException e) {
e.printStackTrace();
}

Message msg = new Message();
msg.setTarget(messageHandler);
msg.what = MESSAGE_MOVED_ITEM;
msg.sendToTarget();
}
};

thread.start();
}

public static void renameItem(final Handler messageHandler, final String nzoId, final String newName)
{
Thread thread = new Thread()
{
@Override
public void run()
{
try {
makeApiCall("queue", "name=rename&value=" + nzoId + "&value2=" + newName);
} catch (ServerConnectionException e) {
e.printStackTrace();
}

Message msg = new Message();
msg.setTarget(messageHandler);
msg.what = MESSAGE_RENAMED_ITEM;
msg.sendToTarget();
}
};

thread.start();
}
}
16 changes: 8 additions & 8 deletions src/com/googlecode/sabdroidplus/util/HttpUtil.java
Expand Up @@ -37,9 +37,9 @@ public static HttpUtil instance()
/**
* Gets data from URL
* throws {@link RuntimeException} If anything goes wrong
* @throws ServerConnectinoException
* @throws ServerConnectionException
*/
public String getData(String url) throws ServerConnectinoException
public String getData(String url) throws ServerConnectionException
{
try
{
Expand All @@ -51,7 +51,7 @@ public String getData(String url) throws ServerConnectinoException

if (status != HttpStatus.SC_OK)
{
throw new ServerConnectinoException("Connection Error: " + response.getStatusLine().getReasonPhrase());
throw new ServerConnectionException("Connection Error: " + response.getStatusLine().getReasonPhrase());
}
else
{
Expand All @@ -60,13 +60,13 @@ public String getData(String url) throws ServerConnectinoException
return inputStreamAsString(content);
}
}
catch (ServerConnectinoException e)
catch (ServerConnectionException e)
{
throw new ServerConnectinoException(e.getMessage());
throw new ServerConnectionException(e.getMessage());
}
catch (IOException e)
{
throw new ServerConnectinoException("Connection timeout!");
throw new ServerConnectionException("Connection timeout!");
}
catch (Throwable e)
{
Expand All @@ -92,11 +92,11 @@ public static String inputStreamAsString(InputStream stream) throws IOException
return result.substring(0, result.length() - 1);
}

public class ServerConnectinoException extends Exception
public class ServerConnectionException extends Exception
{
private static final long serialVersionUID = -7812290125811215338L;

public ServerConnectinoException(String message)
public ServerConnectionException(String message)
{
super(message);
}
Expand Down

0 comments on commit 0767402

Please sign in to comment.