Added restart password and back in application.

This commit is contained in:
Marcin Wozniak 2019-10-19 16:45:05 +02:00
parent 7ac96f9de4
commit ed65588778
5 changed files with 96 additions and 17 deletions

View File

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
@ -16,7 +17,6 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

View File

@ -1,17 +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 FormPassword extends Activity {
private static final String FILE_NAME = "passwd.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_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

@ -10,6 +10,7 @@ import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileReader;
@ -45,8 +46,6 @@ public class MainActivity extends AppCompatActivity {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -57,30 +56,54 @@ public class MainActivity extends AppCompatActivity {
restartpassword = (Button) findViewById(R.id.restartpassword);
textPassword = findViewById(R.id.editText);
restartpassword.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
FormPassword.class);
startActivity(myIntent);
finish();
}
});
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());
try {
if (textPassword.getText().toString().equals(readFile(filename).toString())) {
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
finish();
}
} catch (IOException e) {
e.printStackTrace();
}
File file = new File(filename);
try {
if (isFileExists(file)) {
if (textPassword.getText().toString().equals(readFile(filename).toString())) {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is correct!",
Toast.LENGTH_SHORT);
toast.show();
Intent myIntent2 = new Intent(MainActivity.this,
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();
}
} catch(IOException e){
e.printStackTrace();
}
}
});
}

View File

@ -72,7 +72,7 @@ public class Noto extends AppCompatActivity {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
ChangeNote.getText().clear();
/*ChangeNote.getText().clear();*/
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {

View File

@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files