Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 1
package com.saholic.profittill.BroadcastReceivers;
2
 
3
import android.content.BroadcastReceiver;
4
import android.content.Context;
5
import android.content.Intent;
6
import android.content.SharedPreferences;
7
import android.os.AsyncTask;
8
import android.os.Bundle;
9
import android.telephony.SmsManager;
10
import android.telephony.SmsMessage;
11
import android.util.Log;
12
import android.widget.Toast;
13
 
14
import com.saholic.profittill.Constants.ProfitTillConstants;
15
 
16
import org.apache.http.HttpEntity;
17
import org.apache.http.HttpResponse;
18
import org.apache.http.NameValuePair;
19
import org.apache.http.client.HttpClient;
20
import org.apache.http.client.entity.UrlEncodedFormEntity;
21
import org.apache.http.client.methods.HttpPost;
22
import org.apache.http.impl.client.DefaultHttpClient;
23
import org.apache.http.message.BasicNameValuePair;
24
 
25
import java.io.IOException;
26
import java.io.UnsupportedEncodingException;
27
import java.util.ArrayList;
28
 
29
/**
30
 * Created by rajender on 5/5/17.
31
 */
32
public class SmsReceiver extends BroadcastReceiver {
33
    SharedPreferences.Editor apiDataEditor,userDataEditor;
34
    SharedPreferences apiData,userData;
35
    ArrayList<NameValuePair> messageNameValuePair;
36
 
37
    // Get the object of SmsManager
38
    public SmsReceiver() {
39
    }
40
    final SmsManager sms = SmsManager.getDefault();
41
    public void onReceive(Context context, Intent intent) {
42
        apiData = context.getSharedPreferences("API_Data", Context.MODE_PRIVATE);
43
        userData = context.getSharedPreferences("User_Data", Context.MODE_PRIVATE);
44
        userDataEditor = userData.edit();
45
        apiDataEditor = apiData.edit();
46
        Log.d("recieved","received");
47
        // Retrieves a map of extended data from the intent.
48
        final Bundle bundle = intent.getExtras();
49
 
50
        try {
51
 
52
            if (bundle != null) {
53
 
54
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
55
 
56
                for (int i = 0; i < pdusObj.length; i++) {
57
 
58
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
59
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
60
 
61
                    String senderNum = phoneNumber;
62
                    String message = currentMessage.getDisplayMessageBody();
63
 
64
                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
65
                    String lastTwo = null;
66
                    if (senderNum != null && senderNum.length() >= 6) {
67
                        lastTwo = senderNum.substring(senderNum.length() - 6);
68
                    }
69
                    try {
70
                        if (lastTwo.equalsIgnoreCase(apiData.getString("Sender_CLI","PROFTM"))){
71
                              messageNameValuePair = new ArrayList<>();
72
                              messageNameValuePair.add(new BasicNameValuePair("message",message));
73
 
74
                              new sendOtpmessage().execute();
75
                            int duration = Toast.LENGTH_LONG;
76
                            Toast toast = Toast.makeText(context,"senderNum: "+ senderNum + ", message: " + message, duration);
77
                            toast.show();
78
 
79
                        }
80
                        else{
81
 
82
                        }
83
                    }
84
                    catch(Exception e){
85
                        e.printStackTrace();
86
                    }
87
 
88
 
89
                }
90
            }
91
 
92
        } catch (Exception e) {
93
            Log.e("SmsReceiver", "Exception smsReceiver" +e);
94
 
95
        }
96
    }
97
 
98
    class sendOtpmessage extends AsyncTask<String, Integer, String> {
99
 
100
        @Override
101
        protected void onPreExecute() {
102
 
103
            super.onPreExecute();
104
        }
105
 
106
        @Override
107
        protected String doInBackground(String... params) {
108
            try {
109
            HttpClient httpclient = new DefaultHttpClient();
110
            HttpPost httppost = new HttpPost(ProfitTillConstants.Web_Api+"/otp/parseOTP");
111
            httppost.setHeader("Auth-Token",userData.getString("token",""));
112
            httppost.setEntity(new UrlEncodedFormEntity(messageNameValuePair));
113
            HttpResponse response = httpclient.execute(httppost);
114
            HttpEntity entity = response.getEntity();
115
            int status = response.getStatusLine().getStatusCode();
116
            }
117
            catch (UnsupportedEncodingException e) {
118
                e.printStackTrace();
119
            }
120
            catch (IOException e) {
121
                e.printStackTrace();
122
            }
123
            return null;
124
        }
125
        @Override
126
        protected void onPostExecute(String result) {
127
            super.onPostExecute(result);
128
 
129
        }
130
 
131
    }
132
    }