Subversion Repositories SmartDukaan

Rev

Rev 19653 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
package com.saholic.profittill;
2
 
21169 rajender 3
import android.Manifest;
14792 manas 4
import android.app.AlertDialog;
5
import android.app.ProgressDialog;
6
import android.content.Context;
7
import android.content.DialogInterface;
8
import android.content.Intent;
9
import android.content.SharedPreferences;
21169 rajender 10
import android.content.pm.PackageManager;
14792 manas 11
import android.net.ConnectivityManager;
16080 manas 12
import android.net.Uri;
14792 manas 13
import android.os.AsyncTask;
21169 rajender 14
import android.os.Build;
14792 manas 15
import android.os.Bundle;
21169 rajender 16
import android.support.annotation.NonNull;
17
import android.support.v4.app.ActivityCompat;
18
import android.support.v4.content.ContextCompat;
14792 manas 19
import android.support.v7.app.ActionBarActivity;
20
import android.telephony.TelephonyManager;
14991 manas 21
import android.util.Log;
14792 manas 22
import android.view.Menu;
23
import android.view.MenuItem;
24
import android.widget.ProgressBar;
21169 rajender 25
import android.widget.Toast;
14792 manas 26
import com.google.android.gms.analytics.HitBuilders;
27
import com.google.android.gms.analytics.Tracker;
21169 rajender 28
import com.google.android.gms.common.ConnectionResult;
29
import com.google.android.gms.common.api.GoogleApiClient;
30
 
31
import com.google.firebase.iid.FirebaseInstanceId;
14792 manas 32
import com.saholic.profittill.Constants.ProfitTillConstants;
14991 manas 33
import com.saholic.profittill.Utils.UtilityFunctions;
21169 rajender 34
import com.saholic.profittill.main.BasicInformation;
14792 manas 35
import com.saholic.profittill.main.GoogleAnalyticsTracker;
36
import com.saholic.profittill.main.LoginActivity;
37
import com.saholic.profittill.main.MainActivity;
38
import com.saholic.profittill.main.MobileNumber;
21169 rajender 39
import com.saholic.profittill.main.PendingActivity;
14792 manas 40
import com.testin.agent.TestinAgent;
41
 
42
import org.apache.http.HttpEntity;
43
import org.apache.http.HttpResponse;
44
import org.apache.http.NameValuePair;
45
import org.apache.http.client.HttpClient;
46
import org.apache.http.client.entity.UrlEncodedFormEntity;
47
import org.apache.http.client.methods.HttpPost;
48
import org.apache.http.impl.client.DefaultHttpClient;
49
import org.apache.http.message.BasicNameValuePair;
50
import org.apache.http.util.EntityUtils;
51
import org.json.JSONArray;
52
import org.json.JSONObject;
53
 
54
import java.util.ArrayList;
55
 
56
 
21169 rajender 57
public class SplashScreen extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
58
    SharedPreferences apiData, userData, apkData;
59
    SharedPreferences.Editor apiEditor, userEditor, apkDataEditor;
14991 manas 60
    UtilityFunctions utilityFunctions;
14792 manas 61
    AlertDialog dialog1;
62
    AlertDialog.Builder b;
63
    ProgressBar pd;
21169 rajender 64
    FirebaseInstanceId fcm;
15588 manas 65
    String regId;
14792 manas 66
    ArrayList<NameValuePair> apkDataListValue;
15588 manas 67
    ArrayList<NameValuePair> nameValuePairsGcm;
21169 rajender 68
    public static final int READ_PHONE_STATE_PERMISSION = 100;
69
    private boolean PermissionIngranted=false;
14792 manas 70
    @Override
71
    protected void onCreate(Bundle savedInstanceState) {
72
        TestinAgent.init(this);
73
        super.onCreate(savedInstanceState);
74
        setContentView(R.layout.activity_splash_screen);
75
        getSupportActionBar().hide();
76
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
77
        apkData = getApplicationContext().getSharedPreferences("APK_Data", MODE_PRIVATE);
78
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
79
        apkDataEditor = apkData.edit();
80
        userEditor = userData.edit();
81
        apiEditor = apiData.edit();
21169 rajender 82
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
83
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE_PERMISSION);
84
        }
85
        else{
86
            PermissionIngranted=true;
87
        }
88
        String url = "";
14792 manas 89
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
90
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
91
        t.setScreenName("Splash Screen");
92
        t.send(new HitBuilders.ScreenViewBuilder().build());
93
        try {
94
            String apkVersionCode = apkData.getString("versionCode","");
95
            String apkVersionName = apkData.getString("versionName","");
96
            if(userData.getString("id","").isEmpty()) {
97
 
98
            }else {
99
                if (apkVersionCode.isEmpty()) {
14991 manas 100
                    utilityFunctions=new UtilityFunctions();
101
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 102
 
103
                    if(isInternetOn()) {
104
                        new pushApkData().execute();
105
                    }
106
                } else if (!(apkData.getString("checkId","blank").equalsIgnoreCase(userData.getString("id","blank")))) {
14991 manas 107
                    utilityFunctions=new UtilityFunctions();
108
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 109
                    if(isInternetOn()) {
110
                        new pushApkData().execute();
111
                    }
112
                } else if (Integer.parseInt(apkVersionCode) == getPackageManager().getPackageInfo(getPackageName(), 0).versionCode) {
113
 
114
                }  else {
14991 manas 115
                    utilityFunctions=new UtilityFunctions();
116
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 117
                    if(isInternetOn()) {
118
                        new pushApkData().execute();
119
                    }
120
                }
121
            }
122
        }catch (Exception e){
123
            e.printStackTrace();
124
        }
125
        if(isInternetOn()){
21169 rajender 126
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
127
                requestphoneUpdates();
128
            }
129
            else{
130
                    new fetchAPISettings().execute();
131
                }
14792 manas 132
        }
133
        else{
134
            b= new AlertDialog.Builder(this);
135
            b.setMessage("No internet connection. Please try again.");
136
            b.setCancelable(false);
137
            b.setPositiveButton("Retry", new DialogInterface.OnClickListener()
138
            {
139
                @Override
140
                public void onClick(DialogInterface dialog, int which) {
141
                    if(isInternetOn()){
142
                        new fetchAPISettings().execute();
143
                        dialog1.dismiss();
144
                    }
145
                    else{
146
                        dialog1 = b.create();
147
                        dialog1.show();
148
                    }
149
                }
150
 
151
            });
152
            b.setNegativeButton("No", new DialogInterface.OnClickListener() {
153
                @Override
154
                public void onClick(DialogInterface dialog, int which) {
155
                    finish();
156
 
157
                }
158
            });
159
            dialog1 = b.create();
160
            dialog1.show();
161
        }
162
    }
163
 
164
 
21169 rajender 165
  public void requestphoneUpdates(){
166
 
167
      if (ContextCompat.checkSelfPermission(SplashScreen.this,
168
              Manifest.permission.READ_PHONE_STATE)
169
              != PackageManager.PERMISSION_GRANTED) {
170
          if (ActivityCompat.shouldShowRequestPermissionRationale(SplashScreen.this,
171
                  Manifest.permission.READ_PHONE_STATE)) {
172
 
173
          } else {
174
              ActivityCompat.requestPermissions(SplashScreen.this,
175
                      new String[]{Manifest.permission.READ_PHONE_STATE},
176
                      READ_PHONE_STATE_PERMISSION);
177
          }
178
          return;
179
      }
180
  }
14792 manas 181
    @Override
21169 rajender 182
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
183
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
184
        switch (requestCode){
185
 
186
            case READ_PHONE_STATE_PERMISSION:
187
                try {
188
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
189
                        PermissionIngranted = true;
190
 
191
                        if(isInternetOn()) {
192
                              new fetchAPISettings().execute();
193
 
194
        } else {
195
                b = new AlertDialog.Builder(this);
196
                b.setMessage("No internet connection. Please try again.");
197
                b.setCancelable(false);
198
                b.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
199
                    @Override
200
                    public void onClick(DialogInterface dialog, int which) {
201
                        if (isInternetOn()) {
202
                            new fetchAPISettings().execute();
203
                            dialog1.dismiss();
204
                        } else {
205
                            dialog1 = b.create();
206
                            dialog1.show();
207
                        }
208
                    }
209
 
210
                });
211
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
212
            @Override
213
            public void onClick(DialogInterface dialog, int which) {
214
                finish();
215
 
216
            }
217
        });
218
        dialog1 = b.create();
219
        dialog1.show();
220
    }}
221
        else {
222
          Toast.makeText(getApplicationContext(),"Permission is required to enter in application",Toast.LENGTH_LONG).show();
223
            PermissionIngranted = false;
224
        }
225
    }
226
    catch (Exception e){
227
        e.printStackTrace();
228
    }
229
 
230
         break;
231
        }
232
 
233
    }
234
 
235
    @Override
14792 manas 236
    public boolean onCreateOptionsMenu(Menu menu) {
237
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
238
        return true;
239
    }
240
 
241
    @Override
242
    public boolean onOptionsItemSelected(MenuItem item) {
243
        int id = item.getItemId();
244
        if (id == R.id.action_settings) {
245
            return true;
246
        }
247
 
248
        return super.onOptionsItemSelected(item);
249
    }
250
 
21169 rajender 251
    @Override
252
    public void onConnected(Bundle bundle) {
253
     requestphoneUpdates();
254
    }
255
 
256
    @Override
257
    public void onConnectionSuspended(int i) {
258
 
259
    }
260
 
261
    @Override
262
    public void onConnectionFailed(ConnectionResult connectionResult) {
263
 
264
    }
265
 
266
 
267
 
14792 manas 268
    class fetchAPISettings extends AsyncTask<String, Integer, JSONObject> {
269
 
270
        @Override
271
        protected void onPreExecute() {
272
            super.onPreExecute();
273
        }
274
 
275
        @Override
276
        protected JSONObject doInBackground(String... arg0) {
277
            try {
21169 rajender 278
 
15588 manas 279
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
14792 manas 280
                HttpClient httpclient = new DefaultHttpClient();
15588 manas 281
                String url = ProfitTillConstants.MOBILE_API+"?t="+apkData.getString("timestamp","0")+"&imeinumber="+telephonyManager.getDeviceId();
282
                HttpPost httppost=new HttpPost(url);
14792 manas 283
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
284
                HttpResponse response = httpclient.execute(httppost);
285
                HttpEntity entity = response.getEntity();
286
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
287
                return jObjGmail;
288
 
289
            } catch (Exception e) {
21169 rajender 290
                e.printStackTrace();
14792 manas 291
                return null;
292
 
293
            }
294
 
295
        }
296
        @Override
297
        protected void onPostExecute(JSONObject result) {
298
            super.onPostExecute(result);
299
            if(result==null){
300
                finish();
301
            }
302
            try {
303
                JSONArray objects = result.getJSONArray("settings");
304
                if(objects.length()==0){
305
                    System.out.println("Null settings array");
306
                }else {
307
                    for (int i = 0; i < objects.length(); i++) {
308
                        JSONObject jsonObject = objects.getJSONObject(i);
309
                        JSONObject j = jsonObject.getJSONObject("Mobileappsetting");
310
                        apiEditor.remove(j.getString("setting"));
311
                        apiEditor.commit();
312
                        apiEditor.putString(j.getString("setting"), j.getString("value"));
313
                        apiEditor.commit();
314
                    }
315
                }
316
                String timestamp=result.getString("t");
15588 manas 317
                String failureCount=result.getString("failureCount");
14792 manas 318
                apkDataEditor.putString("timestamp",timestamp);
319
                apkDataEditor.commit();
320
                final String idCheck = userData.getString("id",null);
15588 manas 321
                if(Integer.parseInt(failureCount)<=0) {
21169 rajender 322
                    if(userData.getString("docsSubmitted","false").equalsIgnoreCase("true")){
323
                        Intent i = new Intent(SplashScreen.this, PendingActivity.class);
324
                        startActivity(i);
325
                    }
326
                   else  if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
327
                        Intent startActivityIntent=new Intent(SplashScreen.this, BasicInformation.class);
328
 
18082 manas 329
                        startActivity(startActivityIntent);
15588 manas 330
                    } else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
18082 manas 331
                        Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
332
                        Intent intent = getIntent();
333
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
334
                            Uri uri = intent.getData();
335
                            startActivityIntent.setAction("31");
336
                            startActivityIntent.setData(Uri.parse(uri.toString()));
337
                        }
338
                        startActivity(startActivityIntent);
15588 manas 339
                    } else if (idCheck != null) {
16080 manas 340
                        Intent startActivityIntent=new Intent(SplashScreen.this, MainActivity.class);
341
                        Intent intent = getIntent();
342
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
343
                            Uri uri = intent.getData();
344
                            startActivityIntent.setAction("31");
345
                            startActivityIntent.setData(Uri.parse(uri.toString()));
346
                        }
347
                        startActivity(startActivityIntent);
15588 manas 348
                    } else {
21169 rajender 349
                        Intent startActivityIntent=new Intent(SplashScreen.this,LoginActivity   .class);
18082 manas 350
                        Intent intent = getIntent();
351
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
352
                            Uri uri = intent.getData();
353
                            startActivityIntent.setAction("31");
354
                            startActivityIntent.setData(Uri.parse(uri.toString()));
355
                        }
356
                        startActivity(startActivityIntent);
15588 manas 357
                    }
358
                }else{
359
                    registerGCM();
14792 manas 360
                }
361
            }catch (Exception e){
362
                finish();
363
            }
364
        }
365
    }
366
 
367
    class pushApkData extends AsyncTask<String, Integer, String> {
368
 
369
        @Override
370
        protected void onPreExecute() {
371
            super.onPreExecute();
372
        }
373
 
374
        @Override
375
        protected String doInBackground(String... arg0) {
376
            try {
377
                HttpClient httpclient = new DefaultHttpClient();
378
                HttpPost httppost = new HttpPost("http://api.profittill.com/devices/add");
379
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
380
                httppost.setEntity(new UrlEncodedFormEntity(apkDataListValue));
381
                HttpResponse response = httpclient.execute(httppost);
382
                HttpEntity entity = response.getEntity();
383
                int status = response.getStatusLine().getStatusCode();
384
 
385
                if(status == 200){
386
                    apkDataEditor.putString("checkId",userData.getString("id",""));
387
                    apkDataEditor.commit();
388
                    return "success";
389
                } else {
390
                    return "failure";
391
                }
392
 
393
            } catch (Exception e) {
394
                return null;
395
 
396
            }
397
 
398
        }
399
        @Override
400
        protected void onPostExecute(String result) {
401
            super.onPostExecute(result);
402
         }
403
    }
404
 
405
 
406
    public final boolean isInternetOn() {
407
 
408
        ConnectivityManager connection =
409
                (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
410
 
14991 manas 411
        if (    connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
14792 manas 412
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
413
            return true;
414
 
415
        } else if (
14991 manas 416
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
417
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
14792 manas 418
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
419
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
420
            return false;
421
        }
422
        return false;
423
    }
424
 
425
    private void showProgressDialog()
426
    {
427
        pDlg =  new ProgressDialog(getApplicationContext());
428
        pDlg.setIndeterminate(true);
429
        pDlg.setCancelable(false);
430
        pDlg.show();
431
    }
432
 
433
    ProgressDialog pDlg;
14991 manas 434
 
15588 manas 435
    public void registerGCM() {
436
 
21169 rajender 437
       // gcm = GoogleCloudMessaging.getInstance(SplashScreen.this);
15588 manas 438
        registerInBackground();
439
 
440
    }
441
 
442
    private void registerInBackground() {
443
        new AsyncTask<Void, Void, String>() {
444
            @Override
445
            protected String doInBackground(Void... params) {
446
                String msg = "";
21169 rajender 447
                if (fcm == null) {
448
                    regId = FirebaseInstanceId.getInstance().getToken();
15588 manas 449
                    msg = "Device registered";
450
                }
451
                return msg;
452
            }
453
 
454
            @Override
455
            protected void onPostExecute(String msg) {
456
                new gcmPushData().execute(msg);
457
            }
458
        }.execute(null, null, null);
459
    }
460
 
461
 
462
    class gcmPushData extends AsyncTask<String, Integer, String> {
463
 
464
        @Override
465
        protected void onPreExecute() {
466
            super.onPreExecute();
467
        }
468
 
469
        @Override
470
        protected String doInBackground(String... arg0) {
471
            String id = null;
472
            try {
473
                HttpClient httpclient = new DefaultHttpClient();
19653 manas 474
                HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url", "http://api.profittill.com/gcm_users/add"));
15588 manas 475
                nameValuePairsGcm = new ArrayList<>();
476
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
477
                nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
478
                nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
479
                nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
480
                nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
21169 rajender 481
                nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
19653 manas 482
                nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(SplashScreen.this)));
15588 manas 483
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
484
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
485
                HttpResponse response = httpclient.execute(httppost);
486
                HttpEntity entity = response.getEntity();
487
                int status = response.getStatusLine().getStatusCode();
488
 
489
                if (status == 200) {
490
                    Log.d("ResponseCode GCM ", status + "");
491
                } else {
492
                    Log.d("ResponseCode GCM ", status + "");
493
                }
494
 
495
                nameValuePairsGcm.clear();
496
                Log.e("pass 1", "connection success ");
497
            } catch (Exception e) {
498
                Log.e("Fail 1", e.toString());
499
 
500
            }
501
            return id;
502
        }
503
 
504
        @Override
505
        protected void onPostExecute(String result) {
506
            super.onPostExecute(result);
507
            final String idCheck = userData.getString("id",null);
21169 rajender 508
            if(userData.getString("docsSubmitted","false").equalsIgnoreCase("true")){
509
                Intent i = new Intent(SplashScreen.this, PendingActivity.class);
510
                startActivity(i);
511
            }
512
         else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
513
                Intent startActivityIntent=new Intent(SplashScreen.this, BasicInformation.class);
18082 manas 514
                Intent intent = getIntent();
515
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
516
                    Uri uri = intent.getData();
517
                    startActivityIntent.setAction("31");
518
                    startActivityIntent.setData(Uri.parse(uri.toString()));
519
                }
520
                startActivity(startActivityIntent);
521
                //startActivity(new Intent(SplashScreen.this, ReferrerActivity.class));
21169 rajender 522
            }
523
            else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
18082 manas 524
                Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
525
                Intent intent = getIntent();
526
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
527
                    Uri uri = intent.getData();
528
                    startActivityIntent.setAction("31");
529
                    startActivityIntent.setData(Uri.parse(uri.toString()));
530
                }
531
                startActivity(startActivityIntent);
532
                //startActivity(new Intent(SplashScreen.this, MobileNumber.class));
15588 manas 533
            } else if (idCheck != null) {
16080 manas 534
                Intent startActivityIntent=new Intent(SplashScreen.this, MainActivity.class);
535
                Intent intent = getIntent();
536
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
537
                    Uri uri = intent.getData();
538
                    startActivityIntent.setAction("31");
539
                    startActivityIntent.setData(Uri.parse(uri.toString()));
540
                }
541
                startActivity(startActivityIntent);
15588 manas 542
            } else {
18082 manas 543
                Intent startActivityIntent=new Intent(SplashScreen.this, LoginActivity.class);
544
                Intent intent = getIntent();
545
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
546
                    Uri uri = intent.getData();
547
                    startActivityIntent.setAction("31");
548
                    startActivityIntent.setData(Uri.parse(uri.toString()));
549
                }
550
                startActivity(startActivityIntent);
551
                //startActivity(new Intent(SplashScreen.this, LoginActivity.class));
15588 manas 552
            }
553
        }
554
    }
17057 manas 555
}