Skip to content

Commit 8853adc

Browse files
committed
improve import and export, background loading picture
1 parent ad13827 commit 8853adc

File tree

14 files changed

+193
-41
lines changed

14 files changed

+193
-41
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.katcom.androidFileVault.login.Login;
2222

2323
import java.io.BufferedOutputStream;
24+
import java.io.ByteArrayOutputStream;
2425
import java.io.File;
2526
import java.io.FileDescriptor;
2627
import java.io.FileInputStream;
@@ -117,12 +118,16 @@ public void update(){
117118
if(!vaultFolder.exists()){
118119
vaultFolder.mkdirs();
119120
}
121+
120122
mFiles = new ArrayList<ProtectedFile>();
121123
String[] files = vaultFolder.list();
122124

123125
for(String filename: files){
124126
String path = mContext.getFilesDir() + "/" + sVaultDirectory +"/" + filename;
125127
ProtectedFile file = new ProtectedFile(filename,path,UUID.randomUUID());
128+
129+
int size = mContext.getResources().getDimensionPixelSize(R.dimen.small_preview_image_size);
130+
//file.setPreview(getPreview(file,size,size));
126131
mFiles.add(file);
127132
}
128133
}
@@ -172,7 +177,21 @@ private Bitmap getPicturePreview(ProtectedFile file,int sizeX,int sizeY) {
172177

173178
try {
174179
CipherInputStream in = getDecryptedInputStream(file.getFilepath());
175-
bitmap = BitmapFactory.decodeStream(in);
180+
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
181+
182+
int i=0;
183+
byte[] buffer = new byte[1024];
184+
while (true) {
185+
try {
186+
if (!((i = in.read(buffer)) != -1)) break;
187+
} catch (IOException e) {
188+
e.printStackTrace();
189+
}
190+
out.write(buffer, 0, i);
191+
}
192+
byte[] image = out.toByteArray();
193+
bitmap = Utils.getScaledBitmap(mContext,image,sizeX,sizeY);
194+
176195
//in.close();
177196
return bitmap;
178197
} catch (UnrecoverableEntryException | NoSuchAlgorithmException | KeyStoreException | FileNotFoundException e) {
@@ -201,7 +220,7 @@ public void importFile(String filepath, String targetPath,String filename){
201220
* @param targetPath
202221
* @throws FileNotFoundException
203222
*/
204-
public void importFile(String filename,InputStream in, String targetPath) throws FileNotFoundException {
223+
public void importFile(String filename,InputStream in, String targetPath) throws FileNotFoundException, UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException {
205224
importAndEncrypt(in,targetPath);
206225
addFileEntry(filename,targetPath);
207226
}
@@ -214,9 +233,9 @@ public void importFile(String filename,InputStream in, String targetPath) throws
214233
* @param targetPath
215234
* @throws FileNotFoundException
216235
*/
217-
private void importAndEncrypt(InputStream in, String targetPath) throws FileNotFoundException {
236+
private void importAndEncrypt(InputStream in, String targetPath) throws FileNotFoundException, UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException {
218237
OutputStream out=null;
219-
out = getFileOutputStream(targetPath);
238+
out = getEncryptedOutputStream(targetPath);
220239

221240
writeFile(in,out);
222241
}
@@ -353,7 +372,7 @@ private void saveEncryptionIv(String filePath, byte[] iv) {
353372

354373
String encodeIv = Base64.encodeToString(iv, Base64.DEFAULT);
355374
editor.putString(filePath,encodeIv);
356-
editor.apply();
375+
editor.commit();
357376
}
358377

359378
/**
@@ -382,6 +401,7 @@ public CipherInputStream getDecryptedInputStream(String filepath) throws Unrecov
382401
CipherInputStream cin = null;
383402

384403
FileInputStream in = new FileInputStream(filepath);
404+
385405
SecretKey key =getSecretKey();
386406
byte[] encryptionIv = getEncryptionIv(filepath);
387407
Cipher cipher = getDecryptCipher(key,encryptionIv);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ImageViewerActivity extends SingleFragmentActivity {
99

1010
@Override
1111
protected Fragment createFragment() {
12-
ProtectedFile file= (ProtectedFile) getIntent().getSerializableExtra(EXTRA_FILE);
12+
ProtectedFile file= (ProtectedFile) getIntent().getParcelableExtra(EXTRA_FILE);
1313

1414
return ImageViewerFragment.newInstance(file);
1515
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Intent;
5+
import android.graphics.Bitmap;
56
import android.net.Uri;
67
import android.os.Bundle;
78
import androidx.fragment.app.Fragment;
@@ -34,15 +35,17 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
3435
View view = inflater.inflate(R.layout.fragment_image_viewer, container, false);
3536
setHasOptionsMenu(true);
3637

37-
mFile = (ProtectedFile) getArguments().getSerializable(ARG_FILE);
38+
mFile = (ProtectedFile) getArguments().getParcelable(ARG_FILE);
3839

3940
//String imgPath = getImagePath();
4041
//String imgName = getImageName();
4142
String imgPath = mFile.getFilepath();
4243
String imgName = mFile.getFilename();
4344

4445
mImageView = view.findViewById(R.id.fragment_image_viwer_image_view); // Bind the controller to the image view in the layout
45-
mImageView.setImageBitmap(Utils.getScaledBitmap(getContext(), imgPath, 700, 700));
46+
47+
Bitmap img = getImage(mFile);
48+
mImageView.setImageBitmap(FileManager.get(getContext()).getPreview(mFile,700,700));
4649

4750
// Use the Builder class for convenient dialog construction
4851
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
@@ -52,6 +55,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5255
return view;
5356
}
5457

58+
private Bitmap getImage(ProtectedFile mFile) {
59+
return null;
60+
}
61+
5562
/*/private String getImagePath() {
5663
return (String) getArguments().getSerializable(ARG_FILE_PATH);
5764
}*/
@@ -64,7 +71,7 @@ private String getImageName(){
6471

6572
public static ImageViewerFragment newInstance(ProtectedFile file) {
6673
Bundle args = new Bundle();
67-
args.putSerializable(ARG_FILE, file);
74+
args.putParcelable(ARG_FILE, file);
6875

6976
ImageViewerFragment fragment = new ImageViewerFragment();
7077
fragment.setArguments(args);
@@ -112,14 +119,8 @@ private void export(Uri uri) {
112119
FileManager vault = FileManager.get(getContext());
113120
try {
114121
vault.exportFile(mFile,getContext().getContentResolver().openOutputStream(uri));
115-
} catch (FileNotFoundException e) {
116-
e.printStackTrace();
117-
} catch (NoSuchAlgorithmException e) {
118-
e.printStackTrace();
119-
} catch (KeyStoreException e) {
120-
e.printStackTrace();
121-
} catch (UnrecoverableEntryException e) {
122-
e.printStackTrace();
122+
} catch (FileNotFoundException | NoSuchAlgorithmException | KeyStoreException | UnrecoverableEntryException e) {
123+
Log.e(TAG,e.toString());
123124
}
124125

125126
}

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package com.katcom.androidFileVault;
22

3+
import android.graphics.Bitmap;
4+
import android.os.Parcel;
5+
import android.os.Parcelable;
6+
7+
import java.io.ByteArrayOutputStream;
38
import java.io.Serializable;
49
import java.util.UUID;
510

6-
public class ProtectedFile implements Serializable {
11+
public class ProtectedFile implements Parcelable {
712
private String filename;
813
private String filepath;
914
private String type;
1015
private UUID id;
1116

17+
private Bitmap preview;
1218
public ProtectedFile(String filename,String filepath,UUID id){
1319
this.filename = filename;
1420
this.filepath = filepath;
1521
this.id = id;
1622
}
1723

24+
25+
1826
////////////////////////// Getter and Setter/////////////////////////////////////
1927
public UUID getId() {
2028
return id;
@@ -44,6 +52,45 @@ public void setType(String type) {
4452
this.type = type;
4553
}
4654

55+
public Bitmap getPreview() {
56+
return preview;
57+
}
58+
59+
public void setPreview(Bitmap preview) {
60+
this.preview=preview;
61+
}
62+
63+
64+
@Override
65+
public int describeContents() {
66+
return 0;
67+
}
68+
69+
@Override
70+
public void writeToParcel(Parcel dest, int flags) {
71+
dest.writeString(filename);
72+
dest.writeString(filepath);
73+
dest.writeString(type);
74+
//dest.writeParcelable(preview, flags);
75+
76+
}
77+
78+
protected ProtectedFile(Parcel in) {
79+
filename = in.readString();
80+
filepath = in.readString();
81+
type = in.readString();
82+
//preview = in.readParcelable(Bitmap.class.getClassLoader());
83+
}
4784

85+
public static final Creator<ProtectedFile> CREATOR = new Creator<ProtectedFile>() {
86+
@Override
87+
public ProtectedFile createFromParcel(Parcel in) {
88+
return new ProtectedFile(in);
89+
}
4890

91+
@Override
92+
public ProtectedFile[] newArray(int size) {
93+
return new ProtectedFile[size];
94+
}
95+
};
4996
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919

2020
public class SecureSharePreference implements SharedPreferences {
2121
private static SharedPreferences share;
22-
private static SecureSharePreference secretShare;
22+
//private static SecureSharePreference secretShare;
2323
private SecureSharePreference(Context context,String name){
2424
share = context.getSharedPreferences(name, Context.MODE_PRIVATE);
2525
}
2626

2727
public static SecureSharePreference getInstance(Context context,String name){
28-
if(secretShare == null){
29-
secretShare = new SecureSharePreference(context,name);
30-
return secretShare;
31-
}
28+
return new SecureSharePreference(context,name);
3229

33-
return secretShare;
3430
}
3531

3632
@Override

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,33 @@ public static Bitmap getScaledBitmap(Context context,InputStream in,int sizeX,in
148148
return BitmapFactory.decodeStream(in,null,options);
149149
}
150150

151+
public static Bitmap getScaledBitmap(Context context,byte[] imageArray,int sizeX,int sizeY){
152+
BitmapFactory.Options options = new BitmapFactory.Options();
153+
options.inJustDecodeBounds = true;
154+
BitmapFactory.decodeByteArray(imageArray,0,imageArray.length,options);
155+
156+
float srcWidth = options.outWidth;
157+
float srcHeight = options.outHeight;
158+
159+
float targetWidth = Utils.dip2px(context,sizeX);
160+
float targetHeight = Utils.dip2px(context,sizeY);
161+
162+
// Scale the picture
163+
int inSampleSize = 1;
164+
if(srcHeight > targetHeight || srcHeight > targetHeight){
165+
if(srcWidth > targetWidth){
166+
inSampleSize = Math.round(srcWidth/targetWidth);
167+
}else{
168+
inSampleSize = Math.round(srcHeight/targetHeight);
169+
}
170+
}
171+
172+
options = new BitmapFactory.Options();
173+
options.inSampleSize = inSampleSize;
174+
175+
return BitmapFactory.decodeByteArray(imageArray,0,imageArray.length,options);
176+
177+
}
151178

152179
/**
153180
* Calculate px (pixel) from dp

0 commit comments

Comments
 (0)