diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java
--- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java
+++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java
@@ -69,6 +69,10 @@
NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());
}
+ public void cancel() {
+ notificationManager.cancel(notificationId);
+ }
+
public int getId() {
return notificationId;
}
diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java
--- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java
+++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java
@@ -15,8 +15,8 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
-*/
+ * along with this program. If not, see .
+ */
package org.kde.kdeconnect.Plugins.SharePlugin;
@@ -40,6 +40,7 @@
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.ContextCompat;
+import android.support.v4.content.FileProvider;
import android.support.v4.provider.DocumentFile;
import android.util.Log;
import android.widget.Toast;
@@ -192,20 +193,26 @@
final long fileLength = np.getPayloadSize();
final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis()));
+ String filename = originalFilename;
+ final DocumentFile destinationFolderDocument;
+
//We need to check for already existing files only when storing in the default path.
//User-defined paths use the new Storage Access Framework that already handles this.
- final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context);
- final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath();
- final String filename = customDestination ? originalFilename : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename);
-
+ //If the file should be opened immediately store it in the standard location to avoid the FileProvider trouble (See ShareNotification::setURI)
+ if (np.getBoolean("open") || !ShareSettingsActivity.isCustomDestinationEnabled(context)) {
+ final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath();
+ filename = FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename);
+ destinationFolderDocument = DocumentFile.fromFile(new File(defaultPath));
+ } else {
+ destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context);
+ }
String displayName = FilesHelper.getFileNameWithoutExt(filename);
final String mimeType = FilesHelper.getMimeTypeFromFile(filename);
if ("*/*".equals(mimeType)) {
displayName = filename;
}
- final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context);
final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, displayName);
final OutputStream destinationOutput;
try {
@@ -247,20 +254,39 @@
Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath());
- //Update the notification and allow to open the file from it
- notification.setFinished(true);
- notification.setURI(destinationUri, mimeType);
- notification.show();
- if (!customDestination && Build.VERSION.SDK_INT >= 12) {
- Log.i("SharePlugin", "Adding to downloads");
- DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
- manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false);
+ if (np.getBoolean("open")) {
+
+ notification.cancel();
+
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ if (Build.VERSION.SDK_INT >= 24) {
+ //Nougat and later require "content://" uris instead of "file://" uris
+ File file = new File(destinationUri.getPath());
+ Uri contentUri = FileProvider.getUriForFile(device.getContext(), "org.kde.kdeconnect_tp.fileprovider", file);
+ intent.setDataAndType(contentUri, mimeType);
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+ } else {
+ intent.setDataAndType(destinationUri, mimeType);
+ }
+
+ context.startActivity(intent);
} else {
- //Make sure it is added to the Android Gallery anyway
- MediaStoreHelper.indexFile(context, destinationUri);
- }
+ //Update the notification and allow to open the file from it
+ notification.setFinished(true);
+ notification.setURI(destinationUri, mimeType);
+ notification.show();
+
+ if (!ShareSettingsActivity.isCustomDestinationEnabled(context) && Build.VERSION.SDK_INT >= 12) {
+ Log.i("SharePlugin", "Adding to downloads");
+ DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
+ manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false);
+ } else {
+ //Make sure it is added to the Android Gallery anyway
+ MediaStoreHelper.indexFile(context, destinationUri);
+ }
+ }
} catch (Exception e) {
Log.e("SharePlugin", "Receiver thread exception");
e.printStackTrace();
@@ -287,7 +313,8 @@
parentActivity.startActivity(intent);
}
- static void queuedSendUriList(Context context, final Device device, final ArrayList uriList) {
+ static void queuedSendUriList(Context context, final Device device,
+ final ArrayList uriList) {
//Read all the data early, as we only have permissions to do it while the activity is alive
final ArrayList toSend = new ArrayList<>();