mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-22 18:07:55 +00:00
Allow opening files shared to android using the notification
Summary: Since Android Nougat, the previous way to open a file shared to android did not work anymore (using `file://` urls), as marked with a TODO. This diff implements the new way of opening files: a content provider url (`content://`) allows opening files through the KDE Connect app. As an added benefit, apps without external files permission can now open files received by KDE Connect. Test Plan: Works for me. Reviewers: #kde_connect, albertvaka Reviewed By: #kde_connect, albertvaka Subscribers: albertvaka, nicolasfella Differential Revision: https://phabricator.kde.org/D9331
This commit is contained in:
parent
90f03555c5
commit
c6746e38a0
@ -198,6 +198,15 @@
|
||||
android:name="android.service.chooser.chooser_target_service"
|
||||
android:value="org.kde.kdeconnect.Plugins.SharePlugin.ShareChooserTargetService" />
|
||||
</activity>
|
||||
<provider
|
||||
android:name="android.support.v4.content.FileProvider"
|
||||
android:authorities="org.kde.kdeconnect_tp.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/fileprovider_paths" />
|
||||
</provider>
|
||||
<service
|
||||
android:name="org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver"
|
||||
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
|
||||
|
4
res/xml/fileprovider_paths.xml
Normal file
4
res/xml/fileprovider_paths.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<external-path name="external_files" path="." />
|
||||
</paths>
|
@ -31,11 +31,14 @@ import android.os.Build;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.TaskStackBuilder;
|
||||
import android.support.v4.content.FileProvider;
|
||||
|
||||
import org.kde.kdeconnect.Device;
|
||||
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ShareNotification {
|
||||
|
||||
private final String filename;
|
||||
@ -88,11 +91,28 @@ public class ShareNotification {
|
||||
}
|
||||
|
||||
public void setURI(Uri destinationUri, String mimeType) {
|
||||
// Nougat requires share:// URIs instead of file:// URIs
|
||||
// TODO use FileProvider for >=Nougat
|
||||
if (Build.VERSION.SDK_INT < 24) {
|
||||
/*
|
||||
* We only support file URIs (because sending a content uri to another app does not work for security reasons).
|
||||
* In effect, that means only the default download folder currently works.
|
||||
*
|
||||
* TODO: implement our own content provider (instead of support-v4's FileProvider). It should:
|
||||
* - Proxy to real files (in case of the default download folder)
|
||||
* - Proxy to the underlying content uri (in case of a custom download folder)
|
||||
*/
|
||||
if (!"file".equals(destinationUri.getScheme())) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
TaskStackBuilder stackBuilder = TaskStackBuilder.create(device.getContext());
|
||||
stackBuilder.addNextIntent(intent);
|
||||
@ -100,5 +120,4 @@ public class ShareNotification {
|
||||
builder.setContentText(device.getContext().getResources().getString(R.string.received_file_text, filename))
|
||||
.setContentIntent(resultPendingIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user