-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[settings] allow to change cloud password
- Loading branch information
Showing
13 changed files
with
315 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/me/capcom/smsgateway/ui/settings/BasePreferenceFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package me.capcom.smsgateway.ui.settings | ||
|
||
import android.os.Bundle | ||
import android.util.TypedValue | ||
import android.view.View | ||
import android.widget.Toast | ||
import androidx.preference.PreferenceFragmentCompat | ||
|
||
abstract class BasePreferenceFragment : PreferenceFragmentCompat() { | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
val backgroundValue = TypedValue() | ||
requireContext().theme.resolveAttribute( | ||
android.R.attr.colorBackground, | ||
backgroundValue, | ||
true | ||
) | ||
|
||
view.setBackgroundColor(backgroundValue.data) | ||
} | ||
|
||
protected fun showToast(message: String) { | ||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
app/src/main/java/me/capcom/smsgateway/ui/settings/CloudServerSettingsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package me.capcom.smsgateway.ui.settings | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import android.text.InputType | ||
import android.view.View | ||
import androidx.core.content.edit | ||
import androidx.core.view.isVisible | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.preference.EditTextPreference | ||
import androidx.preference.Preference | ||
import kotlinx.coroutines.launch | ||
import me.capcom.smsgateway.R | ||
import me.capcom.smsgateway.modules.gateway.GatewayService | ||
import me.capcom.smsgateway.modules.gateway.GatewaySettings | ||
import org.koin.android.ext.android.inject | ||
import java.net.URL | ||
|
||
class CloudServerSettingsFragment : BasePreferenceFragment() { | ||
|
||
private val settings: GatewaySettings by inject() | ||
private val service: GatewayService by inject() | ||
|
||
@SuppressLint("NotifyDataSetChanged") | ||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.cloud_server_preferences, rootKey) | ||
|
||
findPreference<EditTextPreference>("gateway.cloud_url")?.setSummaryProvider { | ||
val hostname = preferenceManager.sharedPreferences?.getString(it.key, null) | ||
if (hostname.isNullOrEmpty()) { | ||
preferenceManager.sharedPreferences?.edit(true) { | ||
putString(it.key, GatewaySettings.PUBLIC_URL) | ||
} | ||
return@setSummaryProvider GatewaySettings.PUBLIC_URL | ||
} | ||
return@setSummaryProvider hostname | ||
} | ||
|
||
|
||
findPreference<EditTextPreference>("gateway.cloud_url")?.setOnPreferenceChangeListener { _, newValue -> | ||
val value = newValue as? String | ||
if (value.isNullOrEmpty()) { | ||
return@setOnPreferenceChangeListener true | ||
} | ||
|
||
try { | ||
URL(value) | ||
} catch (e: Exception) { | ||
showToast(getString(R.string.invalid_url)) | ||
return@setOnPreferenceChangeListener false | ||
} | ||
|
||
true | ||
} | ||
|
||
findPreference<EditTextPreference>("gateway.username")?.setSummaryProvider { | ||
settings.username ?: getString(R.string.not_set) | ||
} | ||
findPreference<EditTextPreference>("gateway.password")?.apply { | ||
setSummaryProvider { | ||
settings.password ?: getString(R.string.not_set) | ||
} | ||
|
||
setOnPreferenceChangeListener { _, newValue -> | ||
val value = newValue as? String | ||
if (value == null || value.length < 14) { | ||
showToast(getString(R.string.password_must_be_at_least_14_characters)) | ||
return@setOnPreferenceChangeListener false | ||
} | ||
|
||
this@CloudServerSettingsFragment.lifecycleScope.launch { | ||
try { | ||
requireActivity().findViewById<View>(R.id.progressBar).isVisible = true | ||
service.changePassword(settings.password ?: "", value) | ||
listView.adapter?.notifyDataSetChanged() | ||
showToast(getString(R.string.password_changed_successfully)) | ||
} catch (e: Exception) { | ||
showToast(getString(R.string.failed_to_change_password, e.message)) | ||
} finally { | ||
requireActivity().findViewById<View>(R.id.progressBar).isVisible = false | ||
} | ||
} | ||
|
||
true | ||
} | ||
} | ||
} | ||
|
||
override fun onDisplayPreferenceDialog(preference: Preference) { | ||
if (preference.key == "gateway.cloud_url") { | ||
(preference as EditTextPreference).setOnBindEditTextListener { | ||
it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI | ||
it.setSelectAllOnFocus(true) | ||
it.selectAll() | ||
} | ||
} | ||
|
||
if (preference.key == "gateway.private_token") { | ||
(preference as EditTextPreference).setOnBindEditTextListener { | ||
it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD | ||
it.setSelectAllOnFocus(true) | ||
it.selectAll() | ||
} | ||
} | ||
|
||
if (preference.key == "gateway.password") { | ||
(preference as EditTextPreference).setOnBindEditTextListener { | ||
it.inputType = InputType.TYPE_CLASS_TEXT | ||
it.text = null | ||
} | ||
} | ||
|
||
super.onDisplayPreferenceDialog(preference) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.