Пример #1
0
 private void showNotificationIfNotBoundAndHasPendingRequestsOtherwiseHideNotification() {
   // http://stackoverflow.com/a/13359680/693752
   if (notification == null || isJUnit) {
     return;
   }
   Ln.v("Pending requests : " + currentPendingRequestCount);
   if (isBound || currentPendingRequestCount == 0) {
     Ln.v("Stop foreground");
     stopForeground(true);
   } else {
     Ln.v("Start foreground");
     startForeground(notification);
   }
 }
  @Override
  public String saveDataToCacheAndReturnData(final String data, final Object cacheKey)
      throws CacheSavingException {
    Ln.v("Saving String " + data + " into cacheKey = " + cacheKey);
    try {
      if (isAsyncSaveEnabled) {

        Thread t =
            new Thread() {
              @Override
              public void run() {
                try {
                  FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8);
                } catch (IOException e) {
                  Ln.e(
                      e, "An error occured on saving request " + cacheKey + " data asynchronously");
                } finally {
                  // notify that saving is
                  // finished for test
                  // purpose
                  lock.lock();
                  condition.signal();
                  lock.unlock();
                }
              };
            };
        t.start();
      } else {
        FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8);
      }
    } catch (Exception e) {
      throw new CacheSavingException(e);
    }
    return data;
  }
 @Override
 public String loadDataFromCache(Object cacheKey, long maxTimeInCacheBeforeExpiry)
     throws CacheLoadingException {
   Ln.v("Loading String for cacheKey = " + cacheKey);
   File file = getCacheFile(cacheKey);
   if (file.exists()) {
     long timeInCache = System.currentTimeMillis() - file.lastModified();
     if (maxTimeInCacheBeforeExpiry == 0 || timeInCache <= maxTimeInCacheBeforeExpiry) {
       try {
         return FileUtils.readFileToString(file, CharEncoding.UTF_8);
       } catch (FileNotFoundException e) {
         // Should not occur (we test before if
         // file exists)
         // Do not throw, file is not cached
         Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
         return null;
       } catch (Exception e) {
         throw new CacheLoadingException(e);
       }
     }
   }
   Ln.v("file " + file.getAbsolutePath() + " does not exists");
   return null;
 }
Пример #4
0
 private void stopIfNotBoundAndHasNoPendingRequests() {
   Ln.v("Pending requests : " + currentPendingRequestCount);
   if (currentPendingRequestCount == 0 && !isBound) {
     stopSelf();
   }
 }
Пример #5
0
 public void dumpState() {
   Ln.v(requestProcessor.toString());
 }