-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11078 from nextcloud/backport/11059/stable-3.23
[stable-3.23] Allow Pin to Home with file actions bottom sheet
- Loading branch information
Showing
10 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Nextcloud Android client application | ||
* | ||
* @author Felix Nüsse | ||
* | ||
* Copyright (C) 2022 Felix Nüsse | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or any later version. | ||
* | ||
* 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 AFFERO GENERAL PUBLIC LICENSE for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.nextcloud.utils | ||
|
||
import android.app.PendingIntent | ||
import android.app.PendingIntent.FLAG_IMMUTABLE | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import android.graphics.drawable.BitmapDrawable | ||
import android.graphics.drawable.Drawable | ||
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 | ||
import com.owncloud.android.datamodel.ThumbnailsCacheManager | ||
import com.owncloud.android.ui.activity.FileActivity | ||
import com.owncloud.android.ui.activity.FileDisplayActivity | ||
import com.owncloud.android.utils.MimeTypeUtil | ||
import com.owncloud.android.utils.theme.ViewThemeUtils | ||
import javax.inject.Inject | ||
|
||
class ShortcutUtil @Inject constructor(private val mContext: Context) { | ||
|
||
/** | ||
* Adds a pinned shortcut to the home screen that points to the passed file/folder. | ||
* | ||
* @param file The file/folder to which a pinned shortcut should be added to the home screen. | ||
*/ | ||
fun addShortcutToHomescreen(file: OCFile, viewThemeUtils: ViewThemeUtils) { | ||
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: IconCompat | ||
var thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( | ||
ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.remoteId | ||
) | ||
if (thumbnail != null) { | ||
thumbnail = bitmapToAdaptiveBitmap(thumbnail) | ||
icon = IconCompat.createWithAdaptiveBitmap(thumbnail) | ||
} else if (file.isFolder) { | ||
val bitmapIcon = MimeTypeUtil.getFolderTypeIcon( | ||
file.isSharedWithMe || file.isSharedWithSharee, | ||
file.isSharedViaLink, | ||
file.isEncrypted, | ||
file.isGroupFolder, | ||
file.mountType, | ||
mContext, | ||
viewThemeUtils | ||
).toBitmap() | ||
icon = IconCompat.createWithBitmap(bitmapIcon) | ||
} else { | ||
icon = IconCompat.createWithResource( | ||
mContext, | ||
MimeTypeUtil.getFileTypeIconId(file.mimeType, file.fileName) | ||
) | ||
} | ||
val longLabel = mContext.getString(R.string.pin_shortcut_label, file.fileName) | ||
val pinShortcutInfo = ShortcutInfoCompat.Builder(mContext, shortcutId) | ||
.setShortLabel(file.fileName) | ||
.setLongLabel(longLabel) | ||
.setIcon(icon) | ||
.setIntent(intent) | ||
.build() | ||
val pinnedShortcutCallbackIntent = | ||
ShortcutManagerCompat.createShortcutResultIntent(mContext, pinShortcutInfo) | ||
val successCallback = PendingIntent.getBroadcast( | ||
mContext, | ||
0, | ||
pinnedShortcutCallbackIntent, | ||
FLAG_IMMUTABLE | ||
) | ||
ShortcutManagerCompat.requestPinShortcut( | ||
mContext, | ||
pinShortcutInfo, | ||
successCallback.intentSender | ||
) | ||
} | ||
} | ||
|
||
private fun bitmapToAdaptiveBitmap(orig: Bitmap): Bitmap { | ||
val adaptiveIconSize = mContext.resources.getDimensionPixelSize(R.dimen.adaptive_icon_size) | ||
val adaptiveIconOuterSides = mContext.resources.getDimensionPixelSize(R.dimen.adaptive_icon_padding) | ||
val drawable: Drawable = BitmapDrawable(mContext.resources, orig) | ||
val bitmap = Bitmap.createBitmap(adaptiveIconSize, adaptiveIconSize, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(bitmap) | ||
drawable.setBounds( | ||
adaptiveIconOuterSides, | ||
adaptiveIconOuterSides, | ||
adaptiveIconSize - adaptiveIconOuterSides, | ||
adaptiveIconSize - adaptiveIconOuterSides | ||
) | ||
drawable.draw(canvas) | ||
return bitmap | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!-- | ||
Nextcloud Android client application | ||
Copyright (C) 2022 Nextcloud. | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
License as published by the Free Software Foundation; either | ||
version 3 of the License, or any later version. | ||
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 AFFERO GENERAL PUBLIC LICENSE for more details. | ||
You should have received a copy of the GNU Affero General Public | ||
License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
Icon provided by Android Material Library in Apache License 2.0 | ||
--> | ||
<vector android:autoMirrored="true" android:height="24dp" | ||
android:tint="#000000" android:viewportHeight="24" | ||
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M18,1.01L8,1c-1.1,0 -2,0.9 -2,2v3c0,0.55 0.45,1 1,1s1,-0.45 1,-1V5h10v14H8v-1c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v3c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2V3c0,-1.1 -0.9,-1.99 -2,-1.99zM11,15c0.55,0 1,-0.45 1,-1V9c0,-0.55 -0.45,-1 -1,-1H6c-0.55,0 -1,0.45 -1,1s0.45,1 1,1h2.59L3.7,14.89c-0.39,0.39 -0.39,1.02 0,1.41 0.39,0.39 1.02,0.39 1.41,0L10,11.41V14c0,0.55 0.45,1 1,1z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters