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

View File

@ -1,17 +1,73 @@
package com.zadanie1.mw; package com.zadanie1.mw;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; 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 { public class FormPassword extends Activity {
private static final String FILE_NAME = "passwd.txt";
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_password); 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.view.View.OnClickListener;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -45,8 +46,6 @@ public class MainActivity extends AppCompatActivity {
} }
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -57,26 +56,50 @@ public class MainActivity extends AppCompatActivity {
restartpassword = (Button) findViewById(R.id.restartpassword); restartpassword = (Button) findViewById(R.id.restartpassword);
textPassword = findViewById(R.id.editText); textPassword = findViewById(R.id.editText);
restartpassword.setOnClickListener(new OnClickListener() { restartpassword.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) { public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Intent myIntent = new Intent(MainActivity.this,
FormPassword.class); FormPassword.class);
startActivity(myIntent); startActivity(myIntent);
finish();
} }
}); });
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
private final String filename = getFilesDir() + "/passwd.txt"; private final String filename = getFilesDir() + "/passwd.txt";
private boolean isFileExists(File file) {
return file.exists() && !file.isDirectory();
}
@Override @Override
public void onClick(View view) { public void onClick(View view) {
response.setText(textPassword.getText()); response.setText(textPassword.getText());
File file = new File(filename);
try { try {
if (isFileExists(file)) {
if (textPassword.getText().toString().equals(readFile(filename).toString())) { 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, Intent myIntent2 = new Intent(MainActivity.this,
Noto.class); Noto.class);
startActivity(myIntent2); startActivity(myIntent2);
finish();
}
} 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){ } catch(IOException e){
e.printStackTrace(); e.printStackTrace();

View File

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

View File

@ -7,7 +7,7 @@ buildscript {
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files