fixed compose shortcut

made quick access shortcut icon work
This commit is contained in:
Mariotaku Lee 2017-03-21 13:15:00 +08:00
parent d61eafb86c
commit 6498392b6e
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 97 additions and 12 deletions

View File

@ -393,6 +393,7 @@
</activity>
<activity
android:name=".activity.shortcut.CreateComposeShortcutActivity"
android:icon="@drawable/ic_app_shortcut_compose"
android:label="@string/title_compose"
android:theme="@style/Theme.Twidere.NoDisplay">
<intent-filter>

View File

@ -21,6 +21,8 @@ package org.mariotaku.twidere.activity.shortcut
import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.os.Bundle
import org.mariotaku.twidere.BuildConfig
import org.mariotaku.twidere.R
@ -33,11 +35,22 @@ class CreateComposeShortcutActivity : Activity() {
setVisible(true)
val intent = Intent()
val launchIntent = Intent(INTENT_ACTION_COMPOSE).apply {
`package` = BuildConfig.VERSION_NAME
`package` = BuildConfig.APPLICATION_ID
}
val icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher)
val icon = BitmapFactory.decodeResource(resources, R.drawable.ic_app_shortcut_compose,
BitmapFactory.Options().apply { inMutable = true }).apply {
val appIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher, BitmapFactory.Options().apply {
inSampleSize = 3
})
val canvas = Canvas(this)
val appIconLeft = (width - appIcon.width).toFloat()
val appIconTop = (height - appIcon.height).toFloat()
canvas.drawBitmap(appIcon, appIconLeft, appIconTop, null)
appIcon.recycle()
}
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent)
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon)
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.action_compose))
setResult(RESULT_OK, intent)
finish()

View File

@ -21,9 +21,17 @@ package org.mariotaku.twidere.activity.shortcut
import android.app.Activity
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.ui.failUi
import nl.komponents.kovenant.ui.successUi
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.Bundle
import org.mariotaku.ktextension.set
@ -34,12 +42,15 @@ import org.mariotaku.twidere.activity.BaseActivity
import org.mariotaku.twidere.activity.UserListSelectorActivity
import org.mariotaku.twidere.activity.UserSelectorActivity
import org.mariotaku.twidere.constant.nameFirstKey
import org.mariotaku.twidere.constant.profileImageStyleKey
import org.mariotaku.twidere.extension.applyTheme
import org.mariotaku.twidere.extension.loadProfileImage
import org.mariotaku.twidere.fragment.BaseDialogFragment
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.ParcelableUserList
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.util.IntentUtils
import org.mariotaku.twidere.util.glide.DeferredTarget
class CreateQuickAccessShortcutActivity : BaseActivity() {
@ -112,6 +123,7 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
putExtra(Intent.EXTRA_SHORTCUT_NAME, userColorNameManager.getDisplayName(user,
preferences[nameFirstKey]))
})
finish()
}
"user_favorites" -> {
val launchIntent = IntentUtils.userTimeline(accountKey, user.key,
@ -123,20 +135,34 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
putExtra(Intent.EXTRA_SHORTCUT_NAME, userColorNameManager.getDisplayName(user,
preferences[nameFirstKey]))
})
finish()
}
else -> {
val launchIntent = IntentUtils.userProfile(accountKey, user.key,
user.screen_name, profileUrl = user.extras?.statusnet_profile_url)
val icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher)
setResult(Activity.RESULT_OK, Intent().apply {
putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent)
putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon)
putExtra(Intent.EXTRA_SHORTCUT_NAME, userColorNameManager.getDisplayName(user,
preferences[nameFirstKey]))
})
val requestBuilder = Glide.with(this).loadProfileImage(this, user, shapeStyle = preferences[profileImageStyleKey],
cornerRadiusRatio = 0.1f, size = getString(R.string.profile_image_size))
val deferred = deferred<GlideDrawable, Exception>()
requestBuilder.into(DeferredTarget(deferred))
deferred.promise.successUi { drawable ->
val icon = Bitmap.createBitmap(drawable.intrinsicWidth,
drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(icon)
drawable.setBounds(0, 0, icon.width, icon.height)
drawable.draw(canvas)
setResult(Activity.RESULT_OK, Intent().apply {
putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent)
putExtra(Intent.EXTRA_SHORTCUT_ICON, icon)
putExtra(Intent.EXTRA_SHORTCUT_NAME, userColorNameManager.getDisplayName(user,
preferences[nameFirstKey]))
})
finish()
}.failUi {
setResult(Activity.RESULT_CANCELED)
finish()
}
}
}
finish()
}
REQUEST_SELECT_USER_LIST -> {
if (resultCode != Activity.RESULT_OK || data == null) {
@ -190,7 +216,7 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
class QuickAccessShortcutTypeDialogFragment : BaseDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(context)
builder.setItems(R.array.entries_quick_access_shortcut_types) { dialog, which ->
builder.setItems(R.array.entries_quick_access_shortcut_types) { _, which ->
(activity as CreateQuickAccessShortcutActivity).onItemSelected(which)
}
return builder.create().apply {
@ -200,5 +226,9 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
}
}
}
override fun onCancel(dialog: DialogInterface?) {
activity?.finish()
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) 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 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 <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.glide
import android.graphics.drawable.Drawable
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import nl.komponents.kovenant.Deferred
/**
* Created by mariotaku on 2017/3/21.
*/
class DeferredTarget<R>(val deferred: Deferred<R, Exception>) : SimpleTarget<R>() {
override fun onLoadFailed(e: Exception, errorDrawable: Drawable?) {
deferred.reject(e)
}
override fun onResourceReady(resource: R, glideAnimation: GlideAnimation<in R>) {
deferred.resolve(resource)
}
}