From e36f95b65eeedbc8de5fb7efc0a3748b1612d3f5 Mon Sep 17 00:00:00 2001 From: stom79 Date: Fri, 24 Nov 2017 18:49:02 +0100 Subject: [PATCH] Adds asynctask --- .../mastodon/activities/BaseMainActivity.java | 23 -- .../activities/InstanceHealthActivity.java | 11 +- .../fr/gouv/etalab/mastodon/client/API.java | 42 ++++ .../client/Entities/InstanceSocial.java | 208 ++++++++++++++++++ 4 files changed, 258 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceSocial.java diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 9981ff98f..daaf8d3a7 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -40,7 +40,6 @@ import android.support.v7.app.AlertDialog; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.SearchView; import android.support.v7.widget.SwitchCompat; -import android.util.Log; import android.util.Patterns; import android.view.Gravity; import android.view.LayoutInflater; @@ -85,7 +84,6 @@ import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Notification; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.Entities.Version; -import fr.gouv.etalab.mastodon.client.HttpsConnection; import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader; import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment; import fr.gouv.etalab.mastodon.fragments.DisplayDraftsFragment; @@ -231,28 +229,7 @@ public abstract class BaseMainActivity extends AppCompatActivity startSreaming(false); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setTabMode(TabLayout.MODE_FIXED); - new Thread(new Runnable(){ - @Override - public void run() { - try { - HashMap parameters = new HashMap<>(); - parameters.put("name", "mastodon.social"); - final String response = new HttpsConnection().get("https://instances.social/api/1.0/instances/show", 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); - Log.v(Helper.TAG,"resp: " + response); - runOnUiThread(new Runnable() { - public void run() { - - } - }); - - } catch (HttpsConnection.HttpsConnectionException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } - }).start(); tabLayout.addTab(tabHome); tabLayout.addTab(tabNotif); if( display_local) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java index a23378cbe..67638cf97 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/InstanceHealthActivity.java @@ -20,10 +20,14 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; import android.view.MenuItem; + +import org.json.JSONObject; + import java.util.HashMap; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.Entities.InstanceSocial; import fr.gouv.etalab.mastodon.client.HttpsConnection; import fr.gouv.etalab.mastodon.helper.Helper; @@ -35,7 +39,7 @@ import fr.gouv.etalab.mastodon.helper.Helper; public class InstanceHealthActivity extends AppCompatActivity { - + private InstanceSocial instanceSocial; @Override protected void onCreate(Bundle savedInstanceState) { @@ -75,7 +79,8 @@ public class InstanceHealthActivity extends AppCompatActivity { HashMap parameters = new HashMap<>(); parameters.put("name", instance.trim()); final String response = new HttpsConnection().get("https://instances.social/api/1.0/instances/show", 30, parameters, Helper.THEKINRAR_SECRET_TOKEN ); - Log.v(Helper.TAG,"resp: " + response); + if( response != null) + instanceSocial = API.parseInstanceSocialResponse(getApplicationContext(), new JSONObject(response)); runOnUiThread(new Runnable() { public void run() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 11f6c99a1..40b529408 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -1153,6 +1153,48 @@ public class API { return results; } + + /** + * Parse json response an unique instance social result + * @param resobj JSONObject + * @return InstanceSocial + */ + public static InstanceSocial parseInstanceSocialResponse(Context context, JSONObject resobj){ + + InstanceSocial instanceSocial = new InstanceSocial(); + try { + instanceSocial.setAdded_at(Helper.mstStringToDate(context, resobj.get("added_at").toString())); + instanceSocial.setChecked_at(Helper.mstStringToDate(context, resobj.get("checked_at").toString())); + instanceSocial.setUpdated_at(Helper.mstStringToDate(context, resobj.get("updated_at").toString())); + + instanceSocial.setUptime(Float.parseFloat(resobj.get("uptime").toString())); + instanceSocial.setUp(Boolean.parseBoolean(resobj.get("up").toString())); + + instanceSocial.setConnections(Long.parseLong(resobj.get("connections").toString())); + instanceSocial.setDead(Boolean.parseBoolean(resobj.get("dead").toString())); + instanceSocial.setHttps_rank(resobj.get("https_rank").toString()); + instanceSocial.setHttps_score(Integer.parseInt(resobj.get("https_score").toString())); + instanceSocial.setId(resobj.get("id").toString()); + + instanceSocial.setInfo(resobj.get("info").toString()); + instanceSocial.setVersion(resobj.get("version").toString()); + instanceSocial.setName(resobj.get("name").toString()); + instanceSocial.setObs_rank(resobj.get("obs_rank").toString()); + instanceSocial.setThumbnail(resobj.get("thumbnail").toString()); + instanceSocial.setIpv6(Boolean.parseBoolean(resobj.get("ipv6").toString())); + instanceSocial.setObs_score(Integer.parseInt(resobj.get("obs_score").toString())); + instanceSocial.setOpen_registrations(Boolean.parseBoolean(resobj.get("open_registrations").toString())); + + instanceSocial.setUsers(Long.parseLong(resobj.get("users").toString())); + instanceSocial.setStatuses(Long.parseLong(resobj.get("statuses").toString())); + + } catch (JSONException e) { + e.printStackTrace(); + } + return instanceSocial; + } + + /** * Parse Tags * @param jsonArray JSONArray diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceSocial.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceSocial.java new file mode 100644 index 000000000..fe03c4d5e --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceSocial.java @@ -0,0 +1,208 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab 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 Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.client.Entities; + +import java.util.Date; + +/** + * Created by Thomas on 24/11/2017. + * Describes instance from instances.social + */ + +public class InstanceSocial { + + private String id; + private String name; + private Date added_at; + private Date updated_at; + private Date checked_at; + private float uptime; + private boolean up; + private boolean dead; + private String version; + private boolean ipv6; + private int https_score; + private String https_rank; + private int obs_score; + private String obs_rank; + private long users; + private long statuses; + private long connections; + private boolean open_registrations; + private String info; + private String thumbnail; + + + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getAdded_at() { + return added_at; + } + + public void setAdded_at(Date added_at) { + this.added_at = added_at; + } + + public Date getUpdated_at() { + return updated_at; + } + + public void setUpdated_at(Date updated_at) { + this.updated_at = updated_at; + } + + public Date getChecked_at() { + return checked_at; + } + + public void setChecked_at(Date checked_at) { + this.checked_at = checked_at; + } + + public float getUptime() { + return uptime; + } + + public void setUptime(float uptime) { + this.uptime = uptime; + } + + public boolean isUp() { + return up; + } + + public void setUp(boolean up) { + this.up = up; + } + + public boolean isDead() { + return dead; + } + + public void setDead(boolean dead) { + this.dead = dead; + } + + public boolean isIpv6() { + return ipv6; + } + + public void setIpv6(boolean ipv6) { + this.ipv6 = ipv6; + } + + public int getHttps_score() { + return https_score; + } + + public void setHttps_score(int https_score) { + this.https_score = https_score; + } + + public String getHttps_rank() { + return https_rank; + } + + public void setHttps_rank(String https_rank) { + this.https_rank = https_rank; + } + + public int getObs_score() { + return obs_score; + } + + public void setObs_score(int obs_score) { + this.obs_score = obs_score; + } + + public String getObs_rank() { + return obs_rank; + } + + public void setObs_rank(String obs_rank) { + this.obs_rank = obs_rank; + } + + public long getUsers() { + return users; + } + + public void setUsers(long users) { + this.users = users; + } + + public long getStatuses() { + return statuses; + } + + public void setStatuses(long statuses) { + this.statuses = statuses; + } + + public long getConnections() { + return connections; + } + + public void setConnections(long connections) { + this.connections = connections; + } + + public boolean isOpen_registrations() { + return open_registrations; + } + + public void setOpen_registrations(boolean open_registrations) { + this.open_registrations = open_registrations; + } + + public String getInfo() { + return info; + } + + public void setInfo(String info) { + this.info = info; + } + + public String getThumbnail() { + return thumbnail; + } + + public void setThumbnail(String thumbnail) { + this.thumbnail = thumbnail; + } +}