2016-12-09 03:02:41 +01:00
|
|
|
package org.mariotaku.twidere.task
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.net.Uri
|
2017-04-20 14:04:37 +02:00
|
|
|
import android.widget.Toast
|
2017-03-27 15:56:03 +02:00
|
|
|
import org.mariotaku.microblog.library.MicroBlog
|
2016-12-09 03:02:41 +01:00
|
|
|
import org.mariotaku.microblog.library.MicroBlogException
|
|
|
|
import org.mariotaku.twidere.R
|
|
|
|
import org.mariotaku.twidere.TwidereConstants.LOGTAG
|
2017-04-20 19:23:11 +02:00
|
|
|
import org.mariotaku.twidere.extension.model.api.toParcelable
|
2017-03-27 15:56:03 +02:00
|
|
|
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
|
2017-04-20 14:04:37 +02:00
|
|
|
import org.mariotaku.twidere.model.AccountDetails
|
|
|
|
import org.mariotaku.twidere.model.ParcelableMedia
|
2016-12-09 03:02:41 +01:00
|
|
|
import org.mariotaku.twidere.model.ParcelableUser
|
|
|
|
import org.mariotaku.twidere.model.UserKey
|
2017-02-09 09:00:12 +01:00
|
|
|
import org.mariotaku.twidere.model.event.ProfileUpdatedEvent
|
2017-04-20 14:04:37 +02:00
|
|
|
import org.mariotaku.twidere.task.twitter.UpdateStatusTask
|
2017-02-10 16:38:59 +01:00
|
|
|
import org.mariotaku.twidere.util.DebugLog
|
2016-12-09 03:02:41 +01:00
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/3/11.
|
|
|
|
*/
|
|
|
|
open class UpdateProfileBackgroundImageTask<ResultHandler>(
|
2017-02-08 17:45:20 +01:00
|
|
|
context: Context,
|
2017-04-20 14:04:37 +02:00
|
|
|
accountKey: UserKey,
|
2016-12-09 03:02:41 +01:00
|
|
|
private val imageUri: Uri,
|
|
|
|
private val tile: Boolean,
|
|
|
|
private val deleteImage: Boolean
|
2017-04-20 14:04:37 +02:00
|
|
|
) : AbsAccountRequestTask<Any?, ParcelableUser, ResultHandler>(context, accountKey) {
|
2016-12-09 03:02:41 +01:00
|
|
|
|
2017-04-20 14:04:37 +02:00
|
|
|
private val profileImageSize = context.getString(R.string.profile_image_size)
|
2016-12-09 03:02:41 +01:00
|
|
|
|
2017-04-20 14:04:37 +02:00
|
|
|
override fun onExecute(account: AccountDetails, params: Any?): ParcelableUser {
|
|
|
|
val microBlog = account.newMicroBlogInstance(context, MicroBlog::class.java)
|
2016-12-09 03:02:41 +01:00
|
|
|
try {
|
2020-06-09 01:48:04 +02:00
|
|
|
UpdateStatusTask.getBodyFromMedia(context, imageUri, ParcelableMedia.Type.IMAGE,
|
|
|
|
isDeleteAlways = true,
|
|
|
|
isDeleteOnSuccess = true,
|
|
|
|
sizeLimit = null,
|
|
|
|
chucked = false,
|
|
|
|
readListener = null
|
|
|
|
).use {
|
2017-04-20 14:04:37 +02:00
|
|
|
microBlog.updateProfileBackgroundImage(it.body, tile)
|
2016-12-09 03:02:41 +01:00
|
|
|
}
|
|
|
|
} catch (e: IOException) {
|
2017-04-20 14:04:37 +02:00
|
|
|
throw MicroBlogException(e)
|
2016-12-09 03:02:41 +01:00
|
|
|
}
|
2017-04-20 14:04:37 +02:00
|
|
|
// Wait for 5 seconds, see
|
|
|
|
// https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image
|
|
|
|
try {
|
|
|
|
Thread.sleep(5000L)
|
|
|
|
} catch (e: InterruptedException) {
|
|
|
|
DebugLog.w(LOGTAG, tr = e)
|
|
|
|
}
|
|
|
|
val user = microBlog.verifyCredentials()
|
2017-04-22 08:25:42 +02:00
|
|
|
return user.toParcelable(account, profileImageSize = profileImageSize)
|
2017-04-20 14:04:37 +02:00
|
|
|
}
|
2016-12-09 03:02:41 +01:00
|
|
|
|
2017-04-20 14:04:37 +02:00
|
|
|
override fun onSucceed(callback: ResultHandler?, result: ParcelableUser) {
|
|
|
|
Toast.makeText(context, R.string.message_toast_profile_background_image_updated,
|
|
|
|
Toast.LENGTH_SHORT).show()
|
|
|
|
bus.post(ProfileUpdatedEvent(result))
|
2016-12-09 03:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|