ShortcutUtil: use ShortcutManagerCompat to avoid min SDK requirement

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-11-22 10:25:37 +01:00
parent 2e3ef2eace
commit 52e1e8cef5
No known key found for this signature in database
GPG key ID: 2585783189A62105
4 changed files with 20 additions and 23 deletions

View file

@ -55,6 +55,9 @@
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
android:maxSdkVersion="25" />
<!-- Apps that target Android 9 (API level 28) or higher and use foreground services
must request the FOREGROUND_SERVICE permission -->

View file

@ -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
)

View file

@ -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<Integer> 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);
}
}

View file

@ -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;
}
}