Skip to content

Commit 8712f2b

Browse files
authored
Merge pull request #79 from git03-Nguyen/dev
Dev
2 parents 8434283 + abe4c77 commit 8712f2b

File tree

5 files changed

+138
-14
lines changed

5 files changed

+138
-14
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ dependencies {
3737
implementation("com.google.android.material:material:1.10.0")
3838
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
3939
implementation("com.android.car.ui:car-ui-lib:2.5.1")
40+
implementation("androidx.security:security-crypto:1.1.0-alpha05")
41+
4042
testImplementation("junit:junit:4.13.2")
4143
androidTestImplementation("androidx.test.ext:junit:1.1.5")
4244
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

app/src/main/java/edu/team08/infinitegallery/singlephoto/ViewPagerAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import java.util.Objects;
2626

2727
import edu.team08.infinitegallery.R;
28+
import edu.team08.infinitegallery.trashbin.SingleTrashActivity;
29+
import edu.team08.infinitegallery.trashbin.TrashBinManager;
2830

2931
public class ViewPagerAdapter extends PagerAdapter {
3032
static final int MAX_CACHE_SIZE = 16;
31-
Context context;
33+
static Context context;
3234
File[] photoFiles;
3335
LayoutInflater inflater;
3436
PhotoView zoomViewImage;
@@ -42,7 +44,11 @@ public ViewPagerAdapter(Context context, File[] photoFiles){
4244

4345
static private Drawable getDrawable(String key){
4446
if(!cache.containsKey(key)){
45-
cache.put(key, Drawable.createFromPath(key));
47+
if (context instanceof SingleTrashActivity) {
48+
cache.put(key, new BitmapDrawable(context.getResources(), new TrashBinManager(context).decryptPhoto(new File(key))));
49+
} else {
50+
cache.put(key, Drawable.createFromPath(key));
51+
}
4652
}
4753

4854
if(cache.size() >= MAX_CACHE_SIZE){

app/src/main/java/edu/team08/infinitegallery/trashbin/TrashAdapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void onBindViewHolder(@NonNull TrashAdapter.ViewHolder holder, @SuppressL
7878
// Set item to the ImageView using Glide library
7979
// holder.imageItem.setImageDrawable(Drawable.createFromPath(picturePath));
8080
Glide.with(context)
81-
.load(trash)
81+
.load(trashBinManager.decryptPhoto(trash))
8282
.placeholder(R.drawable.img_image_placeholder)
8383
.into(holder.imageItem);
8484
holder.imageItem.setOnClickListener(new View.OnClickListener() {
@@ -121,11 +121,6 @@ public void onClick(View v) {
121121
}
122122
}
123123

124-
private void addRemainingTime() {
125-
RelativeLayout customLayout = new RelativeLayout(context);
126-
127-
}
128-
129124
@Override
130125
public int getItemCount() {
131126
return trashPhotos.size();

app/src/main/java/edu/team08/infinitegallery/trashbin/TrashBinActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class TrashBinActivity extends AppCompatActivity {
2525
private int spanCount = 4;
2626
File[] trashFiles;
27+
File[] trashPhotos;
2728
private TrashBinManager trashBinManager;
2829
TrashAdapter trashAdapter;
2930
RecyclerView photosRecView;
@@ -38,15 +39,15 @@ protected void onCreate(Bundle savedInstanceState) {
3839

3940
trashBinManager = new TrashBinManager(this);
4041
trashFiles = null;
42+
trashPhotos = null;
4143
photosRecView = findViewById(R.id.recViewTrash);
4244
viewSwitcher = findViewById(R.id.viewSwitcher);
4345
}
4446

4547
@Override
4648
public void onResume() {
4749
super.onResume();
48-
trashBinManager.checkAndCleanTrashBin();
49-
trashFiles = trashBinManager.getTrashFiles();
50+
trashFiles = trashBinManager.checkAndCleanTrashBin().toArray(new File[0]);
5051
if (trashFiles.length > 0) {
5152
if (photosRecView.getId() == viewSwitcher.getNextView().getId()) {
5253
viewSwitcher.showNext();

app/src/main/java/edu/team08/infinitegallery/trashbin/TrashBinManager.java

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55
import android.content.Context;
66
import android.database.Cursor;
77
import android.database.sqlite.SQLiteDatabase;
8+
import android.graphics.Bitmap;
9+
import android.graphics.BitmapFactory;
810

11+
import androidx.security.crypto.EncryptedFile;
12+
import androidx.security.crypto.MasterKey;
13+
14+
import java.io.ByteArrayOutputStream;
915
import java.io.File;
1016
import java.io.FileInputStream;
1117
import java.io.FileOutputStream;
1218
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
1321
import java.nio.channels.FileChannel;
22+
import java.security.GeneralSecurityException;
1423
import java.util.ArrayList;
1524
import java.util.List;
1625
import java.util.UUID;
@@ -65,6 +74,70 @@ public void moveFile(File src, File dst) throws IOException {
6574
src.delete();
6675
}
6776

77+
private void encrypt(File src, File dst) throws GeneralSecurityException, IOException {
78+
if (!dst.getParentFile().exists()) {
79+
dst.getParentFile().mkdirs();
80+
}
81+
82+
if (dst.exists()) {
83+
// If the destination file already exists, rename it with a postfix
84+
dst = getUniqueDestination(dst);
85+
}
86+
87+
MasterKey mainKey = new MasterKey.Builder(context)
88+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
89+
.build();
90+
91+
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
92+
dst,
93+
mainKey,
94+
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
95+
).build();
96+
97+
InputStream inputStream = new FileInputStream(src);
98+
OutputStream outputStream = encryptedFile.openFileOutput();
99+
// Write data from source file to encrypted output stream
100+
byte[] buffer = new byte[4096];
101+
int bytesRead;
102+
while ((bytesRead = inputStream.read(buffer)) != -1) {
103+
outputStream.write(buffer, 0, bytesRead);
104+
}
105+
106+
inputStream.close();
107+
outputStream.flush();
108+
outputStream.close();
109+
110+
}
111+
112+
private void decrypt(final File src, File dst) throws GeneralSecurityException, IOException {
113+
dst = getUniqueDestination(dst);
114+
115+
MasterKey mainKey = new MasterKey.Builder(context)
116+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
117+
.build();
118+
119+
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
120+
src,
121+
mainKey,
122+
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
123+
).build();
124+
125+
InputStream inputStream = encryptedFile.openFileInput();
126+
OutputStream outputStream = new FileOutputStream(dst);
127+
128+
// Write data from encrypted source file to output stream
129+
byte[] buffer = new byte[4096];
130+
int bytesRead;
131+
while ((bytesRead = inputStream.read(buffer)) != -1) {
132+
outputStream.write(buffer, 0, bytesRead);
133+
}
134+
135+
inputStream.close();
136+
outputStream.flush();
137+
outputStream.close();
138+
139+
}
140+
68141
public void copyFile(File src, File dst) throws IOException {
69142
if (!dst.getParentFile().exists()) {
70143
dst.getParentFile().mkdirs();
@@ -146,7 +219,14 @@ public void moveToTrash(File photo) throws IOException {
146219
values.put("DELETE_DATE", System.currentTimeMillis());
147220
db.insert(TRASH_BIN_TABLE_NAME, null, values);
148221

149-
moveFile(photo, trash);
222+
//moveFile(photo, trash);
223+
try {
224+
encrypt(photo, trash);
225+
photo.delete();
226+
} catch (GeneralSecurityException e) {
227+
throw new RuntimeException(e);
228+
}
229+
150230
}
151231

152232
public void permanentDelete(File trash) {
@@ -185,7 +265,12 @@ public String restorePhoto(File trash) throws IOException {
185265

186266
if (originalPath != null) {
187267
File original = new File(originalPath);
188-
moveFile(trash, original);
268+
try {
269+
decrypt(trash, original);
270+
trash.delete();
271+
} catch (GeneralSecurityException e) {
272+
throw new RuntimeException(e);
273+
}
189274
} else {}
190275

191276
return originalPath;
@@ -238,17 +323,52 @@ public int[] getDaysRemain(File[] trashFiles) {
238323
}
239324

240325

241-
public void checkAndCleanTrashBin() {
326+
public List<File> checkAndCleanTrashBin() {
242327
File[] trashFiles = getTrashFiles();
243328

244329
int[] daysRemain = getDaysRemain(trashFiles);
330+
ArrayList returnList = new ArrayList<File>();
245331

246332
for (int i = 0; i < daysRemain.length; i++) {
247-
if (daysRemain[i] < 0) {
333+
if (daysRemain[i] < 0 || !trashFiles[i].exists()) {
248334
permanentDelete(trashFiles[i]);
249335
trashFiles[i] = null;
336+
} else {
337+
returnList.add(trashFiles[i]);
250338
}
251339
}
340+
return returnList;
341+
342+
}
343+
344+
public Bitmap decryptPhoto(File src) {
345+
346+
Bitmap myBitmap = null;
347+
MasterKey mainKey = null;
348+
try {
349+
mainKey = new MasterKey.Builder(context)
350+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
351+
.build();
352+
353+
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
354+
src,
355+
mainKey,
356+
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
357+
).build();
358+
359+
InputStream inputStream = encryptedFile.openFileInput();
360+
// Convert InputStream to Bitmap
361+
myBitmap = BitmapFactory.decodeStream(inputStream);
362+
363+
} catch (GeneralSecurityException e) {
364+
throw new RuntimeException(e);
365+
} catch (IOException e) {
366+
throw new RuntimeException(e);
367+
}
368+
252369

370+
371+
return myBitmap;
253372
}
373+
254374
}

0 commit comments

Comments
 (0)