Subversion Repositories SmartDukaan

Rev

Rev 14991 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14991 Rev 15356
Line 1... Line 1...
1
package com.saholic.profittill.main;
1
package com.saholic.profittill.main;
2
 
2
 
3
import android.app.Activity;
-
 
4
import android.app.AlertDialog;
3
import android.app.AlertDialog;
-
 
4
import android.app.Fragment;
5
import android.app.ProgressDialog;
5
import android.app.ProgressDialog;
6
import android.content.Context;
6
import android.content.Context;
7
import android.content.DialogInterface;
-
 
8
import android.content.Intent;
7
import android.content.Intent;
9
import android.content.SharedPreferences;
8
import android.content.SharedPreferences;
10
import android.graphics.Bitmap;
9
import android.graphics.Bitmap;
11
import android.net.ConnectivityManager;
10
import android.net.ConnectivityManager;
12
import android.net.Uri;
11
import android.net.Uri;
13
import android.os.AsyncTask;
12
import android.os.AsyncTask;
14
import android.os.Bundle;
13
import android.os.Bundle;
15
import android.app.Fragment;
14
import android.os.Handler;
16
import android.util.Base64;
15
import android.util.Base64;
17
import android.util.Log;
16
import android.util.Log;
18
import android.view.KeyEvent;
17
import android.view.KeyEvent;
19
import android.view.LayoutInflater;
18
import android.view.LayoutInflater;
20
import android.view.View;
19
import android.view.View;
Line 23... Line 22...
23
import android.webkit.CookieSyncManager;
22
import android.webkit.CookieSyncManager;
24
import android.webkit.HttpAuthHandler;
23
import android.webkit.HttpAuthHandler;
25
import android.webkit.JavascriptInterface;
24
import android.webkit.JavascriptInterface;
26
import android.webkit.WebView;
25
import android.webkit.WebView;
27
import android.webkit.WebViewClient;
26
import android.webkit.WebViewClient;
28
import android.widget.Button;
-
 
29
import android.widget.Toast;
-
 
30
 
27
 
31
import com.facebook.Session;
28
import com.facebook.Session;
32
import com.google.android.gms.analytics.GoogleAnalytics;
-
 
33
import com.google.android.gms.analytics.HitBuilders;
-
 
34
import com.google.android.gms.analytics.Tracker;
-
 
35
import com.google.android.gms.auth.GoogleAuthUtil;
-
 
36
import com.google.android.gms.common.ConnectionResult;
29
import com.google.android.gms.common.ConnectionResult;
37
import com.google.android.gms.common.Scopes;
-
 
38
import com.google.android.gms.common.api.GoogleApiClient;
30
import com.google.android.gms.common.api.GoogleApiClient;
39
import com.google.android.gms.plus.Plus;
31
import com.google.android.gms.plus.Plus;
40
import com.mixpanel.android.mpmetrics.MixpanelAPI;
32
import com.mixpanel.android.mpmetrics.MixpanelAPI;
41
import com.saholic.profittill.Constants.ProfitTillConstants;
33
import com.saholic.profittill.Constants.ProfitTillConstants;
42
import com.saholic.profittill.R;
34
import com.saholic.profittill.R;
Line 60... Line 52...
60
 
52
 
61
import java.io.IOException;
53
import java.io.IOException;
62
import java.text.SimpleDateFormat;
54
import java.text.SimpleDateFormat;
63
import java.util.ArrayList;
55
import java.util.ArrayList;
64
import java.util.Date;
56
import java.util.Date;
65
import java.util.HashMap;
-
 
66
import java.util.List;
57
import java.util.List;
67
import java.util.Map;
-
 
68
 
58
 
69
public class MyWallet extends Fragment {
59
public class MyWallet extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
70
    WebView myWalletWebView;
60
    WebView myWalletWebView;
71
    SharedPreferences userData;
61
    SharedPreferences userData;
72
    SharedPreferences apiData;
62
    SharedPreferences apiData;
73
    SharedPreferences.Editor userDataEditor;
63
    SharedPreferences.Editor userDataEditor;
74
    SharedPreferences.Editor apiSettingsEditor;
64
    SharedPreferences.Editor apiSettingsEditor;
75
    String saholicUrl;
65
    String saholicUrl;
76
    JSONObject jSaholic;
66
    JSONObject jSaholic;
77
    JSONArray jsArraySaholic;
67
    JSONArray jsArraySaholic;
78
    int pageRedirect=0;
68
    int pageRedirect=0;
79
    boolean displayActionBar;
69
    boolean displayActionBar;
-
 
70
    String type1;
-
 
71
    public GoogleApiClient mGoogleApiClient;
80
    ArrayList<NameValuePair> nameValuePairsRawHtml;
72
    ArrayList<NameValuePair> nameValuePairsRawHtml;
81
    public MyWallet() {
73
    public MyWallet() {
82
 
74
 
83
    }
75
    }
84
 
76
 
Line 105... Line 97...
105
            props.put("Screen", "My Wallet");
97
            props.put("Screen", "My Wallet");
106
            mixpanel.track("My Wallet", props);
98
            mixpanel.track("My Wallet", props);
107
        } catch (JSONException e) {
99
        } catch (JSONException e) {
108
            e.printStackTrace();
100
            e.printStackTrace();
109
        }
101
        }
-
 
102
        type1 = userData.getString("type",null);
110
        if(isInternetOn()) {
103
        if(isInternetOn()) {
-
 
104
            if(type1!=null && type1.equalsIgnoreCase("google")) {
-
 
105
                mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
-
 
106
                        .addConnectionCallbacks(this)
-
 
107
                        .addOnConnectionFailedListener(this).addApi(Plus.API)
-
 
108
                        .addScope(Plus.SCOPE_PLUS_LOGIN).build();
-
 
109
                mGoogleApiClient.connect();
-
 
110
            }
111
            WebClientClass webViewClient = new WebClientClass();
111
            WebClientClass webViewClient = new WebClientClass();
112
            myWalletWebView = (WebView) rootView.findViewById(R.id.myWalletWebView);
112
            myWalletWebView = (WebView) rootView.findViewById(R.id.myWalletWebView);
113
            myWalletWebView.setClickable(true);
113
            myWalletWebView.setClickable(true);
114
            myWalletWebView.setFocusableInTouchMode(true);
114
            myWalletWebView.setFocusableInTouchMode(true);
115
            myWalletWebView.getSettings().setDomStorageEnabled(true);
115
            myWalletWebView.getSettings().setDomStorageEnabled(true);
116
            myWalletWebView.getSettings().setJavaScriptEnabled(true);
116
            myWalletWebView.getSettings().setJavaScriptEnabled(true);
117
            myWalletWebView.addJavascriptInterface(new javascriptInterface(getActivity()), "HTMLOUT");
117
            myWalletWebView.addJavascriptInterface(new javascriptInterface(getActivity()), "HTMLOUT");
118
            if(Uri.parse(landingUrl).getHost().contains("api.profittill")) {
118
            if(Uri.parse(landingUrl).getHost().contains("api.profittill")) {
119
                userDataEditor.putString("saholic.data", UtilityFunctions.clearCookiesNew(landingUrl,getActivity())).commit();
119
                userDataEditor.putString("saholic.data", UtilityFunctions.clearCookiesNew(landingUrl,getActivity())).commit();
120
            }
120
            }
-
 
121
            CookieSyncManager.createInstance(getActivity());
-
 
122
            CookieManager cookieManager = CookieManager.getInstance();
-
 
123
            cookieManager.setCookie(url, "token="+userData.getString("token",""));
-
 
124
            cookieManager.setCookie(url, "walletAuthentication=true");
121
            myWalletWebView.loadUrl(landingUrl);
125
            myWalletWebView.loadUrl(landingUrl);
122
            myWalletWebView.setWebViewClient(webViewClient);
126
            myWalletWebView.setWebViewClient(webViewClient);
123
        }else{
127
        }else{
124
            SearchFragment nextFrag= new SearchFragment();
128
            SearchFragment nextFrag= new SearchFragment();
125
            Bundle args = new Bundle();
129
            Bundle args = new Bundle();
Line 185... Line 189...
185
            return apiData.getString("store.code.spice",null);
189
            return apiData.getString("store.code.spice",null);
186
        }
190
        }
187
        return null;
191
        return null;
188
    }
192
    }
189
 
193
 
-
 
194
    @Override
-
 
195
    public void onConnected(Bundle bundle) {
-
 
196
 
-
 
197
    }
-
 
198
 
-
 
199
    @Override
-
 
200
    public void onConnectionSuspended(int i) {
-
 
201
 
-
 
202
    }
-
 
203
 
-
 
204
    @Override
-
 
205
    public void onConnectionFailed(ConnectionResult connectionResult) {
-
 
206
 
-
 
207
    }
-
 
208
 
190
    public class WebClientClass extends WebViewClient {
209
    public class WebClientClass extends WebViewClient {
191
        ProgressDialog pd = null;
210
        ProgressDialog pd = null;
192
 
211
 
193
        @Override
212
        @Override
194
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
213
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
195
            super.onPageStarted(view, url, favicon);
214
            super.onPageStarted(view, url, favicon);
196
            CookieSyncManager.createInstance(getActivity());
-
 
197
            CookieManager cookieManager = CookieManager.getInstance();
-
 
198
            cookieManager.setCookie(url, "token="+userData.getString("token",""));
-
 
199
          try {
215
          try {
200
                getDisplayActionBar(url);
216
                getDisplayActionBar(url);
201
            }catch (Exception e){
217
            }catch (Exception e){
202
                e.printStackTrace();
218
                e.printStackTrace();
203
            }
219
            }
Line 205... Line 221...
205
        @Override
221
        @Override
206
        public void onPageFinished(WebView view, String url) {
222
        public void onPageFinished(WebView view, String url) {
207
            super.onPageFinished(view, url);
223
            super.onPageFinished(view, url);
208
            obj = new JSONObject();
224
            obj = new JSONObject();
209
            try {
225
            try {
210
                if(Uri.parse(url).getHost().contains("saholic") && apiData.getString("saholic.data","false").equalsIgnoreCase("true")){
226
                if(Uri.parse(url).getHost().contains("saholic") && userData.getString("saholic.data","false").equalsIgnoreCase("true")){
211
                    saholicUrl=url;
227
                    saholicUrl=url;
212
                    view.loadUrl("javascript:window.HTMLOUT.saholicHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
228
                    view.loadUrl("javascript:window.HTMLOUT.saholicHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
213
                }else {
229
                }else {
214
                    JSONObject j = new JSONObject();
230
                    JSONObject j = new JSONObject();
215
                    JSONArray jsArray = new JSONArray();
231
                    JSONArray jsArray = new JSONArray();
Line 277... Line 293...
277
                    .replace(R.id.frame_container, nextFrag)
293
                    .replace(R.id.frame_container, nextFrag)
278
                    .addToBackStack(null)
294
                    .addToBackStack(null)
279
                    .commit();
295
                    .commit();
280
        }
296
        }
281
 
297
 
282
      /*  @Override
298
        @Override
283
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
299
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
-
 
300
 
-
 
301
            if(url.contains(apiData.getString("wallet.auth","api.profittill.com/special/native/login"))){
-
 
302
                /*Toast.makeText(getActivity(), apiData.getString("wallet.error", "Error while logging in to the wallet. Please log in again"), Toast.LENGTH_LONG).show();*/
-
 
303
                AlertDialog alert;
-
 
304
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
-
 
305
                builder.setMessage(apiData.getString("wallet.auth.message","You are not authenticated. Please log in again")).setCancelable(false);
-
 
306
                alert = builder.create();
-
 
307
                alert.show();
-
 
308
                android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
-
 
309
                android.webkit.CookieSyncManager.createInstance(getActivity());
-
 
310
                cookieManager.removeAllCookie();
-
 
311
                try{
-
 
312
                    if (mGoogleApiClient.isConnected()) {
-
 
313
                        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
-
 
314
                        mGoogleApiClient.disconnect();
-
 
315
                        mGoogleApiClient.connect();
-
 
316
                        redirectPage(alert);
284
            Map<String, String> headers1 = new HashMap<>();
317
                        /*userDataEditor.clear().commit();
-
 
318
                        startActivity(new Intent(getActivity(), LoginActivity.class));*/
-
 
319
                    }
-
 
320
                    else if(Session.getActiveSession() != null){
-
 
321
                        Session.getActiveSession().closeAndClearTokenInformation();
-
 
322
                        Session.setActiveSession(null);
-
 
323
                        redirectPage(alert);
285
            headers1.put("token", userData.getString("token",""));
324
                        /*userDataEditor.clear().commit();
-
 
325
                        startActivity(new Intent(getActivity(), LoginActivity.class));*/
-
 
326
                    }
-
 
327
                    else{
-
 
328
                        if(!mGoogleApiClient.isConnected() || Session.getActiveSession()==null){
-
 
329
                            redirectPage(alert);
286
            Log.d("Token","Token " + userData.getString("token",""));
330
                        /*    userDataEditor.clear().commit();
-
 
331
                            startActivity(new Intent(getActivity(), LoginActivity.class));*/
-
 
332
                        }
-
 
333
                    }
-
 
334
                }catch (Exception e){
287
            view.loadUrl(url,headers1);
335
                    redirectPage(alert);
-
 
336
                   /* userDataEditor.clear().commit();
-
 
337
                    startActivity(new Intent(getActivity(), LoginActivity.class));*/
-
 
338
                }
288
            return true;
339
                return true;
-
 
340
            }
-
 
341
            return false;
289
        }*/
342
        }
290
    }
343
    }
-
 
344
    public void redirectPage(final AlertDialog builder){
-
 
345
        Handler h =new Handler();
-
 
346
        h.postDelayed(new Runnable() {
-
 
347
            @Override
-
 
348
            public void run() {
-
 
349
                if(builder!=null){
-
 
350
                    builder.dismiss();
-
 
351
                }
-
 
352
                userDataEditor.clear().commit();
-
 
353
                startActivity(new Intent(getActivity(), LoginActivity.class));
-
 
354
            }
-
 
355
        },3000);
291
 
356
 
-
 
357
    }
292
    @Override
358
    @Override
293
    public void onResume() {
359
    public void onResume() {
294
        super.onResume();
360
        super.onResume();
295
        getView().setFocusableInTouchMode(true);
361
        getView().setFocusableInTouchMode(true);
296
        getView().requestFocus();
362
        getView().requestFocus();
Line 314... Line 380...
314
    class pushData extends AsyncTask<String, Integer, String> {
380
    class pushData extends AsyncTask<String, Integer, String> {
315
 
381
 
316
        @Override
382
        @Override
317
        protected void onPreExecute() {
383
        protected void onPreExecute() {
318
            super.onPreExecute();
384
            super.onPreExecute();
319
            Log.e("Push Service", "Pre execute");
-
 
320
        }
385
        }
321
 
386
 
322
        @Override
387
        @Override
323
        protected String doInBackground(String... arg0) {
388
        protected String doInBackground(String... arg0) {
324
            HttpClient httpclient = new DefaultHttpClient();
389
            HttpClient httpclient = new DefaultHttpClient();
Line 333... Line 398...
333
                StringEntity se = new StringEntity(obj.toString());
398
                StringEntity se = new StringEntity(obj.toString());
334
                HttpResponse response = httpclient.execute(httppost);
399
                HttpResponse response = httpclient.execute(httppost);
335
                int status = response.getStatusLine().getStatusCode();
400
                int status = response.getStatusLine().getStatusCode();
336
 
401
 
337
                if(status == 200){
402
                if(status == 200){
338
                    System.out.println("ResponseCode of record: "+ status + " is " + status);
-
 
339
                    return "success";
403
                    return "success";
340
                } else {
404
                } else {
341
                    Log.d("ResponseCode",status+"");
-
 
342
                    System.out.println(" NOT Transfered");
-
 
343
                    return "failure";
405
                    return "failure";
344
                }
406
                }
345
 
407
 
346
            } catch (ClientProtocolException e) {
408
            } catch (ClientProtocolException e) {
347
                return "failure";
409
                return "failure";
Line 375... Line 437...
375
        }
437
        }
376
        return false;
438
        return false;
377
    }
439
    }
378
    class checkGateWayProblem extends AsyncTask<String, Integer, String> {
440
    class checkGateWayProblem extends AsyncTask<String, Integer, String> {
379
 
441
 
380
        // JSONObject jobj1;
-
 
381
 
-
 
382
        @Override
442
        @Override
383
        protected void onPreExecute() {
443
        protected void onPreExecute() {
384
            super.onPreExecute();
444
            super.onPreExecute();
385
        }
445
        }
386
 
446
 
387
        @Override
447
        @Override
388
        protected String doInBackground(String... arg0) {
448
        protected String doInBackground(String... arg0) {
389
            HttpClient httpclient = new DefaultHttpClient();
449
            HttpClient httpclient = new DefaultHttpClient();
390
            try {
450
            try {
391
                //jobj1 = new JSONObject();
-
 
392
                HttpPost httppost = new HttpPost(arg0[0]);
451
                HttpPost httppost = new HttpPost(arg0[0]);
393
                final String basicAuth = "Basic " + Base64.encodeToString("dtr:dtr18Feb2015".getBytes(), Base64.NO_WRAP);
452
                final String basicAuth = "Basic " + Base64.encodeToString("dtr:dtr18Feb2015".getBytes(), Base64.NO_WRAP);
394
                httppost.setHeader("Authorization", basicAuth);
453
                httppost.setHeader("Authorization", basicAuth);
395
                HttpResponse response = httpclient.execute(httppost);
454
                HttpResponse response = httpclient.execute(httppost);
396
                int status = response.getStatusLine().getStatusCode();
455
                int status = response.getStatusLine().getStatusCode();