コード例 #1
0
  @Override
  public void onCreate() {
    super.onCreate();

    try {
      cacheManager = createCacheManager(getApplication());
    } catch (CacheCreationException e) {
      Ln.e(e);
      stopSelf();
      return;
    }
    if (cacheManager == null) {
      Ln.e(new CacheCreationException("createCacheManager() can't create a null cacheManager"));
      stopSelf();
      return;
    }

    progressReporter = createRequestRequestListenerNotifier();
    spiceServiceListenerNotifier = createSpiceServiceListenerNotifier();

    final ExecutorService executorService = getExecutorService();
    final NetworkStateChecker networkStateChecker = getNetworkStateChecker();

    requestProcessor = createRequestProcessor(executorService, networkStateChecker);
    requestProcessor.setFailOnCacheError(DEFAULT_FAIL_ON_CACHE_ERROR);

    notification = createDefaultNotification();

    Ln.d("SpiceService instance created.");
  }
コード例 #2
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);
   }
 }
コード例 #3
0
  @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;
  }
コード例 #4
0
 private void startForeground(final Notification notification) {
   try {
     final Method setForegroundMethod =
         Service.class.getMethod("startForeground", int.class, Notification.class);
     setForegroundMethod.invoke(this, getNotificationId(), notification);
   } catch (final SecurityException e) {
     Ln.e(e, "Unable to start a service in foreground");
   } catch (final NoSuchMethodException e) {
     Ln.e(e, "Unable to start a service in foreground");
   } catch (final IllegalArgumentException e) {
     Ln.e(e, "Unable to start a service in foreground");
   } catch (final IllegalAccessException e) {
     Ln.e(e, "Unable to start a service in foreground");
   } catch (final InvocationTargetException e) {
     Ln.e(e, "Unable to start a service in foreground");
   }
 }
コード例 #5
0
 @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;
 }
コード例 #6
0
 @Override
 public boolean onCreate() {
   MatcherController controller = new MatcherController();
   for (Class<?> clazz : getExposedClasses()) {
     try {
       if (!clazz.isAnnotationPresent(Contract.class)) {
         throw new Exception(
             "Class " + clazz + " is not annotated with the @Contract annotation.");
       }
       Class<?> contractClazz = ContractHelper.getContractClassForClass(clazz);
       int contentUriPatternMany = ContractHelper.getContentUriPatternMany(contractClazz);
       int contentUriPatternOne = ContractHelper.getContentUriPatternOne(contractClazz);
       controller.add(clazz, SubType.DIRECTORY, "", contentUriPatternMany);
       controller.add(clazz, SubType.ITEM, "#", contentUriPatternOne);
     } catch (Exception e) {
       Ln.e(e);
     }
   }
   setMatcherController(controller);
   return true;
 }
コード例 #7
0
 @Override
 protected T readCacheDataFromFile(File file) throws CacheLoadingException {
   try {
     String resultJson = null;
     synchronized (file.getAbsolutePath().intern()) {
       resultJson = FileUtils.readFileToString(file, CharEncoding.UTF_8);
     }
     if (!StringUtils.isEmpty(resultJson)) {
       T result = deserializeData(resultJson);
       return result;
     }
     throw new CacheLoadingException("Unable to restore cache content : cache file is empty");
   } 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 (CacheLoadingException e) {
     throw e;
   } catch (Exception e) {
     throw new CacheLoadingException(e);
   }
 }
コード例 #8
0
 private void stopIfNotBoundAndHasNoPendingRequests() {
   Ln.v("Pending requests : " + currentPendingRequestCount);
   if (currentPendingRequestCount == 0 && !isBound) {
     stopSelf();
   }
 }
コード例 #9
0
 public void dumpState() {
   Ln.v(requestProcessor.toString());
 }
コード例 #10
0
 @Override
 public void onDestroy() {
   Ln.d("SpiceService instance destroyed.");
   super.onDestroy();
 }
コード例 #11
0
 @Override
 public Contributor loadDataFromNetwork() {
   Ln.d("Call web service ");
   return getService().createAccount(RUC);
 }
コード例 #12
0
 @Override
 public void onCreate() {
   Ln.getConfig().setLoggingLevel(Log.ERROR);
   super.onCreate();
 }