From 01761718d9be24c7bcd13d4c0d7a8e10a3a157c3 Mon Sep 17 00:00:00 2001 From: tobiaskaminsky Date: Fri, 12 May 2017 15:17:16 +0200 Subject: [PATCH] handle Nougat and wallpaper app --- .../ui/helpers/FileOperationsHelper.java | 151 ++++++++++-------- src/main/res/values/strings.xml | 1 + 2 files changed, 85 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java b/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java index fd6ebe870a..243210a236 100755 --- a/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java +++ b/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java @@ -1,23 +1,22 @@ /** - * ownCloud Android client application - * - * @author masensio - * @author David A. Velasco - * @author Juan Carlos González Cabrero - * Copyright (C) 2015 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * 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 . + * ownCloud Android client application * + * @author masensio + * @author David A. Velasco + * @author Juan Carlos González Cabrero + * Copyright (C) 2015 ownCloud Inc. + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + *

+ * 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 com.owncloud.android.ui.helpers; @@ -29,12 +28,14 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; -import android.os.Parcelable; +import android.os.Build; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; +import android.support.v4.content.FileProvider; import android.webkit.MimeTypeMap; import android.widget.Toast; +import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.OCFile; @@ -70,7 +71,7 @@ import java.util.regex.Pattern; public class FileOperationsHelper { private static final String TAG = FileOperationsHelper.class.getSimpleName(); - + private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; protected FileActivity mFileActivity = null; @@ -78,21 +79,21 @@ public class FileOperationsHelper { /// Identifier of operation in progress which result shouldn't be lost private long mWaitingForOpId = Long.MAX_VALUE; - private static final Pattern mPatternUrl = Pattern.compile("^URL=(.+)$"); - private static final Pattern mPatternString = Pattern.compile("(.+)"); + private static final Pattern mPatternUrl = Pattern.compile("^URL=(.+)$"); + private static final Pattern mPatternString = Pattern.compile("(.+)"); public FileOperationsHelper(FileActivity fileActivity) { mFileActivity = fileActivity; } - @Nullable + @Nullable private String getUrlFromFile(String storagePath, Pattern pattern) { String url = null; InputStreamReader fr = null; BufferedReader br = null; try { - fr = new InputStreamReader(new FileInputStream(storagePath), "UTF8"); + fr = new InputStreamReader(new FileInputStream(storagePath), "UTF8"); br = new BufferedReader(fr); String line; @@ -104,7 +105,7 @@ public class FileOperationsHelper { } } } catch (IOException e) { - Log_OC.d(TAG, e.getMessage()); + Log_OC.d(TAG, e.getMessage()); } finally { if (br != null) { try { @@ -123,20 +124,20 @@ public class FileOperationsHelper { } } return url; - } - + } + @Nullable private Intent createIntentFromFile(String storagePath) { String url = null; int lastIndexOfDot = storagePath.lastIndexOf('.'); if (lastIndexOfDot >= 0) { String fileExt = storagePath.substring(lastIndexOfDot + 1); - if (fileExt.equalsIgnoreCase("url") ||fileExt.equalsIgnoreCase("desktop")) { - // Windows internet shortcut file .url - // Ubuntu internet shortcut file .desktop + if (fileExt.equalsIgnoreCase("url") || fileExt.equalsIgnoreCase("desktop")) { + // Windows internet shortcut file .url + // Ubuntu internet shortcut file .desktop url = getUrlFromFile(storagePath, mPatternUrl); } else if (fileExt.equalsIgnoreCase("webloc")) { - // mac internet shortcut file .webloc + // mac internet shortcut file .webloc url = getUrlFromFile(storagePath, mPatternString); } } @@ -164,7 +165,7 @@ public class FileOperationsHelper { } } - if(openFileWithIntent == null) { + if (openFileWithIntent == null) { openFileWithIntent = createIntentFromFile(storagePath); } @@ -181,7 +182,7 @@ public class FileOperationsHelper { List launchables = mFileActivity.getPackageManager(). queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS); - if(launchables != null && launchables.size() > 0) { + if (launchables != null && launchables.size() > 0) { try { mFileActivity.startActivity( Intent.createChooser( @@ -249,7 +250,7 @@ public class FileOperationsHelper { } } - public void getFileWithLink(OCFile file){ + public void getFileWithLink(OCFile file) { if (isSharedSupported()) { if (file != null) { mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext(). @@ -286,7 +287,7 @@ public class FileOperationsHelper { if (file != null) { // TODO check capability? mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext(). - getString(R.string.wait_a_moment)); + getString(R.string.wait_a_moment)); Intent service = new Intent(mFileActivity, OperationsService.class); service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE); @@ -334,7 +335,7 @@ public class FileOperationsHelper { queueShareIntent(unshareService); } - public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){ + public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup) { // Unshare the file: Create the intent Intent unshareService = new Intent(mFileActivity, OperationsService.class); @@ -348,7 +349,7 @@ public class FileOperationsHelper { } - private void queueShareIntent(Intent shareIntent){ + private void queueShareIntent(Intent shareIntent) { if (isSharedSupported()) { // Unshare the file mWaitingForOpId = mFileActivity.getOperationsServiceBinder(). @@ -372,10 +373,10 @@ public class FileOperationsHelper { * * @param file File to share or unshare. */ - public void showShareFile(OCFile file){ + public void showShareFile(OCFile file) { Intent intent = new Intent(mFileActivity, ShareActivity.class); - intent.putExtra(mFileActivity.EXTRA_FILE, (Parcelable) file); - intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount()); + intent.putExtra(FileActivity.EXTRA_FILE, file); + intent.putExtra(FileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount()); mFileActivity.startActivity(intent); } @@ -503,8 +504,8 @@ public class FileOperationsHelper { // set MimeType sendIntent.setType(file.getMimetype()); sendIntent.putExtra( - Intent.EXTRA_STREAM, - file.getExposedFileUri(mFileActivity) + Intent.EXTRA_STREAM, + file.getExposedFileUri(mFileActivity) ); sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action @@ -519,7 +520,7 @@ public class FileOperationsHelper { } public void syncFiles(Collection files) { - for (OCFile file: files) { + for (OCFile file : files) { syncFile(file); } } @@ -527,13 +528,29 @@ public class FileOperationsHelper { public void setPictureAs(OCFile file) { if (file != null) { if (file.isDown()) { - File externalFile = new File(file.getStoragePath()); - Uri sendUri = Uri.fromFile(externalFile); - Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); - intent.setDataAndType(sendUri, file.getMimetype()); - intent.putExtra("mimeType", file.getMimetype()); - mFileActivity.startActivityForResult(Intent.createChooser(intent, - mFileActivity.getString(R.string.set_as)), 200); + Context context = MainApp.getAppContext(); + + try { + File externalFile = new File(file.getStoragePath()); + Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); + Uri sendUri; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + sendUri = FileProvider.getUriForFile(context, + context.getResources().getString(R.string.file_provider_authority), externalFile); + } else { + sendUri = Uri.fromFile(externalFile); + } + + intent.setDataAndType(sendUri, file.getMimetype()); + intent.putExtra("mimeType", file.getMimetype()); + mFileActivity.startActivityForResult(Intent.createChooser(intent, + mFileActivity.getString(R.string.set_as)), 200); + + } catch (ActivityNotFoundException exception) { + Toast.makeText(context, R.string.picture_set_as_no_app, Toast.LENGTH_LONG).show(); + } } } else { Log_OC.wtf(TAG, "Trying to send a NULL OCFile"); @@ -546,7 +563,7 @@ public class FileOperationsHelper { * @param file The file or folder to synchronize */ public void syncFile(OCFile file) { - if (!file.isFolder()){ + if (!file.isFolder()) { Intent intent = new Intent(mFileActivity, OperationsService.class); intent.setAction(OperationsService.ACTION_SYNC_FILE); intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); @@ -554,8 +571,8 @@ public class FileOperationsHelper { intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent); mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext(). - getString(R.string.wait_a_moment)); - + getString(R.string.wait_a_moment)); + } else { Intent intent = new Intent(mFileActivity, OperationsService.class); intent.setAction(OperationsService.ACTION_SYNC_FOLDER); @@ -568,37 +585,37 @@ public class FileOperationsHelper { public void toggleFavoriteFiles(Collection files, boolean shouldBeFavorite) { List alreadyRightStateList = new ArrayList<>(); - for(OCFile file : files) { - if(file.getIsFavorite() == shouldBeFavorite) { + for (OCFile file : files) { + if (file.getIsFavorite() == shouldBeFavorite) { alreadyRightStateList.add(file); } } files.removeAll(alreadyRightStateList); - for (OCFile file: files) { + for (OCFile file : files) { toggleFavoriteFile(file, shouldBeFavorite); } } public void toggleFavoriteFile(OCFile file, boolean shouldBeFavorite) { - if(file.getIsFavorite() != shouldBeFavorite) { + if (file.getIsFavorite() != shouldBeFavorite) { EventBus.getDefault().post(new FavoriteEvent(file.getRemotePath(), shouldBeFavorite, file.getRemoteId())); } } - public void toogleOfflineFiles(Collection files, boolean isAvailableOffline){ + public void toogleOfflineFiles(Collection files, boolean isAvailableOffline) { List alreadyRightStateList = new ArrayList<>(); - for(OCFile file : files) { - if(file.isAvailableOffline() == isAvailableOffline) { + for (OCFile file : files) { + if (file.isAvailableOffline() == isAvailableOffline) { alreadyRightStateList.add(file); } } files.removeAll(alreadyRightStateList); - for (OCFile file: files) { + for (OCFile file : files) { toggleOfflineFile(file, isAvailableOffline); } } @@ -623,7 +640,7 @@ public class FileOperationsHelper { } } } - + public void renameFile(OCFile file, String newFilename) { // RenameFile Intent service = new Intent(mFileActivity, OperationsService.class); @@ -632,7 +649,7 @@ public class FileOperationsHelper { service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); - + mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment)); } @@ -654,7 +671,7 @@ public class FileOperationsHelper { service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); } - + mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment)); } @@ -666,8 +683,8 @@ public class FileOperationsHelper { service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath); service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath); - mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); - + mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); + mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment)); } @@ -765,7 +782,7 @@ public class FileOperationsHelper { mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); mFileActivity.showLoadingDialog( - mFileActivity.getString(R.string.wait_checking_credentials) + mFileActivity.getString(R.string.wait_checking_credentials) ); } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index cae1420011..663bc6f3d2 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -662,6 +662,7 @@ New notification received Logout + No app found to set a picture with!