diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java index 0d9c936a..97acead3 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java @@ -1,188 +1,192 @@ package org.kde.kdeconnect.Plugins.SharePlugin; /* * Copyright 2017 Nicolas Fella * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 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 . */ import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; 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; import java.io.IOException; import java.io.InputStream; class ShareNotification { private final String filename; private final NotificationManager notificationManager; private final int notificationId; private NotificationCompat.Builder builder; private final Device device; //https://documentation.onesignal.com/docs/android-customizations#section-big-picture private static final int bigImageWidth = 1440; private static final int bigImageHeight = 720; public ShareNotification(Device device, String filename) { this.device = device; this.filename = filename; notificationId = (int) System.currentTimeMillis(); notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER) .setContentTitle(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName())) .setContentText(device.getContext().getResources().getString(R.string.incoming_file_text, filename)) .setTicker(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName())) .setSmallIcon(android.R.drawable.stat_sys_download) .setAutoCancel(true) .setOngoing(true) .setProgress(100, 0, true); } public void show() { NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build()); } + public void cancel() { + notificationManager.cancel(notificationId); + } + public int getId() { return notificationId; } public void setProgress(int progress) { builder.setProgress(100, progress, false) .setContentTitle(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName()) + " (" + progress + "%)"); } public void setFinished(boolean success) { String message = success ? device.getContext().getResources().getString(R.string.received_file_title, device.getName()) : device.getContext().getResources().getString(R.string.received_file_fail_title, device.getName()); builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.DEFAULT); builder.setContentTitle(message) .setTicker(message) .setSmallIcon(android.R.drawable.stat_sys_download_done) .setAutoCancel(true) .setOngoing(false); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(device.getContext()); if (prefs.getBoolean("share_notification_preference", true)) { builder.setDefaults(Notification.DEFAULT_ALL); } } public void setURI(Uri destinationUri, String mimeType) { /* * 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 it's an image, try to show it in the notification if (mimeType.startsWith("image/")) { //https://developer.android.com/topic/performance/graphics/load-bitmap final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; try (InputStream decodeBoundsInputStream = device.getContext().getContentResolver().openInputStream(destinationUri); InputStream decodeInputStream = device.getContext().getContentResolver().openInputStream(destinationUri)) { BitmapFactory.decodeStream(decodeBoundsInputStream, null, options); options.inJustDecodeBounds = false; options.inSampleSize = calculateInSampleSize(options, bigImageWidth, bigImageHeight); Bitmap image = BitmapFactory.decodeStream(decodeInputStream, null, options); if (image != null) { builder.setLargeIcon(image); builder.setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(image)); } } catch (IOException ignored) { } } if (!"file".equals(destinationUri.getScheme())) { return; } Intent intent = new Intent(Intent.ACTION_VIEW); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType(mimeType); 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); shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri); } else { intent.setDataAndType(destinationUri, mimeType); shareIntent.putExtra(Intent.EXTRA_STREAM, destinationUri); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent resultPendingIntent = PendingIntent.getActivity( device.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ); builder.setContentText(device.getContext().getResources().getString(R.string.received_file_text, filename)) .setContentIntent(resultPendingIntent); shareIntent = Intent.createChooser(shareIntent, device.getContext().getString(R.string.share_received_file, destinationUri.getLastPathSegment())); PendingIntent sharePendingIntent = PendingIntent.getActivity(device.getContext(), (int) System.currentTimeMillis(), shareIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action.Builder shareAction = new NotificationCompat.Action.Builder( R.drawable.ic_share_white, device.getContext().getString(R.string.share), sharePendingIntent); builder.addAction(shareAction.build()); } private int calculateInSampleSize(BitmapFactory.Options options, int targetWidth, int targetHeight) { int inSampleSize = 1; if (options.outHeight > targetHeight || options.outWidth > targetWidth) { final int halfHeight = options.outHeight / 2; final int halfWidth = options.outWidth / 2; while ((halfHeight / inSampleSize) >= targetHeight && (halfWidth / inSampleSize) >= targetWidth) { inSampleSize *= 2; } } return inSampleSize; } } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index cd24dc8c..12c20822 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -1,459 +1,485 @@ /* * Copyright 2014 Albert Vaca Cintora * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 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 . */ package org.kde.kdeconnect.Plugins.SharePlugin; import android.Manifest; import android.app.Activity; import android.app.DownloadManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ClipboardManager; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.NotificationCompat; 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; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.FilesHelper; import org.kde.kdeconnect.Helpers.MediaStoreHelper; import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.UserInterface.DeviceSettingsActivity; import org.kde.kdeconnect_tp.R; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; public class SharePlugin extends Plugin { private final static String PACKET_TYPE_SHARE_REQUEST = "kdeconnect.share.request"; private final static boolean openUrlsDirectly = true; @Override public boolean onCreate() { optionalPermissionExplanation = R.string.share_optional_permission_explanation; return true; } @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_sharereceiver); } @Override public Drawable getIcon() { return ContextCompat.getDrawable(context, R.drawable.share_plugin_action); } @Override public String getDescription() { return context.getResources().getString(R.string.pref_plugin_sharereceiver_desc); } @Override public boolean hasMainActivity() { return true; } @Override public String getActionName() { return context.getString(R.string.send_files); } @Override public void startMainActivity(Activity parentActivity) { Intent intent = new Intent(parentActivity, SendFileActivity.class); intent.putExtra("deviceId", device.getDeviceId()); parentActivity.startActivity(intent); } @Override public boolean hasSettings() { return true; } @Override public boolean onPacketReceived(NetworkPacket np) { try { if (np.hasPayload()) { Log.i("SharePlugin", "hasPayload"); if (isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { receiveFile(np); } else { Log.i("SharePlugin", "no Permission for Storage"); } } else if (np.has("text")) { Log.i("SharePlugin", "hasText"); receiveText(np); } else if (np.has("url")) { receiveUrl(np); } else { Log.e("SharePlugin", "Error: Nothing attached!"); } } catch (Exception e) { Log.e("SharePlugin", "Exception"); e.printStackTrace(); } return true; } private void receiveUrl(NetworkPacket np) { String url = np.getString("url"); Log.i("SharePlugin", "hasUrl: " + url); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (openUrlsDirectly) { context.startActivity(browserIntent); } else { Resources res = context.getResources(); PendingIntent resultPendingIntent = PendingIntent.getActivity( context, 0, browserIntent, PendingIntent.FLAG_UPDATE_CURRENT ); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification noti = new NotificationCompat.Builder(context, NotificationHelper.Channels.DEFAULT) .setContentTitle(res.getString(R.string.received_url_title, device.getName())) .setContentText(res.getString(R.string.received_url_text, url)) .setContentIntent(resultPendingIntent) .setTicker(res.getString(R.string.received_url_title, device.getName())) .setSmallIcon(R.drawable.ic_notification) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .build(); NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti); } } private void receiveText(NetworkPacket np) { String text = np.getString("text"); ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); cm.setText(text); Toast.makeText(context, R.string.shareplugin_text_saved, Toast.LENGTH_LONG).show(); } private void receiveFile(NetworkPacket np) { final InputStream input = np.getPayload(); 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 { destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri()); } catch (FileNotFoundException e) { e.printStackTrace(); return; } final Uri destinationUri = destinationDocument.getUri(); final ShareNotification notification = new ShareNotification(device, filename); notification.show(); new Thread(() -> { try { byte data[] = new byte[4096]; long progress = 0, prevProgressPercentage = -1; int count; long lastUpdate = 0; while ((count = input.read(data)) >= 0) { progress += count; destinationOutput.write(data, 0, count); if (fileLength > 0) { if (progress >= fileLength) break; long progressPercentage = (progress * 100 / fileLength); if (progressPercentage != prevProgressPercentage && System.currentTimeMillis() - lastUpdate > 100) { prevProgressPercentage = progressPercentage; lastUpdate = System.currentTimeMillis(); notification.setProgress((int) progressPercentage); notification.show(); } } //else Log.e("SharePlugin", "Infinite loop? :D"); } destinationOutput.flush(); 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 (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); + } - if (!customDestination) { - 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); + 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)) { + 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(); notification.setFinished(false); notification.show(); } finally { try { destinationOutput.close(); } catch (Exception e) { } try { input.close(); } catch (Exception e) { } } }).start(); } @Override public void startPreferencesActivity(DeviceSettingsActivity parentActivity) { Intent intent = new Intent(parentActivity, ShareSettingsActivity.class); intent.putExtra("plugin_display_name", getDisplayName()); intent.putExtra("plugin_key", getPluginKey()); parentActivity.startActivity(intent); } void queuedSendUriList(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<>(); for (Uri uri : uriList) { toSend.add(uriToNetworkPacket(context, uri)); } //Callback that shows a progress notification final NotificationUpdateCallback notificationUpdateCallback = new NotificationUpdateCallback(context, device, toSend); //Do the sending in background new Thread(() -> { //Actually send the files try { for (NetworkPacket np : toSend) { boolean success = device.sendPacketBlocking(np, notificationUpdateCallback); if (!success) { Log.e("SharePlugin", "Error sending files"); return; } } } catch (Exception e) { e.printStackTrace(); } }).start(); } //Create the network package from the URI private static NetworkPacket uriToNetworkPacket(final Context context, final Uri uri) { try { ContentResolver cr = context.getContentResolver(); InputStream inputStream = cr.openInputStream(uri); NetworkPacket np = new NetworkPacket(PACKET_TYPE_SHARE_REQUEST); long size = -1; if (uri.getScheme().equals("file")) { // file:// is a non media uri, so we cannot query the ContentProvider np.set("filename", uri.getLastPathSegment()); try { size = new File(uri.getPath()).length(); } catch (Exception e) { Log.e("SendFileActivity", "Could not obtain file size"); e.printStackTrace(); } } else { // Probably a content:// uri, so we query the Media content provider Cursor cursor = null; try { String[] proj = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.DISPLAY_NAME}; cursor = cr.query(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); np.set("filename", Uri.parse(path).getLastPathSegment()); size = new File(path).length(); } catch (Exception unused) { Log.w("SendFileActivity", "Could not resolve media to a file, trying to get info as media"); 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("SendFileActivity", "Could not obtain file name"); } try { int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE); cursor.moveToFirst(); //For some reason this size can differ from the actual file size! size = cursor.getInt(column_index); } catch (Exception e) { Log.e("SendFileActivity", "Could not obtain file size"); e.printStackTrace(); } } finally { try { cursor.close(); } catch (Exception e) { } } } np.setPayload(inputStream, size); return np; } catch (Exception e) { Log.e("SendFileActivity", "Exception sending files"); e.printStackTrace(); return null; } } public void share(Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { if (extras.containsKey(Intent.EXTRA_STREAM)) { try { ArrayList uriList; if (!Intent.ACTION_SEND.equals(intent.getAction())) { uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); } else { Uri uri = extras.getParcelable(Intent.EXTRA_STREAM); uriList = new ArrayList<>(); uriList.add(uri); } queuedSendUriList(uriList); } catch (Exception e) { Log.e("ShareActivity", "Exception"); e.printStackTrace(); } } else if (extras.containsKey(Intent.EXTRA_TEXT)) { String text = extras.getString(Intent.EXTRA_TEXT); String subject = extras.getString(Intent.EXTRA_SUBJECT); //Hack: Detect shared youtube videos, so we can open them in the browser instead of as text if (subject != null && subject.endsWith("YouTube")) { int index = text.indexOf(": http://youtu.be/"); if (index > 0) { text = text.substring(index + 2); //Skip ": " } } boolean isUrl; try { new URL(text); isUrl = true; } catch (Exception e) { isUrl = false; } NetworkPacket np = new NetworkPacket(SharePlugin.PACKET_TYPE_SHARE_REQUEST); if (isUrl) { np.set("url", text); } else { np.set("text", text); } device.sendPacket(np); } } } @Override public String[] getSupportedPacketTypes() { return new String[]{PACKET_TYPE_SHARE_REQUEST}; } @Override public String[] getOutgoingPacketTypes() { return new String[]{PACKET_TYPE_SHARE_REQUEST}; } @Override public String[] getOptionalPermissions() { return new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; } }