Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Commit

Permalink
fixed app crash when no qty given in medicine and requested sms permi…
Browse files Browse the repository at this point in the history
…ssion for API>=23
  • Loading branch information
shikherjaiswal committed Sep 8, 2018
1 parent 92f1971 commit f40aa8c
Showing 1 changed file with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.peacecorps.malaria.ui.medicine_store;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.SmsManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.peacecorps.malaria.R;
import com.peacecorps.malaria.ui.base.BaseFragment;
Expand All @@ -35,6 +40,8 @@ public class MedicineStoreFragment extends BaseFragment implements MedicineMvpVi
TextView daysLeft;
private Context context;
private MedicineStorePresenter<MedicineStoreFragment> presenter;
private int PERMISSIONS_REQUEST_SEND_SMS = 7;
private boolean smsPermissionGranted = false;

@Nullable
@Override
Expand Down Expand Up @@ -138,18 +145,44 @@ public void onClick(View view) {
}
});
//implement the message button
//Todo ask for SEND Message permission

orderMedicineDialog.findViewById(R.id.btn_order_by_msg).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (presenter.checkMedicineNumberValidity(quantity.getText().toString().trim())) {
if (!presenter.checkMedicineNumberValidity(quantity.getText().toString().trim())) {
quantity.setError("Quantity Required");
quantity.requestFocus();
} else {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("123", null,
presenter.getMessageBodyForOrder() +
Integer.parseInt(quantity.getText().toString()), null, null);
orderMedicineDialog.dismiss();
//Entered Order quantity is greater than 0
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Ask for permission
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.SEND_SMS},
PERMISSIONS_REQUEST_SEND_SMS);

}
else{
smsPermissionGranted = true;
}

if(smsPermissionGranted) {
SmsManager smsManager = SmsManager.getDefault();
//Sms cannot parse html tags so remove them and use appropriate alternative
String message = (presenter.getMessageBodyForOrder() +Integer.parseInt(quantity.getText().toString())).toString();
String patternRegex = "<[/]*b>"; // To match <b> and </b>
message = message.replaceAll("<br>","\n");
message = message.replaceAll(patternRegex," ");
String phoneNo = "121";
smsManager.sendTextMessage(phoneNo, null,message, null, null);
Toast.makeText(getActivity(), "Message sent", Toast.LENGTH_LONG).show();
orderMedicineDialog.dismiss();
}
else{
//Show Toast message
Toast.makeText(getActivity(), "Cannot use this feature without Send SMS permission", Toast.LENGTH_LONG).show();
}
}
}
});
Expand Down Expand Up @@ -210,4 +243,14 @@ public void onClick(View view) {
settingsDialog.show();

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == PERMISSIONS_REQUEST_SEND_SMS && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
smsPermissionGranted = true;
}
}

}

0 comments on commit f40aa8c

Please sign in to comment.