Subversion Repositories SmartDukaan

Rev

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