/* (non-Javadoc)
  * @see android.app.Activity#onRestart()
  */
 @Override
 protected void onRestart() {
   Log.d(LOG_PREFIX, "onRestart called");
   // restarting file download
   if (fileDownloader != null && fileDownloader.hasRemainingDownloads()) {
     Log.d(LOG_PREFIX, "Continuing downloading of photos");
     fileDownloader.execute();
   }
   super.onRestart();
 }
  /* (non-Javadoc)
   * @see android.app.Activity#onStop()
   */
  @Override
  protected void onStop() {
    Log.d(LOG_PREFIX, "onStop called");
    if (slideshowTimerTask != null) {
      // interupt thread if necessary... we need to kill it
      slideshowTimerTask.cancel(true);
    }
    if (fileDownloader != null && fileDownloader.hasRemainingDownloads()) {
      Log.d(LOG_PREFIX, "Stopping downloading of photos");
      fileDownloader.stop();
    }

    super.onStop();
  }
  /**
   * Called when the list of all photos have been downloaded from backend
   *
   * @param slideShowPhotos Photos in the feed, may not exist in cache yet
   */
  private void actionOnPhotoUrlsDownloaded(List<SlideshowPhoto> slideShowPhotos) {
    Log.i(LOG_PREFIX, "Photo gallery definition downloaded, now looking through the results");

    // Let's add the existing one to the adapter immediately, and send the other to the
    // FileDownloader
    ArrayList<DownloadableObject> notCachedPhotos = new ArrayList<DownloadableObject>(100);
    ArrayList<SlideshowPhoto> cachedPhotos = new ArrayList<SlideshowPhoto>(200);

    for (Iterator<SlideshowPhoto> iterator = slideShowPhotos.iterator(); iterator.hasNext(); ) {
      SlideshowPhoto slideshowPhoto = iterator.next();
      if (slideshowPhoto.isCacheExisting(rootFileDirectory)) {
        cachedPhotos.add(slideshowPhoto);
      } else {
        notCachedPhotos.add(slideshowPhoto);
      }
    }

    if (cachedPhotos.size() > 0) {
      // lets randomize all the cached photos
      long seed = System.nanoTime();
      Collections.shuffle(cachedPhotos, new Random(seed));

      addSlideshowPhoto(cachedPhotos);
    }

    if (notCachedPhotos.size() > 0) {
      // Rules for download
      // 1. Never download on roaming
      if (AndroidUtils.isConnectedRoaming(getApplicationContext())) {
        notifyUser(getString(R.string.msg_connected_roaming));
        return;
      }

      boolean connectOn3G = SlideshowPreferences.doDownloadOn3G(getApplicationContext());
      boolean isConnectedToWifi = AndroidUtils.isConnectedToWifi(getApplicationContext());
      boolean isConnectedToWired = AndroidUtils.isConnectedToWired(getApplicationContext());
      // 2. Do not download if not connected to Wifi and user has not changed connect to Wifi
      // setting
      if (isConnectedToWifi == false && isConnectedToWired == false && connectOn3G == false) {
        if (AndroidUtils.isGoogleTV(getApplicationContext())) {
          String msg =
              "On GoogleTV, but not connected to wifi or wired. Ignoring this. WifiCon="
                  + isConnectedToWifi
                  + " WiredCon="
                  + isConnectedToWired;
          Log.w(LOG_PREFIX, msg);
          isConnectedToWifi = true;
        } else {
          notifyUser(getString(R.string.msg_connected_mobile));
        }
      }

      // 3. Connect if on wifi or if not connected to wifi and wifi setting is changed
      if ((isConnectedToWifi == true || isConnectedToWired == true) || connectOn3G == true) {
        Log.i(
            LOG_PREFIX,
            "Downloading photos. ConnectedToWifi="
                + isConnectedToWifi
                + " ConnectOn3G="
                + connectOn3G);

        // lets randomize all the non-cached photos
        long seed = System.nanoTime();
        Collections.shuffle(notCachedPhotos, new Random(seed));
        fileDownloader =
            new FileDownloader(this.getBaseContext(), this, rootFileDirectory, notCachedPhotos);
        fileDownloader.execute();
      }

    } else {
      Log.i(LOG_PREFIX, "No new photos to download");
    }
  }