Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
package com.saholic.profittill.main;
2
 
3
import android.app.AlertDialog;
4
import android.app.ProgressDialog;
5
import android.content.Context;
6
import android.content.DialogInterface;
7
import android.content.Intent;
8
import android.content.SharedPreferences;
9
import android.graphics.Color;
10
import android.net.ConnectivityManager;
11
import android.net.Uri;
12
import android.os.AsyncTask;
15356 manas 13
import android.os.Bundle;
14792 manas 14
import android.support.v7.app.ActionBarActivity;
15
import android.text.InputType;
16
import android.util.Log;
17
import android.view.KeyEvent;
18
import android.view.Menu;
19
import android.view.MenuItem;
20
import android.view.View;
21
import android.view.inputmethod.InputMethodManager;
22
import android.widget.Button;
23
import android.widget.EditText;
24
import android.widget.TextView;
25
import android.widget.Toast;
26
 
27
import com.facebook.Session;
28
import com.google.android.gms.analytics.HitBuilders;
29
import com.google.android.gms.analytics.Tracker;
30
import com.google.android.gms.common.ConnectionResult;
31
import com.google.android.gms.common.api.GoogleApiClient;
32
import com.google.android.gms.plus.Plus;
33
import com.mixpanel.android.mpmetrics.MixpanelAPI;
34
import com.saholic.profittill.Constants.ProfitTillConstants;
35
import com.saholic.profittill.R;
15356 manas 36
import com.saholic.profittill.Utils.UtilityFunctions;
14792 manas 37
 
38
import org.apache.http.HttpEntity;
39
import org.apache.http.HttpResponse;
40
import org.apache.http.NameValuePair;
41
import org.apache.http.client.ClientProtocolException;
42
import org.apache.http.client.HttpClient;
43
import org.apache.http.client.entity.UrlEncodedFormEntity;
44
import org.apache.http.client.methods.HttpPost;
45
import org.apache.http.impl.client.DefaultHttpClient;
46
import org.apache.http.message.BasicNameValuePair;
47
import org.apache.http.util.EntityUtils;
48
import org.json.JSONException;
49
import org.json.JSONObject;
50
 
51
import java.io.IOException;
52
import java.text.SimpleDateFormat;
53
import java.util.ArrayList;
54
import java.util.Date;
55
 
56
public class ReferrerActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
57
    EditText referalCode;
58
    EditText mobileNumber;
59
/*
60
    TextView inviteError;
61
    TextView thanks;
62
    TextView onlyPress;
63
    TextView getback;
64
    TextView inviteNumber;
65
    TextView giveusyour,or;
66
    TextView havent,press;
67
    TextView giveus;
68
    TextView inviteMessage,inviteMessage1;
69
*/
70
MixpanelAPI mixpanel;
71
    TextView inviteMessage,inviteMessage1,inviteNumber,giveus,thanks,incorrect;
72
    Button referrerSend,mobileNumberSend;
73
    String type1;
74
    String invitationCode;
75
    SharedPreferences userData;
76
    SharedPreferences apiData;
77
    SharedPreferences.Editor userDataEditor;
78
    SharedPreferences.Editor apiSettingsEditor;
79
    SharedPreferences inviteD;
80
    SharedPreferences.Editor inviteDEditor;
81
    ArrayList<NameValuePair> nameValuePairsGcm;
82
    public GoogleApiClient mGoogleApiClient;
83
    boolean glogout;
84
    final ArrayList<String> invitationCodeList = new ArrayList<String>();
85
 
86
    boolean FLAG=false;
87
    ArrayList<NameValuePair> nameValuePairs;
88
    @Override
89
    protected void onCreate(Bundle savedInstanceState) {
90
        super.onCreate(savedInstanceState);
91
        setContentView(R.layout.activity_referrer);
92
        getSupportActionBar().hide();
93
 
94
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
95
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
96
        userDataEditor = userData.edit();
97
        apiSettingsEditor = apiData.edit();
98
        inviteD = getApplicationContext().getSharedPreferences("Invite", MODE_PRIVATE);
99
        inviteDEditor = inviteD.edit();
100
 
101
        inviteMessage=(TextView)findViewById(R.id.inviteMessage);
102
        inviteMessage1=(TextView)findViewById(R.id.inviteMessage1);
103
        inviteNumber=(TextView)findViewById(R.id.inviteNumber);
104
        giveus=(TextView)findViewById(R.id.giveus);
105
        thanks=(TextView)findViewById(R.id.thanks);
106
        incorrect=(TextView)findViewById(R.id.incorrect);
107
 
108
        referalCode=(EditText)findViewById(R.id.referralCode);
109
        mobileNumber=(EditText)findViewById(R.id.mobileNumberReferrer);
110
        referrerSend =(Button)findViewById(R.id.referrerSend);
111
        mobileNumberSend =(Button)findViewById(R.id.mobileNumberReferrerSend);
112
        mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
113
        inviteMessage.setText(apiData.getString("referralinvite.text", "Got your Invite Code?"));
114
        inviteMessage1.setText(apiData.getString("no.referralinvite", "Haven't got the code..."));
115
        giveus.setText(apiData.getString("referralMobileNumber.text","Leave ur no & we'll get back to you"));
116
        inviteNumber.setVisibility(View.VISIBLE);
117
        inviteNumber.setText("Call " + apiData.getString("contactus.number", "") + " now");
118
        /*InputMethodManager imm = (InputMethodManager)getSystemService(
119
                Context.INPUT_METHOD_SERVICE);
120
        imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
121
        imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
122
*/
123
        referalCode.setInputType(0);
124
        mobileNumber.setInputType(0);
125
        if(getIntent().getAction()=="Login"){
126
            nameValuePairsGcm = new ArrayList<>();
127
            nameValuePairsGcm.add(new BasicNameValuePair("cid",getIntent().getExtras().getString("cid")));
128
            nameValuePairsGcm.add(new BasicNameValuePair("user_id",userData.getString("id", "")));
15356 manas 129
            nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
14792 manas 130
            nameValuePairsGcm.add(new BasicNameValuePair("result","referrer"));
131
            getIntent().getExtras().remove("cid");
132
            new NotificationOpenedData().execute(nameValuePairsGcm);
133
        }
134
 
135
        referalCode.setOnClickListener(new View.OnClickListener() {
136
            @Override
137
            public void onClick(View v) {
138
                /*referalCode.setInputType(1);*/
139
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
140
// only will trigger it if no physical keyboard is open
141
                inputMethodManager.showSoftInput(referalCode, InputMethodManager.SHOW_IMPLICIT);
142
                referalCode.requestFocus();
143
            }
144
        });
145
        mobileNumber.setOnClickListener(new View.OnClickListener() {
146
            @Override
147
            public void onClick(View v) {
148
                mobileNumber.setInputType(InputType.TYPE_CLASS_PHONE);
149
            }
150
        });
151
        inviteNumber.setOnClickListener(new View.OnClickListener() {
152
            @Override
153
            public void onClick(View v) {
154
                Intent callIntent = new Intent(Intent.ACTION_CALL);
155
                callIntent.setData(Uri.parse("tel:0" + apiData.getString("contactus.number", "")));
156
                startActivity(callIntent);
157
            }
158
        });
159
 
160
        JSONObject props = new JSONObject();
161
        try {
162
            props.put("Referrer", "Referrer Screen");
163
            mixpanel.track("Referrer Screen", props);
164
        } catch (JSONException e) {
165
 
166
            e.printStackTrace();
167
        }
168
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
169
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
170
        t.setScreenName("Referrer Screen");
171
        t.send(new HitBuilders.ScreenViewBuilder().build());
172
 
173
        type1 = userData.getString("type",null);
174
 
175
        if(type1!=null && type1.equalsIgnoreCase("google")) {
176
            mGoogleApiClient = new GoogleApiClient.Builder(this)
177
                    .addConnectionCallbacks(this)
178
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
179
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
180
            mGoogleApiClient.connect();
181
        }
182
        referalCode.setOnKeyListener(new View.OnKeyListener() {
183
            @Override
184
            public boolean onKey(View v, int keyCode, KeyEvent event) {
185
                if (event.getAction() == KeyEvent.ACTION_DOWN)
186
                {
187
                    switch (keyCode)
188
                    {
189
                        case KeyEvent.KEYCODE_DPAD_CENTER:
190
 
191
                        case KeyEvent.KEYCODE_ENTER:
192
 
193
                            if(referalCode.getText().toString().equalsIgnoreCase("")) {
194
                                incorrect.setVisibility(View.VISIBLE);
195
                                incorrect.setText(apiData.getString("incorrect.referralinvite","Incorrect Code"));
196
                                incorrect.setTextColor(Color.RED);
197
                                referalCode.setHint("Retry");
198
                                InputMethodManager imm = (InputMethodManager)getSystemService(
199
                                        Context.INPUT_METHOD_SERVICE);
200
                                imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
201
                            }
202
                            else{
203
                                if(isInternetOn()) {
204
                                    InputMethodManager imm = (InputMethodManager)getSystemService(
205
                                            Context.INPUT_METHOD_SERVICE);
206
                                    imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
207
                                    new PushReferrerData().execute(referalCode.getText().toString());
208
                                }else{
209
                                    InputMethodManager imm = (InputMethodManager)getSystemService(
210
                                            Context.INPUT_METHOD_SERVICE);
211
                                    imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
212
                                    Toast.makeText(getApplicationContext(), "No internet connection. Please try again.", Toast.LENGTH_SHORT).show();
213
                                }
214
                            }
215
                            return true;
216
 
217
                        default:
218
                            break;
219
                    }
220
                }
221
                return false;
222
            }
223
 
224
 
225
        });
226
 
227
        mobileNumber.setOnKeyListener(new View.OnKeyListener() {
228
            @Override
229
            public boolean onKey(View v, int keyCode, KeyEvent event) {
230
                if (event.getAction() == KeyEvent.ACTION_DOWN)
231
                {
232
                    switch (keyCode)
233
                    {
234
                        case KeyEvent.KEYCODE_DPAD_CENTER:
235
 
236
                        case KeyEvent.KEYCODE_ENTER:
237
                            if(mobileNumber.getText().toString().length()==10){
238
                                if(isInternetOn()) {
239
                                    incorrect.setVisibility(View.GONE);
240
                                    referalCode.setHint("Enter Invite Code");
241
                                    InputMethodManager imm = (InputMethodManager)getSystemService(
242
                                            Context.INPUT_METHOD_SERVICE);
243
                                    imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
244
                                    new pushVerification().execute();
245
                                }else{
246
                                    InputMethodManager imm = (InputMethodManager)getSystemService(
247
                                            Context.INPUT_METHOD_SERVICE);
248
                                    imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
249
                                    Toast.makeText(getApplicationContext(), "No internet connection. Please try again.", Toast.LENGTH_SHORT).show();
250
                                }
251
 
252
                            }
253
                            else{
254
                                InputMethodManager imm = (InputMethodManager)getSystemService(
255
                                        Context.INPUT_METHOD_SERVICE);
256
                                imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
257
                                Toast.makeText(getApplicationContext(), "Mobile Number not valid.", Toast.LENGTH_SHORT).show();
258
                            }
259
                            return true;
260
 
261
                        default:
262
                            break;
263
                    }
264
                }
265
                return false;
266
            }
267
 
268
 
269
        });
270
 
271
        referrerSend.setOnClickListener(new View.OnClickListener() {
272
            @Override
273
            public void onClick(View v) {
274
 
275
 
276
                if(referalCode.getText().toString().equalsIgnoreCase("")) {
277
                    incorrect.setVisibility(View.VISIBLE);
278
                    incorrect.setText(apiData.getString("incorrect.referralinvite","Incorrect Code"));
279
                    incorrect.setTextColor(Color.RED);
280
                    referalCode.setHint("Retry");
281
                    InputMethodManager imm = (InputMethodManager)getSystemService(
282
                            Context.INPUT_METHOD_SERVICE);
283
                    imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
284
                }
285
                else{
286
                    if(isInternetOn()) {
287
                        InputMethodManager imm = (InputMethodManager)getSystemService(
288
                                Context.INPUT_METHOD_SERVICE);
289
                        imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
290
                        new PushReferrerData().execute(referalCode.getText().toString());
291
                    }else{
292
                        InputMethodManager imm = (InputMethodManager)getSystemService(
293
                                Context.INPUT_METHOD_SERVICE);
294
                        imm.hideSoftInputFromWindow(referalCode.getWindowToken(), 0);
295
                        Toast.makeText(getApplicationContext(), "No internet connection. Please try again.", Toast.LENGTH_SHORT).show();
296
                    }
297
                }
298
            }
299
        });
300
 
301
        mobileNumberSend.setOnClickListener(new View.OnClickListener() {
302
            @Override
303
            public void onClick(View v) {
304
 
305
                if(mobileNumber.getText().toString().length()==10){
306
                    if(isInternetOn()) {
307
                        InputMethodManager imm = (InputMethodManager)getSystemService(
308
                                Context.INPUT_METHOD_SERVICE);
309
                        imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
310
                        incorrect.setVisibility(View.GONE);
311
                        referalCode.setHint("Enter Invite Code");
312
                        new pushVerification().execute();
313
                    }else{
314
                        InputMethodManager imm = (InputMethodManager)getSystemService(
315
                                Context.INPUT_METHOD_SERVICE);
316
                        imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
317
                        Toast.makeText(getApplicationContext(), "No internet connection. Please try again.", Toast.LENGTH_SHORT).show();
318
                    }
319
 
320
                }
321
                else{
322
                    InputMethodManager imm = (InputMethodManager)getSystemService(
323
                            Context.INPUT_METHOD_SERVICE);
324
                    imm.hideSoftInputFromWindow(mobileNumber.getWindowToken(), 0);
325
                    Toast.makeText(getApplicationContext(), "Mobile Number not valid.", Toast.LENGTH_SHORT).show();
326
                }
327
            }
328
        });
329
    }
330
 
331
 
332
    @Override
333
    public boolean onCreateOptionsMenu(Menu menu) {
334
        // Inflate the menu; this adds items to the action bar if it is present.
335
        getMenuInflater().inflate(R.menu.menu_referrer, menu);
336
        return true;
337
    }
338
 
339
    @Override
340
    public boolean onOptionsItemSelected(MenuItem item) {
341
        // Handle action bar item clicks here. The action bar will
342
        // automatically handle clicks on the Home/Up button, so long
343
        // as you specify a parent activity in AndroidManifest.xml.
344
        int id = item.getItemId();
345
 
346
        //noinspection SimplifiableIfStatement
347
        if (id == R.id.action_settings) {
348
            return true;
349
        }
350
 
351
        return super.onOptionsItemSelected(item);
352
    }
353
    @Override
354
    public void onConnected(Bundle bundle) {
355
 
356
    }
357
 
358
    @Override
359
    public void onConnectionSuspended(int i) {
360
 
361
    }
362
 
363
    @Override
364
    public void onConnectionFailed(ConnectionResult connectionResult) {
365
 
366
    }
367
 
368
    class PushReferrerData extends AsyncTask<String, Integer, String> {
369
 
370
        @Override
371
        protected void onPreExecute() {
372
            super.onPreExecute();
373
            referrerSend.setEnabled(false);
374
            referalCode.setInputType(0);
375
            mobileNumber.setInputType(0);
376
            pd2 = new ProgressDialog(ReferrerActivity.this);
377
            pd2.setMessage("Please wait...");
378
            pd2.setCancelable(false);
379
            pd2.show();
380
 
381
        }
382
 
383
        @Override
384
        protected String doInBackground(String... arg0) {
385
            String id=null;
386
            try {
387
                HttpClient httpclient = new DefaultHttpClient();
388
 
389
                nameValuePairs = new ArrayList<>();
390
                String updateURL = apiData.getString("useredit.url","");
391
                String url = updateURL+userData.getString("id","");
392
                 nameValuePairs.add(new BasicNameValuePair("id",userData.getString("id",null)));
393
                nameValuePairs.add(new BasicNameValuePair("referrer",arg0[0]));
394
                HttpPost httppost = new HttpPost(url);
395
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
396
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
397
                HttpResponse response = httpclient.execute(httppost);
398
                HttpEntity entity = response.getEntity();
399
                int status = response.getStatusLine().getStatusCode();
400
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
401
                System.out.println("JSON Object in referrer " + jObjGmail.toString());
402
                String success = jObjGmail.getString("success");
403
                if(status == 200){
404
                    inviteDEditor.putString("referrerCode",arg0[0]);
405
                    inviteDEditor.commit();
406
                    Log.d("In if","in if referrer" +inviteD.getString("referrerCode","").equalsIgnoreCase("") + inviteD.getString("referrerCode","") );
407
                    Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
408
                            GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
409
                    t.send(new HitBuilders.EventBuilder()
410
                            .setCategory("Referrer ")
411
                            .setAction("Referrer Saved Successfully")
412
                            .setLabel("Referrer for User" +userData.getString("id",null) + " is " + arg0[0])
413
                            .build());
414
                } else {
415
                    //TODO : handle properly. Don't allow user to proceed
416
                    Log.d("Status ","status " + status);
417
                }
418
 
419
                nameValuePairs.clear();
420
                Log.e("pass 1", "connection success ");
421
                if(success.equalsIgnoreCase("true")){
422
                    MixpanelAPI mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
423
                    JSONObject props = new JSONObject();
424
                    try {
425
                        props.put("Referrer", "Referrer Saved Successfully");
426
                        mixpanel.track("Referrer Saved", props);
427
                    } catch (JSONException e) {
428
 
429
                        e.printStackTrace();
430
                    }
431
                    userDataEditor.putString("referralCodeRequired","false");;
432
                    userDataEditor.commit();
433
                    return "true";
434
                }else{
435
                    userDataEditor.putString("referralCodeRequired","true");;
436
                    userDataEditor.commit();
437
                    return "false";
438
                }
439
 
440
            } catch (Exception e) {
441
                Log.e("Fail 1", e.toString());
442
                return "false";
443
            }
444
        }
445
 
446
        @Override
447
        protected void onPostExecute(String result) {
448
            super.onPostExecute(result);
449
            pd2.dismiss();
450
            if(result.equalsIgnoreCase("true")) {
451
                if(userData.getString("message","false").equalsIgnoreCase("true")) {
452
                    Intent i = new Intent(ReferrerActivity.this, MobileNumber.class);
453
                    i.putExtra("displayView", "7");
454
                    startActivity(i);
455
                }else{
456
                    if(!(userData.getString("mobileNumber","").isEmpty())) {
457
                        Intent i = new Intent(ReferrerActivity.this, MobileVerificationFirstTime.class);
458
                        i.putExtra("numberVerification", userData.getString("mobileNumber",""));
459
                        startActivity(i);
460
                    }else{
461
                        Intent i = new Intent(ReferrerActivity.this, MainActivity.class);
462
                        i.putExtra("displayView", "7");
463
                        startActivity(i);
464
 
465
                    }
466
                }
467
            }else{
468
                referrerSend.setEnabled(true);
469
                incorrect.setVisibility(View.VISIBLE);
470
                incorrect.setText(apiData.getString("incorrect.referralinvite","Incorrect Code"));
471
                incorrect.setTextColor(Color.RED);
472
                referalCode.setHint("Retry");
473
            }
474
        }
475
    }
476
    class pushVerification extends AsyncTask<String, Integer, String> {
477
 
478
        @Override
479
        protected void onPreExecute() {
480
            super.onPreExecute();
481
            JSONObject props = new JSONObject();
482
            try {
483
                mixpanel.identify(userData.getString("id",null));
484
                props.put("Screen", "Mobile Entered Referrer");
485
                mixpanel.track("Mobile Number Entered Referrer",props);
486
            } catch (JSONException e) {
487
                e.printStackTrace();
488
            }
489
            referalCode.setInputType(0);
490
            mobileNumber.setInputType(0);
491
            pd2 = new ProgressDialog(ReferrerActivity.this);
492
            pd2.setMessage("Please wait...");
493
            pd2.setCancelable(false);
494
            pd2.show();
495
 
496
        }
497
 
498
        @Override
499
        protected String doInBackground(String... arg0) {
500
            HttpClient httpclient = new DefaultHttpClient();
501
            try {
502
 
503
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
504
                SimpleDateFormat sdf = new SimpleDateFormat(ProfitTillConstants.SQL_DATE_FORMAT);
505
                String dateUpdated = sdf.format(new Date());
506
                nameValuePairs.add(new BasicNameValuePair("id",userData.getString("id","")));
507
                nameValuePairs.add(new BasicNameValuePair("mobile_number",mobileNumber.getText().toString()));
508
                nameValuePairs.add(new BasicNameValuePair("mobile_number_last_updated",dateUpdated));
509
                String updateURL = apiData.getString("useredit.url","");
510
                String url = updateURL+userData.getString("id","");
511
                HttpPost httppost = new HttpPost(url);
512
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
513
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
514
                HttpResponse response = httpclient.execute(httppost);
515
                HttpEntity entity = response.getEntity();
516
                int status = response.getStatusLine().getStatusCode();
517
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
518
                System.out.println("JSON Object in mobile " + jObjGmail.toString());
519
                String success = jObjGmail.getString("success");
520
 
521
                if(status == 200){
522
                    Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
523
                            GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
524
                    t.send(new HitBuilders.EventBuilder()
525
                            .setCategory("First Time Mobile Entered ")
526
                            .setAction("Number entered")
527
                            .setLabel("First Time Mobile entered")
528
                            .build());
529
 
530
                } else {
531
                    System.out.println(" NOT Transfered");
532
 
533
                }
534
 
535
                if(success.equalsIgnoreCase("true")){
536
                    userDataEditor.putString("message","false");;
537
                    userDataEditor.putString("mobileNumber",mobileNumber.getText().toString());
538
                    userDataEditor.commit();
539
                    return "true";
540
                }else{
541
                    userDataEditor.putString("message","true");;
542
                    userDataEditor.commit();
543
                    return "false";
544
                }
545
            } catch (ClientProtocolException e) {
546
                return "false";
547
            } catch (IOException e) {
548
                return "false";
549
            }catch (Exception e){
550
                return "false";
551
            }
552
        }
553
 
554
        @Override
555
        protected void onPostExecute(String result) {
556
            super.onPostExecute(result);
557
            pd2.dismiss();
558
 
559
            thanks.setText(apiData.getString("thanks.referrer.message","Thanks and we will get back to you"));
560
            thanks.setVisibility(View.VISIBLE);
561
        }
562
    }
563
    ProgressDialog pd2;
564
    @Override
565
    public void onBackPressed() {
566
        /* super.onBackPressed();*/
567
        new AlertDialog.Builder(this)
568
                .setIcon(android.R.drawable.ic_dialog_alert)
569
                .setTitle("Exit!")
570
                .setMessage("Are you sure you want to sign out?")
571
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
572
                {
573
                    @Override
574
                    public void onClick(DialogInterface dialog, int which) {
575
                        JSONObject props = new JSONObject();
576
                        try {
577
                            mixpanel.identify(userData.getString("id",null));
578
                            props.put("Screen", "Signing Out");
579
                            mixpanel.track("Signing Out Referrer",props);
580
                        } catch (JSONException e) {
581
                            e.printStackTrace();
582
                        }
583
 
584
                        if(type1.equalsIgnoreCase("facebook")) {
585
                            Log.d("fblogout","logout");
586
                            if (Session.getActiveSession() != null) {
587
                                Session.getActiveSession().closeAndClearTokenInformation();
588
                            }
589
                            Session.setActiveSession(null);
590
                            userDataEditor.clear().commit();
591
                            startActivity(new Intent(ReferrerActivity.this, LoginActivity.class));
592
                        }
593
                        else{
594
                            Log.d("gmailLogout","logout");
595
                            signOutFromGplus();
596
                            if(glogout==true){
597
                                startActivity(new Intent(ReferrerActivity.this, LoginActivity.class));
598
                            }
599
                        }
600
 
601
                    }
602
 
603
                })
604
                .setNegativeButton("No", null)
605
                .show();
606
    }
607
    public void signOutFromGplus() {
608
        if (mGoogleApiClient.isConnected()) {
609
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
610
            mGoogleApiClient.disconnect();
611
            mGoogleApiClient.connect();
612
            glogout=true;
613
/*
614
            userDataEditor.remove("id");
615
            userDataEditor.remove("type");
616
            userDataEditor.remove("email");
617
            userDataEditor.commit();
618
*/
619
            userDataEditor.clear().commit();
620
        } else {
621
            glogout=false;
622
        }
623
    }
624
 
625
    public final boolean isInternetOn() {
626
 
627
        ConnectivityManager connection =
628
                (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
629
 
630
        if ( connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
631
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
632
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
633
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
634
 
635
            /*Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();*/
636
            return true;
637
 
638
        } else if (
639
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
640
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
641
 
642
 /*           Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();*/
643
            return false;
644
        }
645
        return false;
646
    }
647
 
648
 
649
    class NotificationOpenedData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
650
 
651
        @Override
652
        protected void onPreExecute() {
653
            super.onPreExecute();
654
 
655
        }
656
 
657
        @Override
658
        protected String doInBackground(ArrayList<NameValuePair>... arg0) {
659
 
660
            try {
661
                HttpClient httpclient = new DefaultHttpClient();
662
                HttpPost httppost = new HttpPost(apiData.getString("notication.data.url","http://api.profittill.com/pushnotifications/add"));
663
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
664
                httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
665
                HttpResponse response = httpclient.execute(httppost);
666
                HttpEntity entity = response.getEntity();
667
                int status = response.getStatusLine().getStatusCode();
668
 
669
                if(status == 200){
670
                    Log.d("Notification Opened","Notication opened and sent to server");
671
                    Log.d("ResponseCode GCM ",status+"");
672
                } else {
673
                    Log.d("ResponseCode GCM ",status+"");
674
                }
675
 
676
                nameValuePairsGcm.clear();
677
                Log.e("pass 1", "connection success ");
678
            } catch (Exception e) {
679
                Log.e("Fail 1", e.toString());
680
 
681
            }
682
            return "success";
683
        }
684
 
685
        @Override
686
        protected void onPostExecute(String result) {
687
            super.onPostExecute(result);
688
            Log.d("Notification Opened","Notication opened and login page");
689
        }
690
    }
691
}
692