android: start improving handling of different types of entries

This commit is contained in:
Daniel Erat 2011-03-15 22:14:39 -07:00
parent b1ff0bff7f
commit 190f1ef9c6
1 changed files with 47 additions and 62 deletions

View File

@ -24,8 +24,8 @@ import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import android.widget.Toast;
import org.json.JSONArray; import org.json.JSONArray;
@ -46,29 +46,27 @@ public class BrowseActivity extends ListActivity {
private static final String KEY_TYPE = "type"; private static final String KEY_TYPE = "type";
private DownloadService mService = null; private DownloadService mService = null;
private SimpleAdapter mAdapter; private ArrayAdapter mAdapter;
private String mBlobRef = ""; private String mBlobRef = "";
private ArrayList<HashMap<String, String>> mEntries = private ArrayList<Entry> mEntries = new ArrayList<Entry>();
new ArrayList<HashMap<String, String>>(); private HashMap<String, Entry> mEntriesByBlobRef = new HashMap<String, Entry>();
private HashMap<String, HashMap<String, String>> mEntriesByBlobRef =
new HashMap<String, HashMap<String, String>>();
private enum DirectoryEntryType { private enum EntryType {
UNKNOWN("unknown"), UNKNOWN("unknown"),
FILE("file"), FILE("file"),
DIRECTORY("directory"); DIRECTORY("directory");
private String mName; private String mName;
DirectoryEntryType(String name) { EntryType(String name) {
mName = name; mName = name;
} }
public static DirectoryEntryType fromString(String str) { public static EntryType fromString(String str) {
if (str != null) { if (str != null) {
for (DirectoryEntryType type : DirectoryEntryType.values()) { for (EntryType type : EntryType.values()) {
if (type.mName.equals(str)) if (type.mName.equals(str))
return type; return type;
} }
@ -77,26 +75,31 @@ public class BrowseActivity extends ListActivity {
} }
} }
private class DirectoryEntry { private class Entry {
final private String mBlobRef; final private String mBlobRef;
private String mFilename; private String mFilename = null;
private DirectoryEntryType mType; private EntryType mType = EntryType.UNKNOWN;
private String mContentBlobRef = null;
DirectoryEntry(String blobRef) { Entry(String blobRef) {
mBlobRef = blobRef; mBlobRef = blobRef;
mFilename = null;
mType = DirectoryEntryType.UNKNOWN;
} }
public String getBlobRef() { return mBlobRef; } public String getBlobRef() { return mBlobRef; }
public String getDisplayName() { return mFilename != null ? mFilename : mBlobRef; } public EntryType getType() { return mType; }
public DirectoryEntryType getType() { return mType; } public String getContentBlobRef() { return mContentBlobRef; }
public String toString() { return mFilename != null ? mFilename : mBlobRef; }
public boolean updateFromJSON(String json) { public boolean updateFromJSON(String json) {
try { try {
JSONObject object = (JSONObject) new JSONTokener(json).nextValue(); JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
mFilename = object.getString("fileName"); mFilename = object.getString("fileName");
mType = DirectoryEntryType.fromString(object.getString("camliType")); mType = EntryType.fromString(object.getString("camliType"));
if (mType == EntryType.DIRECTORY) {
mContentBlobRef = mBlobRef;
}
// TODO: Handle contentParts for files.
return true; return true;
} catch (org.json.JSONException e) { } catch (org.json.JSONException e) {
Log.e(TAG, "unable to parse JSON for entry " + mBlobRef, e); Log.e(TAG, "unable to parse JSON for entry " + mBlobRef, e);
@ -119,12 +122,11 @@ public class BrowseActivity extends ListActivity {
startService(serviceIntent); startService(serviceIntent);
bindService(new Intent(this, DownloadService.class), mConnection, 0); bindService(new Intent(this, DownloadService.class), mConnection, 0);
mAdapter = new SimpleAdapter( mAdapter = new ArrayAdapter(
this, this,
mEntries,
R.layout.browse_row, R.layout.browse_row,
new String[]{ KEY_TITLE }, android.R.id.title,
new int[]{ android.R.id.title }); mEntries);
setListAdapter(mAdapter); setListAdapter(mAdapter);
} }
@ -137,16 +139,15 @@ public class BrowseActivity extends ListActivity {
@Override @Override
protected void onListItemClick(ListView listView, View view, int position, long id) { protected void onListItemClick(ListView listView, View view, int position, long id) {
HashMap<String, String> blob = mEntries.get(position); Entry entry = mEntries.get(position);
String type = blob.get("type"); if (entry.getType() == EntryType.DIRECTORY) {
if (type == null) if (entry.getContentBlobRef() == null) {
return; Log.e(TAG, "no content for directory " + entry.getBlobRef());
return;
if (type.equals("directory")) { }
Intent intent = new Intent(this, BrowseActivity.class); Intent intent = new Intent(this, BrowseActivity.class);
intent.putExtra(BUNDLE_BLOBREF, blob.get(KEY_CONTENT)); intent.putExtra(BUNDLE_BLOBREF, entry.getContentBlobRef());
startActivity(intent); startActivity(intent);
} else if (type.equals("file")) {
} }
} }
@ -181,12 +182,12 @@ public class BrowseActivity extends ListActivity {
mEntries.clear(); mEntries.clear();
for (int i = 0; i < array.length(); ++i) { for (int i = 0; i < array.length(); ++i) {
JSONObject jsonEntry = array.getJSONObject(i); JSONObject jsonEntry = array.getJSONObject(i);
Log.d(TAG, "adding entry " + jsonEntry.getString("blobref")); String entryBlobRef = jsonEntry.getString("content");
HashMap<String, String> entry = new HashMap<String, String>(); Log.d(TAG, "adding search entry " + entryBlobRef);
entry.put(KEY_TITLE, jsonEntry.getString("blobref")); Entry entry = new Entry(entryBlobRef);
entry.put(KEY_CONTENT, jsonEntry.getString("content"));
mEntries.add(entry); mEntries.add(entry);
mEntriesByBlobRef.put(jsonEntry.getString("blobref"), entry); mEntriesByBlobRef.put(entryBlobRef, entry);
mService.getBlobAsByteArray(entryBlobRef, mEntryListener);
} }
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
} catch (org.json.JSONException e) { } catch (org.json.JSONException e) {
@ -257,14 +258,11 @@ public class BrowseActivity extends ListActivity {
mEntries.clear(); mEntries.clear();
for (int i = 0; i < members.length(); ++i) { for (int i = 0; i < members.length(); ++i) {
String entryBlobRef = members.getString(i); String entryBlobRef = members.getString(i);
mService.getBlobAsByteArray(entryBlobRef, mEntryListener);
Log.d(TAG, "adding directory entry " + entryBlobRef); Log.d(TAG, "adding directory entry " + entryBlobRef);
HashMap<String, String> entry = new HashMap<String, String>(); Entry entry = new Entry(entryBlobRef);
entry.put(KEY_TITLE, entryBlobRef);
entry.put(KEY_CONTENT, entryBlobRef);
mEntries.add(entry); mEntries.add(entry);
mEntriesByBlobRef.put(entryBlobRef, entry); mEntriesByBlobRef.put(entryBlobRef, entry);
mService.getBlobAsByteArray(entryBlobRef, mEntryListener);
} }
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
} catch (org.json.JSONException e) { } catch (org.json.JSONException e) {
@ -281,28 +279,15 @@ public class BrowseActivity extends ListActivity {
private final DownloadService.ByteArrayListener mEntryListener = new DownloadService.ByteArrayListener() { private final DownloadService.ByteArrayListener mEntryListener = new DownloadService.ByteArrayListener() {
@Override @Override
public void onBlobDownloadSuccess(String blobRef, byte[] bytes) { public void onBlobDownloadSuccess(String blobRef, byte[] bytes) {
try { Entry entry = mEntriesByBlobRef.get(blobRef);
HashMap<String, String> entry = mEntriesByBlobRef.get(blobRef); if (entry == null) {
if (entry == null) { Log.e(TAG, "got unknown entry " + blobRef);
Log.e(TAG, "got unknown entry " + blobRef); return;
return;
}
JSONObject object = (JSONObject) new JSONTokener(new String(bytes)).nextValue();
String fileName = object.getString("fileName");
String type = object.getString("camliType");
if (fileName == null || type == null) {
Log.e(TAG, "entry " + blobRef + " is missing filename or type");
return;
}
Log.d(TAG, "updating directory entry " + blobRef + " to " + fileName);
entry.put(KEY_TITLE, fileName);
entry.put(KEY_TYPE, type);
mAdapter.notifyDataSetChanged();
} catch (org.json.JSONException e) {
Log.e(TAG, "unable to parse JSON for entry " + blobRef, e);
} }
Log.d(TAG, "updating directory entry " + blobRef);
if (entry.updateFromJSON(new String(bytes)))
mAdapter.notifyDataSetChanged();
} }
@Override @Override