I implemented the Android SMS Verification API on activities and fragments on the same project and it went well. My problem is with fragments in tabs. No matter what I do, onActivityResult always returns result code 0 when "Allow" is pressed. Here's my lot of code which was also implemented and tested to be working on the activities and fragments.
override fun onStart() {
super.onStart()
registerToSmsBroadcastReceiver()
}
override fun onStop() {
myActivity.unregisterReceiver(smsBroadcastReceiver)
super.onStop()
}
private fun startSmsUserConsent() {
SmsRetriever.getClient(myActivity).also {
it.startSmsUserConsent(null)
.addOnSuccessListener {
Log.d("LISTENING", "SUCCESS")
}
.addOnFailureListener {
Log.d("LISTENING", "FAIL")
}
}
}
private fun registerToSmsBroadcastReceiver() {
smsBroadcastReceiver = SmsBroadcastReceiver().also {
it.smsBroadcastReceiverListener =
object : SmsBroadcastReceiver.SmsBroadcastReceiverListener {
override fun onSuccess(intent: Intent?) {
intent?.let { context -> startActivityForResult(context, REQ_USER_CONSENT) }
}
override fun onFailure() {
}
}
}
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
myActivity.registerReceiver(smsBroadcastReceiver, intentFilter)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQ_USER_CONSENT -> {
if ((resultCode == Activity.RESULT_OK) && (data != null)) {
val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
val code = message?.let { fetchVerificationCode(it) }
otpField.setText(code)
}
}
}
}
private fun fetchVerificationCode(message: String): String {
return Regex("(\\d{6})").find(message)?.value ?: ""
}
Oh, and startSmsUserConsent() is called whenever I call for the API to send an OTP. Anything I missed?
Thank you.
I solved the issue by handling the OTP SMS Retrieval on the activity instead of on the fragment, then passed on the fragment if need be.
Related
I can scan a barcode successfully but I somehow cannot get the result. I found out that since I am calling the barcode scanner in a fragment, I need change my code to use this:
class AddIerFragment : Fragment() { ....
val intentIntegrator = IntentIntegrator.forFragment(this)
....
}
The problem is, the "this" keyword is not allowed because it gives me an error of
Type mismatch
Requred: Fragment
Found AddIerFragment
See image below.
I have this code in the fragment
companion object {
#JvmStatic
fun newInstance(param1: String, param2: String) =
AddIerFragment().apply {
arguments = Bundle().apply {
}
}
private const val CAMERA = 1
private const val GALLERY = 2
private const val SCAN = 3
}
R.id.button_atgScan -> {
Dexter.withContext(context!!).withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
)
.withListener(object: MultiplePermissionsListener {
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
report?.let{
if(report!!.areAllPermissionsGranted()) {
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES)
intentIntegrator.setPrompt("Scan a barcode")
intentIntegrator.setCameraId(0)
intentIntegrator.setBeepEnabled(false)
intentIntegrator.setBarcodeImageEnabled(true)
intentIntegrator.setOrientationLocked(false)
intentIntegrator.initiateScan()
}
}
}
override fun onPermissionRationaleShouldBeShown(
p0: MutableList<PermissionRequest>?,
p1: PermissionToken?
) {
showRationalDialogForPermission()
}
}).onSameThread().check()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == GALLERY) {
data?.let {
val selectedPhotoUri = data.data
file = File(getPath(selectedPhotoUri))
gView!!.iv_ier_image.setImageURI(selectedPhotoUri)
}
} else if (requestCode == CAMERA) {
data?.extras?.let {
val thumbnail: Bitmap =
data.extras!!.get("data") as Bitmap
file = savebitmap(thumbnail)!!
gView!!.iv_ier_image.setImageBitmap(thumbnail)
}
}
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) {
Log.i("TAG", "NOTHING")
} else {
Log.i("TAG", result.contents)
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
For support or androidx fragments you should use IntentIntegrator.forSupportFragment(this)
AddIerFragment must not be a subclass of the correct Fragment. At the top of its file, make sure you have imported androidx.fragment.app.Fragment instead of android.app.Fragment. And assuming you're using zxing-android-embedded, make sure you call forSupportFragment, not forFragment.
I am currently listening for Google Play services broadcasted action SmsRetriever.SMS_RETRIEVED_ACTION to check whether OTP SMS was retrieved. First, I start SmsRetriever:
SmsRetriever.getClient(context).startSmsUserConsent(null)
My BroadcastReceiver looks like this and works perfectly fine in real scenario:
object : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
try {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as? Status
when (smsRetrieverStatus?.statusCode) {
CommonStatusCodes.SUCCESS -> {
extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)?.let {
myOtpFragment.startActivityForResult(it, SMS_REQUEST_CODE)
}
}
}
}
} catch (e: Exception) {
Timber.e(e)
}
}
I want to somehow mock this onReceive method, in order to test and verify that my onActivityResult code works as expected and autofills EditText with retrieved OTP.
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SMS_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val otp = data?.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE) ?: ""
et_otp?.let {
it.setText(otp)
adjustOtpSelection(it)
}
}
Any tips on how could I do that with Espresso and Mockito?
I am trying to follow MVVM pattern in android project, I have to call network api onAcitivityResult method. According to MVVM repository should interact with network calls and viewmodel should do the interaction between Activity and repository. So if I have to access network api then I have to call viewmodel method in onActivityResult. This is my onActivityResult method:
class Profile : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val networkConnectionInterceptor = NetworkConnectionInterceptor(this)
val api = Api.invoke(networkConnectionInterceptor)
val repository = UserRepository(api)
val factory = ProfileViewModelFactory(repository, Photo(""))
val viewModel = ViewModelProvider(this, factory).get(ProfileViewModel::class.java)
val binding: ActivityProfileBinding =
DataBindingUtil.setContentView(this, R.layout.activity_profile)
binding.viewmodel = viewModel
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == Activity.RESULT_OK) {
if (data != null) {
when (requestCode) {
ImageIntent.CAMERA_REQUEST -> {
/* I want to call Viewmodel method here */
viewmodel.onProfileImageUpload(ImageIntent.imageUri)
}
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
toast("Image upload cancelled !")
}
}
This is the method defined in my viewmodel :
fun onProfileImageUpload(uri: Uri) {
Coroutines.main{
try {
val imageResponse = repository.updateProfileAvatar(
ImageUtil.getImageForUpload(
uri,
"avatar"
)
)
Log.d("avatar_resonse", "$imageResponse")
} catch(e : Exception) {}
}
}
The problem is I have to initialize the viewmodel in Activity onCreate method so I cannot have the viewmodel instance in the onActivityResult. How do I make a network call from there ?
try this
if(requestCode==your code){
if(resultCode==Activity.RESULT_OK){
if(data!=null){
// your api and if you calling image from start activity result get it from data
}
}
}
In my Android application, when I pick a contact from the Contacts activity, instead of returning to the activity that called it, the application closes without any exception or error message.
I tried to launch this activity with different flags, but with no result
class AddPhoneCompatActivity : BaseCompatActivity(), AddPhoneContract.View, View.OnClickListener {
#Inject
lateinit var presenter: AddPhonePresenter
override fun init(savedInstanceState: Bundle?) {
log("Add phone screen - Loading view", LOGS_SIMPLE_FILE_NAME)
setContentView(R.layout.activity_add_phone)
MyLocationNotifierApp.getInjector().inject(this)
presenter.attach(this)
GeneralUtil.checkPermission(
Manifest.permission.READ_CONTACTS,
GeneralUtil.READ_CONTACTS_REQUEST_CODE,
applicationContext,
this
)
presenter.checkIntent(intent)
btnAddPhoneFromContacts.setOnClickListener(this)
btnPhoneNext.setOnClickListener(this)
}
override fun onBackPressed() {
goBack()
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item?.itemId) {
android.R.id.home -> {
goBack()
}
}
return true
}
private fun goBack() {
intent.setClass(this, AddLabelCompatActivity::class.java)
intent.putExtra(GeneralUtil.PHONE_SERIALIZATION_KEY, edAddPhoneNum.text.toString())
startActivity(intent)
finish()
}
override fun onClick(v: View?) {
when (v?.id) {
btnAddPhoneFromContacts.id -> {
log("Starting contacts picker", LOGS_SIMPLE_FILE_NAME)
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
intent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE;
startActivityForResult(intent, GeneralUtil.REQUEST_CODE_SEARCH_CONTACT)
}
btnPhoneNext.id -> {
presenter.proceedNext(edAddPhoneNum.text.toString(), intent)
}
}
}
override fun updatePhoneEditField(phoneNum: String?) = edAddPhoneNum.setText(phoneNum)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
log("Add phone screen - onActivityResult called. Checking passed data.", LOGS_SIMPLE_FILE_NAME)
if (requestCode == GeneralUtil.REQUEST_CODE_SEARCH_CONTACT) {
if (resultCode == Activity.RESULT_OK) {
presenter.processAddPhoneRequestFomIntent(data)
}
}
}
}
It is supposed that after picking a contact, I return to the activity and show a dialog with the list of phone numbers corresponding to the contact. But instead, the the app closes without any notification.
Found the reason. The entity creation flow is not using startActivityForResult, but the pieces of data passed between activities. And I started this flow in the starting activity using flags Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_HISTORY. When I deleted this line in the starting flow activity, it began working as intended.
My MainActivity implements the Observer class. I also have a class called ObservedObject that extends the Observable class.
Here is my custom Observable , called ObservedObject:
class ObservedObject(var value: Boolean) : Observable() {
init {
value = false
}
fun setVal(vals: Boolean) {
value = vals
setChanged()
notifyObservers()
}
fun printVal() {
Log.i("Value" , "" + value)
}
}
Here is my Application called SpeechApp which contains my ObservedObject (an Observable actually):
class SpeechApp: Application() {
var isDictionaryRead = ObservedObject(false)
override fun onCreate() {
super.onCreate()
wordslist = ArrayList()
Thread {
execute()
}.start()
}
fun execute() {
while (/* Condition */) {
//Log.i("Read" , line)
/*Does Something Here*/
}
isDictionaryRead.setVal(true)
}
}
In my MainActivity, I mainly have a dialog, that should be displayed after I have got the output after Speech Recognition. It will display as long as the value of isDictionaryRead doesn't change to true:
class MainActivity(private val REQ_CODE_SPEECH_INPUT: Int = 100) : AppCompatActivity() , Observer{
override fun update(o: Observable?, arg: Any?) {
(o as ObservedObject).printVal()
dialog.hide()
}
private lateinit var app : SpeechApp
private lateinit var dialog: MaterialDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dialog = MaterialDialog.Builder(this)
.title("Please Wait")
.content("Loading from the Dictionary")
.progress(true , 0)
.build()
app = application as SpeechApp
app.isDictionaryRead.addObserver(this)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_speech, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val id = item?.itemId
when(id) {
R.id.menu_option_speech -> {
invokeSpeech()
}
}
return super.onOptionsItemSelected(item)
}
private fun invokeSpeech() {
/* Does Something, Works Fine */
try {
startActivityForResult(intent , REQ_CODE_SPEECH_INPUT)
}
catch (ex: ActivityNotFoundException) {
/* Does Something */
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQ_CODE_SPEECH_INPUT -> {
if (resultCode == Activity.RESULT_OK && null != data) {
dialog.show()
}
}
}
super.onActivityResult(requestCode, resultCode, data)
}
}
Now the problem is, when the SpeechApp sets the value of isDictionaryRead to true, I expect it to call the MainActivity update() method, wherein I have given the code to hide the dialog. That particular code is not working, and my dialog box doesn't go away. Where am I going wrong?
PS. I've pushed my code to Github now, just in case anyone could help me where I am going wrong.
The only thing I can think of that would cause this problem is that the execute() thread that was started in SpeechApp.onCreate finished execution and called isDictionaryRead.setVal(true) before the activity could call app.isDictionaryRead.addObserver(this). As a result, notifyObservers is called before the activity even starts observing, and as a result it is not notified. Here's my proposed solution: Start the execute thread in the activity's onCreate method after adding it as an observer.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dialog = MaterialDialog.Builder(this)
.title("Please Wait")
.content("Loading from the Dictionary")
.progress(true , 0)
.build()
app = application as SpeechApp
app.isDictionaryRead.addObserver(this)
app.asyncReadDictionary()
}
Then remove the thread call from SpeechApp.onCreate and use this instead
// in SpeechApp
fun asyncReadDictionary() {
if (!isDictionaryRead.value) {
Thread { execute() }.start()
}
}
private fun execute() {
while (/* Condition */) {
//Log.i("Read" , line)
/*Does Something Here*/
}
isDictionaryRead.value = true
}
Also, reimplement ObservableObject as follows
class ObservedObject : Observable() {
var value: Boolean = false
set(newValue) {
field = newValue
setChanged()
notifyObservers()
}
fun printVal() {
Log.i("Value" , "" + value)
}
}