mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-22 18:07:55 +00:00
Added multi-file transfer and query for filename and file size
This commit is contained in:
parent
754123b9c5
commit
37c2a0e112
@ -127,6 +127,11 @@
|
|||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
<data android:mimeType="*/*" />
|
<data android:mimeType="*/*" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.SEND_MULTIPLE" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<data android:mimeType="*/*" />
|
||||||
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<service android:name="org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver"
|
<service android:name="org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver"
|
||||||
|
@ -158,23 +158,32 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
//Send our own public key
|
//Send our own public key
|
||||||
NetworkPackage np = NetworkPackage.createPublicKeyPackage(context);
|
NetworkPackage np = NetworkPackage.createPublicKeyPackage(context);
|
||||||
boolean success = sendPackage(np);
|
sendPackage(np, new SendPackageFinishedCallback(){
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
for (PairingCallback cb : pairingCallback) cb.pairingFailed(res.getString(R.string.error_could_not_send_package));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendSuccessful() {
|
||||||
pairingTimer = new Timer();
|
pairingTimer = new Timer();
|
||||||
pairingTimer.schedule(new TimerTask() {
|
pairingTimer.schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (PairingCallback cb : pairingCallback) cb.pairingFailed(context.getString(R.string.error_timed_out));
|
for (PairingCallback cb : pairingCallback) {
|
||||||
|
cb.pairingFailed(context.getString(R.string.error_timed_out));
|
||||||
|
}
|
||||||
pairStatus = PairStatus.NotPaired;
|
pairStatus = PairStatus.NotPaired;
|
||||||
}
|
}
|
||||||
}, 20*1000);
|
}, 20*1000);
|
||||||
|
|
||||||
pairStatus = PairStatus.Requested;
|
pairStatus = PairStatus.Requested;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendFailed() {
|
||||||
|
for (PairingCallback cb : pairingCallback) {
|
||||||
|
cb.pairingFailed(context.getString(R.string.error_could_not_send_package));
|
||||||
|
}
|
||||||
|
pairStatus = PairStatus.NotPaired;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,9 +216,7 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
//Send our own public key
|
//Send our own public key
|
||||||
NetworkPackage np = NetworkPackage.createPublicKeyPackage(context);
|
NetworkPackage np = NetworkPackage.createPublicKeyPackage(context);
|
||||||
boolean success = sendPackage(np);
|
sendPackage(np); //TODO: Set a callback
|
||||||
|
|
||||||
if (!success) return;
|
|
||||||
|
|
||||||
pairStatus = PairStatus.Paired;
|
pairStatus = PairStatus.Paired;
|
||||||
|
|
||||||
@ -416,8 +423,16 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface SendPackageFinishedCallback {
|
||||||
|
void sendSuccessful();
|
||||||
|
void sendFailed();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean sendPackage(final NetworkPackage np) {
|
public void sendPackage(NetworkPackage np) {
|
||||||
|
sendPackage(np,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendPackage(final NetworkPackage np, final SendPackageFinishedCallback callback) {
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -429,26 +444,25 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
for(BaseLink link : links) {
|
for(BaseLink link : links) {
|
||||||
|
|
||||||
|
boolean success;
|
||||||
if (useEncryption) {
|
if (useEncryption) {
|
||||||
if (link.sendPackageEncrypted(np, publicKey)) {
|
success = link.sendPackageEncrypted(np, publicKey);
|
||||||
//Log.e("sendPackage", "Sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (link.sendPackage(np)) {
|
success = link.sendPackage(np);
|
||||||
|
}
|
||||||
|
if (success) {
|
||||||
//Log.e("sendPackage", "Sent");
|
//Log.e("sendPackage", "Sent");
|
||||||
|
if (callback != null) callback.sendSuccessful();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if (callback != null) callback.sendFailed();
|
||||||
|
|
||||||
Log.e("sendPackage","Error: Package could not be sent ("+links.size()+" links available)");
|
Log.e("sendPackage","Error: Package could not be sent ("+links.size()+" links available)");
|
||||||
|
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
return !links.isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ import android.view.View;
|
|||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
|
||||||
|
import org.apache.http.client.utils.URIUtils;
|
||||||
import org.kde.kdeconnect.BackgroundService;
|
import org.kde.kdeconnect.BackgroundService;
|
||||||
import org.kde.kdeconnect.Device;
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
@ -26,6 +27,7 @@ import org.kde.kdeconnect.UserInterface.List.SectionItem;
|
|||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@ -84,7 +86,8 @@ public class ShareToReceiver extends ActionBarActivity {
|
|||||||
|
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
|
|
||||||
if (!Intent.ACTION_SEND.equals(intent.getAction())) {
|
String action = intent.getAction();
|
||||||
|
if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) {
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -117,28 +120,45 @@ public class ShareToReceiver extends ActionBarActivity {
|
|||||||
|
|
||||||
Device device = devicesList.get(i-1); //NOTE: -1 because of the title!
|
Device device = devicesList.get(i-1); //NOTE: -1 because of the title!
|
||||||
|
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_FILETRANSFER);
|
|
||||||
|
|
||||||
Bundle extras = intent.getExtras();
|
Bundle extras = intent.getExtras();
|
||||||
if (extras.containsKey(Intent.EXTRA_STREAM)) {
|
if (extras.containsKey(Intent.EXTRA_STREAM)) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
ArrayList<Uri> uriList;
|
||||||
|
if (!Intent.ACTION_SEND.equals(intent.getAction())) {
|
||||||
|
uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
||||||
|
} else {
|
||||||
Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
|
Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
|
||||||
|
uriList = new ArrayList<Uri>();
|
||||||
|
uriList.add(uri);
|
||||||
|
}
|
||||||
|
|
||||||
ContentResolver cr = getContentResolver();
|
queuedSendUriList(device, uriList);
|
||||||
|
|
||||||
InputStream inputStream = cr.openInputStream(uri);
|
|
||||||
|
|
||||||
//TODO: Figure out a way to find the file size, so we can show a progress bar in KDE
|
|
||||||
np.setPayload(inputStream, -1);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(this.getClass().getName(), e.toString());
|
Log.e(this.getClass().getName(), e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (extras.containsKey(Intent.EXTRA_TEXT)) {
|
} else if (extras.containsKey(Intent.EXTRA_TEXT)) {
|
||||||
np.set("text",extras.getString(Intent.EXTRA_TEXT));
|
String text = extras.getString(Intent.EXTRA_TEXT);
|
||||||
|
boolean isUrl;
|
||||||
|
try {
|
||||||
|
new URL(text);
|
||||||
|
isUrl = true;
|
||||||
|
} catch(Exception e) {
|
||||||
|
isUrl = false;
|
||||||
|
}
|
||||||
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_FILETRANSFER);
|
||||||
|
if (isUrl) {
|
||||||
|
np.set("url", text);
|
||||||
|
} else {
|
||||||
|
np.set("text", text);
|
||||||
|
}
|
||||||
|
device.sendPackage(np);
|
||||||
}
|
}
|
||||||
|
|
||||||
device.sendPackage(np);
|
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
@ -150,6 +170,65 @@ public class ShareToReceiver extends ActionBarActivity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void queuedSendUriList(final Device device, final ArrayList<Uri> uriList) {
|
||||||
|
try {
|
||||||
|
Uri uri = uriList.remove(0);
|
||||||
|
ContentResolver cr = getContentResolver();
|
||||||
|
InputStream inputStream = cr.openInputStream(uri);
|
||||||
|
|
||||||
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_FILETRANSFER);
|
||||||
|
|
||||||
|
String[] proj = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.DISPLAY_NAME };
|
||||||
|
Cursor cursor = managedQuery(uri, proj, null, null, null);
|
||||||
|
|
||||||
|
int size = -1;
|
||||||
|
try {
|
||||||
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
|
||||||
|
cursor.moveToFirst();
|
||||||
|
size = cursor.getInt(column_index);
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.e("ShareToReceiver", "Could not obtain file size");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Log.e("ShareToReceiver", "Size "+size);
|
||||||
|
np.setPayload(inputStream, size);
|
||||||
|
|
||||||
|
try {
|
||||||
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
|
||||||
|
cursor.moveToFirst();
|
||||||
|
String path = cursor.getString(column_index);
|
||||||
|
np.set("filename", Uri.parse(path).getLastPathSegment());
|
||||||
|
} catch(Exception _) {
|
||||||
|
try {
|
||||||
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
|
||||||
|
cursor.moveToFirst();
|
||||||
|
String name = cursor.getString(column_index);
|
||||||
|
np.set("filename", name);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.e("ShareToReceiver", "Could not obtain file name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
device.sendPackage(np, new Device.SendPackageFinishedCallback() {
|
||||||
|
@Override
|
||||||
|
public void sendSuccessful() {
|
||||||
|
if (!uriList.isEmpty()) queuedSendUriList(device, uriList);
|
||||||
|
else Log.e("ShareToReceiver", "All files sent");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendFailed() {
|
||||||
|
Log.e("ShareToReceiver", "Failed to send attachment");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.e("ShareToReceiver", "Failed to send attachment");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user