Skip to content

Commit 783a49e

Browse files
committed
Using CacheThreadPool
1 parent 2b55044 commit 783a49e

File tree

4 files changed

+76
-18
lines changed

4 files changed

+76
-18
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ public Bitmap getPreview(ProtectedFile file,int sizeX,int sizeY){
4949
options.inSampleSize = inSampleSize;
5050
in = vault.getDecryptedInputStream(file);
5151
//in.close();
52-
return BitmapFactory.decodeStream(in,null,options);
52+
Bitmap preview = BitmapFactory.decodeStream(in,null,options);
53+
54+
// If loading with options succeed, return the image
55+
if(preview != null) return preview;
56+
57+
// If failed to load the image with options, drop the options and load it again
58+
in = vault.getDecryptedInputStream(file);
59+
return BitmapFactory.decodeStream(in);
60+
5361
} catch (UnrecoverableEntryException | NoSuchAlgorithmException | KeyStoreException | IOException e) {
5462
Log.e(TAG,e.toString());
5563
}

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Intent;
66
import android.database.Cursor;
77
import android.graphics.Bitmap;
8+
import android.graphics.BitmapFactory;
89
import android.net.Uri;
910
import android.os.AsyncTask;
1011
import android.os.Build;
@@ -27,6 +28,7 @@
2728
import android.view.View;
2829
import android.view.ViewGroup;
2930
import android.widget.ImageButton;
31+
import android.widget.ProgressBar;
3032
import android.widget.Toast;
3133

3234
import com.google.android.material.navigation.NavigationView;
@@ -43,7 +45,11 @@
4345
import java.util.Date;
4446
import java.util.List;
4547
import java.util.concurrent.Executor;
48+
import java.util.concurrent.ExecutorService;
4649
import java.util.concurrent.Executors;
50+
import java.util.concurrent.SynchronousQueue;
51+
import java.util.concurrent.ThreadPoolExecutor;
52+
import java.util.concurrent.TimeUnit;
4753

4854
import static android.app.Activity.RESULT_OK;
4955

@@ -65,6 +71,7 @@ public class VaultFragment extends Fragment {
6571
private final String TAG="VaultFragment";
6672
private List<ProtectedFile> mFiles;
6773
private Uri tempPictureUir;
74+
private PreviewManager previewManager;
6875

6976
@Override
7077
public void onCreate(Bundle savedInstanceState) {
@@ -108,8 +115,13 @@ public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
108115
mFiles = mVault.getFiles();
109116

110117

118+
// Create a PreviewManager to load preview of files
119+
previewManager = new PreviewManager(getContext());
120+
111121
// Set the preview mode, by default small preview
112122
mPreviewMode = PreviewMode.PREVIEW_SMALL;
123+
124+
// Start loading preview
113125
loadPreviewData();
114126

115127
updateUI();
@@ -131,6 +143,8 @@ public void onClick(View v) {
131143
}
132144
});
133145

146+
147+
134148
Log.i(TAG,"Create Vault View");
135149
return view;
136150
}
@@ -286,6 +300,36 @@ private void saveTempPictureUri(Uri uri) {
286300
this.tempPictureUir = uri;
287301
}
288302

303+
/**
304+
* This methods loads preview image of each file on the background;
305+
*/
306+
private void loadPreviewData(){
307+
308+
Executor executor = getCacheExecutor();
309+
310+
for(ProtectedFile file:mFiles){
311+
/*Log.v(TAG,"LOADING PREVIEW"+file.getFilename());
312+
Bitmap preview = previewManager.getPreview(file,120,120);
313+
file.setPreview(preview);
314+
if(file.getPreview() != null){
315+
Log.v(TAG,"Done Loading preview on background for : "+file.getFilename());
316+
}else{
317+
Log.v(TAG,"Failed Loading preview on background for : "+file.getFilename());
318+
}**/
319+
new FetchSinglePreviewImage().executeOnExecutor(executor,file);
320+
};
321+
}
322+
323+
/**
324+
* This method returns a Executor that provides 15 threads to do things on the background.
325+
* @return
326+
*/
327+
private ExecutorService getCacheExecutor() {
328+
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
329+
120L, TimeUnit.SECONDS,
330+
new SynchronousQueue<Runnable>());
331+
}
332+
289333
/**
290334
* Create an menu on the vault fragment
291335
* @param menu
@@ -446,12 +490,7 @@ protected void showHelpInfo(){
446490
boolean has = mVault.hasPrivateKey();
447491
}
448492

449-
private void loadPreviewData(){
450-
Executor executor = Executors.newFixedThreadPool(15);
451-
for(ProtectedFile file:mFiles){
452-
new FetchSinglePreviewImage().executeOnExecutor(executor,file);
453-
};
454-
}
493+
455494

456495
private class FetchPreviewImage extends AsyncTask<Integer,Void,Void>{
457496

@@ -471,19 +510,25 @@ protected void onPostExecute(Void aVoid) {
471510
}
472511
}
473512

474-
private class FetchSinglePreviewImage extends AsyncTask<ProtectedFile,Void,Void>{
513+
private class FetchSinglePreviewImage extends AsyncTask<ProtectedFile,Void,ProtectedFile>{
475514

476515
@Override
477-
protected Void doInBackground(ProtectedFile... protectedFiles) {
516+
protected ProtectedFile doInBackground(ProtectedFile... protectedFiles) {
478517
ProtectedFile file = protectedFiles[0];
479518
//Bitmap preview = mVault.getPreview(file,120,120);
480-
Bitmap preview = new PreviewManager(getContext()).getPreview(file,120,120);
481-
file.setPreview(preview);
482-
return null;
519+
Log.v(TAG,"Loading preview on background for : "+file.getFilename());
520+
Bitmap preview = previewManager.getPreview(file,120,120);
521+
522+
return file;
483523
}
484524

485525
@Override
486-
protected void onPostExecute(Void aVoid) {
526+
protected void onPostExecute(ProtectedFile file) {
527+
if(file.getPreview() != null){
528+
Log.v(TAG,"Done Loading preview on background for : "+file.getFilename());
529+
}else{
530+
Log.v(TAG,"Failed Loading preview on background for : "+file.getFilename());
531+
}
487532
updateUI();
488533
}
489534
}

app/src/main/java/com/katcom/androidFileVault/fileRecyclerView/previewHolders/SmallPreviewHolder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.katcom.androidFileVault.fileRecyclerView.previewHolders;
22

33
import android.content.Context;
4+
import android.graphics.BitmapFactory;
45
import android.view.View;
56
import android.widget.ImageView;
67
import android.widget.TextView;
@@ -27,7 +28,10 @@ public SmallPreviewHolder(@NonNull View itemView) {
2728
public void bindViewData(final ProtectedFile file, final Context context) {
2829
mFileTextView.setText(file.getFilename()); // Bind the file name to the user interface
2930
//mImageView.setImageBitmap(FileManager.get(context).getPreview(file,60,60)); // Bind the preview image to the user interface
30-
if(file.getPreview() != null) mImageView.setImageBitmap(file.getPreview());
31+
if(file.getPreview() != null){
32+
mImageView.setImageBitmap(file.getPreview());
33+
}
34+
3135
mImageView.setOnClickListener(new View.OnClickListener() {
3236
@Override
3337
public void onClick(View v) {

app/src/main/res/layout/item_file_preview_filename.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
android:src="@drawable/ic_stat_name"/>
1414
<TextView
1515
android:id="@+id/file_preview_filename_name_text_view"
16-
android:layout_width="wrap_content"
17-
android:layout_height="30dp"
18-
android:textSize="25dp"
16+
android:layout_width="match_parent"
17+
android:layout_height="35dp"
18+
android:textSize="15dp"
1919
android:ellipsize="middle"
20-
android:text="TextView"
20+
android:singleLine="true"
21+
android:text="ExampleFile.fileExampleFile.fileExampleFile.fileExampleFile.fileExampleFile.file"
2122
/>
2223
</LinearLayout>

0 commit comments

Comments
 (0)