2017-01-25 13:08:23 +01:00
|
|
|
package org.mariotaku.twidere.fragment
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.os.Bundle
|
2020-01-26 08:35:15 +01:00
|
|
|
import androidx.loader.app.LoaderManager
|
|
|
|
import androidx.loader.content.Loader
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
2017-01-25 13:08:23 +01:00
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.*
|
2017-02-04 11:42:14 +01:00
|
|
|
import com.rengwuxian.materialedittext.MaterialEditText
|
2017-01-25 13:08:23 +01:00
|
|
|
import org.mariotaku.twidere.R
|
|
|
|
import org.mariotaku.twidere.adapter.ArrayAdapter
|
|
|
|
import org.mariotaku.twidere.adapter.BaseArrayAdapter
|
|
|
|
import org.mariotaku.twidere.annotation.AccountType
|
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_API_CONFIG
|
|
|
|
import org.mariotaku.twidere.constant.defaultAPIConfigKey
|
2017-02-05 14:42:20 +01:00
|
|
|
import org.mariotaku.twidere.extension.applyTheme
|
2017-06-19 15:45:41 +02:00
|
|
|
import org.mariotaku.twidere.extension.onShow
|
2017-01-25 13:08:23 +01:00
|
|
|
import org.mariotaku.twidere.extension.setSelectedItem
|
2017-04-18 11:56:59 +02:00
|
|
|
import org.mariotaku.twidere.loader.DefaultAPIConfigLoader
|
2017-01-25 13:08:23 +01:00
|
|
|
import org.mariotaku.twidere.model.CustomAPIConfig
|
|
|
|
import org.mariotaku.twidere.model.account.cred.Credentials
|
|
|
|
import org.mariotaku.twidere.util.ParseUtils
|
2017-02-04 11:42:14 +01:00
|
|
|
import org.mariotaku.twidere.util.view.ConsumerKeySecretValidator
|
2017-01-25 13:08:23 +01:00
|
|
|
|
|
|
|
class APIEditorDialogFragment : BaseDialogFragment() {
|
|
|
|
|
2020-01-26 08:35:15 +01:00
|
|
|
private val loadDefaults by lazy { dialog!!.findViewById<View>(R.id.loadDefaults) }
|
|
|
|
private val editAPIUrlFormat by lazy { dialog!!.findViewById<EditText>(R.id.editApiUrlFormat) }
|
|
|
|
private val editSameOAuthSigningUrl by lazy { dialog!!.findViewById<CheckBox>(R.id.editSameOAuthSigningUrl) }
|
|
|
|
private val editNoVersionSuffix by lazy { dialog!!.findViewById<CheckBox>(R.id.editNoVersionSuffix) }
|
|
|
|
private val editConsumerKey by lazy { dialog!!.findViewById<MaterialEditText>(R.id.editConsumerKey) }
|
|
|
|
private val editConsumerSecret by lazy { dialog!!.findViewById<MaterialEditText>(R.id.editConsumerSecret) }
|
|
|
|
private val editAuthType by lazy { dialog!!.findViewById<RadioGroup>(R.id.editAuthType) }
|
|
|
|
private val apiFormatHelpButton by lazy { dialog!!.findViewById<View>(R.id.apiUrlFormatHelp) }
|
|
|
|
private val accountTypeSpinner by lazy { dialog!!.findViewById<Spinner>(R.id.accountTypeSpinner) }
|
2017-01-25 13:08:23 +01:00
|
|
|
|
|
|
|
private var editNoVersionSuffixChanged: Boolean = false
|
|
|
|
private lateinit var apiConfig: CustomAPIConfig
|
|
|
|
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
2020-05-31 07:54:57 +02:00
|
|
|
val builder = AlertDialog.Builder(requireContext())
|
2017-02-04 11:42:14 +01:00
|
|
|
builder.setView(R.layout.dialog_api_editor)
|
2017-03-21 14:36:06 +01:00
|
|
|
builder.setPositiveButton(R.string.action_save) { _, _ ->
|
2017-01-25 13:08:23 +01:00
|
|
|
val targetFragment = this.targetFragment
|
|
|
|
val parentFragment = this.parentFragment
|
|
|
|
val host = this.host
|
|
|
|
if (targetFragment is APIEditorCallback) {
|
|
|
|
targetFragment.onSaveAPIConfig(applyCustomAPIConfig())
|
|
|
|
} else if (parentFragment is APIEditorCallback) {
|
|
|
|
parentFragment.onSaveAPIConfig(applyCustomAPIConfig())
|
|
|
|
} else if (host is APIEditorCallback) {
|
|
|
|
host.onSaveAPIConfig(applyCustomAPIConfig())
|
|
|
|
} else {
|
|
|
|
kPreferences[defaultAPIConfigKey] = applyCustomAPIConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
|
|
|
|
val dialog = builder.create()
|
2017-06-19 15:45:41 +02:00
|
|
|
dialog.onShow {
|
2017-02-05 14:42:20 +01:00
|
|
|
it.applyTheme()
|
2019-10-24 17:52:11 +02:00
|
|
|
if (arguments?.getBoolean(EXTRA_SHOW_LOAD_DEFAULTS) == true) {
|
2017-01-25 13:08:23 +01:00
|
|
|
loadDefaults.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
loadDefaults.visibility = View.GONE
|
|
|
|
}
|
|
|
|
loadDefaults.setOnClickListener {
|
|
|
|
val df = LoadDefaultsChooserDialogFragment()
|
|
|
|
df.show(childFragmentManager, "load_defaults")
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:12:25 +01:00
|
|
|
accountTypeSpinner.adapter = AccountTypeSpinnerAdapter(this)
|
2017-02-04 11:42:14 +01:00
|
|
|
|
2020-05-31 07:54:57 +02:00
|
|
|
editConsumerKey.addValidator(ConsumerKeySecretValidator(requireContext().getString(R.string.invalid_consumer_key)))
|
|
|
|
editConsumerSecret.addValidator(ConsumerKeySecretValidator(requireContext().getString(R.string.invalid_consumer_secret)))
|
2017-02-04 11:42:14 +01:00
|
|
|
|
2017-03-21 14:36:06 +01:00
|
|
|
editNoVersionSuffix.setOnCheckedChangeListener { _, _ -> editNoVersionSuffixChanged = true }
|
|
|
|
editAuthType.setOnCheckedChangeListener { _, checkedId ->
|
2017-01-25 13:08:23 +01:00
|
|
|
val authType = getCheckedAuthType(checkedId)
|
|
|
|
val isOAuth = Credentials.Type.OAUTH == authType || Credentials.Type.XAUTH == authType
|
|
|
|
editSameOAuthSigningUrl.visibility = if (isOAuth) View.VISIBLE else View.GONE
|
|
|
|
editConsumerKey.visibility = if (isOAuth) View.VISIBLE else View.GONE
|
|
|
|
editConsumerSecret.visibility = if (isOAuth) View.VISIBLE else View.GONE
|
|
|
|
if (!editNoVersionSuffixChanged) {
|
|
|
|
editNoVersionSuffix.isChecked = Credentials.Type.EMPTY == authType
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 14:28:43 +01:00
|
|
|
apiFormatHelpButton.setOnClickListener {
|
|
|
|
MessageDialogFragment.show(childFragmentManager, message = getString(R.string.message_api_url_format_help),
|
|
|
|
tag = "api_url_format_help")
|
|
|
|
}
|
2017-01-25 13:08:23 +01:00
|
|
|
|
2020-06-08 23:19:10 +02:00
|
|
|
apiConfig = if (savedInstanceState != null) {
|
|
|
|
savedInstanceState.getParcelable(EXTRA_API_CONFIG)!!
|
2017-01-25 13:08:23 +01:00
|
|
|
} else {
|
2020-06-08 23:19:10 +02:00
|
|
|
arguments?.getParcelable(EXTRA_API_CONFIG) ?: kPreferences[defaultAPIConfigKey]
|
2017-01-25 13:08:23 +01:00
|
|
|
}
|
|
|
|
displayCustomApiConfig()
|
|
|
|
}
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
outState.putParcelable(EXTRA_API_CONFIG, applyCustomAPIConfig())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun applyCustomAPIConfig(): CustomAPIConfig {
|
|
|
|
return apiConfig.apply {
|
|
|
|
apiUrlFormat = ParseUtils.parseString(editAPIUrlFormat.text)
|
|
|
|
credentialsType = getCheckedAuthType(editAuthType.checkedRadioButtonId)
|
|
|
|
consumerKey = ParseUtils.parseString(editConsumerKey.text)
|
|
|
|
consumerSecret = ParseUtils.parseString(editConsumerSecret.text)
|
|
|
|
isSameOAuthUrl = editSameOAuthSigningUrl.isChecked
|
|
|
|
isNoVersionSuffix = editNoVersionSuffix.isChecked
|
|
|
|
type = accountTypeSpinner.selectedItem as String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun displayCustomApiConfig() {
|
|
|
|
editAPIUrlFormat.setText(apiConfig.apiUrlFormat)
|
|
|
|
editSameOAuthSigningUrl.isChecked = apiConfig.isSameOAuthUrl
|
|
|
|
editNoVersionSuffix.isChecked = apiConfig.isNoVersionSuffix
|
|
|
|
editConsumerKey.setText(apiConfig.consumerKey)
|
|
|
|
editConsumerSecret.setText(apiConfig.consumerSecret)
|
|
|
|
editAuthType.check(getAuthTypeId(apiConfig.credentialsType))
|
|
|
|
if (editAuthType.checkedRadioButtonId == -1) {
|
|
|
|
editAuthType.check(R.id.oauth)
|
|
|
|
}
|
|
|
|
accountTypeSpinner.setSelectedItem(apiConfig.type)
|
|
|
|
}
|
|
|
|
|
|
|
|
interface APIEditorCallback {
|
|
|
|
fun onSaveAPIConfig(config: CustomAPIConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LoadDefaultsChooserDialogFragment : BaseDialogFragment(), DialogInterface.OnClickListener,
|
2017-04-18 11:56:59 +02:00
|
|
|
LoaderManager.LoaderCallbacks<List<CustomAPIConfig>> {
|
2017-01-25 13:08:23 +01:00
|
|
|
private lateinit var adapter: ArrayAdapter<CustomAPIConfig>
|
|
|
|
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
2020-05-31 07:54:57 +02:00
|
|
|
adapter = CustomAPIConfigArrayAdapter(requireContext())
|
|
|
|
val builder = AlertDialog.Builder(requireContext())
|
2017-01-25 13:08:23 +01:00
|
|
|
builder.setAdapter(adapter, this)
|
2017-01-26 16:15:05 +01:00
|
|
|
loaderManager.initLoader(0, null, this)
|
2017-02-05 14:42:20 +01:00
|
|
|
val dialog = builder.create()
|
2017-06-19 15:45:41 +02:00
|
|
|
dialog.onShow { it.applyTheme() }
|
2017-02-05 14:42:20 +01:00
|
|
|
return dialog
|
2017-01-25 13:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(dialog: DialogInterface, which: Int) {
|
|
|
|
val fragment = parentFragment as APIEditorDialogFragment
|
|
|
|
fragment.apiConfig = adapter.getItem(which)
|
|
|
|
fragment.displayCustomApiConfig()
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:56:59 +02:00
|
|
|
override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<CustomAPIConfig>> {
|
2020-05-31 07:54:57 +02:00
|
|
|
return DefaultAPIConfigLoader(requireContext())
|
2017-01-25 13:08:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:56:59 +02:00
|
|
|
override fun onLoadFinished(loader: Loader<List<CustomAPIConfig>>, data: List<CustomAPIConfig>) {
|
2017-01-25 13:08:23 +01:00
|
|
|
adapter.clear()
|
|
|
|
adapter.addAll(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<List<CustomAPIConfig>?>) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:56:59 +02:00
|
|
|
private class CustomAPIConfigArrayAdapter(context: Context) :
|
|
|
|
ArrayAdapter<CustomAPIConfig>(context, android.R.layout.simple_list_item_1) {
|
2017-01-25 13:08:23 +01:00
|
|
|
|
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
|
|
|
val view = super.getView(position, convertView, parent)
|
|
|
|
val type = getItem(position)
|
2017-06-19 06:11:28 +02:00
|
|
|
view.findViewById<TextView>(android.R.id.text1).text = type.getLocalizedName(context)
|
2017-01-25 13:08:23 +01:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:12:25 +01:00
|
|
|
private class AccountTypeSpinnerAdapter(
|
|
|
|
fragment: APIEditorDialogFragment
|
2020-05-31 07:54:57 +02:00
|
|
|
) : BaseArrayAdapter<String>(fragment.requireContext(), R.layout.support_simple_spinner_dropdown_item,
|
2017-09-03 15:23:45 +02:00
|
|
|
requestManager = fragment.requestManager) {
|
2017-01-25 13:08:23 +01:00
|
|
|
init {
|
|
|
|
add(AccountType.TWITTER)
|
|
|
|
add(AccountType.FANFOU)
|
2017-04-18 11:56:59 +02:00
|
|
|
add(AccountType.MASTODON)
|
2017-01-25 13:08:23 +01:00
|
|
|
add(AccountType.STATUSNET)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
|
|
|
val view = super.getView(position, convertView, parent)
|
2017-06-19 06:11:28 +02:00
|
|
|
val textView = view.findViewById<TextView>(android.R.id.text1)
|
2017-04-18 11:56:59 +02:00
|
|
|
textView.text = getTypeTitle(context, getItem(position))
|
2017-01-25 13:08:23 +01:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
|
|
|
val view = super.getDropDownView(position, convertView, parent)
|
2017-06-19 06:11:28 +02:00
|
|
|
val textView = view.findViewById<TextView>(android.R.id.text1)
|
2017-04-18 11:56:59 +02:00
|
|
|
textView.text = getTypeTitle(context, getItem(position))
|
2017-01-25 13:08:23 +01:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
const val EXTRA_SHOW_LOAD_DEFAULTS = "show_load_defaults"
|
|
|
|
|
|
|
|
@Credentials.Type
|
|
|
|
fun getCheckedAuthType(checkedId: Int): String {
|
|
|
|
when (checkedId) {
|
|
|
|
R.id.xauth -> {
|
|
|
|
return Credentials.Type.XAUTH
|
|
|
|
}
|
|
|
|
R.id.basic -> {
|
|
|
|
return Credentials.Type.BASIC
|
|
|
|
}
|
|
|
|
R.id.twipO -> {
|
|
|
|
return Credentials.Type.EMPTY
|
|
|
|
}
|
2017-04-18 11:56:59 +02:00
|
|
|
R.id.oauth2 -> {
|
|
|
|
return Credentials.Type.OAUTH2
|
|
|
|
}
|
2017-01-25 13:08:23 +01:00
|
|
|
else -> {
|
|
|
|
return Credentials.Type.OAUTH
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getAuthTypeId(authType: String): Int {
|
|
|
|
when (authType) {
|
|
|
|
Credentials.Type.XAUTH -> {
|
|
|
|
return R.id.xauth
|
|
|
|
}
|
|
|
|
Credentials.Type.BASIC -> {
|
|
|
|
return R.id.basic
|
|
|
|
}
|
|
|
|
Credentials.Type.EMPTY -> {
|
|
|
|
return R.id.twipO
|
|
|
|
}
|
2017-04-18 11:56:59 +02:00
|
|
|
Credentials.Type.OAUTH2 -> {
|
|
|
|
return R.id.oauth2
|
|
|
|
}
|
2017-01-25 13:08:23 +01:00
|
|
|
else -> {
|
|
|
|
return R.id.oauth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 11:56:59 +02:00
|
|
|
|
|
|
|
fun getTypeTitle(context: Context, @AccountType type: String): CharSequence {
|
|
|
|
return when (type) {
|
|
|
|
AccountType.TWITTER -> context.getString(R.string.account_type_twitter)
|
|
|
|
AccountType.FANFOU -> context.getString(R.string.account_type_fanfou)
|
|
|
|
AccountType.MASTODON -> context.getString(R.string.account_type_mastodon)
|
|
|
|
AccountType.STATUSNET -> context.getString(R.string.account_type_statusnet)
|
|
|
|
else -> throw UnsupportedOperationException(type)
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 13:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|