Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/activity/AccountSelectorActivity.kt

161 lines
6.0 KiB
Kotlin
Raw Normal View History

2016-07-08 06:46:54 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 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.activity
2016-12-03 16:45:13 +01:00
import android.accounts.AccountManager
2016-07-08 06:46:54 +02:00
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ListView
import android.widget.Toast
2017-03-01 15:12:25 +01:00
import com.bumptech.glide.Glide
2016-07-08 06:46:54 +02:00
import kotlinx.android.synthetic.main.activity_account_selector.*
2016-08-30 14:23:59 +02:00
import org.mariotaku.ktextension.toTypedArray
2016-07-08 06:46:54 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
2016-12-03 16:45:13 +01:00
import org.mariotaku.twidere.adapter.AccountDetailsAdapter
2016-12-03 06:48:40 +01:00
import org.mariotaku.twidere.annotation.AccountType
2016-07-08 06:46:54 +02:00
import org.mariotaku.twidere.app.TwidereApplication
2017-02-28 08:34:00 +01:00
import org.mariotaku.twidere.extension.model.isOAuth
2016-07-16 09:27:46 +02:00
import org.mariotaku.twidere.model.UserKey
2016-12-03 16:45:13 +01:00
import org.mariotaku.twidere.model.util.AccountUtils
2016-12-15 06:11:32 +01:00
import org.mariotaku.twidere.util.DataStoreUtils
2016-07-08 06:46:54 +02:00
2017-02-04 09:51:01 +01:00
class AccountSelectorActivity : BaseActivity(), OnItemClickListener {
2016-07-08 06:46:54 +02:00
2016-12-08 15:56:21 +01:00
private lateinit var adapter: AccountDetailsAdapter
2016-07-08 06:46:54 +02:00
2017-02-15 07:48:30 +01:00
private val onlyIncludeKeys: Array<UserKey>?
2016-12-05 03:10:21 +01:00
get() {
return intent.getParcelableArrayExtra(EXTRA_ACCOUNT_KEYS)?.toTypedArray(UserKey.CREATOR)
2016-07-08 06:46:54 +02:00
}
2016-12-05 03:10:21 +01:00
private val isOAuthOnly: Boolean
get() {
return intent.getBooleanExtra(EXTRA_OAUTH_ONLY, false)
}
2016-07-08 06:46:54 +02:00
2016-12-16 16:21:50 +01:00
/**
* If not null, account selector will only show accounts matched this host.
*/
2016-12-05 03:10:21 +01:00
private val accountHost: String?
get() {
return intent.getStringExtra(EXTRA_ACCOUNT_HOST)
}
2016-07-08 06:46:54 +02:00
2016-12-05 03:10:21 +01:00
private val isSelectNoneAllowed: Boolean
get() {
return intent.getBooleanExtra(EXTRA_ALLOW_SELECT_NONE, false)
2016-07-08 06:46:54 +02:00
}
2016-12-05 03:10:21 +01:00
private val isSingleSelection: Boolean
get() {
2017-02-04 09:51:01 +01:00
return intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, true)
2016-12-05 03:10:21 +01:00
}
2016-12-16 16:21:50 +01:00
/**
* True if you want account picked automatically if there are only one match.
*/
2016-12-16 16:00:32 +01:00
private val isSelectOnlyItemAutomatically: Boolean
get() = intent.getBooleanExtra(EXTRA_SELECT_ONLY_ITEM_AUTOMATICALLY, false)
2016-12-05 03:10:21 +01:00
private val startIntent: Intent?
get() {
val startIntent = intent.getParcelableExtra<Intent>(EXTRA_START_INTENT)
startIntent?.setExtrasClassLoader(TwidereApplication::class.java.classLoader)
return startIntent
}
2016-07-08 06:46:54 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_account_selector)
2016-12-15 06:11:32 +01:00
DataStoreUtils.prepareDatabase(this)
2017-03-02 07:59:19 +01:00
adapter = AccountDetailsAdapter(this, Glide.with(this)).apply {
switchEnabled = !isSingleSelection
sortEnabled = false
2016-12-03 16:45:13 +01:00
val am = AccountManager.get(context)
2016-12-15 06:11:32 +01:00
val allAccountDetails = AccountUtils.getAllAccountDetails(am, AccountUtils.getAccounts(am), false)
2017-02-15 07:48:30 +01:00
val extraKeys = onlyIncludeKeys
2016-12-16 16:27:00 +01:00
val oauthOnly = isOAuthOnly
2016-12-05 03:10:21 +01:00
val accountHost = accountHost
2016-12-03 16:45:13 +01:00
addAll(allAccountDetails.filter {
2016-12-05 03:10:21 +01:00
if (extraKeys != null) {
return@filter extraKeys.contains(it.key)
}
2017-02-28 08:34:00 +01:00
if (oauthOnly && !it.isOAuth) {
2016-12-03 16:45:13 +01:00
return@filter false
}
if (USER_TYPE_TWITTER_COM == accountHost) {
2016-12-05 03:10:21 +01:00
if (it.key.host != null && it.type != AccountType.TWITTER) return@filter false
2016-12-03 16:45:13 +01:00
} else if (accountHost != null) {
if (accountHost != it.key.host) return@filter false
}
return@filter true
})
}
2016-07-08 06:46:54 +02:00
accountsList.choiceMode = if (isSingleSelection) ListView.CHOICE_MODE_NONE else ListView.CHOICE_MODE_MULTIPLE
if (isSingleSelection) {
accountsList.onItemClickListener = this
}
selectAccountButtons.visibility = if (isSingleSelection) View.GONE else View.VISIBLE
accountsList.adapter = adapter
2016-12-16 16:00:32 +01:00
if (adapter.count == 1 && isSelectOnlyItemAutomatically) {
selectSingleAccount(0)
}
2017-02-04 09:51:01 +01:00
confirmSelection.setOnClickListener {
val checkedIds = accountsList.checkedItemIds
if (checkedIds.isEmpty() && !isSelectNoneAllowed) {
Toast.makeText(this, R.string.message_toast_no_account_selected, Toast.LENGTH_SHORT).show()
return@setOnClickListener
2016-12-05 03:10:21 +01:00
}
2017-02-04 09:51:01 +01:00
val data = Intent()
data.putExtra(EXTRA_IDS, checkedIds)
data.putExtra(EXTRA_EXTRAS, intent.getBundleExtra(EXTRA_EXTRAS))
2017-02-04 09:51:01 +01:00
setResult(Activity.RESULT_OK, data)
finish()
2016-07-08 06:46:54 +02:00
}
2016-12-05 03:10:21 +01:00
}
2016-07-08 06:46:54 +02:00
2016-12-05 03:10:21 +01:00
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
selectSingleAccount(position)
}
2016-07-08 06:46:54 +02:00
2017-02-04 09:51:01 +01:00
private fun selectSingleAccount(position: Int) {
2016-12-05 03:10:21 +01:00
val account = adapter.getItem(position)
val data = Intent()
data.putExtra(EXTRA_ID, account.key.id)
data.putExtra(EXTRA_ACCOUNT_KEY, account.key)
data.putExtra(EXTRA_EXTRAS, intent.getBundleExtra(EXTRA_EXTRAS))
2016-07-08 06:46:54 +02:00
2016-12-05 03:10:21 +01:00
val startIntent = startIntent
if (startIntent != null) {
startIntent.putExtra(EXTRA_ACCOUNT_KEY, account.key)
startActivity(startIntent)
2016-07-08 06:46:54 +02:00
}
2016-12-05 03:10:21 +01:00
setResult(Activity.RESULT_OK, data)
finish()
2016-07-08 06:46:54 +02:00
}
}