| 21478 |
rajender |
1 |
package com.saholic.profittill.main;
|
|
|
2 |
|
|
|
3 |
import android.Manifest;
|
|
|
4 |
import android.app.AlertDialog;
|
|
|
5 |
import android.app.ProgressDialog;
|
|
|
6 |
import android.content.Context;
|
|
|
7 |
import android.content.DialogInterface;
|
|
|
8 |
import android.content.Intent;
|
|
|
9 |
import android.content.IntentSender;
|
|
|
10 |
import android.content.SharedPreferences;
|
|
|
11 |
import android.content.pm.PackageManager;
|
|
|
12 |
import android.content.res.Configuration;
|
|
|
13 |
import android.net.ConnectivityManager;
|
|
|
14 |
import android.net.Uri;
|
|
|
15 |
import android.os.AsyncTask;
|
|
|
16 |
import android.os.Build;
|
|
|
17 |
import android.os.Bundle;
|
|
|
18 |
import android.support.v4.app.ActivityCompat;
|
|
|
19 |
import android.support.v4.content.ContextCompat;
|
|
|
20 |
import android.support.v7.app.ActionBarActivity;
|
|
|
21 |
import android.telephony.TelephonyManager;
|
|
|
22 |
import android.text.TextUtils;
|
|
|
23 |
import android.util.Log;
|
|
|
24 |
import android.view.Menu;
|
|
|
25 |
import android.view.MenuItem;
|
|
|
26 |
import android.view.View;
|
|
|
27 |
import android.widget.ImageButton;
|
|
|
28 |
import android.widget.Toast;
|
|
|
29 |
|
|
|
30 |
import com.google.android.gms.analytics.HitBuilders;
|
|
|
31 |
import com.google.android.gms.analytics.Tracker;
|
|
|
32 |
import com.google.android.gms.auth.GoogleAuthException;
|
|
|
33 |
import com.google.android.gms.auth.GoogleAuthUtil;
|
|
|
34 |
import com.google.android.gms.auth.UserRecoverableAuthException;
|
|
|
35 |
import com.google.android.gms.common.ConnectionResult;
|
|
|
36 |
import com.google.android.gms.common.GooglePlayServicesUtil;
|
|
|
37 |
import com.google.android.gms.common.Scopes;
|
|
|
38 |
import com.google.android.gms.common.api.GoogleApiClient;
|
|
|
39 |
import com.google.android.gms.common.api.ResultCallback;
|
|
|
40 |
import com.google.android.gms.common.api.Status;
|
|
|
41 |
import com.google.android.gms.plus.Plus;
|
|
|
42 |
import com.google.android.gms.plus.model.people.Person;
|
|
|
43 |
import com.google.firebase.iid.FirebaseInstanceId;
|
|
|
44 |
import com.mixpanel.android.mpmetrics.MixpanelAPI;
|
|
|
45 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
46 |
import com.saholic.profittill.R;
|
|
|
47 |
import com.saholic.profittill.Utils.UtilityFunctions;
|
|
|
48 |
import com.testin.agent.TestinAgent;
|
|
|
49 |
|
|
|
50 |
import org.apache.http.HttpEntity;
|
|
|
51 |
import org.apache.http.HttpResponse;
|
|
|
52 |
import org.apache.http.NameValuePair;
|
|
|
53 |
import org.apache.http.client.HttpClient;
|
|
|
54 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
55 |
import org.apache.http.client.methods.HttpPost;
|
|
|
56 |
import org.apache.http.entity.StringEntity;
|
|
|
57 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
58 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
59 |
import org.apache.http.util.EntityUtils;
|
|
|
60 |
import org.json.JSONException;
|
|
|
61 |
import org.json.JSONObject;
|
|
|
62 |
|
|
|
63 |
import java.io.IOException;
|
|
|
64 |
import java.util.ArrayList;
|
|
|
65 |
import java.util.Date;
|
|
|
66 |
|
|
|
67 |
public class LoginActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
|
|
|
68 |
public GoogleApiClient mGoogleApiClient;
|
|
|
69 |
String scope = "oauth2:" + Scopes.PLUS_LOGIN +" " +Scopes.PLUS_ME + " " + Scopes.EMAIL;
|
|
|
70 |
private boolean mIntentInProgress;
|
|
|
71 |
private boolean mSignInClicked;
|
|
|
72 |
private ConnectionResult mConnectionResult;
|
|
|
73 |
private static final int RC_SIGN_IN = 0;
|
|
|
74 |
public static final int REQUEST_CODE_TOKEN_AUTH =1;
|
|
|
75 |
ArrayList<NameValuePair> nameValuePairs;
|
|
|
76 |
ArrayList<NameValuePair> nameValuePairsGcm;
|
|
|
77 |
SharedPreferences userData;
|
|
|
78 |
SharedPreferences apiData;
|
|
|
79 |
SharedPreferences.Editor inviteDataEditor;
|
|
|
80 |
SharedPreferences inviteData;
|
|
|
81 |
boolean FLAG=false;
|
|
|
82 |
SharedPreferences.Editor userDataEditor;
|
|
|
83 |
SharedPreferences.Editor apiSettingsEditor;
|
|
|
84 |
SharedPreferences inviteD;
|
|
|
85 |
SharedPreferences.Editor inviteDEditor;
|
|
|
86 |
ImageButton googlelogin;
|
|
|
87 |
String mobile;
|
|
|
88 |
MixpanelAPI mixpanel;
|
|
|
89 |
FirebaseInstanceId fcm;
|
|
|
90 |
Context context;
|
|
|
91 |
String regId;
|
|
|
92 |
String msg = "";
|
|
|
93 |
public static final int READ_Token_PERMISSION=101;
|
|
|
94 |
public static final String REG_ID = "regId";
|
|
|
95 |
private static final String APP_VERSION = "appVersion";
|
|
|
96 |
static final String TAG = "Register Activity";
|
|
|
97 |
private boolean PermissionIngranted=false;
|
|
|
98 |
@Override
|
|
|
99 |
|
|
|
100 |
protected void onCreate(Bundle savedInstanceState) {
|
|
|
101 |
|
|
|
102 |
context = getApplicationContext();
|
|
|
103 |
mixpanel= MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
|
|
|
104 |
super.onCreate(savedInstanceState);
|
|
|
105 |
setContentView(R.layout.activity_login);
|
|
|
106 |
TestinAgent.init(this);
|
|
|
107 |
getSupportActionBar().hide();
|
|
|
108 |
requestToken();
|
|
|
109 |
userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
|
|
|
110 |
apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
|
|
|
111 |
userDataEditor = userData.edit();
|
|
|
112 |
apiSettingsEditor = apiData.edit();
|
|
|
113 |
inviteData = context.getSharedPreferences("Invite_Data", Context.MODE_PRIVATE);
|
|
|
114 |
inviteDataEditor = inviteData.edit();
|
|
|
115 |
inviteD = getApplicationContext().getSharedPreferences("Invite", MODE_PRIVATE);
|
|
|
116 |
inviteDEditor = inviteD.edit();
|
|
|
117 |
googlelogin =(ImageButton)findViewById(R.id.google_login_button);
|
|
|
118 |
if(getIntent().getAction()=="Login"){
|
|
|
119 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
120 |
nameValuePairsGcm.add(new BasicNameValuePair("cid",getIntent().getExtras().getString("cid")));
|
|
|
121 |
nameValuePairsGcm.add(new BasicNameValuePair("result","login"));
|
|
|
122 |
nameValuePairsGcm.add(new BasicNameValuePair("timestamp",UtilityFunctions.notificationDate()));
|
|
|
123 |
getIntent().getExtras().remove("cid");
|
|
|
124 |
new NotificationOpenedData().execute(nameValuePairsGcm);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
else if(getIntent().getAction()=="31"){
|
|
|
128 |
String emailURL = String.valueOf(getIntent().getData());
|
|
|
129 |
Uri url = Uri.parse(emailURL);
|
|
|
130 |
String campaignId = url.getQueryParameter("campaign");
|
|
|
131 |
if(url.getQueryParameter("intent_type").equalsIgnoreCase("url")) {
|
|
|
132 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
133 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
134 |
t.send(new HitBuilders.EventBuilder()
|
|
|
135 |
.setCategory("Message/Email")
|
|
|
136 |
.setAction("Message/Email Opened For User Id " + UtilityFunctions.campaignUserId(url))
|
|
|
137 |
.setLabel("Campaign Id " + campaignId)
|
|
|
138 |
.build());
|
|
|
139 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
140 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id", UtilityFunctions.campaignUserId(url)));
|
|
|
141 |
nameValuePairsGcm.add(new BasicNameValuePair("cid", campaignId));
|
|
|
142 |
nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
|
|
|
143 |
nameValuePairsGcm.add(new BasicNameValuePair("result", "message_opened_login"));
|
|
|
144 |
new NotificationOpenedData().execute(nameValuePairsGcm);
|
|
|
145 |
}
|
|
|
146 |
else{
|
|
|
147 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
148 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id","")));
|
|
|
149 |
nameValuePairsGcm.add(new BasicNameValuePair("cid", campaignId));
|
|
|
150 |
nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
|
|
|
151 |
nameValuePairsGcm.add(new BasicNameValuePair("result", "message_opened_login"));
|
|
|
152 |
new NotificationOpenedData().execute(nameValuePairsGcm);
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
156 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
157 |
t.setScreenName("Login Screen");
|
|
|
158 |
t.send(new HitBuilders.ScreenViewBuilder().build());
|
|
|
159 |
|
|
|
160 |
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
|
|
|
161 |
JSONObject props = new JSONObject();
|
|
|
162 |
try {
|
|
|
163 |
props.put("Screen", "Login Screen");
|
|
|
164 |
mixpanel.track("Login Page", props);
|
|
|
165 |
} catch (JSONException e) {
|
|
|
166 |
e.printStackTrace();
|
|
|
167 |
}
|
|
|
168 |
googlelogin.setOnClickListener(new View.OnClickListener() {
|
|
|
169 |
@Override
|
|
|
170 |
public void onClick(View v) {
|
|
|
171 |
if(isInternetOn()) {
|
|
|
172 |
if (TextUtils.isEmpty(regId)) {
|
|
|
173 |
regId = registerGCM();
|
|
|
174 |
Log.d("RegisterActivity", "GCM RegId: " + regId);
|
|
|
175 |
} else {
|
|
|
176 |
}
|
|
|
177 |
signInWithGplus();
|
|
|
178 |
}
|
|
|
179 |
else{
|
|
|
180 |
Toast.makeText(getApplicationContext(),"Sorry your internet is not working. Please check again",Toast.LENGTH_SHORT).show();
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
});
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
mGoogleApiClient = new GoogleApiClient.Builder(this)
|
|
|
187 |
.addConnectionCallbacks(this)
|
|
|
188 |
.addOnConnectionFailedListener(this).addApi(Plus.API)
|
|
|
189 |
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
|
|
|
190 |
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public void requestToken(){
|
|
|
194 |
try{
|
|
|
195 |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
|
|
196 |
if (ContextCompat.checkSelfPermission(this,
|
|
|
197 |
Manifest.permission.GET_ACCOUNTS)
|
|
|
198 |
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
199 |
ActivityCompat.requestPermissions(LoginActivity.this,
|
|
|
200 |
new String[]{Manifest.permission.GET_ACCOUNTS},
|
|
|
201 |
READ_Token_PERMISSION);
|
|
|
202 |
} else {
|
|
|
203 |
PermissionIngranted = true;
|
|
|
204 |
}
|
|
|
205 |
} else {
|
|
|
206 |
PermissionIngranted = true;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
}catch (Exception e){
|
|
|
210 |
e.printStackTrace();
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
private void signInWithGplus() {
|
|
|
215 |
if (!mGoogleApiClient.isConnecting()) {
|
|
|
216 |
mSignInClicked = true;
|
|
|
217 |
resolveSignInError();
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
@Override
|
|
|
222 |
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
223 |
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
224 |
getMenuInflater().inflate(R.menu.menu_login, menu);
|
|
|
225 |
return true;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
@Override
|
|
|
229 |
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
230 |
// Handle action bar item clicks here. The action bar will
|
|
|
231 |
// automatically handle clicks on the Home/Up button, so long
|
|
|
232 |
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
233 |
int id = item.getItemId();
|
|
|
234 |
|
|
|
235 |
//noinspection SimplifiableIfStatement
|
|
|
236 |
if (id == R.id.action_settings) {
|
|
|
237 |
return true;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return super.onOptionsItemSelected(item);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* Method to resolve any signin errors
|
|
|
245 |
* */
|
|
|
246 |
private void resolveSignInError() {
|
|
|
247 |
if (mConnectionResult.hasResolution()) {
|
|
|
248 |
try {
|
|
|
249 |
mIntentInProgress = true;
|
|
|
250 |
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
|
|
|
251 |
} catch (IntentSender.SendIntentException e) {
|
|
|
252 |
e.printStackTrace();
|
|
|
253 |
TestinAgent.uploadException(this,"Exception in Resolve sign in error", new Exception());
|
|
|
254 |
mIntentInProgress = false;
|
|
|
255 |
//mGoogleApiClient.connect();
|
|
|
256 |
revokeGplusAccess();
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Revoking access from google
|
|
|
263 |
* */
|
|
|
264 |
private void revokeGplusAccess() {
|
|
|
265 |
if (mGoogleApiClient.isConnected()) {
|
|
|
266 |
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
|
|
|
267 |
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
|
|
|
268 |
.setResultCallback(new ResultCallback<Status>() {
|
|
|
269 |
@Override
|
|
|
270 |
public void onResult(Status arg0) {
|
|
|
271 |
Log.e(TAG, "User access revoked!");
|
|
|
272 |
mGoogleApiClient.connect();
|
|
|
273 |
//updateUI(false);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
});
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
@Override
|
|
|
281 |
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
282 |
super.onConfigurationChanged(newConfig);
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
protected void onStart() {
|
|
|
286 |
super.onStart();
|
|
|
287 |
mGoogleApiClient.connect();
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
protected void onStop() {
|
|
|
291 |
super.onStop();
|
|
|
292 |
if (mGoogleApiClient.isConnected()) {
|
|
|
293 |
mGoogleApiClient.disconnect();
|
|
|
294 |
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
@Override
|
|
|
299 |
public void onConnectionFailed(ConnectionResult result) {
|
|
|
300 |
if (!result.hasResolution()) {
|
|
|
301 |
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,0).show();
|
|
|
302 |
return;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
if (!mIntentInProgress) {
|
|
|
306 |
mConnectionResult = result;
|
|
|
307 |
if (mSignInClicked) {
|
|
|
308 |
resolveSignInError();
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
@Override
|
|
|
314 |
protected void onActivityResult(int requestCode, int responseCode,
|
|
|
315 |
Intent intent) {
|
|
|
316 |
if (requestCode == RC_SIGN_IN) {
|
|
|
317 |
if (responseCode != RESULT_OK) {
|
|
|
318 |
mSignInClicked = false;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
mIntentInProgress = false;
|
|
|
322 |
|
|
|
323 |
if (!mGoogleApiClient.isConnecting()) {
|
|
|
324 |
mGoogleApiClient.connect();
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
else if (requestCode==REQUEST_CODE_TOKEN_AUTH &&responseCode==RESULT_OK){
|
|
|
328 |
getAccessToken();
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
private void getAccessToken() {
|
|
|
333 |
new getToken().execute();
|
|
|
334 |
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
@Override
|
|
|
338 |
public void onConnectionSuspended(int arg0) {
|
|
|
339 |
mGoogleApiClient.connect();
|
|
|
340 |
}
|
|
|
341 |
@Override
|
|
|
342 |
public void onConnected(Bundle arg0) {
|
|
|
343 |
mSignInClicked=false;
|
|
|
344 |
getAccessToken();
|
|
|
345 |
}
|
|
|
346 |
private void showProgressDialog()
|
|
|
347 |
{
|
|
|
348 |
pDlg = new ProgressDialog(LoginActivity.this);
|
|
|
349 |
pDlg.setMessage("Logging In");
|
|
|
350 |
pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
|
|
|
351 |
pDlg.setCancelable(false);
|
|
|
352 |
pDlg.show();
|
|
|
353 |
|
|
|
354 |
}
|
|
|
355 |
private ProgressDialog pDlg = null;
|
|
|
356 |
class getToken extends AsyncTask<String, Integer, String> {
|
|
|
357 |
|
|
|
358 |
@Override
|
|
|
359 |
protected void onPreExecute() {
|
|
|
360 |
showProgressDialog();
|
|
|
361 |
}
|
|
|
362 |
@Override
|
|
|
363 |
protected String doInBackground(String... params) {
|
|
|
364 |
String registered=null;
|
|
|
365 |
String googletoken = null;
|
|
|
366 |
Bundle appActivities = new Bundle();
|
|
|
367 |
appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,"MainActivity");
|
|
|
368 |
try {
|
|
|
369 |
googletoken = GoogleAuthUtil.getToken(
|
|
|
370 |
LoginActivity.this,
|
|
|
371 |
Plus.AccountApi.getAccountName(mGoogleApiClient),
|
|
|
372 |
scope,appActivities);
|
|
|
373 |
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
|
|
|
374 |
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
|
|
|
375 |
String imageUrl = currentPerson.getImage().getUrl();
|
|
|
376 |
int i = currentPerson.getGender();
|
|
|
377 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
378 |
String json = "";
|
|
|
379 |
JSONObject jsonObject = new JSONObject();
|
|
|
380 |
jsonObject.put("type", "GOOGLE");
|
|
|
381 |
jsonObject.put("token", googletoken);
|
|
|
382 |
json = jsonObject.toString();
|
|
|
383 |
HttpPost httppost = new HttpPost(ProfitTillConstants.Web_Api+"/user/googleLogin");
|
|
|
384 |
StringEntity se = new StringEntity(json);
|
|
|
385 |
httppost.setEntity(se);
|
|
|
386 |
httppost.setHeader("Content-type", "application/json");
|
|
|
387 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
388 |
HttpEntity entity = response.getEntity();
|
|
|
389 |
JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
|
|
|
390 |
JSONObject jobj= jObjGmail.getJSONObject("response");
|
|
|
391 |
registered=jobj.getString("registered");
|
|
|
392 |
if(!registered.equalsIgnoreCase(null)) {
|
|
|
393 |
String token = jobj.getString("token");
|
|
|
394 |
Log.d("token",token);
|
|
|
395 |
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
|
|
|
396 |
userDataEditor.putString("token", token);
|
|
|
397 |
userDataEditor.putString("email", email);
|
|
|
398 |
userDataEditor.putString("registered", registered);
|
|
|
399 |
userDataEditor.putString("type", "google");
|
|
|
400 |
userDataEditor.commit();
|
|
|
401 |
}
|
|
|
402 |
else{
|
|
|
403 |
|
|
|
404 |
}
|
|
|
405 |
}
|
|
|
406 |
else{
|
|
|
407 |
return "failure";
|
|
|
408 |
}
|
|
|
409 |
}
|
|
|
410 |
catch (IOException transientEx) {
|
|
|
411 |
Log.e("InputOutput", transientEx.toString());
|
|
|
412 |
|
|
|
413 |
}
|
|
|
414 |
catch (UserRecoverableAuthException e) {
|
|
|
415 |
Log.d("Here","Here " + e.getMessage());
|
|
|
416 |
Intent recover = e.getIntent();
|
|
|
417 |
startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
|
|
|
418 |
}
|
|
|
419 |
catch (GoogleAuthException authEx) {
|
|
|
420 |
Log.e("AuthEX", authEx.toString());
|
|
|
421 |
}
|
|
|
422 |
catch (Exception e) {
|
|
|
423 |
Log.e("Exception main", e.toString());
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
return registered;
|
|
|
427 |
}
|
|
|
428 |
@Override
|
|
|
429 |
protected void onPostExecute(String result){
|
|
|
430 |
super.onPostExecute(result);
|
|
|
431 |
if(pDlg!=null){
|
|
|
432 |
pDlg.dismiss();
|
|
|
433 |
}
|
|
|
434 |
userDataEditor.commit();
|
|
|
435 |
|
|
|
436 |
try {
|
|
|
437 |
if (userData.getString("registered",null) == null) {
|
|
|
438 |
if(mGoogleApiClient.isConnected()) {
|
|
|
439 |
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
|
|
|
440 |
mGoogleApiClient.disconnect();
|
|
|
441 |
userDataEditor.clear().commit();
|
|
|
442 |
}
|
|
|
443 |
else{
|
|
|
444 |
userDataEditor.clear().commit();
|
|
|
445 |
}
|
|
|
446 |
if(pDlg!=null){
|
|
|
447 |
pDlg.dismiss();
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
else {
|
|
|
451 |
Intent i = new Intent(LoginActivity.this,MainActivity.class);
|
|
|
452 |
i.putExtra("displayView","0");
|
|
|
453 |
startActivity(i);
|
|
|
454 |
//UtilityFunctions utf = new UtilityFunctions();
|
|
|
455 |
//new pushApkData().execute(utf.getDeviceInformation(getApplicationContext()));
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
|
|
|
459 |
}
|
|
|
460 |
catch (Exception e){
|
|
|
461 |
e.printStackTrace();
|
|
|
462 |
}
|
|
|
463 |
}}
|
|
|
464 |
|
|
|
465 |
|
|
|
466 |
@Override
|
|
|
467 |
public void onBackPressed() {
|
|
|
468 |
new AlertDialog.Builder(this)
|
|
|
469 |
.setIcon(android.R.drawable.ic_dialog_alert)
|
|
|
470 |
.setTitle("Exit!")
|
|
|
471 |
.setMessage("Are you sure you want to close?")
|
|
|
472 |
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
|
|
|
473 |
{
|
|
|
474 |
@Override
|
|
|
475 |
public void onClick(DialogInterface dialog, int which) {
|
|
|
476 |
Intent startMain = new Intent(Intent.ACTION_MAIN);
|
|
|
477 |
startMain.addCategory(Intent.CATEGORY_HOME);
|
|
|
478 |
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
479 |
startActivity(startMain);
|
|
|
480 |
}
|
|
|
481 |
})
|
|
|
482 |
.setNegativeButton("No", null)
|
|
|
483 |
.show();
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
|
|
|
487 |
private void registerInBackground() {
|
|
|
488 |
new AsyncTask<Void, Void, String>() {
|
|
|
489 |
@Override
|
|
|
490 |
protected String doInBackground(Void... params) {
|
|
|
491 |
|
|
|
492 |
if (fcm == null) {
|
|
|
493 |
regId =FirebaseInstanceId.getInstance().getToken();
|
|
|
494 |
Log.d("RegisterActivity", "registerInBackground - regId: "
|
|
|
495 |
+ regId);
|
|
|
496 |
msg = "Device registered";
|
|
|
497 |
}
|
|
|
498 |
Log.d("RegisterActivity", "AsyncTask completed: " + msg);
|
|
|
499 |
return msg;
|
|
|
500 |
}
|
|
|
501 |
|
|
|
502 |
@Override
|
|
|
503 |
protected void onPostExecute(String msg) {
|
|
|
504 |
}
|
|
|
505 |
}.execute(null, null, null);
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
public String registerGCM() {
|
|
|
509 |
regId = getRegistrationId(context);
|
|
|
510 |
registerInBackground();
|
|
|
511 |
return regId;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
private String getRegistrationId(Context context) {
|
|
|
515 |
|
|
|
516 |
String registrationId = userData.getString("gcm_regid", "");
|
|
|
517 |
if (registrationId.isEmpty()) {
|
|
|
518 |
Log.i(TAG, "Registration not found.");
|
|
|
519 |
return "";
|
|
|
520 |
}
|
|
|
521 |
return registrationId;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
class gcmPushData extends AsyncTask<String, Integer, String> {
|
|
|
525 |
|
|
|
526 |
@Override
|
|
|
527 |
protected void onPreExecute() {
|
|
|
528 |
super.onPreExecute();
|
|
|
529 |
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
|
|
|
530 |
MixpanelAPI.People people = mixpanel.getPeople();
|
|
|
531 |
JSONObject props = new JSONObject();
|
|
|
532 |
try {
|
|
|
533 |
mixpanel.identify(userData.getString("id",null));
|
|
|
534 |
people.identify(userData.getString("id",null));
|
|
|
535 |
props.put("Screen", "Logged In");
|
|
|
536 |
people.set("$last_login",new Date());
|
|
|
537 |
people.set("user_id" , userData.getString("id",null));
|
|
|
538 |
mixpanel.track("Successfully Logged In", props);
|
|
|
539 |
} catch (JSONException e) {
|
|
|
540 |
e.printStackTrace();
|
|
|
541 |
}
|
|
|
542 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
543 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
544 |
t.set("&uid",userData.getString("id",null));
|
|
|
545 |
t.send(new HitBuilders.EventBuilder()
|
|
|
546 |
.setCategory("Login ")
|
|
|
547 |
.setAction("Login Successful")
|
|
|
548 |
.setLabel("Successful for User" +userData.getString("id",null))
|
|
|
549 |
.build());
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
@Override
|
|
|
553 |
protected String doInBackground(String... arg0) {
|
|
|
554 |
String id=null;
|
|
|
555 |
try {
|
|
|
556 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
557 |
HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url",null));
|
|
|
558 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
559 |
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
560 |
nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid",regId));
|
|
|
561 |
nameValuePairsGcm.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
|
|
|
562 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id",userData.getString("id", null)));
|
|
|
563 |
nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
|
|
|
564 |
nameValuePairsGcm.add(new BasicNameValuePair("device_message",arg0[0]));
|
|
|
565 |
nameValuePairsGcm.add(new BasicNameValuePair("androidid",UtilityFunctions.androidId(LoginActivity.this)));
|
|
|
566 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
567 |
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
|
|
|
568 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
569 |
HttpEntity entity = response.getEntity();
|
|
|
570 |
int status = response.getStatusLine().getStatusCode();
|
|
|
571 |
|
|
|
572 |
if(status == 200){
|
|
|
573 |
Log.d("ResponseCode GCM",status+"");
|
|
|
574 |
userDataEditor.putString("fcm_token_sent", "true");
|
|
|
575 |
userDataEditor.commit();
|
|
|
576 |
} else {
|
|
|
577 |
Log.d("ResponseCode GCM",status+"");
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
nameValuePairsGcm.clear();
|
|
|
581 |
Log.e("pass 1", "connection success ");
|
|
|
582 |
} catch (Exception e) {
|
|
|
583 |
Log.e("Fail 1", e.toString());
|
|
|
584 |
|
|
|
585 |
}
|
|
|
586 |
return id;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
@Override
|
|
|
590 |
protected void onPostExecute(String result) {
|
|
|
591 |
super.onPostExecute(result);
|
|
|
592 |
if(pDlg!=null){
|
|
|
593 |
pDlg.dismiss();
|
|
|
594 |
}
|
|
|
595 |
userDataEditor.putString("gcm_regid", regId);
|
|
|
596 |
userDataEditor.commit();
|
|
|
597 |
|
|
|
598 |
String check = userData.getString("message","");
|
|
|
599 |
if(userData.getString("docsSubmitted","").equalsIgnoreCase("true")){
|
|
|
600 |
// Intent i = new Intent(LoginActivity.this, PendingActivity.class);
|
|
|
601 |
// startActivity(i);
|
|
|
602 |
}
|
|
|
603 |
else if(userData.getString("referralCodeRequired","").equalsIgnoreCase("true")){
|
|
|
604 |
Log.d("In if","in if" +inviteD.getString("referrerCode","").equalsIgnoreCase("") + inviteD .getString("referrerCode","") );
|
|
|
605 |
//Intent i = new Intent(LoginActivity.this, BasicInformation.class);
|
|
|
606 |
//startActivity(i);
|
|
|
607 |
}
|
|
|
608 |
else if(check.equalsIgnoreCase("true")){
|
|
|
609 |
Log.d("In if","in else");
|
|
|
610 |
//Intent i = new Intent(LoginActivity.this, MobileNumber.class);
|
|
|
611 |
// i.putExtra("displayView", "7");
|
|
|
612 |
// startActivity(i);
|
|
|
613 |
}
|
|
|
614 |
else{
|
|
|
615 |
Intent i = new Intent(LoginActivity.this,MainActivity.class);
|
|
|
616 |
i.putExtra("displayView","0");
|
|
|
617 |
startActivity(i);
|
|
|
618 |
}
|
|
|
619 |
}
|
|
|
620 |
|
|
|
621 |
|
|
|
622 |
}
|
|
|
623 |
public final boolean isInternetOn() {
|
|
|
624 |
|
|
|
625 |
ConnectivityManager connection =
|
|
|
626 |
(ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
|
|
|
627 |
|
|
|
628 |
if ( connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
|
|
|
629 |
connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
630 |
connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
|
|
|
631 |
connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
|
|
|
632 |
|
|
|
633 |
return true;
|
|
|
634 |
|
|
|
635 |
} else if (
|
|
|
636 |
connection.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
|
|
|
637 |
connection.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
|
|
|
638 |
|
|
|
639 |
return false;
|
|
|
640 |
}
|
|
|
641 |
return false;
|
|
|
642 |
}
|
|
|
643 |
class NotificationOpenedData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
|
|
|
644 |
|
|
|
645 |
@Override
|
|
|
646 |
protected void onPreExecute() {
|
|
|
647 |
super.onPreExecute();
|
|
|
648 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
649 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
650 |
t.send(new HitBuilders.EventBuilder()
|
|
|
651 |
.setCategory("Notification")
|
|
|
652 |
.setAction("Notification Opened Login")
|
|
|
653 |
.setLabel("For User Id " + userData.getString("id","0"))
|
|
|
654 |
.build());
|
|
|
655 |
}
|
|
|
656 |
|
|
|
657 |
@Override
|
|
|
658 |
protected String doInBackground(ArrayList<NameValuePair>... arg0) {
|
|
|
659 |
|
|
|
660 |
try {
|
|
|
661 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
662 |
HttpPost httppost = new HttpPost(apiData.getString("notication.data.url","http://api.profittill.com/pushnotifications/add"));
|
|
|
663 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
664 |
httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
|
|
|
665 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
666 |
HttpEntity entity = response.getEntity();
|
|
|
667 |
int status = response.getStatusLine().getStatusCode();
|
|
|
668 |
|
|
|
669 |
if(status == 200){
|
|
|
670 |
Log.d("Notification Opened","Notication opened and sent to server");
|
|
|
671 |
Log.d("ResponseCode GCM ",status+"");
|
|
|
672 |
} else {
|
|
|
673 |
Log.d("ResponseCode GCM ",status+"");
|
|
|
674 |
}
|
|
|
675 |
|
|
|
676 |
nameValuePairsGcm.clear();
|
|
|
677 |
Log.e("pass 1", "connection success ");
|
|
|
678 |
} catch (Exception e) {
|
|
|
679 |
Log.e("Fail 1", e.toString());
|
|
|
680 |
|
|
|
681 |
}
|
|
|
682 |
return "success";
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
@Override
|
|
|
686 |
protected void onPostExecute(String result) {
|
|
|
687 |
super.onPostExecute(result);
|
|
|
688 |
Log.d("Notification Opened","Notication opened and login page");
|
|
|
689 |
}
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
class pushApkData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
|
|
|
693 |
|
|
|
694 |
@Override
|
|
|
695 |
protected void onPreExecute() {
|
|
|
696 |
super.onPreExecute();
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
@Override
|
|
|
700 |
protected String doInBackground(ArrayList<NameValuePair>... arg0) {
|
|
|
701 |
try {
|
|
|
702 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
703 |
HttpPost httppost = new HttpPost("http://api.profittill.com/devices/add");
|
|
|
704 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
705 |
httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
|
|
|
706 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
707 |
HttpEntity entity = response.getEntity();
|
|
|
708 |
int status = response.getStatusLine().getStatusCode();
|
|
|
709 |
if(status == 200){
|
|
|
710 |
return "success";
|
|
|
711 |
} else {
|
|
|
712 |
return "failure";
|
|
|
713 |
}
|
|
|
714 |
} catch (Exception e) {
|
|
|
715 |
return null;
|
|
|
716 |
}
|
|
|
717 |
}
|
|
|
718 |
@Override
|
|
|
719 |
protected void onPostExecute(String result) {
|
|
|
720 |
super.onPostExecute(result);
|
|
|
721 |
if(userData.getString("fcm_token_sent","false").equalsIgnoreCase("false")){
|
|
|
722 |
new gcmPushData().execute(msg);
|
|
|
723 |
}
|
|
|
724 |
|
|
|
725 |
}
|
|
|
726 |
}
|
|
|
727 |
}
|
|
|
728 |
|