Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from MoreliaTalk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rus-ai authored Jul 27, 2020
2 parents 37a7ea0 + 6708e22 commit d37d39f
Show file tree
Hide file tree
Showing 25 changed files with 178 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/Admin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "ru.wtw.moreliatalkclient"
minSdkVersion 14
targetSdkVersion 30
versionCode 2
versionName "0.1.2"
versionCode 4
versionName "0.1.3.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -27,9 +27,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'org.java-websocket:Java-WebSocket:1.4.0'
implementation 'org.java-websocket:Java-WebSocket:1.5.1'
implementation 'com.google.code.gson:gson:2.8.6'
}
Binary file modified app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions app/src/main/java/ru/wtw/moreliatalkclient/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;


public class LoginActivity extends Activity {
Expand All @@ -34,27 +35,42 @@ protected void onCreate(Bundle savedInstanceState) {
if (settings.contains("password")) {
((EditText)findViewById(R.id.editPassword)).setText(settings.getString("password", ""));
}
((Switch)findViewById(R.id.switchReconnect)).setChecked(settings.getBoolean("reconnect", false));
((Switch)findViewById(R.id.switchJSON)).setChecked(settings.getBoolean("outjson", false));
((Switch)findViewById(R.id.switchNewAPI)).setChecked(settings.getBoolean("newapi", false));

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editServername = findViewById(R.id.editServername);
EditText editUsername = findViewById(R.id.editUsername);
EditText editPassword = findViewById(R.id.editPassword);
Switch switchReconnect = findViewById(R.id.switchReconnect);
Switch switchJSON = findViewById(R.id.switchJSON);
Switch switchNewAPI = findViewById(R.id.switchNewAPI);
String username = editUsername.getText().toString();
String password = editPassword.getText().toString();
String servername = editServername.getText().toString();
boolean reconnect = switchReconnect.isChecked();
boolean outJSON = switchJSON.isChecked();
boolean newAPI = switchNewAPI.isChecked();
if (servername.isEmpty() || username.isEmpty() || password.isEmpty()) {
Log.e("SERVER", "Must be filled");
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("username", username);
intent.putExtra("password", password);
intent.putExtra("servername", servername);
intent.putExtra("reconnect", reconnect);
intent.putExtra("outjson", outJSON);
intent.putExtra("newapi", newAPI);
SharedPreferences.Editor editor = settings.edit();
editor.putString("servername", servername);
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("reconnect", reconnect);
editor.putBoolean("outjson", outJSON);
editor.putBoolean("newapi", newAPI);
editor.apply();
startActivity(intent);
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/ru/wtw/moreliatalkclient/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ protected void onCreate(Bundle savedInstanceState) {
network.setUsername(extras.getString("username"));
network.setPassword(extras.getString("password"));
network.setServername(extras.getString("servername"));
network.setReconnect(extras.getBoolean("reconnect"));
network.setShowJSON(extras.getBoolean("outjson"));
network.setUseNewAPI(extras.getBoolean("newapi"));
network.connect();
}

Expand Down
122 changes: 89 additions & 33 deletions app/src/main/java/ru/wtw/moreliatalkclient/Network.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package ru.wtw.moreliatalkclient;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.NonReadableChannelException;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import com.google.gson.Gson;


public class Network {
private URI socketURI;
private WebSocketClient socket;
Expand All @@ -24,6 +26,10 @@ public class Network {
private String username;
private String password;
private String servername;
private boolean reconnect;
private boolean showJSON;
private boolean useNewAPI;

private boolean isConnected;

public Network(Activity activity){
Expand All @@ -43,65 +49,97 @@ public void setPassword(String password) {
this.password = password;
}

public void setReconnect(boolean reconnect) { this.reconnect = reconnect; }

public void setShowJSON(boolean showJSON) { this.showJSON = showJSON; }

public void setUseNewAPI(boolean useNewAPI) { this.useNewAPI = useNewAPI; }

public boolean isConnected() {
return isConnected;
}

public void reconnect(){
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
socket.reconnect();
}
}, 1000);
}

public void connect() {
try {
socketURI = new URI(servername);
} catch (URISyntaxException e) {
e.printStackTrace();
return;
if (socketURI == null) {
try {
socketURI = new URI(servername);
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}

}
socket = new WebSocketClient(socketURI) {
@Override
if (socket == null) socket = new WebSocketClient(socketURI) {

@Override
public void onOpen(ServerHandshake handshakedata) {
isConnected=true;
sendAuth();
Log.i("SERVER","Connected");
isConnected = true;
if (useNewAPI) {
sendReg();
} else {
sendAuth();
}
Log.i("SERVER", "Connected");
}

@Override
public void onMessage(String message) {
Protocol protocol = new Gson().fromJson(message,Protocol.class);
String status=protocol.getStatus();
String reply;
if (protocol.getMode().equals("message")) {
reply = protocol.getUsername() + ": " + protocol.getText();
outChat(reply);
}
if (protocol.getMode().equals("reg")) {
reply = activity.getResources().getString(R.string.auth_status_unknown);
if (status.equals("true"))
reply = activity.getResources().getString(R.string.auth_status_true);
if (status.equals("false"))
reply = activity.getResources().getString(R.string.auth_status_false);
if (status.equals("newreg"))
reply = activity.getResources().getString(R.string.auth_status_newreg);
outChat(reply);
if (showJSON) {
outChat("Received: "+message);
} else {
Protocol protocol = new Gson().fromJson(message, Protocol.class);
String status = protocol.getStatus();
String reply;
if (protocol.getMode().equals("message")) {
reply = protocol.getTime() + " - " + protocol.getUsername() + ": " + protocol.getText();
outChat(reply);
}
if (protocol.getMode().equals("reg")) {
reply = activity.getResources().getString(R.string.auth_status_unknown);
if (status.equals("true"))
reply = activity.getResources().getString(R.string.auth_status_true);
if (status.equals("false"))
reply = activity.getResources().getString(R.string.auth_status_false);
if (status.equals("newreg"))
reply = activity.getResources().getString(R.string.auth_status_newreg);
outChat(reply);
}
}
Log.i("SERVER","Message: "+message);
Log.i("SERVER", "Message: " + message);
}

@Override
public void onMessage(ByteBuffer message) {
Log.i("SERVER","Message byte buffer");
Log.i("SERVER", "Message byte buffer");
}

@Override
public void onClose(final int code, String reason, boolean remote) {
Log.i("SERVER","Disconnected with exit code " + code + " additional info: " + reason);
outChat(activity.getResources().getString(R.string.socket_close)+code);
Log.i("SERVER", "Disconnected with exit code " + String.valueOf(code) + " additional info: " + reason);
outChat(activity.getResources().getString(R.string.socket_close) + String.valueOf(code));
if (reconnect) {
outChat(activity.getResources().getString(R.string.reconnecting));
Network.this.reconnect();
}
}

@Override
public void onError(Exception ex) {
Log.e("SERVER","Error", ex);
outChat(activity.getResources().getString(R.string.socket_error)+ex.toString());
Log.e("SERVER", "Error", ex);
outChat(activity.getResources().getString(R.string.socket_error) + ex.toString());
}
};

Log.i("SERVER","Connect");
socket.connect();

Expand All @@ -125,6 +163,22 @@ public void run()
});
}

public void sendReg () {
Gson gson = new Gson();
Protocol protocol = new Protocol();
protocol.setMode("reg");
protocol.setUsername(username);
protocol.setPassword(password);
String json = "{ \"type\": \"register_user\", \"data\": { \"user\": { \"password\": \"" + password+
"\", \"login\": \""+username+"\", \"email\": \"[email protected]\", \"username\": \"User1\" }, "+
"\"meta\": \"None\" }, \"jsonapi\": { \"version\": \"1.0\" }, \"meta\": \"None\" }";
if (socket != null && socket.isOpen()) {
if (showJSON) outChat("Sending: "+json);
Log.i("SERVER","Send reg");
socket.send(json);
}
}

public void sendAuth () {
Gson gson = new Gson();
Protocol protocol = new Protocol();
Expand All @@ -133,6 +187,7 @@ public void sendAuth () {
protocol.setPassword(password);
String json = gson.toJson(protocol);
if (socket != null && socket.isOpen()) {
if (showJSON) outChat("Sending: "+json);
Log.i("SERVER","Send auth");
socket.send(json);
}
Expand All @@ -146,6 +201,7 @@ public void sendMessage (String text) {
protocol.setText(text);
String json = gson.toJson(protocol);
if (socket != null && socket.isOpen()) {
if (showJSON) outChat("Sending: "+json);
Log.i("SERVER","Send text");
Log.i("SERVER",json);
socket.send(json);
Expand Down
28 changes: 21 additions & 7 deletions app/src/main/java/ru/wtw/moreliatalkclient/Protocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package ru.wtw.moreliatalkclient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static java.util.TimeZone.LONG;

@SuppressWarnings("unused")
public class Protocol {

Expand All @@ -26,6 +32,21 @@ public String getStatus() {
return status;
}

public String getPassword() {
return password;
}

public String getTimestamp() {
return timestamp;
}

public String getTime() {
long javatime = Math.round(Double.parseDouble(timestamp) * 1000);
Date time = new Date(javatime);
DateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(time);
}

public void setStatus(String status) { this.status = status; }

public void setTimestamp(String timestamp) { this.timestamp = timestamp; }
Expand All @@ -46,12 +67,5 @@ public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return password;
}

public String getTimestamp() {
return timestamp;
}
}

33 changes: 33 additions & 0 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@
android:inputType="textPassword" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:id="@+id/switchReconnect"
android:layout_span="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switch_reconnect" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:id="@+id/switchJSON"
android:layout_span="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switch_json_output" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:id="@+id/switchNewAPI"
android:layout_span="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switch_new_api" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand Down
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d37d39f

Please sign in to comment.