Skip to content

Commit

Permalink
Final clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroBoy404NotFound committed Apr 2, 2023
1 parent 1d860ec commit ffc9866
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.electronicsboy.yapca.TempStorage;
import me.electronicsboy.yapca.data.model.LoggedInUser;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.StringUtil;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -34,12 +35,7 @@ public Result<LoggedInUser> login(String username, @NonNull String password) {
}

public Result<LoggedInUser> register(String username, @NonNull String password) {
if(password.length() < 16) {
StringBuilder passwordBuilder = new StringBuilder(password);
while(passwordBuilder.length() < 16)
passwordBuilder.append('0');
password = passwordBuilder.toString();
}
if(password.length() < 16) password = StringUtil.convertTo16chars(password);
HashMap<String, String> loginData = (HashMap<String, String>) TempStorage.get("LOGIN_DATA");
if(loginData.get(username) != null)
return new Result.Error(new LoginException("Username with username \"" + username + "\" already exists!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import me.electronicsboy.yapca.R;
import me.electronicsboy.yapca.TempStorage;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.ReportSystem;

public class MessageAdapter extends ArrayAdapter<MessageItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Objects;

import me.electronicsboy.yapca.R;
import me.electronicsboy.yapca.ui.splash.ChatAppSplashScreen;
import me.electronicsboy.yapca.ui.splash.ChatScreenSplashScreen;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.StringUtil;

public class ChatLoginScreen extends AppCompatActivity {

Expand All @@ -29,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_chat_login_screen);
HashMap<String, String> bannedUsers = (HashMap<String, String>) get("BANNED_USERS_CHATS");
if(bannedUsers.get(get("OPEN_CHAT")) != null) {
for (String u : bannedUsers.get(get("OPEN_CHAT")).split(",")) {
for (String u : Objects.requireNonNull(bannedUsers.get(get("OPEN_CHAT"))).split(",")) {
System.out.println(u);
if (u.equals(get("USERNAME"))) {
Toast.makeText(this, "You are banned from " + get("OPEN_CHAT") + "!", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -63,12 +65,9 @@ public void afterTextChanged(Editable s) {
((Button)findViewById(R.id.buttoncreate)).setOnClickListener((v) -> {
HashMap<String, String> keys = (HashMap<String, String>) get("CHAT_KEYS");
try {
StringBuilder password = new StringBuilder(((EditText) findViewById(R.id.editTextTextPassword)).getText().toString());
if(password.length() < 16)
while(password.length() < 16)
password.append('0');
if(Crypto.getSHA256(password.toString()).equals(keys.get(get("OPEN_CHAT")))){
addOrSet("CT_CP", password.toString());
String password = StringUtil.convertTo16chars(((EditText) findViewById(R.id.editTextTextPassword)).getText().toString());
if(Crypto.getSHA256(password).equals(keys.get(get("OPEN_CHAT")))){
addOrSet("CT_CP", password);
startActivity(new Intent(ChatLoginScreen.this, ChatScreenSplashScreen.class));
}else Toast.makeText(ChatLoginScreen.this, "Incorrect Password!", Toast.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.Toast;

Expand Down Expand Up @@ -127,7 +126,7 @@ public void onCancelled(@NonNull DatabaseError error) {
exit.setBackgroundColor(Color.GRAY);
exit.setOnClickListener((notUsed1) -> popupWindow.dismiss());
Button back = new Button(ChatScreen.this);
back.setText("Back");
back.setText("Exit Room");
back.setBackgroundColor(Color.GRAY);
back.setOnClickListener((notUsed1) -> startActivity(new Intent(ChatScreen.this, ChatSelectScreen.class)));
ln.addView(back);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;

import me.electronicsboy.yapca.R;
import me.electronicsboy.yapca.TempStorage;
import me.electronicsboy.yapca.ui.splash.ChatAppSplashScreen;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.StringUtil;

public class CreateRoomScreen extends AppCompatActivity {
@Override
Expand Down Expand Up @@ -85,18 +87,15 @@ private boolean isUserNameValid(String username) {
Toast.makeText(CreateRoomScreen.this, "Room already exists!", Toast.LENGTH_SHORT).show();
else {
try {
StringBuilder password = new StringBuilder(((EditText) findViewById(R.id.editTextTextPassword3)).getText().toString());
if(password.length() < 16)
while(password.length() < 16)
password.append('0');
((HashMap<String, String>) TempStorage.get("CHAT_KEYS")).put(((EditText)findViewById(R.id.editTextTextPersonName)).getText().toString(), Crypto.getSHA256(password.toString()));
FirebaseDatabase.getInstance().getReference("ChatKeys/" + ((EditText)findViewById(R.id.editTextTextPersonName)).getText().toString()).setValue(Crypto.getSHA256(password.toString()));
String password = StringUtil.convertTo16chars(((EditText) findViewById(R.id.editTextTextPassword3)).getText().toString());
((HashMap<String, String>) TempStorage.get("CHAT_KEYS")).put(((EditText)findViewById(R.id.editTextTextPersonName)).getText().toString(), Crypto.getSHA256(password));
FirebaseDatabase.getInstance().getReference("ChatKeys/" + ((EditText)findViewById(R.id.editTextTextPersonName)).getText().toString()).setValue(Crypto.getSHA256(password));
List<String> chats = (List<String>) TempStorage.get("CHATS_DATA");
if(chats == null) chats = new ArrayList<String>();
if(chats == null) chats = new ArrayList<>();
chats.add(((EditText)findViewById(R.id.editTextTextPersonName)).getText().toString());
StringBuilder finalData = new StringBuilder();
for(int i = 0; i < chats.size(); i++) {
finalData.append(Crypto.encrypt(chats.get(i), (String) TempStorage.get("PASSWORD_CLEARTXT")).replace("\n", ""));
finalData.append(Objects.requireNonNull(Crypto.encrypt(chats.get(i), (String) TempStorage.get("PASSWORD_CLEARTXT"))).replace("\n", ""));
if(i < chats.size()-1) finalData.append(',');
}
System.out.println(finalData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import me.electronicsboy.yapca.TempStorage;
import me.electronicsboy.yapca.ui.splash.ChatAppSplashScreen;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.StringUtil;

public class OpenNewChatScreen extends AppCompatActivity {

Expand Down Expand Up @@ -84,18 +85,15 @@ private boolean isUserNameValid(String username) {
if(((HashMap<String, String>) TempStorage.get("CHAT_KEYS")).get(((EditText)findViewById(R.id.username)).getText().toString()) == null)
Toast.makeText(OpenNewChatScreen.this, "Room does not exists!", Toast.LENGTH_SHORT).show();
else {
StringBuilder password = new StringBuilder(((EditText) findViewById(R.id.password)).getText().toString());
if(password.length() < 16)
while(password.length() < 16)
password.append('0');
String password = StringUtil.convertTo16chars(((EditText) findViewById(R.id.password)).getText().toString());
HashMap<String, String> chatKeys = ((HashMap<String, String>) TempStorage.get("CHAT_KEYS"));
try {
if(Objects.requireNonNull(chatKeys.get(((EditText) findViewById(R.id.username)).getText().toString())).equals(Crypto.getSHA256(password.toString()))) {
if(Objects.requireNonNull(chatKeys.get(((EditText) findViewById(R.id.username)).getText().toString())).equals(Crypto.getSHA256(password))) {
List<String> chats = (List<String>) TempStorage.get("CHATS_DATA");
chats.add(((EditText)findViewById(R.id.username)).getText().toString());
StringBuilder finalData = new StringBuilder();
for(int i = 0; i < chats.size(); i++) {
finalData.append(Crypto.encrypt(chats.get(i), (String) TempStorage.get("PASSWORD_CLEARTXT")).replace("\n", ""));
finalData.append(Objects.requireNonNull(Crypto.encrypt(chats.get(i), (String) TempStorage.get("PASSWORD_CLEARTXT"))).replace("\n", ""));
if(i < chats.size()-1) finalData.append(',');
}
System.out.println(finalData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void afterTextChanged(Editable s) {
});

loginButton.setOnClickListener(v -> {
v.setEnabled(false);
loginButton.setEnabled(false);
registerButton.setEnabled(false);
loadingProgressBar.setVisibility(View.VISIBLE);
try {
Expand All @@ -123,7 +123,6 @@ public void afterTextChanged(Editable s) {
}
});
registerButton.setOnClickListener(v -> {
v.setEnabled(false);
loginButton.setEnabled(false);
registerButton.setEnabled(false);
loadingProgressBar.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import me.electronicsboy.yapca.data.model.LoggedInUser;
import me.electronicsboy.yapca.R;
import me.electronicsboy.yapca.util.Crypto;
import me.electronicsboy.yapca.util.StringUtil;

public class LoginViewModel extends ViewModel {

Expand All @@ -32,9 +33,7 @@ LiveData<LoginResult> getLoginResult() {

public void login(String username, String password) throws NoSuchAlgorithmException {
// can be launched in a separate asynchronous job
if(password.length() < 16)
while(password.length() < 16)
password += '0';
password = StringUtil.convertTo16chars(password);
Result<LoggedInUser> result = loginRepository.login(username, password);

if (result instanceof Result.Success) {
Expand Down Expand Up @@ -73,9 +72,7 @@ private boolean isPasswordValid(String password) {
}

public void register(String username, String password) throws NoSuchAlgorithmException {
if(password.length() < 16)
while(password.length() < 16)
password += '0';
password = StringUtil.convertTo16chars(password);
Result<LoggedInUser> result = loginRepository.register(username, password);

if (result instanceof Result.Success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
Expand Down Expand Up @@ -79,8 +78,6 @@ public void onDataChange(@NonNull DataSnapshot snapshot1) {
public void onCancelled(@NonNull DatabaseError error) {
}
});
// startActivity(new Intent(ChatAppSplashScreen.this, ChatSelectScreen.class));
// finish();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.os.Bundle;
import android.util.Log;

import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.os.Bundle;
import android.util.Log;

import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/me/electronicsboy/yapca/util/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import android.view.WindowManager;

public class AppConfig extends Application {
private Context context;
public void onCreate() {
super.onCreate();
context = getApplicationContext();
setupActivityListener();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public static byte[] fromHex64String(String s) {
}
return data;
}

public static String convertTo16chars(String s) {
StringBuilder passwordBuilder = new StringBuilder(s);
while(passwordBuilder.length() < 16)
passwordBuilder.append('0');
return passwordBuilder.toString();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_chat_login_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
android:id="@+id/buttoncreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/res/layout/activity_chat_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,24 @@
<ListView
android:id="@+id/chat"
android:layout_width="fill_parent"
android:layout_height="500sp"
android:layout_height="470sp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296" />
app:layout_constraintVertical_bias="0.352" />

<EditText
android:id="@+id/messagesend"
android:layout_width="400sp"
android:layout_height="61sp"
android:layout_width="367dp"
android:layout_height="52dp"
android:layout_marginTop="528sp"
android:ems="10"
android:hint="Message"
android:inputType="textPersonName"
android:inputType="textLongMessage"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_chat_select_screen.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--suppress CheckTagEmptyBody -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
Expand Down Expand Up @@ -67,7 +68,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"></ListView>
app:layout_constraintVertical_bias="1.0"/>

<Button
android:id="@+id/openroom"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_create_room_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:id="@+id/buttoncreate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Create"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.165" />
app:layout_constraintVertical_bias="0.15" />

<ProgressBar
android:id="@+id/loading"
Expand All @@ -81,13 +81,14 @@
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintHorizontal_bias="0.766"
app:layout_constraintStart_toEndOf="@+id/login"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.164" />
app:layout_constraintVertical_bias="0.154" />

<TextView
android:id="@+id/textView4"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_open_new_chat_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
android:id="@+id/buttonopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Open"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_splash_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
android:layout_height="wrap_content"
android:text="Yet Another Private Chat App"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit ffc9866

Please sign in to comment.