private boolean userAuthenticated( final String userID, final String passKey, final String serverURL) { final AndroidHttpClient http = AndroidHttpClient.newInstance("TrackMe"); final HttpGet httpGet = new HttpGet(serverURL + "/api/v1/xml/validate"); httpGet.addHeader("userid", userID); httpGet.addHeader("passkey", passKey); int code = -1; String message = ""; try { final HttpResponse response = http.execute(httpGet); Log.d(UPLOAD_SERVICE_TAG, response.getStatusLine().toString()); code = response.getStatusLine().getStatusCode(); } catch (final ClientProtocolException e) { message = "Internet not available"; Log.d(UPLOAD_SERVICE_TAG, "Service Timeout"); } catch (final UnknownHostException e) { message = "Server was not known or unreachable"; Log.d(UPLOAD_SERVICE_TAG, "Unknown Host"); } catch (final IllegalStateException e) { message = "Invalid Server URL"; Log.d(UPLOAD_SERVICE_TAG, "Illegal"); } catch (final IOException e) { code = 0; e.printStackTrace(); final Intent intentNotification = new Intent(this, MainActivity.class); final PendingIntent pi = PendingIntent.getActivity(this, 1, intentNotification, 0); final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final Notification notification = new Notification(R.drawable.uploading, "Upload Failed", System.currentTimeMillis()); notification.setLatestEventInfo(this, "UploadFailed", "Unknown Server Error", pi); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(9, notification); } http.close(); if (code == HttpStatus.SC_OK) { Log.d(UPLOAD_SERVICE_TAG, "valid"); return true; } else if (code == -1) { Log.d(UPLOAD_SERVICE_TAG, "Invalid" + " " + code); getApplication().startActivity(UserError.makeIntent(getBaseContext(), message)); cancelUploadAlarm(this); return false; } else if (code == 0) { cancelUploadAlarm(this); return false; } else { message = "Invalid UserID or PassKey"; getApplication().startActivity(UserError.makeIntent(getBaseContext(), message)); cancelUploadAlarm(this); return false; } }
private boolean uploadPossible(final long uploadTime) { final boolean userValidation = myPreference.userDetailsNotNull(); final boolean serverLocationValidation = myPreference.serverLocationSet(); final boolean dbValidation = db.getQueuedLocationsCount(uploadTime) > 0; final boolean networkValidation = isNetworkAvailable(this); boolean possible = true; final StringBuffer message = new StringBuffer(); message.append("Upload not possible due to : "); if (!userValidation) { possible = false; message.append("\nUserID or PassKey not provided"); } if (!serverLocationValidation) { possible = false; message.append("\nServer Location not set"); } if (!dbValidation) { possible = false; message.append("\nNo locations to upload"); } if (!networkValidation) { possible = false; message.append("\nNetwoek not available"); } if (!possible) { getApplication().startActivity(UserError.makeIntent(getBaseContext(), message.toString())); cancelUploadAlarm(this); } return possible; }
@Override public void run() { Log.d(UPLOAD_SERVICE_TAG, "Thread Started"); final String serverURL = myPreference.getServerLocation(); final String userID = myPreference.getUserID(); final String passKey = myPreference.getPassKey(); if (userAuthenticated(userID, passKey, serverURL)) { db.clearUploadIDs(); boolean errorExit; do { errorExit = false; int retryCount = 0; int code = -1; HttpResponse response = null; final String locations = db.getLocationsAsXML(uploadTime); Log.d(UPLOAD_SERVICE_TAG, locations); final AndroidHttpClient http = AndroidHttpClient.newInstance("TrackMe"); final HttpPost httpPost = new HttpPost(serverURL + "/api/v1/xml/store"); GzipHelper.setCompressedEntity(UploadService.this, locations, httpPost); httpPost.addHeader("userid", userID); httpPost.addHeader("passkey", passKey); while (retryCount < MAX_RETRY_COUNT) { try { response = http.execute(httpPost); Log.d(UPLOAD_SERVICE_TAG, response.toString()); code = response.getStatusLine().getStatusCode(); errorExit = false; retryCount = MAX_RETRY_COUNT; } catch (final ClientProtocolException e) { retryCount += 1; errorExit = true; } catch (final IOException e) { retryCount += 1; errorExit = true; e.printStackTrace(); } } http.close(); if (code == HttpStatus.SC_OK) { final Document doc = ResponseParsing.getDomElement(ResponseParsing.getXML(response)); final int uploadID = Integer.parseInt(doc.getDocumentElement().getAttribute("uid")); final NodeList nl = doc.getElementsByTagName("batch"); for (int i = 0; i < nl.getLength(); i++) { final Element e = (Element) nl.item(i); final String sessionID = e.getAttribute("sid"); final int batchID = Integer.parseInt(e.getAttribute("bid")); if (e.getAttribute("accepted").equals("true")) { Log.d(UPLOAD_SERVICE_TAG, "Boolean New " + e.getAttribute("accepted")); final int uploadedCount = db.moveLocationsToSessionTable(uploadID, sessionID, batchID); updatePreferences.addUploadedCount(uploadedCount); final Intent intent = new Intent(MainActivity.MAIN_ACTIVITY_UPDATE_DEBUG_UI); LocalBroadcastManager.getInstance(UploadService.this).sendBroadcast(intent); } else { final int archivedCount = db.archiveLocations(uploadID, sessionID, batchID); updatePreferences.addArchivedCount(archivedCount); final Intent intent = new Intent(MainActivity.MAIN_ACTIVITY_UPDATE_DEBUG_UI); LocalBroadcastManager.getInstance(UploadService.this).sendBroadcast(intent); } } } else { final String message = "Server response:\n" + response.getStatusLine().getReasonPhrase(); getApplication().startActivity(UserError.makeIntent(getBaseContext(), message)); errorExit = true; } } while (db.getQueuedLocationsCount(uploadTime) > 0 && !errorExit); } synchronized (UploadService.this) { running = false; stopForeground(true); } Log.d(UPLOAD_SERVICE_TAG, "Thread Compleated"); }