Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.saholic.profittill.BroadcastReceivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.saholic.profittill.Constants.ProfitTillConstants;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Created by rajender on 5/5/17.
 */
public class SmsReceiver extends BroadcastReceiver {
    SharedPreferences.Editor apiDataEditor,userDataEditor;
    SharedPreferences apiData,userData;
    ArrayList<NameValuePair> messageNameValuePair;

    // Get the object of SmsManager
    public SmsReceiver() {
    }
    final SmsManager sms = SmsManager.getDefault();
    public void onReceive(Context context, Intent intent) {
        apiData = context.getSharedPreferences("API_Data", Context.MODE_PRIVATE);
        userData = context.getSharedPreferences("User_Data", Context.MODE_PRIVATE);
        userDataEditor = userData.edit();
        apiDataEditor = apiData.edit();
        Log.d("recieved","received");
        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
                    String lastTwo = null;
                    if (senderNum != null && senderNum.length() >= 6) {
                        lastTwo = senderNum.substring(senderNum.length() - 6);
                    }
                    try {
                        if (lastTwo.equalsIgnoreCase(apiData.getString("Sender_CLI","PROFTM"))){
                              messageNameValuePair = new ArrayList<>();
                              messageNameValuePair.add(new BasicNameValuePair("message",message));

                              new sendOtpmessage().execute();
                            int duration = Toast.LENGTH_LONG;
                            Toast toast = Toast.makeText(context,"senderNum: "+ senderNum + ", message: " + message, duration);
                            toast.show();

                        }
                        else{

                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }


                }
            }

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }

    class sendOtpmessage extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(ProfitTillConstants.Web_Api+"/otp/parseOTP");
            httppost.setHeader("Auth-Token",userData.getString("token",""));
            httppost.setEntity(new UrlEncodedFormEntity(messageNameValuePair));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            int status = response.getStatusLine().getStatusCode();
            }
            catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

        }

    }
    }