Skip to content

Commit

Permalink
custom device handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nheid committed Jan 11, 2011
1 parent fbd3454 commit 79682c8
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 56 deletions.
16 changes: 16 additions & 0 deletions res/layout/select_device.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/devicelist" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_above="@id/downloadButton" />

<Button android:id="@+id/btn_customname" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="create custom device"
android:layout_alignParentBottom="true" />


</RelativeLayout>

1 change: 1 addition & 0 deletions src/com/unitedcoders/android/gpodroid/GpodRoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class GpodRoid {

public static String LOGTAG = "GpodRoid";
public static Preferences prefs;


}
18 changes: 9 additions & 9 deletions src/com/unitedcoders/android/gpodroid/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
public class Preferences {

private static Preferences preferences;
// private static GpodRoid.prefs preferences;

private Context context;
public static void setPreferences(Preferences preferences) {
Preferences.preferences = preferences;
GpodRoid.prefs = preferences;
}

public static final String PREFS_NAME = "gpodroidPrefs";
Expand Down Expand Up @@ -72,16 +72,16 @@ public void save() {
}

public static Preferences getPreferences(Context context) {
if (preferences == null) {
preferences = new Preferences();
if (GpodRoid.prefs == null) {
GpodRoid.prefs = new Preferences();
}

preferences.setContext(context);
GpodRoid.prefs.setContext(context);
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
preferences.setUsername(settings.getString("USERNAME", ""));
preferences.setPassword(settings.getString("PASSWORD", ""));
preferences.setDevice(settings.getString("DEVICE", ""));
return preferences;
GpodRoid.prefs.setUsername(settings.getString("USERNAME", ""));
GpodRoid.prefs.setPassword(settings.getString("PASSWORD", ""));
GpodRoid.prefs.setDevice(settings.getString("DEVICE", ""));
return GpodRoid.prefs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.Button;
import android.widget.Toast;

import com.unitedcoders.android.gpodroid.GpodRoid;
import com.unitedcoders.android.gpodroid.PodcastElement;
import com.unitedcoders.android.gpodroid.PodcastListAdapter;
import com.unitedcoders.android.gpodroid.Preferences;
Expand All @@ -35,7 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
pcla.setShowCheckbox(true);

// get preferences
Preferences pref = Preferences.getPreferences(getApplicationContext());
Preferences pref = GpodRoid.prefs;
if (pref.getUsername().equals("") || pref.getPassword().equals("") || pref.getDevice().equals("")) {

Toast toast = Toast.makeText(getApplicationContext(), "please enter your settings first",
Expand All @@ -46,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {

}

GpodderUpdates podcast = GpodderAPI.getDownloadList(pref);
GpodderUpdates podcast = GpodderAPI.getDownloadList();

// add items to download list
for (int i = 0; i < podcast.getUpdates().size(); i++) {
Expand Down
5 changes: 5 additions & 0 deletions src/com/unitedcoders/android/gpodroid/activity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import android.widget.Button;
import android.widget.TextView;

import com.unitedcoders.android.gpodroid.GpodRoid;
import com.unitedcoders.android.gpodroid.PodcastElement;
import com.unitedcoders.android.gpodroid.Preferences;
import com.unitedcoders.android.gpodroid.R;

/**
Expand All @@ -37,6 +39,9 @@ protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview);

// load preferences
GpodRoid.prefs = Preferences.getPreferences(getApplicationContext());

// Drawable d = GPodderActions
// .LoadImageFromWebOperations("http://www.sysadminslife.com/wp-content/uploads/2009/07/tux.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void run() {
};

private void backgroundPodcastInfoFetcher() {
GpodderUpdates podcast = GpodderAPI.getDownloadList(pref);
GpodderUpdates podcast = GpodderAPI.getDownloadList();

if(podcast == null){
Log.e(GpodRoid.LOGTAG, "cant display downloads, got empty result");
Expand Down
69 changes: 58 additions & 11 deletions src/com/unitedcoders/android/gpodroid/activity/SelectDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,90 @@

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.unitedcoders.android.gpodroid.Preferences;
import com.unitedcoders.android.gpodroid.R;
import com.unitedcoders.gpodder.GpodderAPI;

/**
* Shows gpodder registered devices.
*
* @author Nico Heid
*
*
*/
public class SelectDevice extends ListActivity {
public class SelectDevice extends ListActivity implements OnClickListener {

Button btnCustomName;
Preferences pref;
ArrayList<String> devices;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.select_device);

devices = new GpodderAPI().getDevices(getApplicationContext());
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, devices));

ArrayList<String> devices = new GpodderAPI("", "").getDevices(getApplicationContext());
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, devices));
btnCustomName = (Button) findViewById(R.id.btn_customname);
btnCustomName.setOnClickListener(this);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);

Preferences pref = Preferences.getPreferences(getApplicationContext());
// super.onListItemClick(l, v, position, id);

String device = (String) l.getItemAtPosition(position);
pref.setDevice(device);
pref.save();

saveDevice(device);
finish();
}

@Override
public void onClick(View v) {

if (v == btnCustomName) {
customNameDialoge();
}

}

private void customNameDialoge() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText customName = new EditText(this);
alert.setView(customName);
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
GpodderAPI.createDevice(getApplicationContext(), customName.getText().toString());
devices = new GpodderAPI().getDevices(getApplicationContext());
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,
devices));

}
});
alert.setNegativeButton("Cancel", null);
alert.show();
}

private void saveDevice(String deviceName) {
pref = Preferences.getPreferences(getApplicationContext());
pref.setDevice(deviceName);
pref.save();

}

}
4 changes: 2 additions & 2 deletions src/com/unitedcoders/android/gpodroid/activity/Subscribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

top25hm = new GpodderAPI("", "").getTopSubscriptions(getApplicationContext());
top25hm = new GpodderAPI().getTopSubscriptions(getApplicationContext());
top25 = new ArrayList<String>(top25hm.keySet());
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, top25));
registerForContextMenu(getListView());
Expand All @@ -41,7 +41,7 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn
AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
String itemAtPosition = (String) getListView().getItemAtPosition(info.position);
Log.d("GPR", "subscribing to " + top25hm.get(itemAtPosition));
new GpodderAPI("", "").addSubcription(getApplicationContext(), top25hm.get(itemAtPosition));
new GpodderAPI().addSubcription(getApplicationContext(), top25hm.get(itemAtPosition));
menu.add("subscribe");

}
Expand Down
54 changes: 23 additions & 31 deletions src/com/unitedcoders/gpodder/GpodderAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ public class GpodderAPI {

private HttpURLConnection connection;
private URL urlGetNewPodcasts;
private String username;
private String password;


static GpodderUpdates downloadListResponse = null;

public GpodderAPI(String user, String password) {
this.username = user;
this.password = password;
public GpodderAPI() {
}

public GpodderUpdates parseResponse(InputStream inputStream) {
Expand All @@ -68,12 +64,12 @@ public GpodderUpdates parseResponse(InputStream inputStream) {

}

public static GpodderUpdates getDownloadList(Preferences pref) {
public static GpodderUpdates getDownloadList() {

if(downloadListResponse != null){
if (downloadListResponse != null) {
return downloadListResponse;
}

// get preferences

URL url;
Expand All @@ -82,14 +78,17 @@ public static GpodderUpdates getDownloadList(Preferences pref) {
Long since = (new Date().getTime() / 1000) - 3600 * 24 * 14;

String urlStr = "http://gpodder.net/api/2/updates/USERNAME/DEVICE.json?since=" + since;
urlStr = urlStr.replace("USERNAME", pref.getUsername());
urlStr = urlStr.replace("DEVICE", pref.getDevice());
urlStr = urlStr.replace("USERNAME", GpodRoid.prefs.getUsername());
urlStr = urlStr.replace("DEVICE", GpodRoid.prefs.getDevice());
url = new URL(urlStr);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Authorization",
"Basic " + Base64.encodeBytes((pref.getUsername() + ":" + pref.getPassword()).getBytes()));
conn.setRequestProperty(
"Authorization",
"Basic "
+ Base64.encodeBytes((GpodRoid.prefs.getUsername() + ":" + GpodRoid.prefs.getPassword())
.getBytes()));
// + BasicAuth.encode(username, password));
GpodderAPI api = new GpodderAPI("", "");
GpodderAPI api = new GpodderAPI();
InputStream is = conn.getInputStream();
downloadListResponse = api.parseResponse(is);
is.close();
Expand All @@ -106,13 +105,15 @@ public static GpodderUpdates getDownloadList(Preferences pref) {

}

public void createDevice(Context context) throws JSONException {
String urlStr = String.format("http://gpodder.net/api/2/devices/%s/gpodroid.json", username);
public static void createDevice(Context context, String deviceName) {
String urlStr = String
.format("http://gpodder.net/api/2/devices/%s/gpodroid.json", GpodRoid.prefs.getUsername());
JSONObject device = new JSONObject();
device.put("caption", "gpodroid");
device.put("type", "mobile");

try {
device.put("caption", deviceName);
device.put("id", deviceName);
device.put("type", "mobile");
URLConnection con = new URL(urlStr).openConnection();
con.setDoOutput(true);
Preferences pref = Preferences.getPreferences(context);
Expand All @@ -133,11 +134,11 @@ public void createDevice(Context context) throws JSONException {
in.close();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(GpodRoid.LOGTAG, "error creating device", e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(GpodRoid.LOGTAG, "error creating device", e);
} catch (JSONException e) {
Log.e(GpodRoid.LOGTAG, "error creating device", e);
}

}
Expand Down Expand Up @@ -169,15 +170,6 @@ public ArrayList<String> getDevices(Context context) {

}

if (!gpodderDevices.contains("gpodroid")) {
try {
createDevice(context);
return getDevices(context);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (MalformedURLException e) {
Log.e("Gpodroid", "error when getting devices " + e);
} catch (IOException e) {
Expand Down

0 comments on commit 79682c8

Please sign in to comment.