kopia lustrzana https://github.com/ge0rg/aprsdroid
Passcode: refactor first run and passcode dialog, fix #298
rodzic
15118727e5
commit
a093047d89
|
@ -7,6 +7,7 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
<TextView
|
||||
android:id="@+id/fr_text"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="10sp"
|
||||
|
@ -17,6 +18,7 @@
|
|||
android:text="@string/fr_text"
|
||||
/>
|
||||
<TextView
|
||||
android:id="@+id/fr_text2"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="10sp"
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<PreferenceCategory
|
||||
android:title="@string/p_conn_aprsis">
|
||||
|
||||
<EditTextPreference
|
||||
<Preference
|
||||
android:key="passcode"
|
||||
android:inputType="number"
|
||||
android:title="@string/p_passcode"
|
||||
|
|
|
@ -19,10 +19,22 @@ class BackendPrefs extends PreferenceActivity
|
|||
val additional_xml = AprsBackend.prefxml_backend(prefs)
|
||||
if (additional_xml != 0) {
|
||||
addPreferencesFromResource(additional_xml)
|
||||
hookPasscode()
|
||||
hookGpsPermission()
|
||||
}
|
||||
}
|
||||
|
||||
def hookPasscode(): Unit = {
|
||||
val p = findPreference("passcode")
|
||||
if (p != null) {
|
||||
p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
def onPreferenceClick(preference: Preference) = {
|
||||
new PasscodeDialog(BackendPrefs.this, false).show()
|
||||
true
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
def hookGpsPermission(): Unit = {
|
||||
val p = findPreference("kenwood.gps")
|
||||
if (p != null) {
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
package org.aprsdroid.app
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.AlertDialog
|
||||
import android.content.{Context, DialogInterface, Intent}
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.EditText
|
||||
|
||||
class PasscodeDialog(act : Activity, firstrun : Boolean) extends AlertDialog(act)
|
||||
with DialogInterface.OnClickListener
|
||||
with DialogInterface.OnCancelListener
|
||||
with TextWatcher
|
||||
with View.OnFocusChangeListener
|
||||
{
|
||||
lazy val prefs = new PrefsWrapper(act)
|
||||
|
||||
val inflater = act.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
|
||||
.asInstanceOf[LayoutInflater]
|
||||
val fr_view = inflater.inflate(R.layout.firstrunview, null, false)
|
||||
val inputCall = fr_view.findViewById(R.id.callsign).asInstanceOf[EditText]
|
||||
val inputPass = fr_view.findViewById(R.id.passcode).asInstanceOf[EditText]
|
||||
lazy val okButton = getButton(DialogInterface.BUTTON_POSITIVE)
|
||||
var movedAwayFromCallsign = false
|
||||
|
||||
setTitle(act.getString(if (firstrun) R.string.fr_title else R.string.p_passcode_entry))
|
||||
if (!firstrun) {
|
||||
fr_view.findViewById(R.id.fr_text).asInstanceOf[View].setVisibility(View.GONE)
|
||||
fr_view.findViewById(R.id.fr_text2).asInstanceOf[View].setVisibility(View.GONE)
|
||||
}
|
||||
setView(fr_view)
|
||||
setIcon(android.R.drawable.ic_dialog_info)
|
||||
|
||||
inputCall.setText(prefs.getCallsign())
|
||||
inputCall.addTextChangedListener(this)
|
||||
inputCall.setOnFocusChangeListener(this)
|
||||
inputPass.setText(prefs.getString("passcode", ""))
|
||||
inputPass.addTextChangedListener(this)
|
||||
|
||||
setButton(DialogInterface.BUTTON_POSITIVE, act.getString(android.R.string.ok), this)
|
||||
setButton(DialogInterface.BUTTON_NEUTRAL, act.getString(R.string.p_passreq), this)
|
||||
setOnCancelListener(this)
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
if (inputCall.getText().toString() == "")
|
||||
okButton.setEnabled(false)
|
||||
if (!firstrun)
|
||||
inputPass.requestFocus()
|
||||
}
|
||||
|
||||
// DialogInterface.OnClickListener
|
||||
override def onClick(d : DialogInterface, which : Int) {
|
||||
which match {
|
||||
case DialogInterface.BUTTON_POSITIVE =>
|
||||
saveFirstRun(true)
|
||||
//TODO checkConfig()
|
||||
case DialogInterface.BUTTON_NEUTRAL =>
|
||||
saveFirstRun(false)
|
||||
act.startActivity(new Intent(Intent.ACTION_VIEW,
|
||||
Uri.parse(act.getString(R.string.passcode_url))))
|
||||
case _ =>
|
||||
cancelled()
|
||||
}
|
||||
}
|
||||
|
||||
// DialogInterface.OnCancelListener
|
||||
override def onCancel(d : DialogInterface) {
|
||||
cancelled()
|
||||
}
|
||||
|
||||
// TextWatcher
|
||||
override def afterTextChanged(s : Editable) {
|
||||
verifyInput()
|
||||
}
|
||||
override def beforeTextChanged(s : CharSequence, start : Int, before : Int, count : Int) {
|
||||
}
|
||||
override def onTextChanged(s : CharSequence, start : Int, before : Int, count : Int) {
|
||||
}
|
||||
|
||||
// OnFocusChangeListener
|
||||
override def onFocusChange(v: View, hasFocus : Boolean) {
|
||||
// only relevant for inputCall
|
||||
if (!hasFocus) {
|
||||
movedAwayFromCallsign = true
|
||||
verifyInput()
|
||||
}
|
||||
}
|
||||
|
||||
def verifyInput() {
|
||||
val call = inputCall.getText().toString()
|
||||
val pass = inputPass.getText().toString()
|
||||
val callError = if (call != "" || !movedAwayFromCallsign) null else act.getString(R.string.p_callsign_entry)
|
||||
val passOK = if (pass != "") AprsPacket.passcodeAllowed(call, pass, true) else true
|
||||
val passError = if (passOK) null else act.getString(R.string.wrongpasscode)
|
||||
inputCall.setError(callError)
|
||||
inputPass.setError(passError)
|
||||
okButton.setEnabled(call != "" && callError == null && passError == null)
|
||||
}
|
||||
def saveFirstRun(completed : Boolean) {
|
||||
val call = inputCall.getText().toString()
|
||||
val passcode = inputPass.getText().toString()
|
||||
val pe = prefs.prefs.edit()
|
||||
call.split("-") match {
|
||||
case Array(callsign) =>
|
||||
pe.putString("callsign", callsign)
|
||||
case Array(callsign, ssid) =>
|
||||
pe.putString("callsign", callsign)
|
||||
pe.putString("ssid", ssid)
|
||||
case _ =>
|
||||
Log.d("PasscodeDialog", "could not split callsign")
|
||||
act.finish()
|
||||
return
|
||||
}
|
||||
pe.putString("passcode", passcode)
|
||||
pe.putBoolean("firstrun", !completed)
|
||||
pe.commit()
|
||||
}
|
||||
|
||||
def cancelled() {
|
||||
if (firstrun) {
|
||||
Log.d("PasscodeDialog", "closing parent activity")
|
||||
act.finish()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ import _root_.android.content.res.Configuration
|
|||
import _root_.android.net.Uri
|
||||
import _root_.android.os.{Build, Environment}
|
||||
import _root_.android.util.Log
|
||||
import _root_.android.view.{ContextMenu, LayoutInflater, Menu, MenuItem, View, WindowManager}
|
||||
import _root_.android.view.{ContextMenu, Menu, MenuItem, View, WindowManager}
|
||||
import _root_.android.widget.AdapterView.AdapterContextMenuInfo
|
||||
import _root_.android.widget.{EditText, Toast}
|
||||
import java.io.{File, PrintWriter}
|
||||
|
@ -21,8 +21,7 @@ import android.provider.Settings
|
|||
trait UIHelper extends Activity
|
||||
with LoadingIndicator
|
||||
with PermissionHelper
|
||||
with DialogInterface.OnClickListener
|
||||
with DialogInterface.OnCancelListener {
|
||||
{
|
||||
|
||||
var menu_id : Int = -1
|
||||
lazy val prefs = new PrefsWrapper(this)
|
||||
|
@ -110,56 +109,8 @@ trait UIHelper extends Activity
|
|||
}
|
||||
}
|
||||
|
||||
def saveFirstRun(call : String, passcode : String) {
|
||||
val pe = prefs.prefs.edit()
|
||||
call.split("-") match {
|
||||
case Array(callsign) =>
|
||||
pe.putString("callsign", callsign)
|
||||
case Array(callsign, ssid) =>
|
||||
pe.putString("callsign", callsign)
|
||||
pe.putString("ssid", ssid)
|
||||
case _ =>
|
||||
Log.d("saveFirstRun", "could not split callsign")
|
||||
finish()
|
||||
return
|
||||
}
|
||||
if (passcode != "")
|
||||
pe.putString("passcode", passcode)
|
||||
pe.putBoolean("firstrun", false)
|
||||
pe.commit()
|
||||
}
|
||||
|
||||
def firstRunDialog() = {
|
||||
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE)
|
||||
.asInstanceOf[LayoutInflater]
|
||||
val fr_view = inflater.inflate(R.layout.firstrunview, null, false)
|
||||
val fr_call = fr_view.findViewById(R.id.callsign).asInstanceOf[EditText]
|
||||
val fr_pass = fr_view.findViewById(R.id.passcode).asInstanceOf[EditText]
|
||||
new AlertDialog.Builder(this).setTitle(getString(R.string.fr_title))
|
||||
.setView(fr_view)
|
||||
.setIcon(android.R.drawable.ic_dialog_info)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
override def onClick(d : DialogInterface, which : Int) {
|
||||
which match {
|
||||
case DialogInterface.BUTTON_POSITIVE =>
|
||||
saveFirstRun(fr_call.getText().toString(),
|
||||
fr_pass.getText().toString())
|
||||
checkConfig()
|
||||
case _ =>
|
||||
finish()
|
||||
}
|
||||
}})
|
||||
.setNeutralButton(R.string.p_passreq, new UrlOpener(this, getString(R.string.passcode_url)))
|
||||
.setOnCancelListener(this)
|
||||
.create.show
|
||||
}
|
||||
// DialogInterface.OnClickListener
|
||||
override def onClick(d : DialogInterface, which : Int) {
|
||||
finish()
|
||||
}
|
||||
// DialogInterface.OnCancelListener
|
||||
override def onCancel(d : DialogInterface) {
|
||||
finish()
|
||||
new PasscodeDialog(this, true).show()
|
||||
}
|
||||
|
||||
def setTitleStatus() {
|
||||
|
|
Ładowanie…
Reference in New Issue