Skip to content

Commit df147aa

Browse files
committed
add preview image function, add comments
1 parent 848d89b commit df147aa

File tree

13 files changed

+275
-27
lines changed

13 files changed

+275
-27
lines changed

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package com.katcom.androidFileVault;
22

33
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.util.Log;
47

8+
import java.io.BufferedInputStream;
59
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.FileNotFoundException;
12+
import java.io.IOException;
13+
import java.io.InputStream;
614
import java.util.ArrayList;
715
import java.util.List;
816
import java.util.UUID;
917

1018
public class FileVault {
11-
public static String sVaultDirectory = "FileVaultOne";
12-
private static FileVault sVault;
13-
private List<ProtectedFile> mFiles;
19+
public static String TAG ="FileVault"; // For debug
20+
public static String sVaultDirectory = "FileVaultOne"; // The directory of the vault in the private storage of this app
21+
private static FileVault sVault; // This class is a singleton, only one instance allowed
22+
private List<ProtectedFile> mFiles; // Entries of all files
23+
private Context mContext; // Android context, used to get to the private storage
24+
1425
private FileVault(Context context){
15-
// Singleton
26+
mContext= context;
1627
File directory = context.getFilesDir();
1728
File vaultFolder = new File(directory, sVaultDirectory);
1829

@@ -21,7 +32,8 @@ private FileVault(Context context){
2132
mFiles = new ArrayList<ProtectedFile>();
2233

2334
for(String filename: files){
24-
ProtectedFile file = new ProtectedFile(filename,null,UUID.randomUUID());
35+
String path = mContext.getFilesDir() + "/" + sVaultDirectory +"/" + filename;
36+
ProtectedFile file = new ProtectedFile(filename,path,UUID.randomUUID());
2537
mFiles.add(file);
2638
}
2739
}
@@ -48,4 +60,51 @@ public ProtectedFile getFile(UUID id){
4860
public String getVaultDirectory(){
4961
return sVaultDirectory;
5062
}
63+
64+
/**
65+
* Get the preview picture of the given file based on its type
66+
* @param file
67+
* @param sizeX
68+
* @param sizeY
69+
* @return
70+
*/
71+
public Bitmap getPreview(ProtectedFile file,int sizeX,int sizeY){
72+
// Currently all the sample files are pictures, we just need to return the preview of pictures
73+
return getPicturePreview(file,sizeX,sizeY);
74+
}
75+
76+
private Bitmap getPicturePreview(ProtectedFile file,int sizeX,int sizeY) {
77+
BitmapFactory.Options options = new BitmapFactory.Options();
78+
options.inJustDecodeBounds = true;
79+
BitmapFactory.decodeFile(file.getFilepath(),options);
80+
81+
float srcWidth = options.outWidth;
82+
float srcHeight = options.outHeight;
83+
84+
float targetWidth = Utils.dip2px(mContext,sizeX);
85+
float targetHeight = Utils.dip2px(mContext,sizeY);
86+
87+
// Scale the picture
88+
int inSampleSize = 1;
89+
if(srcHeight > targetHeight || srcHeight > targetHeight){
90+
if(srcWidth > targetWidth){
91+
inSampleSize = Math.round(srcWidth/targetWidth);
92+
}else{
93+
inSampleSize = Math.round(srcHeight/targetHeight);
94+
}
95+
}
96+
97+
options = new BitmapFactory.Options();
98+
options.inSampleSize = inSampleSize;
99+
100+
return BitmapFactory.decodeFile(file.getFilepath(),options);
101+
}
102+
103+
public void importFile(String filepath){
104+
// TODO
105+
}
106+
public void exportFile(String filename, String sourcePath,String targetPath){
107+
// TODO
108+
}
109+
51110
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
* This class describe different modes of preview. All modes are pre-defined
8+
* It shouldn't be instantiated.
9+
* The static variables describe the supported modes.
10+
* Other class can get the list of all supported mode by calling getModeList() method.
11+
*/
612
public class PreviewMode {
713
final public static String FILE_DETAIL = "File Detail";
814
final public static String PREVIEW_SMALL = "Preview Small";
915
final public static String PREVIEW_MEDIUM = "Preview Medium";
10-
final public static String PREVIEW_BIG = "Preview Big";
16+
//final public static String PREVIEW_BIG = "Preview Big";
1117
private static List<String> modes;
1218

1319
private PreviewMode(){}
@@ -16,11 +22,21 @@ private PreviewMode(){}
1622
modes = new ArrayList<>();
1723
modes.add(FILE_DETAIL);
1824
modes.add(PREVIEW_SMALL);
25+
modes.add(PREVIEW_MEDIUM);
1926
}
2027

28+
/**
29+
*
30+
* @return The list of all modes.
31+
*/
2132
public static List<String> getModeList(){
2233
return modes;
2334
}
35+
36+
/**
37+
*
38+
* @return The number of the supported modes
39+
*/
2440
public static int getModeCount(){
2541
return modes.size();
2642
}

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
public class ProtectedFile {
66
private String filename;
77
private String filepath;
8+
private String type;
9+
private UUID id;
10+
11+
public ProtectedFile(String filename,String filepath,UUID id){
12+
this.filename = filename;
13+
this.filepath = filepath;
14+
this.id = id;
15+
}
16+
17+
public boolean isPicture(){
18+
//TODO
19+
return false;
20+
}
821

22+
////////////////////////// Getter and Setter/////////////////////////////////////
923
public UUID getId() {
1024
return id;
1125
}
1226

13-
private UUID id;
1427
public String getFilename() {
1528
return filename;
1629
}
@@ -27,12 +40,14 @@ public void setFilepath(String filepath) {
2740
this.filepath = filepath;
2841
}
2942

30-
private String type;
43+
public String getType() {
44+
return type;
45+
}
3146

32-
public ProtectedFile(String filename,String filepath,UUID id){
33-
this.filename = filename;
34-
this.filepath = filepath;
35-
this.id = id;
47+
public void setType(String type) {
48+
this.type = type;
3649
}
3750

51+
52+
3853
}

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

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

33
import android.content.Context;
44
import android.content.res.AssetManager;
5+
import android.graphics.Bitmap;
56
import android.util.Log;
67

78
import java.io.BufferedInputStream;
@@ -81,4 +82,25 @@ public static void copyFilesFromAsset(Context context,String source,String targe
8182
}
8283
}
8384
}
85+
86+
/**
87+
* Calculate px (pixel) from dp
88+
* @param context
89+
* @param dpValue
90+
* @return
91+
*/
92+
public static int dip2px(Context context, float dpValue) {
93+
final float scale = context.getResources().getDisplayMetrics().density;
94+
return (int) (dpValue * scale + 0.5f);
95+
}
96+
97+
/**
98+
* Calculate dp from px (pixel)
99+
*/
100+
public static int px2dip(Context context, float pxValue) {
101+
final float scale = context.getResources().getDisplayMetrics().density;
102+
return (int) (pxValue / scale + 0.5f);
103+
}
104+
105+
84106
}

0 commit comments

Comments
 (0)