Ejemplo n.º 1
0
 /**
  * Images are normally fetched from storage or network only as needed, however if the download
  * must start before the image is drawn this method can be invoked.
  */
 public void fetch() {
   if (fetching || imageData != null) {
     return;
   }
   fetching = true;
   try {
     locked = super.isLocked();
     if (storageFile != null) {
       if (Storage.getInstance().exists(storageFile)) {
         unlock();
         imageData = new byte[Storage.getInstance().entrySize(storageFile)];
         InputStream is = Storage.getInstance().createInputStream(storageFile);
         Util.readFully(is, imageData);
         resetCache();
         fetching = false;
         repaintImage = true;
         return;
       }
       if (adapter != null) {
         Util.downloadUrlToStorageInBackground(
             url, storageFile + IMAGE_SUFFIX, new DownloadCompleted());
       } else {
         Util.downloadUrlToStorageInBackground(url, storageFile, new DownloadCompleted());
       }
     } else {
       if (FileSystemStorage.getInstance().exists(fileSystemFile)) {
         unlock();
         imageData = new byte[(int) FileSystemStorage.getInstance().getLength(fileSystemFile)];
         InputStream is = FileSystemStorage.getInstance().openInputStream(fileSystemFile);
         Util.readFully(is, imageData);
         resetCache();
         fetching = false;
         repaintImage = true;
         return;
       }
       if (adapter != null) {
         Util.downloadUrlToFileSystemInBackground(
             url, fileSystemFile + IMAGE_SUFFIX, new DownloadCompleted());
       } else {
         Util.downloadUrlToFileSystemInBackground(
             url, fileSystemFile + IMAGE_SUFFIX, new DownloadCompleted());
       }
     }
   } catch (IOException ioErr) {
     throw new RuntimeException(ioErr.toString());
   }
 }
Ejemplo n.º 2
0
 public void actionPerformed(ActionEvent evt) {
   if (adapter != null) {
     try {
       byte[] d;
       InputStream is;
       if (storageFile != null) {
         d = new byte[Storage.getInstance().entrySize(storageFile + IMAGE_SUFFIX)];
         is = Storage.getInstance().createInputStream(storageFile + IMAGE_SUFFIX);
       } else {
         d =
             new byte
                 [(int)
                     FileSystemStorage.getInstance().getLength(fileSystemFile + IMAGE_SUFFIX)];
         is = FileSystemStorage.getInstance().openInputStream(fileSystemFile + IMAGE_SUFFIX);
       }
       Util.readFully(is, d);
       EncodedImage img = EncodedImage.create(d);
       EncodedImage adapted;
       if (adapter.isAsyncAdapter()) {
         adapt = img;
         Display.getInstance().invokeAndBlock(this);
         adapted = adaptedIns;
         adaptedIns = null;
         adapt = null;
       } else {
         adapted = adapter.adaptImage(img, placeholder);
       }
       if (storageFile != null) {
         OutputStream o = Storage.getInstance().createOutputStream(storageFile);
         o.write(adapted.getImageData());
         o.close();
         Storage.getInstance().deleteStorageFile(storageFile + IMAGE_SUFFIX);
       } else {
         OutputStream o = FileSystemStorage.getInstance().openOutputStream(fileSystemFile);
         o.write(adapted.getImageData());
         o.close();
         FileSystemStorage.getInstance().delete(fileSystemFile + IMAGE_SUFFIX);
       }
     } catch (IOException ex) {
       ex.printStackTrace();
       return;
     }
   }
   fetching = false;
   // invoke fetch again to load the local files
   fetch();
 }
 /**
  * Handle a request made to retrieve a previously stored access token. On successful retrieval,
  * implementors of this method should chain to onAccessToken() to elevate privileges.
  *
  * @param token
  */
 public boolean onLoadAccessToken() {
   if (accessToken == null) {
     accessToken = (AccessToken) Storage.getInstance().readObject(serviceProvider.getId());
   }
   if (accessToken != null) {
     onAuthenticated();
     return true;
   }
   return false;
 }
 /**
  * Handle a newly received access token, use this to handle one-time operations such as
  * persistence, etc.
  *
  * @param token
  */
 public void onSaveAccessToken(AccessToken token) {
   Storage.getInstance().writeObject(serviceProvider.getId(), token);
 }