If app is new runs FirstPassword. Also added SplashActivity.

This commit is contained in:
Marcin Wozniak
2019-10-21 22:26:54 +02:00
parent 830ca9db3e
commit 9932754c8e
9 changed files with 321 additions and 78 deletions

View File

@ -0,0 +1,73 @@
package com.zadanie1.mw;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class FirstPassword extends Activity {
private static final String FILE_NAME = "passwd.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_password);
final EditText newpassword;
final EditText renewpassword;
Button confirm;
newpassword = (EditText) findViewById(R.id.newpassword);
renewpassword = (EditText) findViewById(R.id.renewpassword);
confirm = (Button) findViewById(R.id.button);
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
String text = newpassword.getText().toString();
FileOutputStream fos = null;
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
onBackPressed();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(),
"Password has NOT been changed",
Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}

View File

@ -9,64 +9,106 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class FormPassword extends Activity {
private static final String FILE_NAME = "passwd.txt";
public String readFile(String filename) throws IOException {
String content = null;
File file = new File(filename); // For example, foo.txt
FileReader reader = null;
try {
reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return content;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_password);
final EditText currentpassword;
final EditText newpassword;
final EditText renewpassword;
Button confirm;
currentpassword = (EditText) findViewById(R.id.currentpassword);
newpassword = (EditText) findViewById(R.id.newpassword);
renewpassword = (EditText) findViewById(R.id.renewpassword);
confirm = (Button) findViewById(R.id.button);
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
final String filename = getFilesDir() + "/passwd.txt";
File file = new File(filename);
String text = newpassword.getText().toString();
FileOutputStream fos = null;
try {
if (currentpassword.getText().toString().equals(readFile(filename).toString())) {
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
String text = newpassword.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
onBackPressed();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Password has NOT been changed!",
Toast.LENGTH_SHORT);
toast.show();
}
}
onBackPressed();
}
else
{
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Password has NOT been changed",
"Current password is WRONG!",
Toast.LENGTH_SHORT);
toast.show();
}
} catch (IOException e) {
e.printStackTrace();
}
currentpassword.getText().clear();
newpassword.getText().clear();
renewpassword.getText().clear();
}
});
}

View File

@ -45,12 +45,25 @@ public class MainActivity extends AppCompatActivity {
return content;
}
private boolean isFileExists(File file) {
return file.exists() && !file.isDirectory();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String filename = getFilesDir() + "/passwd.txt";
File file = new File(filename);
if (!isFileExists(file)){
Intent myIntent3 = new Intent(MainActivity.this,
FirstPassword.class);
startActivity(myIntent3);
}
response = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
restartpassword = (Button) findViewById(R.id.restartpassword);
@ -68,11 +81,6 @@ public class MainActivity extends AppCompatActivity {
button.setOnClickListener(new OnClickListener() {
private final String filename = getFilesDir() + "/passwd.txt";
private boolean isFileExists(File file) {
return file.exists() && !file.isDirectory();
}
@Override
public void onClick(View view) {
/*response.setText(textPassword.getText());*/
@ -90,22 +98,24 @@ public class MainActivity extends AppCompatActivity {
Noto.class);
startActivity(myIntent2);
}
} else if (textPassword.getText().toString().equals("FirstPassword!")) {
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is NOT correct",
Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is NOT correct",
Toast.LENGTH_SHORT);
toast.show();
}
} else {
Intent myIntent3 = new Intent(MainActivity.this,
FirstPassword.class);
startActivity(myIntent3);
}
} catch(IOException e){
e.printStackTrace();
}
textPassword.getText().clear();
}
});
}
}

View File

@ -0,0 +1,27 @@
package com.zadanie1.mw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
},3000);
}
}