mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-04-21 07:18:05 -04:00
- don't try to start BT in the background. you can only start/stop a BT server once, else new connections don't work - be more mindful of reading/writing bytes from the input/output streams... make sure bytes are available, because you will block forever if you do not do that - use the device class tag to filter devices in discovery instead of the fdroid name tag - this now successfully connects but there is an error in the certificate fingerprint verification still
99 lines
3.5 KiB
Java
99 lines
3.5 KiB
Java
package org.fdroid.fdroid.net;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
import org.apache.commons.io.input.BoundedInputStream;
|
|
import org.fdroid.fdroid.Utils;
|
|
import org.fdroid.fdroid.net.bluetooth.BluetoothClient;
|
|
import org.fdroid.fdroid.net.bluetooth.BluetoothConnection;
|
|
import org.fdroid.fdroid.net.bluetooth.FileDetails;
|
|
import org.fdroid.fdroid.net.bluetooth.httpish.Request;
|
|
import org.fdroid.fdroid.net.bluetooth.httpish.Response;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.nio.Buffer;
|
|
import java.io.BufferedReader;
|
|
|
|
public class BluetoothDownloader extends Downloader {
|
|
|
|
private static final String TAG = "BluetoothDownloader";
|
|
|
|
private final BluetoothConnection connection;
|
|
private FileDetails fileDetails;
|
|
private final String sourcePath;
|
|
|
|
public BluetoothDownloader(Context context, String macAddress, URL sourceUrl, File destFile) throws IOException {
|
|
super(context, sourceUrl, destFile);
|
|
this.connection = new BluetoothClient(macAddress).openConnection();
|
|
this.sourcePath = sourceUrl.getPath();
|
|
}
|
|
|
|
@Override
|
|
public InputStream getInputStream() throws IOException {
|
|
Request request = Request.createGET(sourcePath, connection);
|
|
Response response = request.send();
|
|
fileDetails = response.toFileDetails();
|
|
|
|
|
|
// TODO: Manage the dependency which includes this class better?
|
|
// Right now, I only needed the one class from apache commons.
|
|
// There are countless classes online which provide this functionality,
|
|
// including some which are available from the Android SDK - the only
|
|
// problem is that they have a funky API which doesn't just wrap a
|
|
// plain old InputStream (the class is ContentLengthInputStream -
|
|
// whereas this BoundedInputStream is much more generic and useful
|
|
// to us).
|
|
BoundedInputStream stream = new BoundedInputStream(response.toContentStream(), fileDetails.getFileSize());
|
|
stream.setPropagateClose(false);
|
|
return stream;
|
|
}
|
|
|
|
/**
|
|
* May return null if an error occurred while getting file details.
|
|
* TODO: Should we throw an exception? Everywhere else in this blue package throws IO exceptions weely-neely.
|
|
* Will probably require some thought as to how the API looks, with regards to all of the public methods
|
|
* and their signatures.
|
|
*/
|
|
public FileDetails getFileDetails() {
|
|
if (fileDetails == null) {
|
|
Log.d(TAG, "Going to Bluetooth \"server\" to get file details.");
|
|
try {
|
|
fileDetails = Request.createHEAD(sourceUrl.getPath(), connection).send().toFileDetails();
|
|
} catch (IOException e) {
|
|
Log.e(TAG, "Error getting file details from Bluetooth \"server\": " + e.getMessage());
|
|
}
|
|
}
|
|
return fileDetails;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasChanged() {
|
|
return getFileDetails().getCacheTag() == null || getFileDetails().getCacheTag().equals(getCacheTag());
|
|
}
|
|
|
|
@Override
|
|
public int totalDownloadSize() {
|
|
return getFileDetails().getFileSize();
|
|
}
|
|
|
|
@Override
|
|
public void download() throws IOException, InterruptedException {
|
|
downloadFromStream(1024);
|
|
connection.closeQuietly();
|
|
}
|
|
|
|
@Override
|
|
public boolean isCached() {
|
|
FileDetails details = getFileDetails();
|
|
return (
|
|
details != null &&
|
|
details.getCacheTag() != null &&
|
|
details.getCacheTag().equals(getCacheTag())
|
|
);
|
|
}
|
|
|
|
}
|