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

fixed app crash when no qty given in medicine and requested sms permi… #374

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to my changes

//Todo ask for SEND Message permission

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to my changes

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;
}
}

}