diff --git a/app/build.gradle b/app/build.gradle index 1c4a152..46e066b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,4 +43,5 @@ dependencies { implementation group: 'com.h2database', name: 'h2-mvstore', version: '1.4.197' implementation 'com.google.android.material:material:1.1.0-alpha04' implementation 'androidx.preference:preference:1.1.0-alpha03' + implementation 'com.google.code.gson:gson:2.8.5' } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observable.java b/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observable.java index 142009e..cdc3983 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observable.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observable.java @@ -1,9 +1,9 @@ package io.github.chronosx88.influence.contracts.observer; -import org.json.JSONObject; +import com.google.gson.JsonObject; public interface Observable { void register(Observer observer, int channelID); void unregister(Observer observer, int channelID); - void notifyObservers(JSONObject jsonObject, int channelID); + void notifyObservers(JsonObject jsonObject, int channelID); } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observer.java b/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observer.java index 0776f34..969e2fb 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observer.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/observer/Observer.java @@ -1,7 +1,7 @@ package io.github.chronosx88.influence.contracts.observer; -import org.json.JSONObject; +import com.google.gson.JsonObject; public interface Observer { - void handleEvent(JSONObject object); + void handleEvent(JsonObject object); } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkActions.java b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkActions.java index 0093ac2..3c6ca26 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkActions.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkActions.java @@ -4,4 +4,6 @@ public class NetworkActions { public static final int START_CHAT = 0x0; public static final int NEW_MESSAGE = 0x1; public static final int MESSAGE_SENT = 0x2; + public static final int PING = 0x3; + public static final int PONG = 0x4; } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/ChatListLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/ChatListLogic.java index 1c8b4a4..3f8971a 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/ChatListLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/ChatListLogic.java @@ -1,7 +1,6 @@ package io.github.chronosx88.influence.logic; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonObject; import java.util.List; @@ -24,17 +23,15 @@ public class ChatListLogic implements ChatListLogicContract, Observer { } @Override - public void handleEvent(JSONObject object) { - try { - switch ((int) object.get("action")) { - case NetworkActions.START_CHAT: { - createChatBySender(new ChatEntity(object.getString("chatID"), object.getString("name"), "")); - AppHelper.getObservable().notifyObservers(new JSONObject().put("action", UIActions.NEW_CHAT), MainObservable.UI_ACTIONS_CHANNEL); - break; - } + public void handleEvent(JsonObject object) { + switch (object.get("action").getAsInt()) { + case NetworkActions.START_CHAT: { + createChatBySender(new ChatEntity(object.get("chatID").getAsString(), object.get("name").getAsString(), "")); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.NEW_CHAT); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); + break; } - } catch (JSONException e) { - e.printStackTrace(); } } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java index 7c58fd0..d9b5187 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java @@ -4,6 +4,10 @@ import android.content.Context; import android.content.SharedPreferences; import android.util.Log; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerDHT; import net.tomp2p.futures.FutureBootstrap; @@ -16,7 +20,6 @@ import net.tomp2p.peers.Number160; import net.tomp2p.peers.PeerAddress; import net.tomp2p.relay.tcp.TCPRelayClientConfig; -import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; @@ -27,6 +30,7 @@ import java.util.UUID; import io.github.chronosx88.influence.contracts.main.MainLogicContract; import io.github.chronosx88.influence.helpers.AppHelper; +import io.github.chronosx88.influence.helpers.NetworkActions; import io.github.chronosx88.influence.helpers.StorageMVStore; import io.github.chronosx88.influence.helpers.UIActions; import io.github.chronosx88.influence.observable.MainObservable; @@ -40,10 +44,12 @@ public class MainLogic implements MainLogicContract { private Context context; private InetAddress bootstrapAddress = null; private PeerAddress bootstrapPeerAddress = null; + private Gson gson; public MainLogic() { this.context = AppHelper.getContext(); this.preferences = context.getSharedPreferences("io.github.chronosx88.influence_preferences", context.MODE_PRIVATE); + gson = new Gson(); } @Override @@ -75,60 +81,42 @@ public class MainLogic implements MainLogicContract { } bootstrapAddress = Inet4Address.getByName(bootstrapIP); } catch (NullPointerException e) { - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.BOOTSTRAP_NOT_SPECIFIED), MainObservable.UI_ACTIONS_CHANNEL); - peerDHT.shutdown(); - return; - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.BOOTSTRAP_NOT_SPECIFIED); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); + peerDHT.shutdown(); + return; } catch (UnknownHostException e) { - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.NETWORK_ERROR), MainObservable.UI_ACTIONS_CHANNEL); - peerDHT.shutdown(); - return; - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.NETWORK_ERROR); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); + peerDHT.shutdown(); + return; } if(!discoverExternalAddress()) { - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.PORT_FORWARDING_ERROR), MainObservable.UI_ACTIONS_CHANNEL); - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.PORT_FORWARDING_ERROR); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); } if(!setupConnectionToRelay()) { - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.RELAY_CONNECTION_ERROR), MainObservable.UI_ACTIONS_CHANNEL); - return; - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.RELAY_CONNECTION_ERROR); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); + return; } if(!bootstrapPeer()) { - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.BOOTSTRAP_ERROR), MainObservable.UI_ACTIONS_CHANNEL); - return; - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.BOOTSTRAP_ERROR); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); + return; } - try { - AppHelper.getObservable().notifyObservers(new JSONObject() - .put("action", UIActions.BOOTSTRAP_SUCCESS), MainObservable.UI_ACTIONS_CHANNEL); - } catch (JSONException ex) { - ex.printStackTrace(); - } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", UIActions.BOOTSTRAP_SUCCESS); + AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL); AppHelper.storePeerID(preferences.getString("peerID", null)); AppHelper.storePeerDHT(peerDHT); setReceiveHandler(); @@ -183,7 +171,13 @@ public class MainLogic implements MainLogicContract { private void setReceiveHandler() { AppHelper.getPeerDHT().peer().objectDataReply((s, r) -> { Log.i(LOG_TAG, "# Incoming message: " + r); - AppHelper.getObservable().notifyObservers(new JSONObject((String) r), MainObservable.OTHER_ACTIONS_CHANNEL); + JSONObject incomingObject = new JSONObject((String) r); + if(incomingObject.getInt("action") == NetworkActions.PING) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("action", NetworkActions.PONG); + return gson.toJson(jsonObject); + } + AppHelper.getObservable().notifyObservers(new JsonParser().parse((String) r).getAsJsonObject(), MainObservable.OTHER_ACTIONS_CHANNEL); return null; }); } diff --git a/app/src/main/java/io/github/chronosx88/influence/observable/MainObservable.java b/app/src/main/java/io/github/chronosx88/influence/observable/MainObservable.java index 025a221..b0adb5f 100644 --- a/app/src/main/java/io/github/chronosx88/influence/observable/MainObservable.java +++ b/app/src/main/java/io/github/chronosx88/influence/observable/MainObservable.java @@ -1,6 +1,6 @@ package io.github.chronosx88.influence.observable; -import org.json.JSONObject; +import com.google.gson.JsonObject; import java.util.ArrayList; @@ -55,7 +55,7 @@ public class MainObservable implements Observable { } @Override - public void notifyObservers(JSONObject jsonObject, int channelID) { + public void notifyObservers(JsonObject jsonObject, int channelID) { switch (channelID) { case UI_ACTIONS_CHANNEL: { for (Observer observer : uiObservers) { diff --git a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java index ea21c26..644478a 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java @@ -6,9 +6,7 @@ import android.view.MenuItem; import android.widget.Toast; import com.google.android.material.bottomnavigation.BottomNavigationView; - -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonObject; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -89,54 +87,49 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie } @Override - public void handleEvent(JSONObject object) { - try { - switch ((int) object.get("action")) { - case UIActions.BOOTSTRAP_NOT_SPECIFIED: { - runOnUiThread(() -> { - progressDialog.dismiss(); - Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show(); - }); - break; - } - case UIActions.NETWORK_ERROR: { - runOnUiThread(() -> { - progressDialog.dismiss(); - Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show(); - }); - break; - } - case UIActions.BOOTSTRAP_SUCCESS: { - runOnUiThread(() -> { - progressDialog.dismiss(); - Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show(); - }); - break; - } - case UIActions.PORT_FORWARDING_ERROR: { - runOnUiThread(() -> { - Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show(); - }); - break; - } - case UIActions.BOOTSTRAP_ERROR: { - runOnUiThread(() -> { - progressDialog.dismiss(); - Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show(); - }); - break; - } - case UIActions.RELAY_CONNECTION_ERROR: { - runOnUiThread(() -> { - progressDialog.dismiss(); - Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show(); - }); - break; - } + public void handleEvent(JsonObject object) { + switch (object.get("action").getAsInt()) { + case UIActions.BOOTSTRAP_NOT_SPECIFIED: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show(); + }); + break; + } + case UIActions.NETWORK_ERROR: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show(); + }); + break; + } + case UIActions.BOOTSTRAP_SUCCESS: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show(); + }); + break; + } + case UIActions.PORT_FORWARDING_ERROR: { + runOnUiThread(() -> { + Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show(); + }); + break; + } + case UIActions.BOOTSTRAP_ERROR: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show(); + }); + break; + } + case UIActions.RELAY_CONNECTION_ERROR: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show(); + }); + break; } - } catch (JSONException e) { - e.printStackTrace(); } - } } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java b/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java index b700ae9..a0d13ee 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java @@ -5,8 +5,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonObject; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -58,15 +57,11 @@ public class ChatListFragment extends Fragment implements ChatListViewContract, } @Override - public void handleEvent(JSONObject object) { - try { - switch (object.getInt("action")) { - case UIActions.NEW_CHAT: { - presenter.updateChatList(); - } + public void handleEvent(JsonObject object) { + switch (object.get("action").getAsInt()) { + case UIActions.NEW_CHAT: { + presenter.updateChatList(); } - } catch (JSONException e) { - e.printStackTrace(); } } }