Subversion Repositories SmartDukaan

Rev

Rev 14991 | Go to most recent revision | Details | 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;
17
import android.view.Menu;
18
import android.view.MenuItem;
19
import android.widget.ProgressBar;
20
 
21
import com.google.android.gms.analytics.HitBuilders;
22
import com.google.android.gms.analytics.Tracker;
23
import com.saholic.profittill.Constants.ProfitTillConstants;
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
 
43
import java.util.ArrayList;
44
 
45
 
46
public class SplashScreen extends ActionBarActivity {
47
    SharedPreferences apiData, userData,apkData;
48
    SharedPreferences.Editor apiEditor,userEditor,apkDataEditor;
49
 
50
    AlertDialog dialog1;
51
    AlertDialog.Builder b;
52
    ProgressBar pd;
53
 
54
    ArrayList<NameValuePair> apkDataListValue;
55
    @Override
56
    protected void onCreate(Bundle savedInstanceState) {
57
        TestinAgent.init(this);
58
        super.onCreate(savedInstanceState);
59
        setContentView(R.layout.activity_splash_screen);
60
        getSupportActionBar().hide();
61
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
62
        apkData = getApplicationContext().getSharedPreferences("APK_Data", MODE_PRIVATE);
63
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
64
        apkDataEditor = apkData.edit();
65
        userEditor = userData.edit();
66
        apiEditor = apiData.edit();
67
        String url="";
68
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
69
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
70
        t.setScreenName("Splash Screen");
71
        t.send(new HitBuilders.ScreenViewBuilder().build());
72
        try {
73
            String apkVersionCode = apkData.getString("versionCode","");
74
            String apkVersionName = apkData.getString("versionName","");
75
            if(userData.getString("id","").isEmpty()) {
76
 
77
            }else {
78
                if (apkVersionCode.isEmpty()) {
79
                    PackageManager manager = this.getPackageManager();
80
                    PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
81
                    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
82
                    apkDataListValue = new ArrayList<>();
83
                    apkDataListValue.add(new BasicNameValuePair("user_id",userData.getString("id","")));
84
                    apkDataListValue.add(new BasicNameValuePair("versioncode",info.versionCode+""));
85
                    apkDataListValue.add(new BasicNameValuePair("versionname",info.versionName+""));
86
                    apkDataListValue.add(new BasicNameValuePair("osversion",android.os.Build.VERSION.SDK_INT+""));
87
                    apkDataListValue.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
88
                    apkDataListValue.add(new BasicNameValuePair("manufacturer",Build.MANUFACTURER));
89
                    apkDataListValue.add(new BasicNameValuePair("model",Build.MODEL));
90
                    apkDataListValue.add(new BasicNameValuePair("brand",Build.BRAND));
91
                    apkDataEditor.putString("versionCode", info.versionCode + "");
92
                    apkDataEditor.putString("versionName", info.versionName);
93
                    apkDataEditor.putString("osVersion",android.os.Build.VERSION.SDK_INT+"");
94
                    apkDataEditor.putString("imeiNumber",telephonyManager.getDeviceId());
95
 
96
                    if(isInternetOn()) {
97
                        new pushApkData().execute();
98
                    }
99
                } else if (!(apkData.getString("checkId","blank").equalsIgnoreCase(userData.getString("id","blank")))) {
100
                    PackageManager manager = this.getPackageManager();
101
                    PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
102
                    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
103
                    apkDataListValue = new ArrayList<>();
104
                    apkDataListValue.add(new BasicNameValuePair("user_id",userData.getString("id","")));
105
                    apkDataListValue.add(new BasicNameValuePair("versioncode",info.versionCode+""));
106
                    apkDataListValue.add(new BasicNameValuePair("versionname",info.versionName+""));
107
                    apkDataListValue.add(new BasicNameValuePair("osversion",android.os.Build.VERSION.SDK_INT+""));
108
                    apkDataListValue.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
109
                    apkDataListValue.add(new BasicNameValuePair("manufacturer",Build.MANUFACTURER));
110
                    apkDataListValue.add(new BasicNameValuePair("model",Build.MODEL));
111
                    apkDataListValue.add(new BasicNameValuePair("brand",Build.BRAND));
112
                    apkDataEditor.putString("versionCode",info.versionCode+"");
113
                    apkDataEditor.putString("versionName",info.versionName);
114
                    apkDataEditor.putString("osVersion",android.os.Build.VERSION.SDK_INT+"");
115
                    apkDataEditor.putString("imeiNumber",telephonyManager.getDeviceId());
116
                    if(isInternetOn()) {
117
                        new pushApkData().execute();
118
                    }
119
                } else if (Integer.parseInt(apkVersionCode) == getPackageManager().getPackageInfo(getPackageName(), 0).versionCode) {
120
 
121
                }  else {
122
                    PackageManager manager = this.getPackageManager();
123
                    PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
124
                    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
125
                    apkDataListValue = new ArrayList<>();
126
                    apkDataListValue.add(new BasicNameValuePair("user_id",userData.getString("id","")));
127
                    apkDataListValue.add(new BasicNameValuePair("versioncode",info.versionCode+""));
128
                    apkDataListValue.add(new BasicNameValuePair("versionname",info.versionName+""));
129
                    apkDataListValue.add(new BasicNameValuePair("osversion",android.os.Build.VERSION.SDK_INT+""));
130
                    apkDataListValue.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
131
                    apkDataListValue.add(new BasicNameValuePair("manufacturer",Build.MANUFACTURER));
132
                    apkDataListValue.add(new BasicNameValuePair("model",Build.MODEL));
133
                    apkDataListValue.add(new BasicNameValuePair("brand",Build.BRAND));
134
                    apkDataEditor.putString("versionCode",info.versionCode+"");
135
                    apkDataEditor.putString("versionName",info.versionName);
136
                    apkDataEditor.putString("osVersion",android.os.Build.VERSION.SDK_INT+"");
137
                    apkDataEditor.putString("imeiNumber",telephonyManager.getDeviceId());
138
                    if(isInternetOn()) {
139
                        new pushApkData().execute();
140
                    }
141
                }
142
            }
143
        }catch (Exception e){
144
            e.printStackTrace();
145
        }
146
 
147
 
148
        if(isInternetOn()){
149
                new fetchAPISettings().execute();
150
        }
151
        else{
152
            b= new AlertDialog.Builder(this);
153
            b.setMessage("No internet connection. Please try again.");
154
            b.setCancelable(false);
155
            b.setPositiveButton("Retry", new DialogInterface.OnClickListener()
156
            {
157
                @Override
158
                public void onClick(DialogInterface dialog, int which) {
159
                    if(isInternetOn()){
160
                        new fetchAPISettings().execute();
161
                        dialog1.dismiss();
162
                    }
163
                    else{
164
                        dialog1 = b.create();
165
                        dialog1.show();
166
                    }
167
                }
168
 
169
            });
170
            b.setNegativeButton("No", new DialogInterface.OnClickListener() {
171
                @Override
172
                public void onClick(DialogInterface dialog, int which) {
173
                    finish();
174
 
175
                }
176
            });
177
            dialog1 = b.create();
178
            dialog1.show();
179
        }
180
 
181
 
182
    }
183
 
184
 
185
    @Override
186
    public boolean onCreateOptionsMenu(Menu menu) {
187
        // Inflate the menu; this adds items to the action bar if it is present.
188
        getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
189
        return true;
190
    }
191
 
192
    @Override
193
    public boolean onOptionsItemSelected(MenuItem item) {
194
        // Handle action bar item clicks here. The action bar will
195
        // automatically handle clicks on the Home/Up button, so long
196
        // as you specify a parent activity in AndroidManifest.xml.
197
        int id = item.getItemId();
198
 
199
        //noinspection SimplifiableIfStatement
200
        if (id == R.id.action_settings) {
201
            return true;
202
        }
203
 
204
        return super.onOptionsItemSelected(item);
205
    }
206
 
207
    class fetchAPISettings extends AsyncTask<String, Integer, JSONObject> {
208
 
209
        @Override
210
        protected void onPreExecute() {
211
            super.onPreExecute();
212
            /*final Dialog dialog = new Dialog(getApplicationContext(), android.R.style.Theme_Translucent);
213
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
214
            //here we set layout of progress dialog
215
            dialog.setContentView(R.layout.activity_splash_internet_not_working);*/
216
            //bar.setVisibility(View.VISIBLE);
217
            //showProgressDialog();
218
        }
219
 
220
        @Override
221
        protected JSONObject doInBackground(String... arg0) {
222
            try {
223
 
224
                HttpClient httpclient = new DefaultHttpClient();
225
                HttpPost httppost = new HttpPost(ProfitTillConstants.MOBILE_API+"?t="+apkData.getString("timestamp","0"));
226
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
227
                HttpResponse response = httpclient.execute(httppost);
228
                HttpEntity entity = response.getEntity();
229
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
230
                System.out.println(jObjGmail.toString());
231
                return jObjGmail;
232
 
233
            } catch (Exception e) {
234
                return null;
235
 
236
            }
237
 
238
        }
239
        @Override
240
        protected void onPostExecute(JSONObject result) {
241
            super.onPostExecute(result);
242
            if(result==null){
243
                finish();
244
            }
245
            try {
246
                JSONArray objects = result.getJSONArray("settings");
247
                if(objects.length()==0){
248
                    System.out.println("Null settings array");
249
                }else {
250
                    for (int i = 0; i < objects.length(); i++) {
251
                        JSONObject jsonObject = objects.getJSONObject(i);
252
                        JSONObject j = jsonObject.getJSONObject("Mobileappsetting");
253
                        System.out.println("Setting " + j.getString("setting"));
254
                        System.out.println("Value " + j.getString("value"));
255
                        apiEditor.remove(j.getString("setting"));
256
                        apiEditor.commit();
257
                        apiEditor.putString(j.getString("setting"), j.getString("value"));
258
                        apiEditor.commit();
259
                    }
260
                }
261
                String timestamp=result.getString("t");
262
                System.out.println("Time stamp " + timestamp);
263
                apkDataEditor.putString("timestamp",timestamp);
264
                apkDataEditor.commit();
265
                final String idCheck = userData.getString("id",null);
266
                //String check = userData.getString("which screen needs to be displayed");
267
                if(userData.getString("referralCodeRequired","false").equalsIgnoreCase("true")){
268
                    startActivity(new Intent(SplashScreen.this,ReferrerActivity.class));
269
                }else if(userData.getString("message","false").equalsIgnoreCase("true")){
270
                    startActivity(new Intent(SplashScreen.this,MobileNumber.class));
271
                }else if(idCheck!=null){
272
                    startActivity(new Intent(SplashScreen.this,MainActivity.class));
273
                }else {
274
                    startActivity(new Intent(SplashScreen.this, LoginActivity.class));
275
 
276
                }
277
            }catch (Exception e){
278
                finish();
279
            }
280
        }
281
    }
282
 
283
    class pushApkData extends AsyncTask<String, Integer, String> {
284
 
285
        @Override
286
        protected void onPreExecute() {
287
            super.onPreExecute();
288
            // showProgressDialog();
289
        }
290
 
291
        @Override
292
        protected String doInBackground(String... arg0) {
293
            try {
294
                HttpClient httpclient = new DefaultHttpClient();
295
                HttpPost httppost = new HttpPost("http://api.profittill.com/devices/add");
296
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
297
                httppost.setEntity(new UrlEncodedFormEntity(apkDataListValue));
298
                HttpResponse response = httpclient.execute(httppost);
299
                HttpEntity entity = response.getEntity();
300
                int status = response.getStatusLine().getStatusCode();
301
 
302
                if(status == 200){
303
                    apkDataEditor.putString("checkId",userData.getString("id",""));
304
                    apkDataEditor.commit();
305
                    return "success";
306
                } else {
307
                    return "failure";
308
                }
309
 
310
            } catch (Exception e) {
311
                return null;
312
 
313
            }
314
 
315
        }
316
        @Override
317
        protected void onPostExecute(String result) {
318
            super.onPostExecute(result);
319
         }
320
    }
321
 
322
 
323
    public final boolean isInternetOn() {
324
 
325
        ConnectivityManager connection =
326
                (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
327
 
328
        if (  connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
329
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
330
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
331
                connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
332
 
333
            /*Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();*/
334
            return true;
335
 
336
        } else if (
337
 
338
                connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
339
                        connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
340
 
341
 /*           Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();*/
342
            return false;
343
        }
344
        return false;
345
    }
346
 
347
    private void showProgressDialog()
348
    {
349
       // bar = (ProgressBar) this.findViewById(R.id.progressBar);
350
       /* pDlg =  new ProgressDialog(new ContextThemeWrapper(SplashScreen.this, android.R.style.Theme_Holo_Light_Dialog));
351
        pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
352
        pDlg.setIndeterminate(true);
353
        pDlg.setCancelable(false);
354
        pDlg.setProgressDrawable(getResources().getDrawable(R.drawable.greenprogressdialog));
355
        pDlg.show();*/
356
 
357
       // pDlg.setContentView(R.layout.activity_splash_internet_not_working);
358
        pDlg =  new ProgressDialog(getApplicationContext());
359
        pDlg.setIndeterminate(true);
360
        pDlg.setCancelable(false);
361
        pDlg.show();
362
    }
363
 
364
    ProgressDialog pDlg;
365
    //ProgressBar bar;
366
}