From 52e1e8cef5031a12fc57e0b75081c55d94164fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Tue, 22 Nov 2022 10:25:37 +0100 Subject: [PATCH] ShortcutUtil: use ShortcutManagerCompat to avoid min SDK requirement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- app/src/main/AndroidManifest.xml | 3 ++ .../java/com/nextcloud/utils/ShortcutUtil.kt | 28 +++++++++---------- .../android/files/FileMenuFilter.java | 5 ++-- .../ui/fragment/OCFileListFragment.java | 7 ++--- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6bb4d2b4ba..48cb023843 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,6 +55,9 @@ + diff --git a/app/src/main/java/com/nextcloud/utils/ShortcutUtil.kt b/app/src/main/java/com/nextcloud/utils/ShortcutUtil.kt index 7daeb7078f..7ccf2f5a24 100644 --- a/app/src/main/java/com/nextcloud/utils/ShortcutUtil.kt +++ b/app/src/main/java/com/nextcloud/utils/ShortcutUtil.kt @@ -25,15 +25,13 @@ import android.app.PendingIntent import android.app.PendingIntent.FLAG_IMMUTABLE import android.content.Context import android.content.Intent -import android.content.pm.ShortcutInfo -import android.content.pm.ShortcutManager import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable -import android.graphics.drawable.Icon -import android.os.Build -import androidx.annotation.RequiresApi +import androidx.core.content.pm.ShortcutInfoCompat +import androidx.core.content.pm.ShortcutManagerCompat +import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.toBitmap import com.owncloud.android.R import com.owncloud.android.datamodel.OCFile @@ -52,22 +50,20 @@ class ShortcutUtil @Inject constructor(private val mContext: Context) { * * @param file The file/folder to which a pinned shortcut should be added to the home screen. */ - @RequiresApi(Build.VERSION_CODES.O) fun addShortcutToHomescreen(file: OCFile, viewThemeUtils: ViewThemeUtils) { - val shortcutManager = mContext.getSystemService(ShortcutManager::class.java) - if (shortcutManager.isRequestPinShortcutSupported) { + if (ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) { val intent = Intent(mContext, FileDisplayActivity::class.java) intent.action = FileDisplayActivity.OPEN_FILE intent.putExtra(FileActivity.EXTRA_FILE, file.remotePath) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) val shortcutId = "nextcloud_shortcut_" + file.remoteId - val icon: Icon + val icon: IconCompat var thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.remoteId ) if (thumbnail != null) { thumbnail = bitmapToAdaptiveBitmap(thumbnail) - icon = Icon.createWithAdaptiveBitmap(thumbnail) + icon = IconCompat.createWithAdaptiveBitmap(thumbnail) } else if (file.isFolder) { val bitmapIcon = MimeTypeUtil.getFolderTypeIcon( file.isSharedWithMe || file.isSharedWithSharee, @@ -78,27 +74,29 @@ class ShortcutUtil @Inject constructor(private val mContext: Context) { mContext, viewThemeUtils ).toBitmap() - icon = Icon.createWithBitmap(bitmapIcon) + icon = IconCompat.createWithBitmap(bitmapIcon) } else { - icon = Icon.createWithResource( + icon = IconCompat.createWithResource( mContext, MimeTypeUtil.getFileTypeIconId(file.mimeType, file.fileName) ) } - val pinShortcutInfo = ShortcutInfo.Builder(mContext, shortcutId) + val pinShortcutInfo = ShortcutInfoCompat.Builder(mContext, shortcutId) .setShortLabel(file.fileName) .setLongLabel("Open " + file.fileName) .setIcon(icon) .setIntent(intent) .build() - val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo) + val pinnedShortcutCallbackIntent = + ShortcutManagerCompat.createShortcutResultIntent(mContext, pinShortcutInfo) val successCallback = PendingIntent.getBroadcast( mContext, 0, pinnedShortcutCallbackIntent, FLAG_IMMUTABLE ) - shortcutManager.requestPinShortcut( + ShortcutManagerCompat.requestPinShortcut( + mContext, pinShortcutInfo, successCallback.intentSender ) diff --git a/app/src/main/java/com/owncloud/android/files/FileMenuFilter.java b/app/src/main/java/com/owncloud/android/files/FileMenuFilter.java index f0feca763b..c7bacf8854 100644 --- a/app/src/main/java/com/owncloud/android/files/FileMenuFilter.java +++ b/app/src/main/java/com/owncloud/android/files/FileMenuFilter.java @@ -26,7 +26,6 @@ package com.owncloud.android.files; import android.accounts.AccountManager; import android.content.Context; -import android.os.Build; import android.view.Menu; import com.nextcloud.android.files.FileLockingHelper; @@ -52,6 +51,7 @@ import java.util.List; import javax.inject.Inject; import androidx.annotation.IdRes; +import androidx.core.content.pm.ShortcutManagerCompat; /** * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile} @@ -265,8 +265,7 @@ public class FileMenuFilter { } private void filterPinToHome(List toHide) { - // Always hide on API's before O, since the shortcut is not available there. - if (!isSingleSelection() || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + if (!isSingleSelection() || !ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { toHide.add(R.id.action_pin_to_homescreen); } } diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 2678d171d9..363fcc872e 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -28,7 +28,6 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -59,6 +58,7 @@ import com.nextcloud.client.utils.Throttler; import com.nextcloud.common.NextcloudClient; import com.nextcloud.ui.fileactions.FileActionsBottomSheet; import com.nextcloud.utils.EditorUtils; +import com.nextcloud.utils.ShortcutUtil; import com.nextcloud.utils.view.FastScrollUtils; import com.owncloud.android.MainApp; import com.owncloud.android.R; @@ -107,7 +107,6 @@ import com.owncloud.android.utils.EncryptionUtils; import com.owncloud.android.utils.FileSortOrder; import com.owncloud.android.utils.FileStorageUtils; import com.owncloud.android.utils.MimeTypeUtil; -import com.nextcloud.utils.ShortcutUtil; import com.owncloud.android.utils.theme.ThemeUtils; import com.owncloud.android.utils.theme.ViewThemeUtils; @@ -1150,9 +1149,7 @@ public class OCFileListFragment extends ExtendedListFragment implements } else if (itemId == R.id.action_unlock_file) { mContainerActivity.getFileOperationsHelper().toggleFileLock(singleFile, false); } else if (itemId == R.id.action_pin_to_homescreen) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - shortcutUtil.addShortcutToHomescreen(singleFile, viewThemeUtils); - } + shortcutUtil.addShortcutToHomescreen(singleFile, viewThemeUtils); return true; } }