mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 13:17:43 +00:00
Added progress bar in notification while sending file
REVIEW: 121980
This commit is contained in:
parent
ded027d3cc
commit
1edca5e45c
@ -67,8 +67,14 @@
|
|||||||
<string name="received_url_text">Tap to open \'%1s\'</string>
|
<string name="received_url_text">Tap to open \'%1s\'</string>
|
||||||
<string name="incoming_file_title">Incoming file from %1s</string>
|
<string name="incoming_file_title">Incoming file from %1s</string>
|
||||||
<string name="incoming_file_text">%1s</string>
|
<string name="incoming_file_text">%1s</string>
|
||||||
|
<string name="outgoing_file_title">Sending file to %1s</string>
|
||||||
|
<string name="outgoing_file_text">%1s</string>
|
||||||
<string name="received_file_title">Received file from %1s</string>
|
<string name="received_file_title">Received file from %1s</string>
|
||||||
<string name="received_file_text">Tap to open \'%1s\'</string>
|
<string name="received_file_text">Tap to open \'%1s\'</string>
|
||||||
|
<string name="sent_file_title">Sent file to %1s</string>
|
||||||
|
<string name="sent_file_text">%1s</string>
|
||||||
|
<string name="sent_file_failed_title">Failed to send file %1s</string>
|
||||||
|
<string name="sent_file_failed_text">%1s</string>
|
||||||
<string name="tap_to_answer">Tap to answer</string>
|
<string name="tap_to_answer">Tap to answer</string>
|
||||||
<string name="reconnect">Reconnect</string>
|
<string name="reconnect">Reconnect</string>
|
||||||
<string name="right_click">Send Right Click</string>
|
<string name="right_click">Send Right Click</string>
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
package org.kde.kdeconnect.Backends;
|
package org.kde.kdeconnect.Backends;
|
||||||
|
|
||||||
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
|
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
@ -71,7 +72,7 @@ public abstract class BaseLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TO OVERRIDE, should be sync
|
//TO OVERRIDE, should be sync
|
||||||
public abstract boolean sendPackage(NetworkPackage np);
|
public abstract boolean sendPackage(NetworkPackage np,Device.SendPackageStatusCallback callback);
|
||||||
public abstract boolean sendPackageEncrypted(NetworkPackage np, PublicKey key);
|
public abstract boolean sendPackageEncrypted(NetworkPackage np,Device.SendPackageStatusCallback callback, PublicKey key);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import org.apache.mina.core.session.IoSession;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.kde.kdeconnect.Backends.BaseLink;
|
import org.kde.kdeconnect.Backends.BaseLink;
|
||||||
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
||||||
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -51,7 +52,7 @@ public class LanLink extends BaseLink {
|
|||||||
this.session = session;
|
this.session = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Thread sendPayload(NetworkPackage np) {
|
private Thread sendPayload(NetworkPackage np, final Device.SendPackageStatusCallback callback) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -87,10 +88,13 @@ public class LanLink extends BaseLink {
|
|||||||
socket = server.accept().getOutputStream();
|
socket = server.accept().getOutputStream();
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
long progress = 0 ;
|
||||||
Log.e("LanLink","Beginning to send payload");
|
Log.e("LanLink","Beginning to send payload");
|
||||||
while ((bytesRead = stream.read(buffer)) != -1) {
|
while ((bytesRead = stream.read(buffer)) != -1) {
|
||||||
//Log.e("ok",""+bytesRead);
|
//Log.e("ok",""+bytesRead);
|
||||||
|
progress += bytesRead;
|
||||||
socket.write(buffer, 0, bytesRead);
|
socket.write(buffer, 0, bytesRead);
|
||||||
|
if (callback != null) callback.progressChanged(progress);
|
||||||
}
|
}
|
||||||
Log.e("LanLink","Finished sending payload");
|
Log.e("LanLink","Finished sending payload");
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
@ -122,7 +126,7 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
//Blocking, do not call from main thread
|
//Blocking, do not call from main thread
|
||||||
@Override
|
@Override
|
||||||
public boolean sendPackage(final NetworkPackage np) {
|
public boolean sendPackage(final NetworkPackage np,Device.SendPackageStatusCallback callback) {
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
Log.e("LanLink", "sendPackage failed: not yet connected");
|
Log.e("LanLink", "sendPackage failed: not yet connected");
|
||||||
@ -132,7 +136,7 @@ public class LanLink extends BaseLink {
|
|||||||
try {
|
try {
|
||||||
Thread thread = null;
|
Thread thread = null;
|
||||||
if (np.hasPayload()) {
|
if (np.hasPayload()) {
|
||||||
thread = sendPayload(np);
|
thread = sendPayload(np,callback);
|
||||||
if (thread == null) return false;
|
if (thread == null) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +159,7 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
//Blocking, do not call from main thread
|
//Blocking, do not call from main thread
|
||||||
@Override
|
@Override
|
||||||
public boolean sendPackageEncrypted(NetworkPackage np, PublicKey key) {
|
public boolean sendPackageEncrypted(NetworkPackage np,Device.SendPackageStatusCallback callback, PublicKey key) {
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
Log.e("LanLink", "sendPackage failed: not yet connected");
|
Log.e("LanLink", "sendPackage failed: not yet connected");
|
||||||
@ -166,7 +170,7 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
Thread thread = null;
|
Thread thread = null;
|
||||||
if (np.hasPayload()) {
|
if (np.hasPayload()) {
|
||||||
thread = sendPayload(np);
|
thread = sendPayload(np,callback);
|
||||||
if (thread == null) return false;
|
if (thread == null) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context);
|
NetworkPackage np2 = NetworkPackage.createIdentityPackage(context);
|
||||||
link.sendPackage(np2);
|
link.sendPackage(np2,null);
|
||||||
|
|
||||||
nioSessions.put(session.getId(), link);
|
nioSessions.put(session.getId(), link);
|
||||||
addLink(identityPackage, link);
|
addLink(identityPackage, link);
|
||||||
|
@ -24,6 +24,7 @@ import android.util.Log;
|
|||||||
|
|
||||||
import org.kde.kdeconnect.Backends.BaseLink;
|
import org.kde.kdeconnect.Backends.BaseLink;
|
||||||
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
import org.kde.kdeconnect.Backends.BaseLinkProvider;
|
||||||
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
|
|
||||||
import java.security.PublicKey;
|
import java.security.PublicKey;
|
||||||
@ -35,7 +36,7 @@ public class LoopbackLink extends BaseLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean sendPackage(NetworkPackage in) {
|
public boolean sendPackage(NetworkPackage in,Device.SendPackageStatusCallback callback) {
|
||||||
String s = in.serialize();
|
String s = in.serialize();
|
||||||
NetworkPackage out= NetworkPackage.unserialize(s);
|
NetworkPackage out= NetworkPackage.unserialize(s);
|
||||||
if (in.hasPayload()) out.setPayload(in.getPayload(), in.getPayloadSize());
|
if (in.hasPayload()) out.setPayload(in.getPayload(), in.getPayloadSize());
|
||||||
@ -44,7 +45,7 @@ public class LoopbackLink extends BaseLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean sendPackageEncrypted(NetworkPackage in, PublicKey key) {
|
public boolean sendPackageEncrypted(NetworkPackage in,Device.SendPackageStatusCallback callback, PublicKey key) {
|
||||||
try {
|
try {
|
||||||
in = in.encrypt(key);
|
in = in.encrypt(key);
|
||||||
String s = in.serialize();
|
String s = in.serialize();
|
||||||
|
@ -191,7 +191,12 @@ 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);
|
||||||
sendPackage(np, new SendPackageFinishedCallback(){
|
sendPackage(np, new SendPackageStatusCallback(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progressChanged(long progress) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendSuccessful() {
|
public void sendSuccessful() {
|
||||||
@ -278,7 +283,12 @@ 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);
|
||||||
sendPackage(np, new SendPackageFinishedCallback() {
|
sendPackage(np, new SendPackageStatusCallback() {
|
||||||
|
@Override
|
||||||
|
public void progressChanged(long progress) {
|
||||||
|
// Do nothng
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendSuccessful() {
|
public void sendSuccessful() {
|
||||||
pairingDone();
|
pairingDone();
|
||||||
@ -490,7 +500,8 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface SendPackageFinishedCallback {
|
public interface SendPackageStatusCallback {
|
||||||
|
void progressChanged(long progress);
|
||||||
void sendSuccessful();
|
void sendSuccessful();
|
||||||
void sendFailed();
|
void sendFailed();
|
||||||
}
|
}
|
||||||
@ -500,7 +511,7 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Async
|
//Async
|
||||||
public void sendPackage(final NetworkPackage np, final SendPackageFinishedCallback callback) {
|
public void sendPackage(final NetworkPackage np, final SendPackageStatusCallback callback) {
|
||||||
|
|
||||||
|
|
||||||
final Exception backtrace = new Exception();
|
final Exception backtrace = new Exception();
|
||||||
@ -520,9 +531,9 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
try {
|
try {
|
||||||
for (BaseLink link : mLinks) {
|
for (BaseLink link : mLinks) {
|
||||||
if (useEncryption) {
|
if (useEncryption) {
|
||||||
success = link.sendPackageEncrypted(np, publicKey);
|
success = link.sendPackageEncrypted(np,callback, publicKey);
|
||||||
} else {
|
} else {
|
||||||
success = link.sendPackage(np);
|
success = link.sendPackage(np,callback);
|
||||||
}
|
}
|
||||||
if (success) break;
|
if (success) break;
|
||||||
}
|
}
|
||||||
|
@ -143,18 +143,20 @@ public class SharePlugin extends Plugin {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
OutputStream output = new FileOutputStream(destinationFullPath.getPath());
|
OutputStream output = new FileOutputStream(destinationFullPath.getPath());
|
||||||
|
|
||||||
byte data[] = new byte[1024];
|
byte data[] = new byte[1024];
|
||||||
long total = 0;
|
long progress = 0,prevProgressPercentage = 0;
|
||||||
int count;
|
int count;
|
||||||
while ((count = input.read(data)) >= 0) {
|
while ((count = input.read(data)) >= 0) {
|
||||||
total += count;
|
progress += count;
|
||||||
output.write(data, 0, count);
|
output.write(data, 0, count);
|
||||||
if (fileLength > 0) {
|
if (fileLength > 0) {
|
||||||
if (total >= fileLength) break;
|
if (progress >= fileLength) break;
|
||||||
float progress = (total * 100 / fileLength);
|
long progressPercentage = (progress * 100 / fileLength);
|
||||||
builder.setProgress(100,(int)progress,false);
|
if ((progressPercentage - prevProgressPercentage) > 0) {
|
||||||
notificationManager.notify(notificationId,builder.build());
|
prevProgressPercentage = progressPercentage;
|
||||||
|
builder.setProgress(100, (int) progressPercentage, false);
|
||||||
|
notificationManager.notify(notificationId, builder.build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//else Log.e("SharePlugin", "Infinite loop? :D");
|
//else Log.e("SharePlugin", "Infinite loop? :D");
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,21 @@
|
|||||||
|
|
||||||
package org.kde.kdeconnect.Plugins.SharePlugin;
|
package org.kde.kdeconnect.Plugins.SharePlugin;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.ActionBarActivity;
|
import android.support.v7.app.ActionBarActivity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -204,6 +213,21 @@ public class ShareToReceiver extends ActionBarActivity {
|
|||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_SHARE);
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_SHARE);
|
||||||
int size = -1;
|
int size = -1;
|
||||||
|
|
||||||
|
final NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
final int notificationId = (int)System.currentTimeMillis();
|
||||||
|
final NotificationCompat.Builder builder ;
|
||||||
|
Resources res = getApplicationContext().getResources();
|
||||||
|
builder = new NotificationCompat.Builder(getApplicationContext())
|
||||||
|
.setContentTitle(res.getString(R.string.outgoing_file_title, device.getName()))
|
||||||
|
.setTicker(res.getString(R.string.outgoing_file_title, device.getName()))
|
||||||
|
.setSmallIcon(android.R.drawable.stat_sys_upload)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setOngoing(true)
|
||||||
|
.setProgress(100,0,true);
|
||||||
|
notificationManager.notify(notificationId,builder.build());
|
||||||
|
|
||||||
|
final Handler progressBarHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
if (uri.getScheme().equals("file")) {
|
if (uri.getScheme().equals("file")) {
|
||||||
// file:// is a non media uri, so we cannot query the ContentProvider
|
// file:// is a non media uri, so we cannot query the ContentProvider
|
||||||
|
|
||||||
@ -260,15 +284,83 @@ public class ShareToReceiver extends ActionBarActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device.sendPackage(np, new Device.SendPackageFinishedCallback() {
|
final long filesize = size;
|
||||||
|
final String filename = np.getString("filename");
|
||||||
|
|
||||||
|
builder.setContentText(res.getString(R.string.outgoing_file_text,filename));
|
||||||
|
notificationManager.notify(notificationId,builder.build());
|
||||||
|
|
||||||
|
device.sendPackage(np, new Device.SendPackageStatusCallback() {
|
||||||
|
|
||||||
|
int prevProgressPercentage = 0,progressPercentage;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progressChanged(final long progress) {
|
||||||
|
// update notification progress
|
||||||
|
progressPercentage = (int)((progress * 100) / filesize);
|
||||||
|
if (filesize > 0 && (progressPercentage - prevProgressPercentage) > 0) {
|
||||||
|
prevProgressPercentage = progressPercentage;
|
||||||
|
progressBarHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
builder.setProgress(100, progressPercentage, false);
|
||||||
|
notificationManager.notify(notificationId, builder.build());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendSuccessful() {
|
public void sendSuccessful() {
|
||||||
|
|
||||||
|
progressBarHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Resources res = getApplicationContext().getResources();
|
||||||
|
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(getApplicationContext())
|
||||||
|
.setContentTitle(res.getString(R.string.sent_file_title, device.getName()))
|
||||||
|
.setContentText(res.getString(R.string.sent_file_text, filename))
|
||||||
|
.setTicker(res.getString(R.string.sent_file_title, device.getName()))
|
||||||
|
.setSmallIcon(android.R.drawable.stat_sys_upload_done)
|
||||||
|
.setOngoing(false)
|
||||||
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||||
|
if (prefs.getBoolean("share_notification_preference", true)) {
|
||||||
|
builder1.setDefaults(Notification.DEFAULT_ALL);
|
||||||
|
}
|
||||||
|
notificationManager.notify(notificationId, builder1.build());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (!uriList.isEmpty()) queuedSendUriList(device, uriList);
|
if (!uriList.isEmpty()) queuedSendUriList(device, uriList);
|
||||||
else Log.e("ShareToReceiver", "All files sent");
|
else Log.e("ShareToReceiver", "All files sent");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendFailed() {
|
public void sendFailed() {
|
||||||
|
|
||||||
|
progressBarHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Resources res = getApplicationContext().getResources();
|
||||||
|
NotificationCompat.Builder builder2 = new NotificationCompat.Builder(getApplicationContext())
|
||||||
|
.setContentTitle(res.getString(R.string.sent_file_failed_title, device.getName()))
|
||||||
|
.setContentText(res.getString(R.string.sent_file_failed_text, filename))
|
||||||
|
.setTicker(res.getString(R.string.sent_file_title, device.getName()))
|
||||||
|
.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||||
|
.setOngoing(false)
|
||||||
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||||
|
if (prefs.getBoolean("share_notification_preference", true)) {
|
||||||
|
builder2.setDefaults(Notification.DEFAULT_ALL);
|
||||||
|
}
|
||||||
|
notificationManager.notify(notificationId, builder2.build());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Log.e("ShareToReceiver", "Failed to send file");
|
Log.e("ShareToReceiver", "Failed to send file");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user