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

@ -2,24 +2,28 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zadanie1.mw">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Noto"></activity>
<activity android:name=".FormPassword" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstPassword" />
<activity android:name=".Noto" />
<activity android:name=".FormPassword" />
<activity android:name=".MainActivity"/>
</application>
</manifest>

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);
}
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:layout_height="match_parent"
tools:context=".FirstPassword"
android:orientation="vertical"
android:padding="20px">
<EditText
android:id="@+id/newpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="New Password"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<EditText
android:id="@+id/renewpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="New Password again"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM NEW PASSWORD"
tools:layout_editor_absoluteX="260dp"
tools:layout_editor_absoluteY="492dp" />
</androidx.appcompat.widget.LinearLayoutCompat>

View File

@ -1,38 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="20px">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="20px">
<EditText
android:id="@+id/newpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<EditText
android:id="@+id/currentpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="Current Password"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<EditText
android:id="@+id/renewpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<EditText
android:id="@+id/newpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="New Password"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM NEW PASSWORD"
tools:layout_editor_absoluteX="260dp"
tools:layout_editor_absoluteY="492dp" />
<EditText
android:id="@+id/renewpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="New Password again"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="409dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM NEW PASSWORD"
tools:layout_editor_absoluteX="260dp"
tools:layout_editor_absoluteY="492dp" />
</androidx.appcompat.widget.LinearLayoutCompat>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/logo_id"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="@drawable/img"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/logo_id"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Encrypted Noto"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="by Marcin Wozniak"
android:textSize="15dp" />
</RelativeLayout>

View File

@ -1,3 +1,3 @@
<resources>
<string name="app_name">My Application</string>
<string name="app_name">Encrypted Noto</string>
</resources>