proton-mail-android/app/src/main/java/ch/protonmail/android/activities/DefaultAddressActivity.kt

204 lines
7.8 KiB
Kotlin
Raw Normal View History

2020-04-16 15:44:53 +00:00
/*
2022-02-28 15:15:59 +00:00
* Copyright (c) 2022 Proton AG
*
2022-02-28 15:15:59 +00:00
* This file is part of Proton Mail.
*
2022-02-28 15:15:59 +00:00
* Proton Mail is free software: you can redistribute it and/or modify
2020-04-16 15:44:53 +00:00
* 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.
*
2022-02-28 15:15:59 +00:00
* Proton Mail is distributed in the hope that it will be useful,
2020-04-16 15:44:53 +00:00
* 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.
*
2020-04-16 15:44:53 +00:00
* You should have received a copy of the GNU General Public License
2022-02-28 15:15:59 +00:00
* along with Proton Mail. If not, see https://www.gnu.org/licenses/.
2020-04-16 15:44:53 +00:00
*/
package ch.protonmail.android.activities
import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
2021-06-01 08:09:20 +00:00
import android.widget.LinearLayout
2020-04-16 15:44:53 +00:00
import android.widget.RadioButton
2021-06-01 08:09:20 +00:00
import android.widget.RadioGroup
2020-04-16 15:44:53 +00:00
import android.widget.TextView
2021-06-01 08:09:20 +00:00
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isVisible
2020-04-16 15:44:53 +00:00
import butterknife.OnClick
import ch.protonmail.android.R
import ch.protonmail.android.core.ProtonMailApplication
import ch.protonmail.android.domain.entity.user.Address
import ch.protonmail.android.domain.entity.user.User
import ch.protonmail.android.domain.entity.user.isPaidUser
2020-04-16 15:44:53 +00:00
import ch.protonmail.android.jobs.UpdateSettingsJob
import ch.protonmail.android.utils.MessageUtils
import ch.protonmail.android.utils.extensions.showToast
class DefaultAddressActivity : BaseActivity() {
private var mInflater: LayoutInflater? = null
private lateinit var mAllRadioButtons: MutableList<RadioButton>
private var mAvailableAddressesMap: MutableMap<Address, RadioButton>? = null
private var mSelectedAddress: Address? = null
private var mUser: User? = null
private var mCurrentSelectedRadioButton: RadioButton? = null
2021-06-01 08:09:20 +00:00
private val addressChooser by lazy { findViewById<LinearLayout>(R.id.addressChooser) }
private val defaultAddress by lazy { findViewById<TextView>(R.id.defaultAddress) }
private val availableAddresses by lazy { findViewById<RadioGroup>(R.id.availableAddresses) }
private val inactiveAddresses by lazy { findViewById<LinearLayout>(R.id.inactiveAddresses) }
private val noAvailableAddresses by lazy { findViewById<TextView>(R.id.noAvailableAddresses) }
private val noInactiveAddresses by lazy { findViewById<TextView>(R.id.noInactiveAddresses) }
2020-04-16 15:44:53 +00:00
private val radioButtonClick = View.OnClickListener { v ->
val selectedAddressRadioButton = v as RadioButton
for ((address, radioButton) in mAvailableAddressesMap!!) {
2020-04-16 15:44:53 +00:00
if (radioButton.id == selectedAddressRadioButton.id) {
// this is selected
if (MessageUtils.isPmMeAddress(address.email.s) && mUser?.isPaidUser() == false) {
mCurrentSelectedRadioButton?.isChecked = true
showToast(String.format(getString(R.string.pm_me_can_not_be_default), address.email.s))
2020-04-16 15:44:53 +00:00
return@OnClickListener
}
mSelectedAddress = address
2020-04-16 15:44:53 +00:00
mCurrentSelectedRadioButton = selectedAddressRadioButton
clearSelection()
selectedAddressRadioButton.isChecked = true
defaultAddress.text = mSelectedAddress?.email?.s
2020-04-16 15:44:53 +00:00
val user = mUserManager.currentUser
2021-04-16 21:19:03 +00:00
val selectedAddress = mSelectedAddress
if (selectedAddress != null && user?.addresses?.primary?.id?.id != selectedAddress.id.id) {
2021-04-16 21:19:03 +00:00
// Add first the selected address.
val addressIds = mutableSetOf(selectedAddress.id.id)
2021-04-16 21:19:03 +00:00
// Add all other.
user?.addresses?.addresses?.forEach { addressIds.add(it.value.id.id) }
2021-04-16 21:19:03 +00:00
//
val job = UpdateSettingsJob(addressIds = addressIds.toList())
2020-04-16 15:44:53 +00:00
mJobManager.addJobInBackground(job)
}
break
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val actionBar = supportActionBar
actionBar?.setDisplayHomeAsUpEnabled(true)
2021-06-01 08:09:20 +00:00
val elevation = resources.getDimension(R.dimen.action_bar_elevation)
2021-06-01 08:09:20 +00:00
actionBar?.elevation = elevation
2020-04-16 15:44:53 +00:00
mAvailableAddressesMap = HashMap()
mAllRadioButtons = ArrayList()
mUser = mUserManager.currentUser
val addresses = mUser?.addresses?.addresses
2020-04-16 15:44:53 +00:00
mInflater = LayoutInflater.from(this)
renderAddresses(addresses)
2020-04-16 15:44:53 +00:00
}
override fun onStart() {
super.onStart()
ProtonMailApplication.getApplication().bus.register(this)
}
override fun onStop() {
super.onStop()
ProtonMailApplication.getApplication().bus.unregister(this)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val itemId = item.itemId
if (itemId == android.R.id.home) {
saveAndFinish()
return true
}
return super.onOptionsItemSelected(item)
}
override fun onBackPressed() {
saveAndFinish()
}
2021-06-01 08:09:20 +00:00
override fun getLayoutId() = R.layout.activity_default_address
2020-04-16 15:44:53 +00:00
2021-06-01 08:09:20 +00:00
@OnClick(R.id.defaultAddress)
2020-04-16 15:44:53 +00:00
fun onDefaultAddressClicked() {
2021-06-01 08:09:20 +00:00
val icon = ResourcesCompat.getDrawable(
resources, if (!addressChooser.isVisible) R.drawable.ic_chevron_down else R.drawable.ic_chevron_up, null
)?.mutate()
defaultAddress.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, icon, null)
addressChooser.isVisible = !addressChooser.isVisible
2020-04-16 15:44:53 +00:00
}
private fun clearSelection() {
availableAddresses.clearCheck()
for (radioButton in mAllRadioButtons) {
radioButton.isChecked = false
}
}
private fun renderAddresses(addresses: Map<Int, Address>?) {
mSelectedAddress = addresses?.values?.elementAt(0)
defaultAddress.text = mSelectedAddress?.email?.s
2020-04-16 15:44:53 +00:00
var mNoAvailableAddresses = true
var mNoInactiveAddresses = true
2021-06-01 08:09:20 +00:00
addresses?.values?.forEachIndexed { index, address ->
val aliasAvailable = address.enabled && address.allowedToReceive
2020-04-16 15:44:53 +00:00
var addressRadio: RadioButton? = null
var inactiveAddress: TextView? = null
if (aliasAvailable) {
addressRadio = mInflater?.inflate(
R.layout.radio_button_list_item,
availableAddresses,
false
) as RadioButton
addressRadio.text = address.email.s
addressRadio.isChecked = index == 0
if (index == 0) {
2020-04-16 15:44:53 +00:00
mCurrentSelectedRadioButton = addressRadio
}
mAllRadioButtons.add(addressRadio)
addressRadio.setOnClickListener(radioButtonClick)
addressRadio.id = View.generateViewId()
mAvailableAddressesMap!![address] = addressRadio
} else {
2021-06-01 08:09:20 +00:00
inactiveAddress =
mInflater?.inflate(R.layout.alias_list_item_inactive, inactiveAddresses, false) as TextView
inactiveAddress.text = address.email.s
2020-04-16 15:44:53 +00:00
}
if (aliasAvailable) {
mNoAvailableAddresses = false
availableAddresses.addView(addressRadio)
} else {
mNoInactiveAddresses = false
inactiveAddresses?.addView(inactiveAddress)
2020-04-16 15:44:53 +00:00
}
}
if (mNoAvailableAddresses) {
noAvailableAddresses.visibility = View.VISIBLE
}
if (mNoInactiveAddresses) {
noInactiveAddresses.visibility = View.VISIBLE
}
}
private fun saveAndFinish() {
setResult(Activity.RESULT_OK)
finish()
}
}