Move OTP settings to preferences

This commit is contained in:
Milos Kozak 2020-03-20 17:31:30 +01:00
parent 4d88786fb6
commit a12762f034
12 changed files with 93 additions and 226 deletions

View file

@ -265,6 +265,13 @@
android:label="@string/pairing_information"
android:theme="@style/AppTheme" />
<activity android:name=".activities.RequestDexcomPermissionActivity" />
<activity android:name=".plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity">
<intent-filter>
<action android:name="info.nightscout.androidaps.plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Medtronic service and activities -->
<service

View file

@ -7,6 +7,7 @@ import info.nightscout.androidaps.activities.*
import info.nightscout.androidaps.historyBrowser.HistoryBrowseActivity
import info.nightscout.androidaps.plugins.general.maintenance.activities.LogSettingActivity
import info.nightscout.androidaps.plugins.general.overview.activities.QuickWizardListActivity
import info.nightscout.androidaps.plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity
import info.nightscout.androidaps.plugins.pump.common.dialog.RileyLinkBLEScanActivity
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog.RileyLinkStatusActivity
import info.nightscout.androidaps.plugins.pump.danaR.activities.DanaRHistoryActivity
@ -43,6 +44,7 @@ abstract class ActivitiesModule {
@ContributesAndroidInjector abstract fun contributesRileyLinkBLEScanActivity(): RileyLinkBLEScanActivity
@ContributesAndroidInjector abstract fun contributesSetupWizardActivity(): SetupWizardActivity
@ContributesAndroidInjector abstract fun contributesSingleFragmentActivity(): SingleFragmentActivity
@ContributesAndroidInjector abstract fun contributesSmsCommunicatorOtpActivity(): SmsCommunicatorOtpActivity
@ContributesAndroidInjector abstract fun contributesStatsActivity(): StatsActivity
@ContributesAndroidInjector abstract fun contributesSurveyActivity(): SurveyActivity
@ContributesAndroidInjector abstract fun contributesTDDStatsActivity(): TDDStatsActivity

View file

@ -27,8 +27,6 @@ import info.nightscout.androidaps.plugins.general.nsclient.NSClientFragment
import info.nightscout.androidaps.plugins.general.overview.OverviewFragment
import info.nightscout.androidaps.plugins.general.overview.dialogs.EditQuickWizardDialog
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorFragment
import info.nightscout.androidaps.plugins.general.smsCommunicator.fragments.SmsCommunicatorLogFragment
import info.nightscout.androidaps.plugins.general.smsCommunicator.fragments.SmsCommunicatorOtpFragment
import info.nightscout.androidaps.plugins.general.tidepool.TidepoolFragment
import info.nightscout.androidaps.plugins.insulin.InsulinFragment
import info.nightscout.androidaps.plugins.profile.local.LocalProfileFragment
@ -77,8 +75,6 @@ abstract class FragmentsModule {
@ContributesAndroidInjector abstract fun contributesNSProfileFragment(): NSProfileFragment
@ContributesAndroidInjector abstract fun contributesNSClientFragment(): NSClientFragment
@ContributesAndroidInjector abstract fun contributesSmsCommunicatorFragment(): SmsCommunicatorFragment
@ContributesAndroidInjector abstract fun contributesSmsCommunicatorLogFragment(): SmsCommunicatorLogFragment
@ContributesAndroidInjector abstract fun contributesSmsCommunicatorOtpFragment(): SmsCommunicatorOtpFragment
@ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsFragment(): TreatmentsFragment

View file

@ -4,78 +4,75 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.smsCommunicator.fragments.SmsCommunicatorLogFragment
import info.nightscout.androidaps.plugins.general.smsCommunicator.fragments.SmsCommunicatorOtpFragment
import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OneTimePassword
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.extensions.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.smscommunicator_fragment.*
import java.util.*
import javax.inject.Inject
import kotlin.math.max
class SmsCommunicatorFragment : DaggerFragment() {
@Inject lateinit var fabricPrivacy : FabricPrivacy
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var otp: OneTimePassword
@Inject lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin
private val disposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.smscommunicator_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
smscomunicator_tab_log.setOnClickListener {
setFragment(SmsCommunicatorLogFragment())
setBackgroundColorOnSelected(it)
}
smscomunicator_tab_otp.setOnClickListener {
setFragment(SmsCommunicatorOtpFragment())
setBackgroundColorOnSelected(it)
}
setFragment(SmsCommunicatorLogFragment())
setBackgroundColorOnSelected(smscomunicator_tab_log)
}
@Synchronized
override fun onResume() {
super.onResume()
disposable += rxBus
.toObservable(EventSmsCommunicatorUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { fabricPrivacy.logException(it) }
updateGui()
}
@Synchronized
override fun onPause() {
super.onPause()
disposable.clear()
}
fun updateGui() {
if (otp.isEnabled()) {
smscomunicator_tab_otp.visibility = View.VISIBLE
} else {
if (smscomunicator_tab_otp.visibility != View.GONE) {
setFragment(SmsCommunicatorLogFragment())
setBackgroundColorOnSelected(smscomunicator_tab_log)
smscomunicator_tab_otp.visibility = View.GONE
class CustomComparator : Comparator<Sms> {
override fun compare(object1: Sms, object2: Sms): Int {
return (object1.date - object2.date).toInt()
}
}
}
Collections.sort(smsCommunicatorPlugin.messages, CustomComparator())
val messagesToShow = 40
val start = max(0, smsCommunicatorPlugin.messages.size - messagesToShow)
var logText = ""
for (x in start until smsCommunicatorPlugin.messages.size) {
val sms = smsCommunicatorPlugin.messages[x]
when {
sms.ignored -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + "" + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
private fun setFragment(selectedFragment: Fragment) {
val ft = childFragmentManager.beginTransaction()
ft.replace(R.id.smscomunicator_fragment_container, selectedFragment)
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
ft.addToBackStack(null)
ft.commit()
}
sms.received -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
private fun setBackgroundColorOnSelected(selected: View) {
smscomunicator_tab_log.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
smscomunicator_tab_otp.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
selected.setBackgroundColor(resourceHelper.gc(R.color.tabBgColorSelected))
sms.sent -> {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
}
}
smscommunicator_log?.text = HtmlHelper.fromHtml(logText)
}
}

View file

@ -1,17 +1,14 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator.fragments
package info.nightscout.androidaps.plugins.general.smsCommunicator.activities
import android.app.Activity
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.common.primitives.Ints.min
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import dagger.android.support.DaggerFragment
import dagger.android.DaggerActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin
@ -21,27 +18,23 @@ import info.nightscout.androidaps.utils.OneTimePassword
import info.nightscout.androidaps.utils.OneTimePasswordValidationResult
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.resources.ResourceHelper
import kotlinx.android.synthetic.main.smscommunicator_fragment_otp.*
import kotlinx.android.synthetic.main.activity_smscommunicator_otp.*
import net.glxn.qrgen.android.QRCode
import javax.inject.Inject
class SmsCommunicatorOtpFragment : DaggerFragment() {
class SmsCommunicatorOtpActivity : DaggerActivity() {
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin
@Inject lateinit var otp: OneTimePassword
@Inject lateinit var resourceHelper: ResourceHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.smscommunicator_fragment_otp, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_smscommunicator_otp)
smscommunicator_otp_verify_edit.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
override fun afterTextChanged(s: Editable) {
if (s != null) {
val checkResult = otp.checkOTP(s.toString())
@ -65,26 +58,21 @@ class SmsCommunicatorOtpFragment : DaggerFragment() {
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})
actions_smscommunicator_otp_reset.setOnClickListener {
if (this.activity != null) {
OKDialog.showConfirmation(this.activity!!,
resourceHelper.gs(R.string.smscommunicator_otp_reset_title),
resourceHelper.gs(R.string.smscommunicator_otp_reset_prompt),
Runnable {
otp.ensureKey(true)
updateGui()
ToastUtils.showToastInUiThread(this.context, R.string.smscommunicator_otp_reset_successful)
})
}
OKDialog.showConfirmation(this,
resourceHelper.gs(R.string.smscommunicator_otp_reset_title),
resourceHelper.gs(R.string.smscommunicator_otp_reset_prompt),
Runnable {
otp.ensureKey(true)
updateGui()
ToastUtils.showToastInUiThread(this, R.string.smscommunicator_otp_reset_successful)
})
}
}
@Synchronized

View file

@ -1,80 +0,0 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.smsCommunicator.Sms
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin
import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.extensions.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.smscommunicator_fragment_log.*
import java.util.*
import javax.inject.Inject
import kotlin.math.max
class SmsCommunicatorLogFragment : DaggerFragment() {
@Inject lateinit var fabricPrivacy : FabricPrivacy
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin
private val disposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.smscommunicator_fragment_log, container, false)
}
@Synchronized
override fun onResume() {
super.onResume()
disposable += rxBus
.toObservable(EventSmsCommunicatorUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { fabricPrivacy.logException(it) }
updateGui()
}
@Synchronized
override fun onPause() {
super.onPause()
disposable.clear()
}
fun updateGui() {
class CustomComparator : Comparator<Sms> {
override fun compare(object1: Sms, object2: Sms): Int {
return (object1.date - object2.date).toInt()
}
}
Collections.sort(smsCommunicatorPlugin.messages, CustomComparator())
val messagesToShow = 40
val start = max(0, smsCommunicatorPlugin.messages.size - messagesToShow)
var logText = ""
for (x in start until smsCommunicatorPlugin.messages.size) {
val sms = smsCommunicatorPlugin.messages[x]
when {
sms.ignored -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + "" + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
sms.received -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
sms.sent -> {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
}
}
smscommunicator_log?.text = HtmlHelper.fromHtml(logText)
}
}

View file

@ -40,7 +40,7 @@ class OneTimePassword @Inject constructor(
* If OTP Authenticator support is enabled by user
*/
fun isEnabled(): Boolean {
return sp.getBoolean(R.string.key_smscommunicator_otp_enabled, false);
return sp.getBoolean(R.string.key_smscommunicator_otp_enabled, true);
}
/**

View file

@ -2,7 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.general.smsCommunicator.fragments.SmsCommunicatorOtpFragment">
tools:context=".plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity">
<ScrollView
android:layout_width="fill_parent"

View file

@ -4,48 +4,15 @@
android:layout_height="match_parent"
tools:context=".plugins.general.smsCommunicator.SmsCommunicatorFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/defaultbackground"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="center">
<TextView
android:id="@+id/smscomunicator_tab_log"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/smscomunicator_tab_log_label" />
<TextView
android:id="@+id/smscomunicator_tab_otp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/smscomunicator_tab_otp_label" />
</com.google.android.flexbox.FlexboxLayout>
<FrameLayout
android:id="@+id/smscomunicator_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/smscommunicator_log"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""/>
</ScrollView>
</FrameLayout>

View file

@ -1,19 +0,0 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.general.smsCommunicator.fragments.SmsCommunicatorLogFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/smscommunicator_log"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
</ScrollView>
</FrameLayout>

View file

@ -1720,8 +1720,7 @@
<string name="smscommunicator_otp_name">User name for Authenticator</string>
<string name="smscommunicator_otp_name_summary">User name displayed along with generated OTP on Authenticator App</string>
<string name="smscomunicator_tab_log_label">Operations Log</string>
<string name="smscomunicator_tab_otp_label">Authenticator</string>
<string name="smscomunicator_tab_otp_label">Authenticator setup</string>
<string name="smscommunicator_otp_verify_label">OTP to check:</string>
<string name="smscommunicator_otp_reset_btn">Reset Authenticators</string>
@ -1734,7 +1733,7 @@
<string name="smscommunicator_otp_step3_test_header">3. Test One-Time-Password</string>
<string name="smscommunicator_otp_reset_header">Reset Authenticators</string>
<string name="smscommunicator_otp_install_info">On each follower phone install Authenticator app that support RFC 6238 TOTP tokens. Popular free apps are:\n • Google Authenticator\n • LastPass Authenticator\n • FreeOTP Authenticator</string>
<string name="smscommunicator_otp_install_info">On each follower phone install Authenticator app that support RFC 6238 TOTP tokens. Popular free apps are:\n • Authy\n • Google Authenticator\n • LastPass Authenticator\n • FreeOTP Authenticator</string>
<string name="smscommunicator_otp_provisioning_warning">DO NOT SHARE this code online!\nUse it only to setup Authenticator App on follower phones.</string>
<string name="smscommunicator_otp_reset_warning">By reseting authenticator you make all already provisioned authenticators invalid. You will need to set up them again!</string>
</resources>

View file

@ -32,15 +32,25 @@
android:enabled="false"
android:key="@string/key_smscommunicator_otp_enabled"
android:summary="@string/smscommunicator_otp_enabled_summary"
android:title="@string/smscommunicator_otp_enabled" />
android:title="@string/smscommunicator_otp_enabled"
app:isPreferenceVisible="false" />
<Preference
android:dependency="@string/key_smscommunicator_remotecommandsallowed"
android:key="otpsetup"
android:title="@string/smscomunicator_tab_otp_label">
<intent android:action="info.nightscout.androidaps.plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity" />
</Preference>
<info.nightscout.androidaps.utils.textValidator.ValidatingEditTextPreference
android:dependency="@string/key_smscommunicator_remotecommandsallowed"
android:key="@string/key_smscommunicator_otp_password"
android:summary="@string/smscommunicator_otp_pin_summary"
android:title="@string/smscommunicator_otp_pin"
validate:testType="pinStrength" />
<info.nightscout.androidaps.utils.textValidator.ValidatingEditTextPreference
android:dependency="@string/key_smscommunicator_remotecommandsallowed"
android:key="@string/key_smscommunicator_otp_name"
android:summary="@string/smscommunicator_otp_name_summary"
android:title="@string/smscommunicator_otp_name"