Subversion Repositories SmartDukaan

Rev

Rev 14792 | Rev 15588 | 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.content.pm.PackageInfo;
10
import android.content.pm.PackageManager;
11
import android.net.ConnectivityManager;
12
import android.os.AsyncTask;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.support.v7.app.ActionBarActivity;
16
import android.telephony.TelephonyManager;
14991 manas 17
import android.util.Log;
14792 manas 18
import android.view.Menu;
19
import android.view.MenuItem;
20
import android.widget.ProgressBar;
14991 manas 21
import android.widget.Toast;
14792 manas 22
 
23
import com.google.android.gms.analytics.HitBuilders;
24
import com.google.android.gms.analytics.Tracker;
25
import com.saholic.profittill.Constants.ProfitTillConstants;
14991 manas 26
import com.saholic.profittill.Utils.UtilityFunctions;
14792 manas 27
import com.saholic.profittill.main.GoogleAnalyticsTracker;
28
import com.saholic.profittill.main.LoginActivity;
29
import com.saholic.profittill.main.MainActivity;
30
import com.saholic.profittill.main.MobileNumber;
31
import com.saholic.profittill.main.ReferrerActivity;
32
import com.testin.agent.TestinAgent;
33
 
34
import org.apache.http.HttpEntity;
35
import org.apache.http.HttpResponse;
36
import org.apache.http.NameValuePair;
37
import org.apache.http.client.HttpClient;
38
import org.apache.http.client.entity.UrlEncodedFormEntity;
39
import org.apache.http.client.methods.HttpPost;
40
import org.apache.http.impl.client.DefaultHttpClient;
41
import org.apache.http.message.BasicNameValuePair;
42
import org.apache.http.util.EntityUtils;
43
import org.json.JSONArray;
44
import org.json.JSONObject;
45
 
46
import java.util.ArrayList;
47
 
48
 
49
public class SplashScreen extends ActionBarActivity {
50
    SharedPreferences apiData, userData,apkData;
51
    SharedPreferences.Editor apiEditor,userEditor,apkDataEditor;
14991 manas 52
    UtilityFunctions utilityFunctions;
14792 manas 53
    AlertDialog dialog1;
54
    AlertDialog.Builder b;
55
    ProgressBar pd;
56
 
57
    ArrayList<NameValuePair> apkDataListValue;
58
    @Override
59
    protected void onCreate(Bundle savedInstanceState) {
60
        TestinAgent.init(this);
61
        super.onCreate(savedInstanceState);
14991 manas 62
        Log.d("On Create","On Create");
14792 manas 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();
71
        String url="";
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
                    /*PackageManager manager = this.getPackageManager();
14792 manas 84
                    PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
85
                    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
86
                    apkDataListValue = new ArrayList<>();
87
                    apkDataListValue.add(new BasicNameValuePair("user_id",userData.getString("id","")));
88
                    apkDataListValue.add(new BasicNameValuePair("versioncode",info.versionCode+""));
89
                    apkDataListValue.add(new BasicNameValuePair("versionname",info.versionName+""));
90
                    apkDataListValue.add(new BasicNameValuePair("osversion",android.os.Build.VERSION.SDK_INT+""));
91
                    apkDataListValue.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
92
                    apkDataListValue.add(new BasicNameValuePair("manufacturer",Build.MANUFACTURER));
93
                    apkDataListValue.add(new BasicNameValuePair("model",Build.MODEL));
94
                    apkDataListValue.add(new BasicNameValuePair("brand",Build.BRAND));
95
                    apkDataEditor.putString("versionCode", info.versionCode + "");
96
                    apkDataEditor.putString("versionName", info.versionName);
97
                    apkDataEditor.putString("osVersion",android.os.Build.VERSION.SDK_INT+"");
14991 manas 98
                    apkDataEditor.putString("imeiNumber",telephonyManager.getDeviceId());*/
99
                    utilityFunctions=new UtilityFunctions();
100
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 101
 
102
                    if(isInternetOn()) {
103
                        new pushApkData().execute();
104
                    }
105
                } else if (!(apkData.getString("checkId","blank").equalsIgnoreCase(userData.getString("id","blank")))) {
14991 manas 106
                    utilityFunctions=new UtilityFunctions();
107
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 108
                    if(isInternetOn()) {
109
                        new pushApkData().execute();
110
                    }
111
                } else if (Integer.parseInt(apkVersionCode) == getPackageManager().getPackageInfo(getPackageName(), 0).versionCode) {
112
 
113
                }  else {
14991 manas 114
                    utilityFunctions=new UtilityFunctions();
115
                    apkDataListValue = utilityFunctions.getDeviceInformation(this);
14792 manas 116
                    if(isInternetOn()) {
117
                        new pushApkData().execute();
118
                    }
119
                }
120
            }
121
        }catch (Exception e){
122
            e.printStackTrace();
123
        }
124
 
125
 
126
        if(isInternetOn()){
127
                new fetchAPISettings().execute();
128
        }
129
        else{
130
            b= new AlertDialog.Builder(this);
131
            b.setMessage("No internet connection. Please try again.");
132
            b.setCancelable(false);
133
            b.setPositiveButton("Retry", new DialogInterface.OnClickListener()
134
            {
135
                @Override
136
                public void onClick(DialogInterface dialog, int which) {
137
                    if(isInternetOn()){
138
                        new fetchAPISettings().execute();
139
                        dialog1.dismiss();
140
                    }
141
                    else{
142
                        dialog1 = b.create();
143
                        dialog1.show();
144
                    }
145
                }
146
 
147
            });
148
            b.setNegativeButton("No", new DialogInterface.OnClickListener() {
149
                @Override
150
                public void onClick(DialogInterface dialog, int which) {
151
                    finish();
152
 
153
                }
154
            });
155
            dialog1 = b.create();
156
            dialog1.show();
157
        }
158
 
159
 
160
    }
161
 
162
 
163
    @Override
164
    public boolean onCreateOptionsMenu(Menu menu) {
165
        // Inflate the menu; this adds items to the action bar if it is present.
166
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
167
        return true;
168
    }
169
 
170
    @Override
171
    public boolean onOptionsItemSelected(MenuItem item) {
172
        // Handle action bar item clicks here. The action bar will
173
        // automatically handle clicks on the Home/Up button, so long
174
        // as you specify a parent activity in AndroidManifest.xml.
175
        int id = item.getItemId();
176
 
177
        //noinspection SimplifiableIfStatement
178
        if (id == R.id.action_settings) {
179
            return true;
180
        }
181
 
182
        return super.onOptionsItemSelected(item);
183
    }
184
 
185
    class fetchAPISettings extends AsyncTask<String, Integer, JSONObject> {
186
 
187
        @Override
188
        protected void onPreExecute() {
189
            super.onPreExecute();
190
        }
191
 
192
        @Override
193
        protected JSONObject doInBackground(String... arg0) {
194
            try {
195
 
196
                HttpClient httpclient = new DefaultHttpClient();
197
                HttpPost httppost = new HttpPost(ProfitTillConstants.MOBILE_API+"?t="+apkData.getString("timestamp","0"));
198
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
199
                HttpResponse response = httpclient.execute(httppost);
200
                HttpEntity entity = response.getEntity();
201
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
202
                System.out.println(jObjGmail.toString());
203
                return jObjGmail;
204
 
205
            } catch (Exception e) {
206
                return null;
207
 
208
            }
209
 
210
        }
211
        @Override
212
        protected void onPostExecute(JSONObject result) {
213
            super.onPostExecute(result);
214
            if(result==null){
215
                finish();
216
            }
217
            try {
218
                JSONArray objects = result.getJSONArray("settings");
219
                if(objects.length()==0){
220
                    System.out.println("Null settings array");
221
                }else {
222
                    for (int i = 0; i < objects.length(); i++) {
223
                        JSONObject jsonObject = objects.getJSONObject(i);
224
                        JSONObject j = jsonObject.getJSONObject("Mobileappsetting");
225
                        System.out.println("Setting " + j.getString("setting"));
226
                        System.out.println("Value " + j.getString("value"));
227
                        apiEditor.remove(j.getString("setting"));
228
                        apiEditor.commit();
229
                        apiEditor.putString(j.getString("setting"), j.getString("value"));
230
                        apiEditor.commit();
231
                    }
232
                }
233
                String timestamp=result.getString("t");
234
                System.out.println("Time stamp " + timestamp);
235
                apkDataEditor.putString("timestamp",timestamp);
236
                apkDataEditor.commit();
237
                final String idCheck = userData.getString("id",null);
238
                //String check = userData.getString("which screen needs to be displayed");
239
                if(userData.getString("referralCodeRequired","false").equalsIgnoreCase("true")){
240
                    startActivity(new Intent(SplashScreen.this,ReferrerActivity.class));
241
                }else if(userData.getString("message","false").equalsIgnoreCase("true")){
242
                    startActivity(new Intent(SplashScreen.this,MobileNumber.class));
243
                }else if(idCheck!=null){
244
                    startActivity(new Intent(SplashScreen.this,MainActivity.class));
245
                }else {
246
                    startActivity(new Intent(SplashScreen.this, LoginActivity.class));
247
 
248
                }
249
            }catch (Exception e){
250
                finish();
251
            }
252
        }
253
    }
254
 
255
    class pushApkData extends AsyncTask<String, Integer, String> {
256
 
257
        @Override
258
        protected void onPreExecute() {
259
            super.onPreExecute();
260
        }
261
 
262
        @Override
263
        protected String doInBackground(String... arg0) {
264
            try {
265
                HttpClient httpclient = new DefaultHttpClient();
266
                HttpPost httppost = new HttpPost("http://api.profittill.com/devices/add");
267
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
268
                httppost.setEntity(new UrlEncodedFormEntity(apkDataListValue));
269
                HttpResponse response = httpclient.execute(httppost);
270
                HttpEntity entity = response.getEntity();
271
                int status = response.getStatusLine().getStatusCode();
272
 
273
                if(status == 200){
274
                    apkDataEditor.putString("checkId",userData.getString("id",""));
275
                    apkDataEditor.commit();
276
                    return "success";
277
                } else {
278
                    return "failure";
279
                }
280
 
281
            } catch (Exception e) {
282
                return null;
283
 
284
            }
285
 
286
        }
287
        @Override
288
        protected void onPostExecute(String result) {
289
            super.onPostExecute(result);
290
         }
291
    }
292
 
293
 
294
    public final boolean isInternetOn() {
295
 
296
        ConnectivityManager connection =
297
                (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
298
 
14991 manas 299
        if (    connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
14792 manas 300
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
301
 
302
            /*Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();*/
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
 
14991 manas 311
/*            Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();*/
14792 manas 312
            return false;
313
        }
314
        return false;
315
    }
316
 
317
    private void showProgressDialog()
318
    {
319
        pDlg =  new ProgressDialog(getApplicationContext());
320
        pDlg.setIndeterminate(true);
321
        pDlg.setCancelable(false);
322
        pDlg.show();
323
    }
324
 
325
    ProgressDialog pDlg;
14991 manas 326
 
327
/*
328
 
329
    @Override
330
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
331
        Log.d("On Restore Instance","On Restore Instance");
332
        super.onRestoreInstanceState(savedInstanceState);
333
    }
334
 
335
    @Override
336
    protected void onRestart() {
337
        Log.d("On Restart","On Restart");
338
        super.onRestart();
339
 
340
        try {
341
            String apkVersionCode = apkData.getString("versionCode","");
342
            String apkVersionName = apkData.getString("versionName","");
343
            if(userData.getString("id","").isEmpty()) {
344
 
345
            }else {
346
                if (apkVersionCode.isEmpty()) {
347
                    apkDataListValue = getDeviceInformation();
348
 
349
                    if(isInternetOn()) {
350
                        new pushApkData().execute();
351
                    }
352
                } else if (!(apkData.getString("checkId","blank").equalsIgnoreCase(userData.getString("id","blank")))) {
353
                    apkDataListValue = getDeviceInformation();
354
                    if(isInternetOn()) {
355
                        new pushApkData().execute();
356
                    }
357
                } else if (Integer.parseInt(apkVersionCode) == getPackageManager().getPackageInfo(getPackageName(), 0).versionCode) {
358
 
359
                }  else {
360
                    apkDataListValue = getDeviceInformation();
361
                    if(isInternetOn()) {
362
                        new pushApkData().execute();
363
                    }
364
                }
365
            }
366
        }catch (Exception e){
367
            e.printStackTrace();
368
        }
369
 
370
        if(isInternetOn()){
371
            new fetchAPISettings().execute();
372
        }
373
        else{
374
            b= new AlertDialog.Builder(this);
375
            b.setMessage("No internet connection. Please try again.");
376
            b.setCancelable(false);
377
            b.setPositiveButton("Retry", new DialogInterface.OnClickListener()
378
            {
379
                @Override
380
                public void onClick(DialogInterface dialog, int which) {
381
                    if(isInternetOn()){
382
                        new fetchAPISettings().execute();
383
                        dialog1.dismiss();
384
                    }
385
                    else{
386
                        dialog1 = b.create();
387
                        dialog1.show();
388
                    }
389
                }
390
 
391
            });
392
            b.setNegativeButton("No", new DialogInterface.OnClickListener() {
393
                @Override
394
                public void onClick(DialogInterface dialog, int which) {
395
                    finish();
396
 
397
                }
398
            });
399
            dialog1 = b.create();
400
            dialog1.show();
401
        }
402
 
403
    }
404
 
405
 
406
    public ArrayList<NameValuePair> getDeviceInformation(){
407
        ArrayList<NameValuePair> deviceDataList=null;
408
        try {
409
            PackageManager manager = this.getPackageManager();
410
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
411
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
412
            deviceDataList = new ArrayList<>();
413
            deviceDataList.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
414
            deviceDataList.add(new BasicNameValuePair("versioncode", info.versionCode + ""));
415
            deviceDataList.add(new BasicNameValuePair("versionname", info.versionName + ""));
416
            deviceDataList.add(new BasicNameValuePair("osversion", android.os.Build.VERSION.SDK_INT + ""));
417
            deviceDataList.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
418
            deviceDataList.add(new BasicNameValuePair("manufacturer", Build.MANUFACTURER));
419
            deviceDataList.add(new BasicNameValuePair("model", Build.MODEL));
420
            deviceDataList.add(new BasicNameValuePair("brand", Build.BRAND));
421
            apkDataEditor.putString("versionCode", info.versionCode + "");
422
            apkDataEditor.putString("versionName", info.versionName);
423
            apkDataEditor.putString("osVersion", android.os.Build.VERSION.SDK_INT + "");
424
            apkDataEditor.putString("imeiNumber", telephonyManager.getDeviceId());
425
        }catch (Exception e){
426
            e.printStackTrace();
427
        }
428
        return deviceDataList;
429
    }
430
*/
431
 
432
    /*@Override
433
    protected void onPause() {
434
        Log.d("On Pause","On Pause");
435
        super.onPause();
436
    }*/
14792 manas 437
}