gotowe bez szyfrowania

This commit is contained in:
mikgaw@st.amu.edu.pl 2023-12-11 22:51:12 +01:00
parent eabc69ddf6
commit 15ae064622
10 changed files with 395 additions and 2 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -12,6 +12,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.BSMfingerptinttutorial"
tools:targetApi="31">
<activity
android:name=".NotepadActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">

View File

@ -7,6 +7,7 @@ import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import android.content.Intent;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
@ -83,6 +84,10 @@ public class MainActivity extends AppCompatActivity {
Log.d("MY_LOGS", "Success! Encrypted txt: " + Arrays.toString(encryptedInfo));
Toast.makeText(getApplicationContext(),"Authentication succeeded!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), NotepadActivity.class);
startActivity(intent);
finish();
}
@Override
@ -95,7 +100,7 @@ public class MainActivity extends AppCompatActivity {
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using biometric credential")
.setNegativeButtonText("")
.setNegativeButtonText("xd")
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
.build();

View File

@ -0,0 +1,31 @@
package com.example.bsm_fingerptint_tutorial;
public class Note {
private String title;
private String content;
public Note(){
}
public Note(String title, String content){
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@ -0,0 +1,230 @@
package com.example.bsm_fingerptint_tutorial;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleCoroutineScope;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
public class NotepadActivity extends AppCompatActivity {
private List<Note> noteList;
private LinearLayout notesContainer;
private static final String SHARED_NAME_NOTES = "Notatki";
Button buttonLogout, buttonAddNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notepad);
noteList = new ArrayList<>();
notesContainer = findViewById(R.id.notes_container);
loadNotesFromPreferencesToList();
displayNotes();
buttonLogout = findViewById(R.id.btn_logout);
buttonAddNote = findViewById(R.id.btn_add_note);
buttonLogout.setOnClickListener(view -> logOut());
buttonAddNote.setOnClickListener(view -> showAddNoteDialog());
}
private void logOut(){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
private void showAddNoteDialog(){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Create new note");
builder.setPositiveButton("Save", (dialogInterface, i) -> {
EditText noteTitleEditText = dialogView.findViewById(R.id.note_title_edit_text);
EditText noteContentEditText = dialogView.findViewById(R.id.note_content_edit_text);
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if(!title.isEmpty() && !content.isEmpty()){
Note note = new Note();
note.setTitle(title);
note.setContent(content);
noteList.add(note);
saveNotesToPreferences("add");
createNoteView(note);
Toast.makeText(NotepadActivity.this, "Note saved!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void showEditNoteDialog(Note note){
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Edit note");
EditText noteTitleEditText = dialogView.findViewById(R.id.note_title_edit_text);
EditText noteContentEditText = dialogView.findViewById(R.id.note_content_edit_text);
noteTitleEditText.setText(note.getTitle());
noteContentEditText.setText(note.getContent());
builder.setPositiveButton("Save", (dialogInterface, i) -> {
String title = noteTitleEditText.getText().toString();
String content = noteContentEditText.getText().toString();
if (!title.isEmpty() && !content.isEmpty()){
deleteNoteAndRefresh(note);
note.setTitle(title);
note.setContent(content);
noteList.add(note);
createNoteView(note);
saveNotesToPreferences("add");
}else {
Toast.makeText(NotepadActivity.this, "Enter title and content!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void saveNotesToPreferences(String mode){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (mode.equals("del")){
int noteCount = sharedPreferences.getInt("_notecount_", 0);
for(int i=0; i<noteCount; i++){
editor.remove(i + "_title_");
editor.remove(i + "_content_");
}
}
editor.putInt("_notecount_", noteList.size());
for(int i=0; i<noteList.size(); i++){
Note note = noteList.get(i);
editor.putString(i + "_title_", note.getTitle());
editor.putString(i + "_content_", note.getContent());
}
editor.apply();
}
private void loadNotesFromPreferencesToList(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
int noteCount = sharedPreferences.getInt("_notecount_", 0);
for(int i=0; i<noteCount; i++){
String title = sharedPreferences.getString(i + "_title_", "");
String content = sharedPreferences.getString(i + "_content_", "");
Note note = new Note();
note.setTitle(title);
note.setContent(content);
noteList.add(note);
}
}
private void createNoteView(final Note note){
@SuppressLint("InflateParams") View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
TextView noteTitleTextView = noteView.findViewById(R.id.note_title_text_view);
TextView noteContentTextView = noteView.findViewById(R.id.note_content_text_view);
Button deleteNoteButton = noteView.findViewById(R.id.btn_delete_note);
noteTitleTextView.setText(note.getTitle());
noteContentTextView.setText(note.getContent());
deleteNoteButton.setOnClickListener(view -> showDeleteDialog(note));
noteView.setOnLongClickListener(view -> {
showEditNoteDialog(note);
return true;
});
notesContainer.addView(noteView);
}
private void refreshNotesView(){
notesContainer.removeAllViews();
displayNotes();
}
private void displayNotes(){
for(Note note : noteList){
createNoteView(note);
}
}
private void deleteNoteAndRefresh(Note note){
noteList.remove(note);
saveNotesToPreferences("del");
refreshNotesView();
}
private void showDeleteDialog(final Note note){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete this note");
builder.setMessage("Are you sure you want to delete it?");
builder.setPositiveButton("Delete", (dialogInterface, i) -> {
deleteNoteAndRefresh(note);
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
}

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".NotepadActivity"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_logout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:text="Log out"/>
<Button
android:id="@+id/btn_add_note"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Add Note"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="18dp"
android:layout_marginTop="15dp"
android:text="Notes:"
android:textStyle="bold"/>
<ScrollView
android:id="@+id/notes_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/notes_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</LinearLayout>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<EditText
android:id="@+id/note_title_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Note Title"/>
<EditText
android:id="@+id/note_content_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Note Content"
android:layout_marginTop="16dp"
android:inputType="textMultiLine"
android:minLines="5"/>
</LinearLayout>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/note_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="Note Title"/>
<TextView
android:id="@+id/note_content_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Note Content"
/>
</LinearLayout>
<Button
android:id="@+id/btn_delete_note"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:text="X"
android:layout_marginLeft="10dp"
/>
</LinearLayout>