Change the DropBoxManager API slightly (this is public, but not yet released) --

take a File instead of a ParcelFileDescriptor (gets opened internally) --
that way the caller doesn't have to worry about closing their PFD and so on.
(Pretty much 100% of the time the caller will be uploading a file, anyway.)
This commit is contained in:
Dan Egnor
2009-11-25 12:38:00 -08:00
parent a85a0ac107
commit eb7a7d57ca
4 changed files with 23 additions and 33 deletions

View File

@ -102263,10 +102263,12 @@
> >
<parameter name="tag" type="java.lang.String"> <parameter name="tag" type="java.lang.String">
</parameter> </parameter>
<parameter name="fd" type="android.os.ParcelFileDescriptor"> <parameter name="file" type="java.io.File">
</parameter> </parameter>
<parameter name="flags" type="int"> <parameter name="flags" type="int">
</parameter> </parameter>
<exception name="IOException" type="java.io.IOException">
</exception>
</method> </method>
<method name="addText" <method name="addText"
return="void" return="void"

View File

@ -230,17 +230,24 @@ public class DropBoxManager {
} }
/** /**
* Stores data read from a file descriptor. The data may be ignored or * Stores the contents of a file, which may be ignored or discarded as with
* discarded as with {@link #addText}. You must close your * {@link #addText}.
* ParcelFileDescriptor object after calling this method!
* *
* @param tag describing the type of entry being stored * @param tag describing the type of entry being stored
* @param fd file descriptor to read from * @param file to read from
* @param flags describing the data * @param flags describing the data
* @throws IOException if the file can't be opened
*/ */
public void addFile(String tag, ParcelFileDescriptor fd, int flags) { public void addFile(String tag, File file, int flags) throws IOException {
if (fd == null) throw new NullPointerException(); if (file == null) throw new NullPointerException();
try { mService.add(new Entry(tag, 0, fd, flags)); } catch (RemoteException e) {} Entry entry = new Entry(tag, 0, file, flags);
try {
mService.add(new Entry(tag, 0, file, flags));
} catch (RemoteException e) {
// ignore
} finally {
entry.close();
}
} }
/** /**

View File

@ -23,7 +23,6 @@ import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.DropBoxManager; import android.os.DropBoxManager;
import android.os.FileUtils; import android.os.FileUtils;
import android.os.ParcelFileDescriptor;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
@ -100,10 +99,6 @@ public class BootReceiver extends BroadcastReceiver {
String setting = "logfile:" + filename; String setting = "logfile:" + filename;
long lastTime = Settings.Secure.getLong(cr, setting, 0); long lastTime = Settings.Secure.getLong(cr, setting, 0);
if (lastTime == fileTime) return; // Already logged this particular file if (lastTime == fileTime) return; // Already logged this particular file
db.addFile(tag, file, DropBoxManager.IS_TEXT);
ParcelFileDescriptor pfd =
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
db.addFile(tag, pfd, DropBoxManager.IS_TEXT);
pfd.close();
} }
} }

View File

@ -20,7 +20,6 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.DropBoxManager; import android.os.DropBoxManager;
import android.os.ParcelFileDescriptor;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.os.StatFs; import android.os.StatFs;
import android.provider.Settings; import android.provider.Settings;
@ -126,22 +125,11 @@ public class DropBoxTest extends AndroidTestCase {
DropBoxManager dropbox = (DropBoxManager) getContext().getSystemService( DropBoxManager dropbox = (DropBoxManager) getContext().getSystemService(
Context.DROPBOX_SERVICE); Context.DROPBOX_SERVICE);
int mode = ParcelFileDescriptor.MODE_READ_ONLY;
ParcelFileDescriptor pfd0 = ParcelFileDescriptor.open(f0, mode); dropbox.addFile("DropBoxTest", f0, DropBoxManager.IS_TEXT);
ParcelFileDescriptor pfd1 = ParcelFileDescriptor.open(f1, mode); dropbox.addFile("DropBoxTest", f1, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED);
ParcelFileDescriptor pfd2 = ParcelFileDescriptor.open(f2, mode); dropbox.addFile("DropBoxTest", f2, 0);
ParcelFileDescriptor pfd3 = ParcelFileDescriptor.open(f3, mode); dropbox.addFile("DropBoxTest", f3, DropBoxManager.IS_GZIPPED);
dropbox.addFile("DropBoxTest", pfd0, DropBoxManager.IS_TEXT);
dropbox.addFile("DropBoxTest", pfd1, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED);
dropbox.addFile("DropBoxTest", pfd2, 0);
dropbox.addFile("DropBoxTest", pfd3, DropBoxManager.IS_GZIPPED);
pfd0.close();
pfd1.close();
pfd2.close();
pfd3.close();
DropBoxManager.Entry e0 = dropbox.getNextEntry("DropBoxTest", before); DropBoxManager.Entry e0 = dropbox.getNextEntry("DropBoxTest", before);
DropBoxManager.Entry e1 = dropbox.getNextEntry("DropBoxTest", e0.getTimeMillis()); DropBoxManager.Entry e1 = dropbox.getNextEntry("DropBoxTest", e0.getTimeMillis());
@ -506,9 +494,7 @@ public class DropBoxTest extends AndroidTestCase {
os.write(bytes); os.write(bytes);
os.close(); os.close();
ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY); dropbox.addFile(tag, f, 0);
dropbox.addFile(tag, fd, 0);
fd.close();
} }
private int getEntrySize(DropBoxManager.Entry e) throws Exception { private int getEntrySize(DropBoxManager.Entry e) throws Exception {