2018-02-20 13:54:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Anthony Chomienne, anthony@mob-dev.fr
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.eu.exodus_privacy.exodusprivacy.manager;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-12-07 11:41:38 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-02-20 13:54:18 +01:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.os.Bundle;
|
2020-12-07 09:04:44 +01:00
|
|
|
|
|
|
|
import org.eu.exodus_privacy.exodusprivacy.R;
|
2020-12-07 11:41:38 +01:00
|
|
|
import org.eu.exodus_privacy.exodusprivacy.Utils;
|
2020-12-07 09:04:44 +01:00
|
|
|
import org.eu.exodus_privacy.exodusprivacy.listener.NetworkListener;
|
|
|
|
import org.eu.exodus_privacy.exodusprivacy.objects.Application;
|
|
|
|
import org.eu.exodus_privacy.exodusprivacy.objects.Report;
|
|
|
|
import org.eu.exodus_privacy.exodusprivacy.objects.Tracker;
|
2018-02-20 13:54:18 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
2019-06-04 11:26:43 +02:00
|
|
|
import java.util.Collections;
|
2020-12-07 09:43:38 +01:00
|
|
|
import java.util.Date;
|
2018-08-31 15:35:42 +02:00
|
|
|
import java.util.HashMap;
|
2018-02-20 13:54:18 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2018-08-31 15:35:42 +02:00
|
|
|
import java.util.Map;
|
2018-03-28 17:00:05 +02:00
|
|
|
import java.util.Random;
|
2018-02-20 13:54:18 +01:00
|
|
|
import java.util.TimeZone;
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
2020-12-07 11:41:38 +01:00
|
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
|
|
|
2018-02-20 13:54:18 +01:00
|
|
|
/*
|
|
|
|
Singleton that handle all network connection
|
|
|
|
*/
|
|
|
|
public class NetworkManager {
|
|
|
|
|
|
|
|
private static NetworkManager instance;
|
|
|
|
private NetworkProcessingThread thread;
|
|
|
|
|
|
|
|
private NetworkManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NetworkManager getInstance() {
|
|
|
|
if (instance == null)
|
|
|
|
instance = new NetworkManager();
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getReports(Context context, NetworkListener listener, ArrayList<String> packageList) {
|
|
|
|
Message mes = new Message();
|
|
|
|
mes.type = Message_Type.GET_REPORTS;
|
|
|
|
mes.context = context;
|
|
|
|
mes.listener = listener;
|
|
|
|
mes.args = new Bundle();
|
2020-12-07 09:04:44 +01:00
|
|
|
mes.args.putStringArrayList("packages", packageList);
|
2018-02-20 13:54:18 +01:00
|
|
|
addMessageToQueue(mes);
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
private void addMessageToQueue(Message mes) {
|
|
|
|
if (thread == null || thread.getState() == Thread.State.TERMINATED || !thread.isRunning)
|
2018-02-20 13:54:18 +01:00
|
|
|
thread = new NetworkProcessingThread();
|
|
|
|
thread.queueMessage(mes);
|
2020-12-07 09:04:44 +01:00
|
|
|
if (thread.getState() == Thread.State.NEW)
|
2018-02-20 13:54:18 +01:00
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
private enum Message_Type {
|
|
|
|
GET_REPORTS,
|
|
|
|
UNKNOWN
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:43:38 +01:00
|
|
|
private static class NetworkProcessingThread extends Thread {
|
2020-12-07 09:04:44 +01:00
|
|
|
private final String apiUrl = "https://reports.exodus-privacy.eu.org/api/";
|
|
|
|
private final List<Message> messageQueue;
|
|
|
|
private final Semaphore sem;
|
2018-02-20 13:54:18 +01:00
|
|
|
boolean isRunning;
|
|
|
|
|
|
|
|
NetworkProcessingThread() {
|
|
|
|
messageQueue = new ArrayList<>();
|
|
|
|
sem = new Semaphore(0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void queueMessage(Message mes) {
|
|
|
|
messageQueue.add(mes);
|
|
|
|
sem.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
isRunning = true;
|
2020-12-07 11:41:38 +01:00
|
|
|
Message mes = null;
|
2018-02-20 13:54:18 +01:00
|
|
|
while (isRunning) {
|
|
|
|
try {
|
|
|
|
sem.acquire();
|
2020-12-07 11:41:38 +01:00
|
|
|
mes = messageQueue.remove(0);
|
2020-12-07 09:43:38 +01:00
|
|
|
//noinspection SwitchStatementWithTooFewBranches
|
2018-02-20 13:54:18 +01:00
|
|
|
switch (mes.type) {
|
|
|
|
case GET_REPORTS:
|
|
|
|
getTrackers(mes);
|
2018-03-28 17:00:05 +02:00
|
|
|
getApplications(mes);
|
2020-12-07 11:41:38 +01:00
|
|
|
SharedPreferences sharedPreferences = mes.context.getSharedPreferences(Utils.APP_PREFS, MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
|
|
editor.putString(Utils.LAST_REFRESH, Utils.dateToString(new Date()));
|
|
|
|
editor.apply();
|
2018-02-20 13:54:18 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
isRunning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:00:05 +02:00
|
|
|
private JSONObject makeDataRequest(Context context, NetworkListener listener, URL url) {
|
|
|
|
if (!isConnectedToInternet(context)) {
|
|
|
|
listener.onError(context.getString(R.string.not_connected));
|
|
|
|
return null;
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
2018-03-28 17:00:05 +02:00
|
|
|
|
2018-02-20 13:54:18 +01:00
|
|
|
InputStream inStream;
|
|
|
|
HttpURLConnection urlConnection;
|
|
|
|
boolean success = true;
|
|
|
|
try {
|
|
|
|
urlConnection = (HttpURLConnection) url.openConnection();
|
|
|
|
urlConnection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
urlConnection.setRequestProperty("Accept", "application/json");
|
2020-12-07 09:04:44 +01:00
|
|
|
urlConnection.setRequestProperty("Authorization", "Token " + context.getString(R.string.exodus));
|
2018-02-20 13:54:18 +01:00
|
|
|
urlConnection.setDoInput(true);
|
|
|
|
} catch (Exception e) {
|
2018-03-28 17:00:05 +02:00
|
|
|
return null;
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
inStream = urlConnection.getInputStream();
|
|
|
|
} catch (Exception e) {
|
|
|
|
success = false;
|
|
|
|
inStream = urlConnection.getErrorStream();
|
|
|
|
}
|
2018-03-28 17:00:05 +02:00
|
|
|
JSONObject object = null;
|
2020-12-07 09:04:44 +01:00
|
|
|
if (success) {
|
2018-02-20 13:54:18 +01:00
|
|
|
String jsonStr = getJSON(inStream);
|
|
|
|
try {
|
2018-03-28 17:00:05 +02:00
|
|
|
object = new JSONObject(jsonStr);
|
2020-12-07 09:04:44 +01:00
|
|
|
} catch (Exception e) {
|
2018-03-28 17:00:05 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-12-07 09:04:44 +01:00
|
|
|
if (inStream != null)
|
2018-03-28 17:00:05 +02:00
|
|
|
inStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-02-20 13:54:18 +01:00
|
|
|
|
2018-03-28 17:00:05 +02:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getTrackers(Message mes) {
|
2020-12-07 09:04:44 +01:00
|
|
|
mes.listener.onProgress(R.string.get_trackers_connection, 0, 0);
|
2018-03-28 17:00:05 +02:00
|
|
|
URL url;
|
|
|
|
try {
|
2020-12-07 09:04:44 +01:00
|
|
|
url = new URL(apiUrl + "trackers");
|
|
|
|
} catch (Exception e) {
|
2018-03-28 17:00:05 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
JSONObject object = makeDataRequest(mes.context, mes.listener, url);
|
|
|
|
mes.listener.onProgress(R.string.get_trackers, 0, 0);
|
2018-03-28 17:00:05 +02:00
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
if (object != null) {
|
2018-03-28 17:00:05 +02:00
|
|
|
try {
|
2018-02-20 13:54:18 +01:00
|
|
|
JSONObject trackers = object.getJSONObject("trackers");
|
|
|
|
List<Tracker> trackersList = new ArrayList<>();
|
2020-12-07 09:04:44 +01:00
|
|
|
JSONArray trackerNames = trackers.names();
|
|
|
|
if (trackerNames != null) {
|
|
|
|
for (int i = 0; i < trackerNames.length(); i++) {
|
|
|
|
mes.listener.onProgress(R.string.parse_trackers, i + 1, trackerNames.length());
|
|
|
|
String trackerId = trackerNames.get(i).toString();
|
|
|
|
JSONObject tracker = trackers.getJSONObject(trackerId);
|
|
|
|
Tracker track = parseTracker(tracker, trackerId);
|
|
|
|
trackersList.add(track);
|
|
|
|
if (trackersList.size() == 20) {
|
|
|
|
DatabaseManager.getInstance(mes.context).insertOrUpdateTrackers(trackersList);
|
|
|
|
trackersList.clear();
|
|
|
|
}
|
2019-10-01 15:46:11 +02:00
|
|
|
}
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
if (!trackersList.isEmpty())
|
2019-10-01 15:46:11 +02:00
|
|
|
DatabaseManager.getInstance(mes.context).insertOrUpdateTrackers(trackersList);
|
|
|
|
trackersList.clear();
|
2018-02-20 13:54:18 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
mes.listener.onError(mes.context.getString(R.string.json_error));
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 17:00:05 +02:00
|
|
|
}
|
2018-02-20 13:54:18 +01:00
|
|
|
|
2018-03-28 17:00:05 +02:00
|
|
|
private void getApplications(Message mes) {
|
2020-12-07 09:04:44 +01:00
|
|
|
mes.listener.onProgress(R.string.get_reports_connection, 0, 0);
|
2018-03-28 17:00:05 +02:00
|
|
|
URL url;
|
2018-02-20 13:54:18 +01:00
|
|
|
try {
|
2020-12-07 09:04:44 +01:00
|
|
|
url = new URL(apiUrl + "applications?option=short");
|
|
|
|
} catch (Exception e) {
|
2018-02-20 13:54:18 +01:00
|
|
|
e.printStackTrace();
|
2018-03-28 17:00:05 +02:00
|
|
|
return;
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
JSONObject object = makeDataRequest(mes.context, mes.listener, url);
|
|
|
|
mes.listener.onProgress(R.string.get_reports, 0, 0);
|
2018-03-28 17:00:05 +02:00
|
|
|
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
if (object != null) {
|
|
|
|
Map<String, Map<String, String>> handles = new HashMap<>();
|
2019-10-01 15:46:11 +02:00
|
|
|
ArrayList<String> packages = mes.args.getStringArrayList("packages");
|
|
|
|
if (packages == null)
|
|
|
|
return;
|
|
|
|
|
2018-03-28 17:00:05 +02:00
|
|
|
try {
|
|
|
|
JSONArray applications = object.getJSONArray("applications");
|
2019-10-01 15:46:11 +02:00
|
|
|
|
|
|
|
//manage handles map (handle,UAID)
|
2020-12-07 09:04:44 +01:00
|
|
|
for (int i = 0; i < applications.length(); i++) {
|
2018-03-28 17:00:05 +02:00
|
|
|
JSONObject app = applications.getJSONObject(i);
|
|
|
|
String handle = app.getString("handle");
|
2018-08-31 15:35:42 +02:00
|
|
|
String auid = app.getString("app_uid");
|
2020-07-01 18:36:00 +02:00
|
|
|
String source = app.getString("source");
|
2020-12-07 09:04:44 +01:00
|
|
|
Map<String, String> sources = handles.get(handle);
|
|
|
|
if (sources == null)
|
2020-07-01 18:36:00 +02:00
|
|
|
sources = new HashMap<>();
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
sources.put(source, auid);
|
2019-10-01 15:46:11 +02:00
|
|
|
if (packages.contains(handle))
|
2020-12-07 09:04:44 +01:00
|
|
|
handles.put(handle, sources);
|
2018-03-28 17:00:05 +02:00
|
|
|
}
|
2019-10-01 15:46:11 +02:00
|
|
|
|
|
|
|
//remove app not analyzed by Exodus
|
|
|
|
packages.retainAll(handles.keySet());
|
|
|
|
|
|
|
|
// Add some random packages to avoid tracking
|
|
|
|
Random rand = new Random(Thread.currentThread().getId());
|
|
|
|
int alea = rand.nextInt(120) % 10 + 11;
|
2020-12-07 09:04:44 +01:00
|
|
|
for (int i = 0; i < alea; i++) {
|
2019-10-01 15:46:11 +02:00
|
|
|
int val = rand.nextInt(applications.length());
|
|
|
|
JSONObject app = applications.getJSONObject(val);
|
|
|
|
String handle = app.getString("handle");
|
2020-12-07 09:04:44 +01:00
|
|
|
handles.put(handle, new HashMap<>());
|
2019-10-01 15:46:11 +02:00
|
|
|
packages.add(handle);
|
|
|
|
}
|
|
|
|
//shuffle the list
|
|
|
|
Collections.shuffle(packages);
|
|
|
|
object.remove("applications");
|
|
|
|
|
2018-03-28 17:00:05 +02:00
|
|
|
} catch (JSONException e) {
|
2019-10-01 15:46:11 +02:00
|
|
|
e.printStackTrace();
|
2018-03-28 17:00:05 +02:00
|
|
|
mes.listener.onError(mes.context.getString(R.string.json_error));
|
|
|
|
}
|
2019-10-01 15:46:11 +02:00
|
|
|
object = null;
|
2020-12-07 09:04:44 +01:00
|
|
|
getReports(mes, handles, packages);
|
2018-03-28 17:00:05 +02:00
|
|
|
}
|
|
|
|
mes.listener.onSuccess();
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
private void getReports(Message mes, Map<String, Map<String, String>> handles, ArrayList<String> packages) {
|
|
|
|
for (int i = 0; i < packages.size(); i++) {
|
|
|
|
mes.listener.onProgress(R.string.parse_application, i + 1, packages.size());
|
|
|
|
getReport(mes, packages.get(i), handles.get(packages.get(i)));
|
2018-03-28 17:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
private void getReport(Message mes, String handle, Map<String, String> sources) {
|
2018-02-20 13:54:18 +01:00
|
|
|
URL url;
|
|
|
|
try {
|
2020-12-07 09:04:44 +01:00
|
|
|
url = new URL(apiUrl + "search/" + handle);
|
|
|
|
} catch (Exception e) {
|
2018-02-20 13:54:18 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
JSONObject object = makeDataRequest(mes.context, mes.listener, url);
|
2018-02-20 13:54:18 +01:00
|
|
|
|
2020-12-07 09:04:44 +01:00
|
|
|
if (object != null) {
|
2018-02-20 13:54:18 +01:00
|
|
|
try {
|
2018-04-03 13:02:26 +02:00
|
|
|
JSONObject application = object.getJSONObject(handle);
|
2018-02-20 13:54:18 +01:00
|
|
|
ArrayList<String> packages = mes.args.getStringArrayList("packages");
|
2020-12-07 09:04:44 +01:00
|
|
|
if (packages != null && packages.contains(handle)) {
|
2018-04-03 13:02:26 +02:00
|
|
|
Application app = parseApplication(application, handle);
|
2020-07-01 18:36:00 +02:00
|
|
|
app.sources = sources;
|
2018-04-03 13:02:26 +02:00
|
|
|
DatabaseManager.getInstance(mes.context).insertOrUpdateApplication(app);
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
mes.listener.onError(mes.context.getString(R.string.json_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Application parseApplication(JSONObject object, String packageName) throws JSONException {
|
|
|
|
Application application = new Application();
|
|
|
|
application.packageName = packageName;
|
|
|
|
application.creator = object.getString("creator");
|
|
|
|
application.name = object.getString("name");
|
2018-08-31 15:35:42 +02:00
|
|
|
|
2018-02-20 13:54:18 +01:00
|
|
|
//parse Report
|
|
|
|
application.reports = new HashSet<>();
|
|
|
|
JSONArray reports = object.getJSONArray("reports");
|
2020-12-07 09:04:44 +01:00
|
|
|
for (int i = 0; i < reports.length(); i++) {
|
2018-02-20 13:54:18 +01:00
|
|
|
Report report = parseReport(reports.getJSONObject(i));
|
|
|
|
application.reports.add(report);
|
|
|
|
}
|
|
|
|
return application;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Report parseReport(JSONObject object) throws JSONException {
|
|
|
|
Report report = new Report();
|
|
|
|
report.id = object.getLong("id");
|
|
|
|
report.downloads = object.getString("downloads");
|
|
|
|
report.version = object.getString("version");
|
2020-07-01 18:36:00 +02:00
|
|
|
report.source = object.getString("source");
|
2020-12-07 09:04:44 +01:00
|
|
|
if (!object.getString("version_code").isEmpty())
|
2018-02-20 13:54:18 +01:00
|
|
|
report.versionCode = Long.parseLong(object.getString("version_code"));
|
|
|
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
|
|
|
|
try {
|
|
|
|
report.updateDate = Calendar.getInstance();
|
|
|
|
report.updateDate.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2020-12-07 09:43:38 +01:00
|
|
|
Date date = dateFormat.parse(object.getString("updated_at"));
|
|
|
|
if (date != null) {
|
|
|
|
report.updateDate.setTime(date);
|
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
report.updateDate.set(Calendar.MILLISECOND, 0);
|
2018-02-20 13:54:18 +01:00
|
|
|
|
|
|
|
report.creationDate = Calendar.getInstance();
|
|
|
|
report.creationDate.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2020-12-07 09:43:38 +01:00
|
|
|
date = dateFormat.parse(object.getString("creation_date"));
|
|
|
|
if (date != null) {
|
|
|
|
report.creationDate.setTime(date);
|
|
|
|
}
|
2020-12-07 09:04:44 +01:00
|
|
|
report.creationDate.set(Calendar.MILLISECOND, 0);
|
2018-02-20 13:54:18 +01:00
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
JSONArray trackersArray = object.getJSONArray("trackers");
|
|
|
|
report.trackers = new HashSet<>();
|
2020-12-07 09:04:44 +01:00
|
|
|
for (int i = 0; i < trackersArray.length(); i++)
|
2018-02-20 13:54:18 +01:00
|
|
|
report.trackers.add(trackersArray.getLong(i));
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Tracker parseTracker(JSONObject object, String trackerId) throws JSONException {
|
|
|
|
Tracker tracker = new Tracker();
|
|
|
|
tracker.id = Long.parseLong(trackerId);
|
|
|
|
tracker.website = object.getString("website");
|
|
|
|
tracker.name = object.getString("name");
|
|
|
|
tracker.description = object.getString("description");
|
|
|
|
tracker.networkSignature = object.getString("network_signature");
|
|
|
|
tracker.codeSignature = object.getString("code_signature");
|
|
|
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
|
|
|
|
try {
|
|
|
|
tracker.creationDate = Calendar.getInstance();
|
|
|
|
tracker.creationDate.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2020-12-07 09:43:38 +01:00
|
|
|
Date date = dateFormat.parse(object.getString("creation_date"));
|
|
|
|
if (date != null) {
|
|
|
|
tracker.creationDate.setTime(date);
|
|
|
|
}
|
2018-02-20 13:54:18 +01:00
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return tracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getJSON(InputStream stream) {
|
|
|
|
InputStreamReader isr = new InputStreamReader(stream);
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
boolean isReading = true;
|
|
|
|
String data;
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
//get all data in a String
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
data = br.readLine();
|
|
|
|
if (data != null)
|
|
|
|
builder.append(data);
|
|
|
|
else
|
|
|
|
isReading = false;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
isReading = false;
|
|
|
|
}
|
|
|
|
} while (isReading);
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isConnectedToInternet(Context context) {
|
|
|
|
//verify the connectivity
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
2020-12-07 09:04:44 +01:00
|
|
|
if (connectivityManager == null)
|
2018-02-20 13:54:18 +01:00
|
|
|
return false;
|
|
|
|
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
|
if (networkInfo != null) {
|
|
|
|
NetworkInfo.State networkState = networkInfo.getState();
|
2020-12-07 09:04:44 +01:00
|
|
|
return networkState.equals(NetworkInfo.State.CONNECTED);
|
2018-02-20 13:54:18 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:43:38 +01:00
|
|
|
private static class Message {
|
2018-02-20 13:54:18 +01:00
|
|
|
Message_Type type = Message_Type.UNKNOWN;
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
NetworkListener listener;
|
|
|
|
Context context;
|
|
|
|
}
|
|
|
|
}
|