Subversion Repositories SmartDukaan

Rev

Rev 19653 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19653 Rev 21169
Line 1... Line 1...
1
package com.saholic.profittill;
1
package com.saholic.profittill;
2
 
2
 
-
 
3
import android.Manifest;
3
import android.app.AlertDialog;
4
import android.app.AlertDialog;
4
import android.app.ProgressDialog;
5
import android.app.ProgressDialog;
5
import android.content.Context;
6
import android.content.Context;
6
import android.content.DialogInterface;
7
import android.content.DialogInterface;
7
import android.content.Intent;
8
import android.content.Intent;
8
import android.content.SharedPreferences;
9
import android.content.SharedPreferences;
-
 
10
import android.content.pm.PackageManager;
9
import android.net.ConnectivityManager;
11
import android.net.ConnectivityManager;
10
import android.net.Uri;
12
import android.net.Uri;
11
import android.os.AsyncTask;
13
import android.os.AsyncTask;
-
 
14
import android.os.Build;
12
import android.os.Bundle;
15
import android.os.Bundle;
-
 
16
import android.support.annotation.NonNull;
-
 
17
import android.support.v4.app.ActivityCompat;
-
 
18
import android.support.v4.content.ContextCompat;
13
import android.support.v7.app.ActionBarActivity;
19
import android.support.v7.app.ActionBarActivity;
14
import android.telephony.TelephonyManager;
20
import android.telephony.TelephonyManager;
15
import android.util.Log;
21
import android.util.Log;
16
import android.view.Menu;
22
import android.view.Menu;
17
import android.view.MenuItem;
23
import android.view.MenuItem;
18
import android.widget.ProgressBar;
24
import android.widget.ProgressBar;
19
 
-
 
-
 
25
import android.widget.Toast;
20
import com.google.android.gms.analytics.HitBuilders;
26
import com.google.android.gms.analytics.HitBuilders;
21
import com.google.android.gms.analytics.Tracker;
27
import com.google.android.gms.analytics.Tracker;
-
 
28
import com.google.android.gms.common.ConnectionResult;
22
import com.google.android.gms.gcm.GoogleCloudMessaging;
29
import com.google.android.gms.common.api.GoogleApiClient;
-
 
30
 
-
 
31
import com.google.firebase.iid.FirebaseInstanceId;
23
import com.saholic.profittill.Constants.ProfitTillConstants;
32
import com.saholic.profittill.Constants.ProfitTillConstants;
24
import com.saholic.profittill.Utils.UtilityFunctions;
33
import com.saholic.profittill.Utils.UtilityFunctions;
-
 
34
import com.saholic.profittill.main.BasicInformation;
25
import com.saholic.profittill.main.GoogleAnalyticsTracker;
35
import com.saholic.profittill.main.GoogleAnalyticsTracker;
26
import com.saholic.profittill.main.LoginActivity;
36
import com.saholic.profittill.main.LoginActivity;
27
import com.saholic.profittill.main.MainActivity;
37
import com.saholic.profittill.main.MainActivity;
28
import com.saholic.profittill.main.MobileNumber;
38
import com.saholic.profittill.main.MobileNumber;
29
import com.saholic.profittill.main.ReferrerActivity;
39
import com.saholic.profittill.main.PendingActivity;
30
import com.testin.agent.TestinAgent;
40
import com.testin.agent.TestinAgent;
31
 
41
 
32
import org.apache.http.HttpEntity;
42
import org.apache.http.HttpEntity;
33
import org.apache.http.HttpResponse;
43
import org.apache.http.HttpResponse;
34
import org.apache.http.NameValuePair;
44
import org.apache.http.NameValuePair;
Line 39... Line 49...
39
import org.apache.http.message.BasicNameValuePair;
49
import org.apache.http.message.BasicNameValuePair;
40
import org.apache.http.util.EntityUtils;
50
import org.apache.http.util.EntityUtils;
41
import org.json.JSONArray;
51
import org.json.JSONArray;
42
import org.json.JSONObject;
52
import org.json.JSONObject;
43
 
53
 
44
import java.io.IOException;
-
 
45
import java.util.ArrayList;
54
import java.util.ArrayList;
46
 
55
 
47
 
56
 
48
public class SplashScreen extends ActionBarActivity {
57
public class SplashScreen extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
49
    SharedPreferences apiData, userData,apkData;
58
    SharedPreferences apiData, userData, apkData;
50
    SharedPreferences.Editor apiEditor,userEditor,apkDataEditor;
59
    SharedPreferences.Editor apiEditor, userEditor, apkDataEditor;
51
    UtilityFunctions utilityFunctions;
60
    UtilityFunctions utilityFunctions;
52
    AlertDialog dialog1;
61
    AlertDialog dialog1;
53
    AlertDialog.Builder b;
62
    AlertDialog.Builder b;
54
    ProgressBar pd;
63
    ProgressBar pd;
55
    GoogleCloudMessaging gcm;
64
    FirebaseInstanceId fcm;
56
    String regId;
65
    String regId;
57
    ArrayList<NameValuePair> apkDataListValue;
66
    ArrayList<NameValuePair> apkDataListValue;
58
    ArrayList<NameValuePair> nameValuePairsGcm;
67
    ArrayList<NameValuePair> nameValuePairsGcm;
-
 
68
    public static final int READ_PHONE_STATE_PERMISSION = 100;
-
 
69
    private boolean PermissionIngranted=false;
59
    @Override
70
    @Override
60
    protected void onCreate(Bundle savedInstanceState) {
71
    protected void onCreate(Bundle savedInstanceState) {
61
        TestinAgent.init(this);
72
        TestinAgent.init(this);
62
        super.onCreate(savedInstanceState);
73
        super.onCreate(savedInstanceState);
63
        setContentView(R.layout.activity_splash_screen);
74
        setContentView(R.layout.activity_splash_screen);
Line 66... Line 77...
66
        apkData = getApplicationContext().getSharedPreferences("APK_Data", MODE_PRIVATE);
77
        apkData = getApplicationContext().getSharedPreferences("APK_Data", MODE_PRIVATE);
67
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
78
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
68
        apkDataEditor = apkData.edit();
79
        apkDataEditor = apkData.edit();
69
        userEditor = userData.edit();
80
        userEditor = userData.edit();
70
        apiEditor = apiData.edit();
81
        apiEditor = apiData.edit();
-
 
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
        }
71
        String url="";
88
        String url = "";
72
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
89
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
73
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
90
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
74
        t.setScreenName("Splash Screen");
91
        t.setScreenName("Splash Screen");
75
        t.send(new HitBuilders.ScreenViewBuilder().build());
92
        t.send(new HitBuilders.ScreenViewBuilder().build());
76
        try {
93
        try {
Line 104... Line 121...
104
            }
121
            }
105
        }catch (Exception e){
122
        }catch (Exception e){
106
            e.printStackTrace();
123
            e.printStackTrace();
107
        }
124
        }
108
        if(isInternetOn()){
125
        if(isInternetOn()){
-
 
126
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-
 
127
                requestphoneUpdates();
-
 
128
            }
-
 
129
            else{
109
                new fetchAPISettings().execute();
130
                    new fetchAPISettings().execute();
-
 
131
                }
110
        }
132
        }
111
        else{
133
        else{
112
            b= new AlertDialog.Builder(this);
134
            b= new AlertDialog.Builder(this);
113
            b.setMessage("No internet connection. Please try again.");
135
            b.setMessage("No internet connection. Please try again.");
114
            b.setCancelable(false);
136
            b.setCancelable(false);
Line 138... Line 160...
138
            dialog1.show();
160
            dialog1.show();
139
        }
161
        }
140
    }
162
    }
141
 
163
 
142
 
164
 
-
 
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
  }
-
 
181
    @Override
-
 
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
 
143
    @Override
235
    @Override
144
    public boolean onCreateOptionsMenu(Menu menu) {
236
    public boolean onCreateOptionsMenu(Menu menu) {
145
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
237
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
146
        return true;
238
        return true;
147
    }
239
    }
Line 154... Line 246...
154
        }
246
        }
155
 
247
 
156
        return super.onOptionsItemSelected(item);
248
        return super.onOptionsItemSelected(item);
157
    }
249
    }
158
 
250
 
-
 
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
 
159
    class fetchAPISettings extends AsyncTask<String, Integer, JSONObject> {
268
    class fetchAPISettings extends AsyncTask<String, Integer, JSONObject> {
160
 
269
 
161
        @Override
270
        @Override
162
        protected void onPreExecute() {
271
        protected void onPreExecute() {
163
            super.onPreExecute();
272
            super.onPreExecute();
164
        }
273
        }
165
 
274
 
166
        @Override
275
        @Override
167
        protected JSONObject doInBackground(String... arg0) {
276
        protected JSONObject doInBackground(String... arg0) {
168
            try {
277
            try {
-
 
278
 
169
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
279
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
170
                HttpClient httpclient = new DefaultHttpClient();
280
                HttpClient httpclient = new DefaultHttpClient();
171
                String url = ProfitTillConstants.MOBILE_API+"?t="+apkData.getString("timestamp","0")+"&imeinumber="+telephonyManager.getDeviceId();
281
                String url = ProfitTillConstants.MOBILE_API+"?t="+apkData.getString("timestamp","0")+"&imeinumber="+telephonyManager.getDeviceId();
172
                HttpPost httppost=new HttpPost(url);
282
                HttpPost httppost=new HttpPost(url);
173
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
283
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
Line 175... Line 285...
175
                HttpEntity entity = response.getEntity();
285
                HttpEntity entity = response.getEntity();
176
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
286
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
177
                return jObjGmail;
287
                return jObjGmail;
178
 
288
 
179
            } catch (Exception e) {
289
            } catch (Exception e) {
-
 
290
                e.printStackTrace();
180
                return null;
291
                return null;
181
 
292
 
182
            }
293
            }
183
 
294
 
184
        }
295
        }
Line 206... Line 317...
206
                String failureCount=result.getString("failureCount");
317
                String failureCount=result.getString("failureCount");
207
                apkDataEditor.putString("timestamp",timestamp);
318
                apkDataEditor.putString("timestamp",timestamp);
208
                apkDataEditor.commit();
319
                apkDataEditor.commit();
209
                final String idCheck = userData.getString("id",null);
320
                final String idCheck = userData.getString("id",null);
210
                if(Integer.parseInt(failureCount)<=0) {
321
                if(Integer.parseInt(failureCount)<=0) {
211
                    if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
322
                    if(userData.getString("docsSubmitted","false").equalsIgnoreCase("true")){
212
                        Intent startActivityIntent=new Intent(SplashScreen.this, ReferrerActivity.class);
323
                        Intent i = new Intent(SplashScreen.this, PendingActivity.class);
213
                        Intent intent = getIntent();
324
                        startActivity(i);
214
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
-
 
215
                            Uri uri = intent.getData();
325
                    }
216
                            startActivityIntent.setAction("31");
326
                   else  if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
217
                            startActivityIntent.setData(Uri.parse(uri.toString()));
327
                        Intent startActivityIntent=new Intent(SplashScreen.this, BasicInformation.class);
218
                        }
-
 
-
 
328
 
219
                        startActivity(startActivityIntent);
329
                        startActivity(startActivityIntent);
220
                    } else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
330
                    } else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
221
                        Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
331
                        Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
222
                        Intent intent = getIntent();
332
                        Intent intent = getIntent();
223
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
333
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Line 234... Line 344...
234
                            startActivityIntent.setAction("31");
344
                            startActivityIntent.setAction("31");
235
                            startActivityIntent.setData(Uri.parse(uri.toString()));
345
                            startActivityIntent.setData(Uri.parse(uri.toString()));
236
                        }
346
                        }
237
                        startActivity(startActivityIntent);
347
                        startActivity(startActivityIntent);
238
                    } else {
348
                    } else {
239
                        Intent startActivityIntent=new Intent(SplashScreen.this, LoginActivity.class);
349
                        Intent startActivityIntent=new Intent(SplashScreen.this,LoginActivity   .class);
240
                        Intent intent = getIntent();
350
                        Intent intent = getIntent();
241
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
351
                        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
242
                            Uri uri = intent.getData();
352
                            Uri uri = intent.getData();
243
                            startActivityIntent.setAction("31");
353
                            startActivityIntent.setAction("31");
244
                            startActivityIntent.setData(Uri.parse(uri.toString()));
354
                            startActivityIntent.setData(Uri.parse(uri.toString()));
Line 322... Line 432...
322
 
432
 
323
    ProgressDialog pDlg;
433
    ProgressDialog pDlg;
324
 
434
 
325
    public void registerGCM() {
435
    public void registerGCM() {
326
 
436
 
327
        gcm = GoogleCloudMessaging.getInstance(SplashScreen.this);
437
       // gcm = GoogleCloudMessaging.getInstance(SplashScreen.this);
328
        registerInBackground();
438
        registerInBackground();
329
 
439
 
330
    }
440
    }
331
 
441
 
332
    private void registerInBackground() {
442
    private void registerInBackground() {
333
        new AsyncTask<Void, Void, String>() {
443
        new AsyncTask<Void, Void, String>() {
334
            @Override
444
            @Override
335
            protected String doInBackground(Void... params) {
445
            protected String doInBackground(Void... params) {
336
                String msg = "";
446
                String msg = "";
337
                try {
-
 
338
                    if (gcm == null) {
447
                if (fcm == null) {
339
                        gcm = GoogleCloudMessaging.getInstance(SplashScreen.this);
448
                    regId = FirebaseInstanceId.getInstance().getToken();
340
                    }
-
 
341
                    regId = gcm.register(ProfitTillConstants.GOOGLE_SENDER_ID);
-
 
342
                    msg = "Device registered";
449
                    msg = "Device registered";
343
 
-
 
344
                } catch (IOException ex) {
-
 
345
                    msg = "Error :" + ex.getMessage();
-
 
346
                }
450
                }
347
                return msg;
451
                return msg;
348
            }
452
            }
349
 
453
 
350
            @Override
454
            @Override
Line 372... Line 476...
372
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
476
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
373
                nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
477
                nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
374
                nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
478
                nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
375
                nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
479
                nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
376
                nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
480
                nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
-
 
481
                nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
377
                nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(SplashScreen.this)));
482
                nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(SplashScreen.this)));
378
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
483
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
379
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
484
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
380
                HttpResponse response = httpclient.execute(httppost);
485
                HttpResponse response = httpclient.execute(httppost);
381
                HttpEntity entity = response.getEntity();
486
                HttpEntity entity = response.getEntity();
Line 398... Line 503...
398
 
503
 
399
        @Override
504
        @Override
400
        protected void onPostExecute(String result) {
505
        protected void onPostExecute(String result) {
401
            super.onPostExecute(result);
506
            super.onPostExecute(result);
402
            final String idCheck = userData.getString("id",null);
507
            final String idCheck = userData.getString("id",null);
-
 
508
            if(userData.getString("docsSubmitted","false").equalsIgnoreCase("true")){
-
 
509
                Intent i = new Intent(SplashScreen.this, PendingActivity.class);
-
 
510
                startActivity(i);
-
 
511
            }
403
            if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
512
         else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
404
                Intent startActivityIntent=new Intent(SplashScreen.this, ReferrerActivity.class);
513
                Intent startActivityIntent=new Intent(SplashScreen.this, BasicInformation.class);
405
                Intent intent = getIntent();
514
                Intent intent = getIntent();
406
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
515
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
407
                    Uri uri = intent.getData();
516
                    Uri uri = intent.getData();
408
                    startActivityIntent.setAction("31");
517
                    startActivityIntent.setAction("31");
409
                    startActivityIntent.setData(Uri.parse(uri.toString()));
518
                    startActivityIntent.setData(Uri.parse(uri.toString()));
410
                }
519
                }
411
                startActivity(startActivityIntent);
520
                startActivity(startActivityIntent);
412
                //startActivity(new Intent(SplashScreen.this, ReferrerActivity.class));
521
                //startActivity(new Intent(SplashScreen.this, ReferrerActivity.class));
-
 
522
            }
413
            } else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
523
            else if (userData.getString("message", "false").equalsIgnoreCase("true")) {
414
                Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
524
                Intent startActivityIntent=new Intent(SplashScreen.this, MobileNumber.class);
415
                Intent intent = getIntent();
525
                Intent intent = getIntent();
416
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
526
                if (Intent.ACTION_VIEW.equals(intent.getAction())) {
417
                    Uri uri = intent.getData();
527
                    Uri uri = intent.getData();
418
                    startActivityIntent.setAction("31");
528
                    startActivityIntent.setAction("31");