Skip to content

Commit ad13827

Browse files
committed
encrypt and decrypt on the fly
1 parent 8f41d82 commit ad13827

File tree

10 files changed

+460
-131
lines changed

10 files changed

+460
-131
lines changed

app/src/main/java/com/katcom/androidFileVault/EntryFragment.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* The entry point of the file vault.
3+
* When the user opens the vault for the first time, it shows a screen to help user setup password.
4+
* And open the vault after the sign up.
5+
* If the user has setup password before, it displays login screen to allow user login to the vault.
6+
*/
17
package com.katcom.androidFileVault;
28

39
import android.content.Intent;
@@ -6,6 +12,8 @@
612
import android.content.res.AssetManager;
713
import android.os.Bundle;
814
import androidx.fragment.app.Fragment;
15+
16+
import android.util.Log;
917
import android.view.LayoutInflater;
1018
import android.view.View;
1119
import android.view.ViewGroup;
@@ -20,8 +28,12 @@
2028
import java.io.FileDescriptor;
2129
import java.io.FileNotFoundException;
2230
import java.io.IOException;
31+
import java.security.KeyStoreException;
32+
import java.security.NoSuchAlgorithmException;
33+
import java.security.UnrecoverableEntryException;
2334

2435
public class EntryFragment extends Fragment {
36+
private static final String TAG = "EntryFragment";
2537
private Button mVaultOpenButton;
2638
private FileManager mVault;
2739
@Override
@@ -41,6 +53,11 @@ public void onClick(View v) {
4153
return view;
4254
}
4355

56+
/**
57+
* If it is the first time user opens the vault, it setup the vault by displaying the SetPassword activity,
58+
* generating a private key for encryption and decryption, and copying sample files into the vault
59+
* Otherwise, shows the screen where user can login by entering the password.
60+
*/
4461
private void openVault() {
4562
if(!hasPassword()){
4663
// Launch activity to setup password
@@ -57,12 +74,16 @@ private void openVault() {
5774
}
5875
}
5976

77+
/**
78+
* Generate a private key in the vault for encryption and decryption of files
79+
*/
6080
private void generatePrivateKey() {
61-
if(!mVault.hasPrivateKey()){
6281
mVault.generateKey();
63-
}
6482
}
6583

84+
/**
85+
* Import some sample files from assets to the vault
86+
*/
6687
private void copySampleFiles() {
6788
File file = new File(this.getContext().getFilesDir() + "/" + FileManager.sVaultDirectory);
6889
if (!file.exists()) {
@@ -89,18 +110,23 @@ private void copySampleFiles() {
89110
mVault.importFileFromAsset(filename,filepath,targetPath);
90111

91112
}
92-
}catch (FileNotFoundException e){
93-
e.printStackTrace();;
94-
} catch (IOException e) {
95-
e.printStackTrace();
113+
}catch (IOException | NoSuchAlgorithmException | KeyStoreException | UnrecoverableEntryException e){
114+
Log.e(TAG,e.toString());
96115
}
97116
}
98117

118+
/**
119+
* Launch the activity to allow user setup a password
120+
*/
99121
private void setPassword() {
100122
Intent intent = new Intent(this.getContext(), SetPasswordActivity.class);
101123
startActivity(intent);
102124
}
103125

126+
/**
127+
* Check if the user has set a password before
128+
* @return if a password has been set
129+
*/
104130
private boolean hasPassword(){
105131
SecureSharePreference secretShare = SecureSharePreference.getInstance(getContext(), Login.LOGIN_INFO_PREF_FILE);
106132
return secretShare.contains(Login.TAG_PASSWORD);

0 commit comments

Comments
 (0)