Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java
Show All 28 Lines | |||||
29 | import android.content.ClipboardManager; | 29 | import android.content.ClipboardManager; | ||
30 | import android.content.ContentResolver; | 30 | import android.content.ContentResolver; | ||
31 | import android.content.Context; | 31 | import android.content.Context; | ||
32 | import android.content.Intent; | 32 | import android.content.Intent; | ||
33 | import android.content.res.Resources; | 33 | import android.content.res.Resources; | ||
34 | import android.database.Cursor; | 34 | import android.database.Cursor; | ||
35 | import android.graphics.drawable.Drawable; | 35 | import android.graphics.drawable.Drawable; | ||
36 | import android.net.Uri; | 36 | import android.net.Uri; | ||
37 | import android.os.Build; | ||||
37 | import android.os.Bundle; | 38 | import android.os.Bundle; | ||
38 | import android.provider.MediaStore; | 39 | import android.provider.MediaStore; | ||
39 | import android.support.v4.app.NotificationCompat; | 40 | import android.support.v4.app.NotificationCompat; | ||
40 | import android.support.v4.content.ContextCompat; | 41 | import android.support.v4.content.ContextCompat; | ||
42 | import android.support.v4.content.FileProvider; | ||||
41 | import android.support.v4.provider.DocumentFile; | 43 | import android.support.v4.provider.DocumentFile; | ||
42 | import android.util.Log; | 44 | import android.util.Log; | ||
43 | import android.widget.Toast; | 45 | import android.widget.Toast; | ||
44 | 46 | | |||
45 | import org.kde.kdeconnect.Device; | 47 | import org.kde.kdeconnect.Device; | ||
46 | import org.kde.kdeconnect.Helpers.FilesHelper; | 48 | import org.kde.kdeconnect.Helpers.FilesHelper; | ||
47 | import org.kde.kdeconnect.Helpers.MediaStoreHelper; | 49 | import org.kde.kdeconnect.Helpers.MediaStoreHelper; | ||
48 | import org.kde.kdeconnect.Helpers.NotificationHelper; | 50 | import org.kde.kdeconnect.Helpers.NotificationHelper; | ||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Line(s) | 63 | public class SharePlugin extends Plugin { | |||
182 | } | 184 | } | ||
183 | 185 | | |||
184 | private void receiveFile(NetworkPacket np) { | 186 | private void receiveFile(NetworkPacket np) { | ||
185 | 187 | | |||
186 | final InputStream input = np.getPayload(); | 188 | final InputStream input = np.getPayload(); | ||
187 | final long fileLength = np.getPayloadSize(); | 189 | final long fileLength = np.getPayloadSize(); | ||
188 | final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis())); | 190 | final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis())); | ||
189 | 191 | | |||
192 | String filename = originalFilename; | ||||
193 | final DocumentFile destinationFolderDocument; | ||||
194 | | ||||
190 | //We need to check for already existing files only when storing in the default path. | 195 | //We need to check for already existing files only when storing in the default path. | ||
191 | //User-defined paths use the new Storage Access Framework that already handles this. | 196 | //User-defined paths use the new Storage Access Framework that already handles this. | ||
192 | final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context); | 197 | //If the file should be opened immediately store it in the standard location to avoid the FileProvider trouble (See ShareNotification::setURI) | ||
198 | if (np.getBoolean("open") || !ShareSettingsActivity.isCustomDestinationEnabled(context)) { | ||||
193 | final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath(); | 199 | final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath(); | ||
194 | final String filename = customDestination ? originalFilename : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename); | 200 | filename = FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename); | ||
195 | 201 | destinationFolderDocument = DocumentFile.fromFile(new File(defaultPath)); | |||
202 | } else { | ||||
203 | destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context); | ||||
204 | } | ||||
196 | String displayName = FilesHelper.getFileNameWithoutExt(filename); | 205 | String displayName = FilesHelper.getFileNameWithoutExt(filename); | ||
197 | final String mimeType = FilesHelper.getMimeTypeFromFile(filename); | 206 | final String mimeType = FilesHelper.getMimeTypeFromFile(filename); | ||
198 | 207 | | |||
199 | if ("*/*".equals(mimeType)) { | 208 | if ("*/*".equals(mimeType)) { | ||
200 | displayName = filename; | 209 | displayName = filename; | ||
201 | } | 210 | } | ||
202 | 211 | | |||
203 | final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context); | | |||
204 | final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, displayName); | 212 | final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, displayName); | ||
205 | final OutputStream destinationOutput; | 213 | final OutputStream destinationOutput; | ||
206 | try { | 214 | try { | ||
207 | destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri()); | 215 | destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri()); | ||
208 | } catch (FileNotFoundException e) { | 216 | } catch (FileNotFoundException e) { | ||
209 | e.printStackTrace(); | 217 | e.printStackTrace(); | ||
210 | return; | 218 | return; | ||
211 | } | 219 | } | ||
Show All 25 Lines | 231 | while ((count = input.read(data)) >= 0) { | |||
237 | } | 245 | } | ||
238 | //else Log.e("SharePlugin", "Infinite loop? :D"); | 246 | //else Log.e("SharePlugin", "Infinite loop? :D"); | ||
239 | } | 247 | } | ||
240 | 248 | | |||
241 | destinationOutput.flush(); | 249 | destinationOutput.flush(); | ||
242 | 250 | | |||
243 | Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath()); | 251 | Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath()); | ||
244 | 252 | | |||
253 | if (np.getBoolean("open")) { | ||||
254 | | ||||
255 | notification.cancel(); | ||||
256 | | ||||
257 | Intent intent = new Intent(Intent.ACTION_VIEW); | ||||
258 | if (Build.VERSION.SDK_INT >= 24) { | ||||
259 | //Nougat and later require "content://" uris instead of "file://" uris | ||||
260 | File file = new File(destinationUri.getPath()); | ||||
261 | Uri contentUri = FileProvider.getUriForFile(device.getContext(), "org.kde.kdeconnect_tp.fileprovider", file); | ||||
262 | intent.setDataAndType(contentUri, mimeType); | ||||
263 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | ||||
264 | } else { | ||||
265 | intent.setDataAndType(destinationUri, mimeType); | ||||
266 | } | ||||
267 | | ||||
268 | context.startActivity(intent); | ||||
269 | } else { | ||||
270 | | ||||
245 | //Update the notification and allow to open the file from it | 271 | //Update the notification and allow to open the file from it | ||
246 | notification.setFinished(true); | 272 | notification.setFinished(true); | ||
247 | notification.setURI(destinationUri, mimeType); | 273 | notification.setURI(destinationUri, mimeType); | ||
248 | notification.show(); | 274 | notification.show(); | ||
249 | 275 | | |||
250 | if (!customDestination) { | 276 | if (!ShareSettingsActivity.isCustomDestinationEnabled(context)) { | ||
albertvaka: Don't check for SDK version > 12. | |||||
251 | Log.i("SharePlugin", "Adding to downloads"); | 277 | Log.i("SharePlugin", "Adding to downloads"); | ||
252 | DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); | 278 | DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); | ||
253 | manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false); | 279 | manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false); | ||
254 | } else { | 280 | } else { | ||
255 | //Make sure it is added to the Android Gallery anyway | 281 | //Make sure it is added to the Android Gallery anyway | ||
256 | MediaStoreHelper.indexFile(context, destinationUri); | 282 | MediaStoreHelper.indexFile(context, destinationUri); | ||
257 | } | 283 | } | ||
258 | 284 | } | |||
259 | } catch (Exception e) { | 285 | } catch (Exception e) { | ||
260 | Log.e("SharePlugin", "Receiver thread exception"); | 286 | Log.e("SharePlugin", "Receiver thread exception"); | ||
261 | e.printStackTrace(); | 287 | e.printStackTrace(); | ||
262 | notification.setFinished(false); | 288 | notification.setFinished(false); | ||
263 | notification.show(); | 289 | notification.show(); | ||
264 | } finally { | 290 | } finally { | ||
265 | try { | 291 | try { | ||
266 | destinationOutput.close(); | 292 | destinationOutput.close(); | ||
▲ Show 20 Lines • Show All 193 Lines • Show Last 20 Lines |
Don't check for SDK version > 12.