Subversion Repositories SmartDukaan

Rev

Rev 14792 | Rev 16278 | 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.main;
2
 
3
import android.app.AlertDialog;
4
import android.content.Context;
5
import android.content.DialogInterface;
6
import android.content.Intent;
7
import android.content.SharedPreferences;
8
import android.net.ConnectivityManager;
9
import android.os.AsyncTask;
10
import android.support.v7.app.ActionBarActivity;
11
import android.os.Bundle;
15977 manas 12
import android.text.SpannableString;
13
import android.text.style.UnderlineSpan;
14792 manas 14
import android.util.Log;
15
import android.view.Menu;
16
import android.view.MenuItem;
17
import android.view.View;
18
import android.widget.Button;
19
import android.widget.EditText;
20
import android.widget.TextView;
21
import android.widget.Toast;
22
 
23
import com.facebook.Session;
24
import com.google.android.gms.analytics.HitBuilders;
25
import com.google.android.gms.analytics.Tracker;
26
import com.google.android.gms.common.ConnectionResult;
27
import com.google.android.gms.common.api.GoogleApiClient;
28
import com.google.android.gms.plus.Plus;
29
import com.mixpanel.android.mpmetrics.MixpanelAPI;
30
import com.saholic.profittill.Constants.ProfitTillConstants;
31
import com.saholic.profittill.R;
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.ClientProtocolException;
38
import org.apache.http.client.HttpClient;
39
import org.apache.http.client.entity.UrlEncodedFormEntity;
40
import org.apache.http.client.methods.HttpPost;
41
import org.apache.http.impl.client.DefaultHttpClient;
42
import org.apache.http.message.BasicNameValuePair;
43
import org.apache.http.util.EntityUtils;
44
import org.json.JSONException;
45
import org.json.JSONObject;
46
 
47
import java.io.IOException;
48
import java.text.SimpleDateFormat;
49
import java.util.ArrayList;
50
import java.util.Date;
51
 
52
public class MobileNumber extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
53
    SharedPreferences userData;
54
    SharedPreferences apiData;
55
    SharedPreferences.Editor userDataEditor;
56
    SharedPreferences.Editor apiSettingsEditor;
57
    EditText phoneNumberFirstTime;
58
    TextView skipMobileNumber;
59
    Button phoneNumberFirstTimeButton;
60
    boolean glogout;
61
    MixpanelAPI mixpanel;
62
    String type1;
63
    public GoogleApiClient mGoogleApiClient;
64
    @Override
65
    protected void onCreate(Bundle savedInstanceState) {
66
        super.onCreate(savedInstanceState);
67
        setContentView(R.layout.activity_mobile_number);
68
        TestinAgent.init(this);
69
        getSupportActionBar().hide();
70
        mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
71
        userData = getApplicationContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
72
        apiData = getApplicationContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
73
        userDataEditor = userData.edit();
74
        apiSettingsEditor = apiData.edit();
75
        TestinAgent.setUserInfo(userData.getString("id", ""));
76
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
77
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
78
        t.setScreenName("First Time Mobile Entered Screen");
79
        t.send(new HitBuilders.ScreenViewBuilder().build());
80
 
81
        type1 = userData.getString("type",null);
82
 
83
        if(type1!=null && type1.equalsIgnoreCase("google")) {
84
            mGoogleApiClient = new GoogleApiClient.Builder(this)
85
                    .addConnectionCallbacks(this)
86
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
87
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
88
            mGoogleApiClient.connect();
89
        }
90
 
91
        phoneNumberFirstTime=(EditText)findViewById(R.id.phoneNumberFirstTime);
92
        skipMobileNumber = (TextView)findViewById(R.id.skipMobileNumber);
93
        phoneNumberFirstTimeButton=(Button)findViewById(R.id.phoneNumberFirstTimeButton);
15977 manas 94
        SpannableString content = new SpannableString(skipMobileNumber.getText().toString());
95
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
96
        skipMobileNumber.setText(content);
14792 manas 97
 
98
        skipMobileNumber.setOnClickListener(new View.OnClickListener() {
99
            @Override
100
            public void onClick(View v) {
101
 
102
                JSONObject props = new JSONObject();
103
                try {
104
                    mixpanel.identify(userData.getString("id",null));
105
                    props.put("Screen", "Mobile Verification");
106
                    mixpanel.track("First Time Mobile Number Skipped",props);
107
                } catch (JSONException e) {
108
                    e.printStackTrace();
109
                }
110
                Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
111
                        GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
112
                t.send(new HitBuilders.EventBuilder()
113
                        .setCategory("First Time Mobile Entered ")
114
                        .setAction("Number not entered")
115
                        .setLabel("First Time Mobile Not entered")
116
                        .build());
117
 
118
                Intent i = new Intent(MobileNumber.this,MainActivity.class);
119
                i.putExtra("displayView","7");
120
                startActivity(i);
121
            }
122
        });
123
        phoneNumberFirstTimeButton.setOnClickListener(new View.OnClickListener() {
124
            @Override
125
            public void onClick(View v) {
126
                if(phoneNumberFirstTime.getText().toString().length()==10){
127
                    if(isInternetOn()) {
128
                        JSONObject props = new JSONObject();
129
                        try {
130
                            mixpanel.identify(userData.getString("id",null));
131
                            props.put("Screen", "Mobile Verification");
132
                            mixpanel.track("First Time Mobile Number Entered",props);
133
                        } catch (JSONException e) {
134
                            e.printStackTrace();
135
                        }
136
                        new pushVerification().execute();
137
                    }else{
138
                        Toast.makeText(getApplicationContext(), "No internet connection. Please try again.", Toast.LENGTH_SHORT).show();
139
                    }
140
 
141
                }
142
                else{
143
                    Toast.makeText(getApplicationContext(), "Mobile Number not valid.", Toast.LENGTH_SHORT).show();
144
                }
145
 
146
            }
147
        });
148
 
149
 
150
    }
151
 
152
 
153
    @Override
154
    public boolean onCreateOptionsMenu(Menu menu) {
155
        getMenuInflater().inflate(R.menu.menu_mobile_number, menu);
156
        return true;
157
    }
158
 
159
    @Override
160
    public boolean onOptionsItemSelected(MenuItem item) {
161
 
162
        int id = item.getItemId();
163
        if (id == R.id.action_settings) {
164
            return true;
165
        }
166
        return super.onOptionsItemSelected(item);
167
    }
168
 
169
    @Override
170
    public void onConnected(Bundle bundle) {
171
 
172
    }
173
 
174
    @Override
175
    public void onConnectionSuspended(int i) {
176
 
177
    }
178
 
179
    @Override
180
    public void onConnectionFailed(ConnectionResult connectionResult) {
181
 
182
    }
183
 
184
    class pushVerification extends AsyncTask<String, Integer, String> {
185
 
186
        @Override
187
        protected void onPreExecute() {
188
            super.onPreExecute();
189
            Log.e("Push Service", "Pre execute");
190
        }
191
 
192
        @Override
193
        protected String doInBackground(String... arg0) {
194
            HttpClient httpclient = new DefaultHttpClient();
195
            try {
196
 
197
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
198
                SimpleDateFormat sdf = new SimpleDateFormat(ProfitTillConstants.SQL_DATE_FORMAT);
199
                String dateUpdated = sdf.format(new Date());
200
                nameValuePairs.add(new BasicNameValuePair("id",userData.getString("id","")));
201
                nameValuePairs.add(new BasicNameValuePair("mobile_number",phoneNumberFirstTime.getText().toString()));
202
                nameValuePairs.add(new BasicNameValuePair("mobile_number_last_updated",dateUpdated));
203
                String updateURL = apiData.getString("useredit.url","");
204
                String url = updateURL+userData.getString("id","");
205
                HttpPost httppost = new HttpPost(url);
206
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
207
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
208
                HttpResponse response = httpclient.execute(httppost);
209
                HttpEntity entity = response.getEntity();
210
                int status = response.getStatusLine().getStatusCode();
211
                JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
212
                System.out.println("JSON Object in mobile " + jObjGmail.toString());
213
                String success = jObjGmail.getString("success");
214
 
215
                if(status == 200){
216
                    Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
217
                            GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
218
                    t.send(new HitBuilders.EventBuilder()
219
                            .setCategory("First Time Mobile Entered ")
220
                            .setAction("Number entered")
221
                            .setLabel("First Time Mobile entered")
222
                            .build());
223
 
224
                } else {
225
                    System.out.println(" NOT Transfered");
226
 
227
                }
228
 
229
                if(success.equalsIgnoreCase("true")){
230
                    userDataEditor.putString("message","false");;
231
                    userDataEditor.commit();
232
                    return "true";
233
                }else{
234
                    userDataEditor.putString("message","true");;
235
                    userDataEditor.commit();
236
                    return "false";
237
                }
238
            } catch (ClientProtocolException e) {
239
                return "false";
240
            } catch (IOException e) {
241
                return "false";
242
            }catch (Exception e){
243
                return "false";
244
            }
245
        }
246
 
247
        @Override
248
        protected void onPostExecute(String result) {
249
            super.onPostExecute(result);
250
            Intent i = new Intent(MobileNumber.this,MobileVerificationFirstTime.class);
251
            i.putExtra("numberVerification",phoneNumberFirstTime.getText().toString());
252
            startActivity(i);
253
        }
254
    }
255
 
256
    @Override
257
    public void onBackPressed() {
258
        /* super.onBackPressed();*/
259
        new AlertDialog.Builder(this)
260
                .setIcon(android.R.drawable.ic_dialog_alert)
261
                .setTitle("Exit!")
262
                .setMessage("Are you sure you want to sign out?")
263
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
264
                {
265
                    @Override
266
                    public void onClick(DialogInterface dialog, int which) {
267
 
268
                        if(type1.equalsIgnoreCase("facebook")) {
269
                            Log.d("fblogout","logout");
270
                            if (Session.getActiveSession() != null) {
271
                                Session.getActiveSession().closeAndClearTokenInformation();
272
                            }
273
                            Session.setActiveSession(null);
274
/*
275
                            userDataEditor.remove("id");
276
                            userDataEditor.remove("type");
277
                            userDataEditor.remove("email");
278
*/
279
                            userDataEditor.clear().commit();
280
/*                            userDataEditor.commit();*/
281
                            startActivity(new Intent(MobileNumber.this, LoginActivity.class));
282
                        }
283
                        else{
284
                            Log.d("gmailLogout","logout");
285
                            signOutFromGplus();
286
                            if(glogout==true){
287
                                startActivity(new Intent(MobileNumber.this, LoginActivity.class));
288
                            }
289
                        }
290
 
291
                    }
292
 
293
                })
294
                .setNegativeButton("No", null)
295
                .show();
296
    }
297
 
298
    public void signOutFromGplus() {
299
        if (mGoogleApiClient.isConnected()) {
300
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
301
            mGoogleApiClient.disconnect();
302
            mGoogleApiClient.connect();
303
            glogout=true;
304
            userDataEditor.clear();
305
            userDataEditor.commit();
306
        } else {
307
            glogout=false;
308
        }
309
    }
310
 
311
    public final boolean isInternetOn() {
312
 
313
        ConnectivityManager connec =
314
                (ConnectivityManager)this.getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
315
 
316
        if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
317
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
318
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
319
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
320
            return true;
321
 
322
        } else if (
323
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
324
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
325
 
326
            return false;
327
        }
328
        return false;
329
    }
330
 
331
}