logincheck + togglepasswordvisibility added

This commit is contained in:
JG-Cody 2021-07-20 21:52:54 +02:00
parent c690cbfde7
commit 41df46611d
6 changed files with 245 additions and 95 deletions

View File

@ -1,5 +1,6 @@
package de.jg_cody.Teraplex.ui.home;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
@ -10,8 +11,11 @@ import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@ -24,11 +28,16 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.util.Objects;
import de.jg_cody.Teraplex.MainActivity;
import de.jg_cody.Teraplex.R;
import de.jg_cody.Teraplex.SSH_connection;
import de.jg_cody.Teraplex.ui.terminal.Terminal;
public class HomeFragment extends Fragment {
@ -37,18 +46,22 @@ public class HomeFragment extends Fragment {
public static EditText ipInput;
public static EditText userInput;
public static EditText passwordInput;
boolean isPasswordVisible = false;
Button reboot_Button;
Button shutdown_Button;
Button loginButton;
Button logoutButton;
Terminal terminal;
Button wlan_aus_Button;
Button wlan_an_Button;
Button kodian;
Button kodiaus;
ImageView togglepassword;
private HomeViewModel homeViewModel;
@SuppressLint("ClickableViewAccessibility")
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
SharedPreferences prefs = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
@ -80,13 +93,16 @@ public class HomeFragment extends Fragment {
loginButton = (Button) root.findViewById(R.id.loginButton);
reboot_Button = (Button) root.findViewById(R.id.reboot);
shutdown_Button = (Button) root.findViewById(R.id.shutdown);
togglepassword = (ImageView) root.findViewById(R.id.togglepassword);
SharedPreferences t = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
if (user == null || ip == null) {
loginButton.setVisibility(View.VISIBLE);
togglepassword.setVisibility(View.VISIBLE);
reboot_Button.setVisibility(View.GONE);
shutdown_Button.setVisibility(View.GONE);
} else {
loginButton.setVisibility(View.INVISIBLE);
togglepassword.setVisibility(View.INVISIBLE);
ipInput.setVisibility(View.GONE);
userInput.setVisibility(View.GONE);
passwordInput.setVisibility(View.GONE);
@ -108,7 +124,7 @@ public class HomeFragment extends Fragment {
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, null )
.setPositiveButton(android.R.string.yes, null)
.create();
Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
@ -117,23 +133,64 @@ public class HomeFragment extends Fragment {
user = userInput.getText().toString();
password = passwordInput.getText().toString();
ip = ipInput.getText().toString();
SharedPreferences p = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
p.edit().putString("user", user).putString("password", password).putString("ip", ip).apply();
Toast.makeText(getContext(), getString(R.string.login_saved), Toast.LENGTH_SHORT).show();
if (user == null || ip == null) {
login_data.setText("");
reboot_Button.setVisibility(View.GONE);
shutdown_Button.setVisibility(View.GONE);
} else {
login_data.setText(getString(R.string.sie_sind_als_angemeldet).replace("{IP}", ip).replace("{USERNAME}", user));
logoutButton.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.INVISIBLE);
ipInput.setVisibility(View.GONE);
userInput.setVisibility(View.GONE);
passwordInput.setVisibility(View.GONE);
reboot_Button.setVisibility(View.VISIBLE);
shutdown_Button.setVisibility(View.VISIBLE);
}
new Thread(new Runnable() {
@Override
public void run() {
try {
JSch jsch = new JSch();
Session session;
session = jsch.getSession(user, ip, 22);
session.setPassword(password);
session.setUserInfo(new Terminal.MyUserInfo() {
@Override
public String getPassword() {
return super.getPassword();
}
});
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("CONNECT");
session.connect(30000);
Channel channel = session.openChannel("shell");
channel.connect(3 * 1000);
session.disconnect();
System.out.println("DISCONNECT");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(), "CONNECTED", Toast.LENGTH_SHORT).show();
SharedPreferences p = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
p.edit().putString("user", user).putString("password", password).putString("ip", ip).apply();
if (user == null || ip == null) {
login_data.setText("");
reboot_Button.setVisibility(View.GONE);
shutdown_Button.setVisibility(View.GONE);
} else {
login_data.setText(getString(R.string.sie_sind_als_angemeldet).replace("{IP}", ip).replace("{USERNAME}", user));
logoutButton.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.INVISIBLE);
togglepassword.setVisibility(View.INVISIBLE);
ipInput.setVisibility(View.GONE);
userInput.setVisibility(View.GONE);
passwordInput.setVisibility(View.GONE);
reboot_Button.setVisibility(View.VISIBLE);
shutdown_Button.setVisibility(View.VISIBLE);
}
}
});
} catch (Exception e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
e.printStackTrace();
Toast.makeText(getContext(), "INVALID INPUT", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
}
});
@ -201,8 +258,6 @@ public class HomeFragment extends Fragment {
mDialog.show();
}
});
shutdown_Button = (Button) root.findViewById(R.id.shutdown);
@ -246,6 +301,27 @@ public class HomeFragment extends Fragment {
}
});
if (togglepassword.getVisibility() == View.VISIBLE) {
togglepassword.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (isPasswordVisible == true) {
passwordInput.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
isPasswordVisible = false;
} else {
passwordInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
isPasswordVisible = true;
}
return false;
}
});
}
return root;
}
}

View File

@ -34,7 +34,7 @@ public class Terminal {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(HomeFragment.user, HomeFragment.ip, 22);
session = jsch.getSession(HomeFragment.user, HomeFragment.ip, 22);
session.setPassword(HomeFragment.password);
session.setUserInfo(new MyUserInfo() {
@ -59,26 +59,30 @@ public class Terminal {
channel.setOutputStream(Output);
channel.connect(3 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTerminalOutput(){
return new String(Output.toByteArray()).replaceAll("(\u001B\\[\\d+;\\d+m)+", "").replaceAll("\u2584", "");
public String getTerminalOutput() {
return new String(Output.toByteArray()).replaceAll("\u001B\\[[;\\d]*m", "");
}
public void sendTerminalCommand(String terminalcommand){
public void sendTerminalCommand(String terminalcommand) {
try {
Input.write((terminalcommand+"\n").getBytes());
Input.write((terminalcommand + "\n").getBytes());
Input.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeTerminal() {
session.disconnect();
}
public static abstract class MyUserInfo
implements UserInfo, UIKeyboardInteractive {
public String getPassword() {
@ -111,5 +115,6 @@ public class Terminal {
boolean[] echo) {
return null;
}
}
}

View File

@ -7,6 +7,8 @@ import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
@ -22,6 +24,7 @@ public class TerminalFragment extends Fragment {
public static String command;
Button send_button;
EditText terminal_edit_text;
TextView terminal_textView;
Terminal terminal;
@ -47,57 +50,83 @@ public class TerminalFragment extends Fragment {
Toast.makeText(getContext(), ("test"), Toast.LENGTH_SHORT).show();
scrollView = (ScrollView) root.findViewById((R.id.scrollView));
terminal_edit_text = (EditText) root.findViewById(R.id.terminal_edit_text);
terminal_edit_text.setOnKeyListener(new View.OnKeyListener() {
send_button = (Button) root.findViewById(R.id.send_button);
send_button.setOnClickListener(new View.OnClickListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
terminal.sendTerminalCommand(terminal_edit_text.getText().toString());
public void onClick(View view) {
} else {
terminal_textView = (TextView) root.findViewById(R.id.terminal_textView);
new Thread() {
public void run() {
terminal = new Terminal();
boolean isFragmentActive = true;
while (isFragmentActive) {
Fragment myFragment = (Fragment) requireActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_terminal);
if (myFragment == null) {
isFragmentActive = false;
}
else if (myFragment.isVisible())
{
isFragmentActive = false;
}
try {
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
boolean atBottom = scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY());
terminal_textView.setText(terminal.getTerminalOutput());
System.out.println(atBottom);
terminal_textView.invalidate();
scrollView.invalidate();
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
return false;
if (terminal != null) {
terminal.sendTerminalCommand(terminal_edit_text.getText().toString());
}
}
});
terminal_edit_text.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
{
boolean handled=false;
// Some phones disregard the IME setting option in the xml, instead
// they send IME_ACTION_UNSPECIFIED so we need to catch that
if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId)
{
if (terminal != null) {
terminal.sendTerminalCommand(terminal_edit_text.getText().toString());
Toast.makeText(getContext(), getString(R.string.login_saved), Toast.LENGTH_SHORT).show();
}
handled=true;
}
return handled;
}
});
terminal_textView = (TextView) root.findViewById(R.id.terminal_textView);
new Thread() {
public void run() {
terminal = new Terminal();
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(),"VERBUNDEN", Toast.LENGTH_SHORT).show();
}
});
boolean isFragmentActive = true;
while (isFragmentActive) {
Fragment myFragment = (Fragment) requireActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_terminal);
if (myFragment == null) {
isFragmentActive = false;
}
else if (myFragment.isVisible())
{
isFragmentActive = false;
}
try {
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
boolean atBottom = scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY());
terminal_textView.setText(terminal.getTerminalOutput());
System.out.println(atBottom);
terminal_textView.invalidate();
scrollView.invalidate();
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@ -107,6 +136,11 @@ public class TerminalFragment extends Fragment {
@Override
public void onDestroy() {
if (terminal != null) {
terminal.closeTerminal();
}
super.onDestroy();
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,6c3.79,0 7.17,2.13 8.82,5.5C19.17,14.87 15.79,17 12,17s-7.17,-2.13 -8.82,-5.5C4.83,8.13 8.21,6 12,6m0,-2C7,4 2.73,7.11 1,11.5 2.73,15.89 7,19 12,19s9.27,-3.11 11,-7.5C21.27,7.11 17,4 12,4zM12,9c1.38,0 2.5,1.12 2.5,2.5S13.38,14 12,14s-2.5,-1.12 -2.5,-2.5S10.62,9 12,9m0,-2c-2.48,0 -4.5,2.02 -4.5,4.5S9.52,16 12,16s4.5,-2.02 4.5,-4.5S14.48,7 12,7z"
android:fillColor="#000000"/>
</vector>

View File

@ -159,7 +159,8 @@
app:layout_constraintEnd_toEndOf="@+id/text_home"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/text_home"
app:layout_constraintTop_toBottomOf="@+id/loginuserInput" />
app:layout_constraintTop_toBottomOf="@+id/loginuserInput"
app:passwordToggleEnabled="true"/>
<Button
android:id="@+id/loginButton"
@ -189,6 +190,17 @@
app:layout_constraintEnd_toEndOf="@+id/text_home"
app:layout_constraintTop_toBottomOf="@+id/loginpasswordInput" />
<ImageView
android:id="@+id/togglepassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-30dp"
android:src="@drawable/visibility_black_24dp"
app:tint="@color/mtrl_btn_text_color_selector"
app:layout_constraintBottom_toBottomOf="@+id/loginpasswordInput"
app:layout_constraintStart_toEndOf="@+id/loginpasswordInput"
app:layout_constraintTop_toBottomOf="@+id/loginuserInput" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,12 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<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"
android:layout_width="match_parent"
android:clickable="true"
android:id="@+id/fragment_terminal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
tools:context=".ui.terminal.TerminalFragment">
<ScrollView
@ -14,32 +13,36 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:scrollbars="horizontal|vertical"
app:layout_constraintBottom_toTopOf="@+id/divider2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/terminal_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textColor="#FFFFFF" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="@+id/terminal_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFFFFF" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
<View
android:id="@+id/divider2"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/background_full"
app:layout_constraintBottom_toTopOf="@+id/terminal_edit_text"
@ -49,16 +52,27 @@
<EditText
android:id="@+id/terminal_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="type commands"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/send_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/button_right"
android:text="SEND"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>