33import android .content .Intent ;
44import android .content .SharedPreferences ;
55import android .content .SharedPreferences .Editor ;
6+ import android .content .res .AssetManager ;
67import android .os .Bundle ;
78import androidx .fragment .app .Fragment ;
89import android .view .LayoutInflater ;
1516import com .katcom .androidFileVault .login .LoginActivity ;
1617import com .katcom .androidFileVault .login .SetPasswordActivity ;
1718
19+ import java .io .File ;
20+ import java .io .FileDescriptor ;
21+ import java .io .FileNotFoundException ;
22+ import java .io .IOException ;
23+
1824public class EntryFragment extends Fragment {
1925 private Button mVaultOpenButton ;
20-
26+ private FileManager mVault ;
2127 @ Override
2228 public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ){
2329 View view = inflater .inflate (R .layout .fragment_entry ,container ,false );
@@ -31,18 +37,65 @@ public void onClick(View v) {
3137 }
3238 });
3339
40+ mVault = FileManager .get (getContext ());
3441 return view ;
3542 }
3643
3744 private void openVault () {
3845 if (!hasPassword ()){
46+ // Launch activity to setup password
3947 setPassword ();
48+
49+ // Generate private key for encryption before copying sample files
50+ generatePrivateKey ();
51+
52+ // Put some sample files in the vault
53+ copySampleFiles ();
4054 }else {
4155 Intent i = new Intent (this .getContext (), LoginActivity .class );
4256 startActivity (i );
4357 }
4458 }
4559
60+ private void generatePrivateKey () {
61+ if (!mVault .hasPrivateKey ()){
62+ mVault .generateKey ();
63+ }
64+ }
65+
66+ private void copySampleFiles () {
67+ File file = new File (this .getContext ().getFilesDir () + "/" + FileManager .sVaultDirectory );
68+ if (!file .exists ()) {
69+ file .mkdir ();
70+ }
71+
72+ String source = "sample_files" ;
73+ String vaultPath = getActivity ().getFilesDir () + "/" + FileManager .sVaultDirectory ;
74+ AssetManager assets = getActivity ().getAssets ();
75+ try {
76+ String [] files = assets .list (source );
77+ for (String filename :files ){
78+ String filepath = source + "/" + filename ;
79+ String targetPath = vaultPath + "/" + filename ;
80+ File outFile = new File (targetPath );
81+ if (outFile .exists ()){
82+ continue ;
83+ }
84+
85+ //FileDescriptor in = assets.openFd(filepath).getFileDescriptor().;
86+
87+ //vault.importFile(filename,in,targetPath);
88+ //assets.openFd(filepath).close();
89+ mVault .importFileFromAsset (filename ,filepath ,targetPath );
90+
91+ }
92+ }catch (FileNotFoundException e ){
93+ e .printStackTrace ();;
94+ } catch (IOException e ) {
95+ e .printStackTrace ();
96+ }
97+ }
98+
4699 private void setPassword () {
47100 Intent intent = new Intent (this .getContext (), SetPasswordActivity .class );
48101 startActivity (intent );
0 commit comments