|
| 1 | +/* |
| 2 | + * This file is part of BOINC. |
| 3 | + * https://boinc.berkeley.edu |
| 4 | + * Copyright (C) 2025 University of California |
| 5 | + * |
| 6 | + * BOINC is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License |
| 8 | + * as published by the Free Software Foundation, |
| 9 | + * either version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * BOINC is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with BOINC. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package edu.berkeley.boinc.attach |
| 20 | + |
| 21 | +import android.content.ComponentName |
| 22 | +import android.content.Intent |
| 23 | +import android.content.ServiceConnection |
| 24 | +import android.os.Bundle |
| 25 | +import android.os.IBinder |
| 26 | +import android.text.InputType |
| 27 | +import android.text.method.PasswordTransformationMethod |
| 28 | +import android.view.View |
| 29 | +import android.widget.CheckBox |
| 30 | +import androidx.appcompat.app.AppCompatActivity |
| 31 | +import edu.berkeley.boinc.attach.BatchProcessingActivity |
| 32 | +import edu.berkeley.boinc.attach.ProjectAttachService |
| 33 | +import edu.berkeley.boinc.attach.ProjectAttachService.LocalBinder |
| 34 | +import edu.berkeley.boinc.databinding.AttachProjectCredentialInputLayoutBinding |
| 35 | +import edu.berkeley.boinc.utils.Logging.Category.GUI_ACTIVITY |
| 36 | +import edu.berkeley.boinc.utils.Logging.Category.USER_ACTION |
| 37 | +import edu.berkeley.boinc.utils.Logging.logError |
| 38 | +import edu.berkeley.boinc.utils.Logging.logVerbose |
| 39 | +import edu.berkeley.boinc.utils.Logging.logWarning |
| 40 | + |
| 41 | +class CredentialInputActivity : AppCompatActivity() { |
| 42 | + private var binding: AttachProjectCredentialInputLayoutBinding? = null |
| 43 | + |
| 44 | + private var attachService: ProjectAttachService? = null |
| 45 | + private var asIsBound = false |
| 46 | + |
| 47 | + public override fun onCreate(savedInstanceState: Bundle?) { |
| 48 | + super.onCreate(savedInstanceState) |
| 49 | + |
| 50 | + logVerbose(GUI_ACTIVITY, "CredentialInputActivity onCreate") |
| 51 | + |
| 52 | + doBindService() |
| 53 | + binding = AttachProjectCredentialInputLayoutBinding.inflate( |
| 54 | + layoutInflater |
| 55 | + ) |
| 56 | + setContentView(binding!!.root) |
| 57 | + |
| 58 | + binding!!.showPwdCb.setOnClickListener { view: View -> |
| 59 | + if ((view as CheckBox).isChecked) { |
| 60 | + binding!!.pwdInput.inputType = |
| 61 | + InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
| 62 | + } else { |
| 63 | + binding!!.pwdInput.inputType = |
| 64 | + InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD |
| 65 | + binding!!.pwdInput.transformationMethod = PasswordTransformationMethod.getInstance() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + override fun onDestroy() { |
| 71 | + doUnbindService() |
| 72 | + super.onDestroy() |
| 73 | + } |
| 74 | + |
| 75 | + // triggered by continue button |
| 76 | + fun continueClicked(@Suppress("UNUSED_PARAMETER") v: View?) { |
| 77 | + logVerbose(USER_ACTION, "CredentialInputActivity.continueClicked.") |
| 78 | + |
| 79 | + // set credentials in service |
| 80 | + if (asIsBound) { |
| 81 | + // verify input and set credentials if valid. |
| 82 | + val email = binding!!.emailInput.text.toString() |
| 83 | + val name = binding!!.nameInput.text.toString() |
| 84 | + val password = binding!!.pwdInput.text.toString() |
| 85 | + if (attachService!!.verifyInput(email, name, password)) { |
| 86 | + attachService!!.setCredentials(email, name, password) |
| 87 | + } else { |
| 88 | + logWarning( |
| 89 | + USER_ACTION, |
| 90 | + "CredentialInputActivity.continueClicked: empty credentials found" |
| 91 | + ) |
| 92 | + |
| 93 | + return |
| 94 | + } |
| 95 | + } else { |
| 96 | + logError(GUI_ACTIVITY, "CredentialInputActivity.continueClicked: service not bound.") |
| 97 | + |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + logVerbose( |
| 102 | + USER_ACTION, |
| 103 | + "CredentialInputActivity.continueClicked: starting BatchProcessingActivity..." |
| 104 | + ) |
| 105 | + |
| 106 | + startActivity(Intent(this, BatchProcessingActivity::class.java)) |
| 107 | + } |
| 108 | + |
| 109 | + // triggered by individual button |
| 110 | + fun individualClicked(@Suppress("UNUSED_PARAMETER") v: View?) { |
| 111 | + logVerbose(USER_ACTION, "CredentialInputActivity.individualClicked.") |
| 112 | + |
| 113 | + // set credentials in service, in case user typed before deciding btwn batch and individual attach |
| 114 | + if (asIsBound) { |
| 115 | + val email = binding!!.emailInput.text.toString() |
| 116 | + val name = binding!!.nameInput.text.toString() |
| 117 | + val password = binding!!.pwdInput.text.toString() |
| 118 | + attachService!!.setCredentials(email, name, password) |
| 119 | + } |
| 120 | + |
| 121 | + val intent = Intent(this, BatchConflictListActivity::class.java) |
| 122 | + intent.putExtra("conflicts", false) |
| 123 | + startActivity(Intent(this, BatchConflictListActivity::class.java)) |
| 124 | + } |
| 125 | + |
| 126 | + private val mASConnection: ServiceConnection = object : ServiceConnection { |
| 127 | + override fun onServiceConnected(className: ComponentName, service: IBinder) { |
| 128 | + // This is called when the connection with the service has been established, getService returns |
| 129 | + // the Monitor object that is needed to call functions. |
| 130 | + attachService = (service as LocalBinder).service |
| 131 | + asIsBound = true |
| 132 | + |
| 133 | + val values = attachService!!.userDefaultValues |
| 134 | + binding!!.emailInput.setText(values[0]) |
| 135 | + binding!!.nameInput.setText(values[1]) |
| 136 | + } |
| 137 | + |
| 138 | + override fun onServiceDisconnected(className: ComponentName) { |
| 139 | + // This should not happen |
| 140 | + attachService = null |
| 141 | + asIsBound = false |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private fun doBindService() { |
| 146 | + // bind to attach service |
| 147 | + bindService(Intent(this, ProjectAttachService::class.java), mASConnection, BIND_AUTO_CREATE) |
| 148 | + } |
| 149 | + |
| 150 | + private fun doUnbindService() { |
| 151 | + if (asIsBound) { |
| 152 | + // Detach existing connection. |
| 153 | + unbindService(mASConnection) |
| 154 | + asIsBound = false |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments