From ffc98663d10f968c2478f1c718b4f74754bbc296 Mon Sep 17 00:00:00 2001 From: elect Date: Sun, 2 Apr 2023 10:35:02 +0530 Subject: [PATCH] Final clean up --- .../yapca/data/LoginDataSource.java | 8 ++------ .../electronicsboy/yapca/data/MessageAdapter.java | 1 - .../yapca/ui/chat/ChatLoginScreen.java | 13 ++++++------- .../electronicsboy/yapca/ui/chat/ChatScreen.java | 3 +-- .../yapca/ui/chat/CreateRoomScreen.java | 15 +++++++-------- .../yapca/ui/chat/OpenNewChatScreen.java | 10 ++++------ .../yapca/ui/login/LoginActivity.java | 3 +-- .../yapca/ui/login/LoginViewModel.java | 9 +++------ .../yapca/ui/splash/ChatAppSplashScreen.java | 3 --- .../yapca/ui/splash/ChatScreenSplashScreen.java | 2 -- .../yapca/ui/splash/SplashScreen.java | 2 -- .../me/electronicsboy/yapca/util/AppConfig.java | 2 -- .../me/electronicsboy/yapca/util/StringUtil.java | 7 +++++++ .../res/layout/activity_chat_login_screen.xml | 1 + app/src/main/res/layout/activity_chat_screen.xml | 13 ++++++++----- .../res/layout/activity_chat_select_screen.xml | 3 ++- .../res/layout/activity_create_room_screen.xml | 1 + app/src/main/res/layout/activity_login.xml | 7 ++++--- .../res/layout/activity_open_new_chat_screen.xml | 1 + .../main/res/layout/activity_splash_screen.xml | 1 - 20 files changed, 48 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/me/electronicsboy/yapca/data/LoginDataSource.java b/app/src/main/java/me/electronicsboy/yapca/data/LoginDataSource.java index 328347d..ef89af0 100644 --- a/app/src/main/java/me/electronicsboy/yapca/data/LoginDataSource.java +++ b/app/src/main/java/me/electronicsboy/yapca/data/LoginDataSource.java @@ -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; @@ -34,12 +35,7 @@ public Result login(String username, @NonNull String password) { } public Result 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 loginData = (HashMap) TempStorage.get("LOGIN_DATA"); if(loginData.get(username) != null) return new Result.Error(new LoginException("Username with username \"" + username + "\" already exists!")); diff --git a/app/src/main/java/me/electronicsboy/yapca/data/MessageAdapter.java b/app/src/main/java/me/electronicsboy/yapca/data/MessageAdapter.java index db9bd66..2b0afe2 100644 --- a/app/src/main/java/me/electronicsboy/yapca/data/MessageAdapter.java +++ b/app/src/main/java/me/electronicsboy/yapca/data/MessageAdapter.java @@ -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 { diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatLoginScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatLoginScreen.java index 69f7be3..ec05bf1 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatLoginScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatLoginScreen.java @@ -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 { @@ -29,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_chat_login_screen); HashMap bannedUsers = (HashMap) 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(); @@ -63,12 +65,9 @@ public void afterTextChanged(Editable s) { ((Button)findViewById(R.id.buttoncreate)).setOnClickListener((v) -> { HashMap keys = (HashMap) 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) { diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatScreen.java index 763852f..86385ff 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/chat/ChatScreen.java @@ -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; @@ -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); diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/chat/CreateRoomScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/chat/CreateRoomScreen.java index 3ea2eeb..590abd8 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/chat/CreateRoomScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/chat/CreateRoomScreen.java @@ -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 @@ -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) 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) 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 chats = (List) TempStorage.get("CHATS_DATA"); - if(chats == null) chats = new ArrayList(); + 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); diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/chat/OpenNewChatScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/chat/OpenNewChatScreen.java index 24e741c..e6757b6 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/chat/OpenNewChatScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/chat/OpenNewChatScreen.java @@ -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 { @@ -84,18 +85,15 @@ private boolean isUserNameValid(String username) { if(((HashMap) 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 chatKeys = ((HashMap) 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 chats = (List) 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); diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginActivity.java b/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginActivity.java index c64568a..9c34359 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginActivity.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginActivity.java @@ -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 { @@ -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); diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginViewModel.java b/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginViewModel.java index 8a965c9..86774a2 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginViewModel.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/login/LoginViewModel.java @@ -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 { @@ -32,9 +33,7 @@ LiveData 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 result = loginRepository.login(username, password); if (result instanceof Result.Success) { @@ -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 result = loginRepository.register(username, password); if (result instanceof Result.Success) { diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatAppSplashScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatAppSplashScreen.java index d2dedb9..532d623 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatAppSplashScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatAppSplashScreen.java @@ -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; @@ -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 diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatScreenSplashScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatScreenSplashScreen.java index 024c4b9..610c463 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatScreenSplashScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/splash/ChatScreenSplashScreen.java @@ -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; diff --git a/app/src/main/java/me/electronicsboy/yapca/ui/splash/SplashScreen.java b/app/src/main/java/me/electronicsboy/yapca/ui/splash/SplashScreen.java index 0d79ae1..e6ed831 100644 --- a/app/src/main/java/me/electronicsboy/yapca/ui/splash/SplashScreen.java +++ b/app/src/main/java/me/electronicsboy/yapca/ui/splash/SplashScreen.java @@ -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; diff --git a/app/src/main/java/me/electronicsboy/yapca/util/AppConfig.java b/app/src/main/java/me/electronicsboy/yapca/util/AppConfig.java index 5f1eb77..20775b6 100644 --- a/app/src/main/java/me/electronicsboy/yapca/util/AppConfig.java +++ b/app/src/main/java/me/electronicsboy/yapca/util/AppConfig.java @@ -7,10 +7,8 @@ import android.view.WindowManager; public class AppConfig extends Application { - private Context context; public void onCreate() { super.onCreate(); - context = getApplicationContext(); setupActivityListener(); } diff --git a/app/src/main/java/me/electronicsboy/yapca/util/StringUtil.java b/app/src/main/java/me/electronicsboy/yapca/util/StringUtil.java index fcefed3..f160ae5 100644 --- a/app/src/main/java/me/electronicsboy/yapca/util/StringUtil.java +++ b/app/src/main/java/me/electronicsboy/yapca/util/StringUtil.java @@ -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(); + } } diff --git a/app/src/main/res/layout/activity_chat_login_screen.xml b/app/src/main/res/layout/activity_chat_login_screen.xml index ab8529f..0e259f4 100644 --- a/app/src/main/res/layout/activity_chat_login_screen.xml +++ b/app/src/main/res/layout/activity_chat_login_screen.xml @@ -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" diff --git a/app/src/main/res/layout/activity_chat_screen.xml b/app/src/main/res/layout/activity_chat_screen.xml index bb635eb..96ca388 100644 --- a/app/src/main/res/layout/activity_chat_screen.xml +++ b/app/src/main/res/layout/activity_chat_screen.xml @@ -36,21 +36,24 @@ + app:layout_constraintVertical_bias="0.352" /> + + app:layout_constraintVertical_bias="1.0"/>