Subversion Repositories SmartDukaan

Rev

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