55import android .content .Intent ;
66import android .database .Cursor ;
77import android .graphics .Bitmap ;
8+ import android .graphics .BitmapFactory ;
89import android .net .Uri ;
910import android .os .AsyncTask ;
1011import android .os .Build ;
2728import android .view .View ;
2829import android .view .ViewGroup ;
2930import android .widget .ImageButton ;
31+ import android .widget .ProgressBar ;
3032import android .widget .Toast ;
3133
3234import com .google .android .material .navigation .NavigationView ;
4345import java .util .Date ;
4446import java .util .List ;
4547import java .util .concurrent .Executor ;
48+ import java .util .concurrent .ExecutorService ;
4649import java .util .concurrent .Executors ;
50+ import java .util .concurrent .SynchronousQueue ;
51+ import java .util .concurrent .ThreadPoolExecutor ;
52+ import java .util .concurrent .TimeUnit ;
4753
4854import 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 }
0 commit comments