示例#1
0
    public void run() {
      final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
      final boolean isNoReleasedEpisodes = DisplaySettings.isNoReleasedEpisodes(mContext);
      final boolean isNoSpecials = DisplaySettings.isHidingSpecials(mContext);

      if (mShowTvdbId > 0) {
        // update single show
        DBUtils.updateLatestEpisode(
            mContext, mShowTvdbId, isNoReleasedEpisodes, isNoSpecials, prefs);
      } else {
        // update all shows
        final Cursor shows =
            mContext
                .getContentResolver()
                .query(Shows.CONTENT_URI, new String[] {Shows._ID}, null, null, null);
        if (shows != null) {
          while (shows.moveToNext()) {
            int showTvdbId = shows.getInt(0);
            DBUtils.updateLatestEpisode(
                mContext, showTvdbId, isNoReleasedEpisodes, isNoSpecials, prefs);
          }
          shows.close();
        }
      }

      // Show adapter gets notified by ContentProvider
      // Lists adapter needs to be notified manually
      mContext.getContentResolver().notifyChange(ListItems.CONTENT_WITH_DETAILS_URI, null);
    }
示例#2
0
  /**
   * Returns the episode number formatted according to the users preference (e.g. '1x01', 'S01E01',
   * ...).
   */
  public static String getEpisodeNumber(Context context, int season, int episode) {
    String format = DisplaySettings.getNumberFormat(context);
    String result = String.valueOf(season);
    if (DisplaySettings.NUMBERFORMAT_DEFAULT.equals(format)) {
      // 1x01 format
      result += "x";
    } else {
      // S01E01 format
      // make season number always two chars long
      if (season < 10) {
        result = "0" + result;
      }
      if (DisplaySettings.NUMBERFORMAT_ENGLISHLOWER.equals(format)) {
        result = "s" + result + "e";
      } else {
        result = "S" + result + "E";
      }
    }

    if (episode != -1) {
      // make episode number always two chars long
      if (episode < 10) {
        result += "0";
      }

      result += episode;
    }
    return result;
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.lists_menu, menu);

    menu.findItem(R.id.menu_action_lists_sort_ignore_articles)
        .setChecked(DisplaySettings.isSortOrderIgnoringArticles(this));

    return super.onCreateOptionsMenu(menu);
  }
  private void toggleSortIgnoreArticles() {
    PreferenceManager.getDefaultSharedPreferences(this)
        .edit()
        .putBoolean(
            DisplaySettings.KEY_SORT_IGNORE_ARTICLE,
            !DisplaySettings.isSortOrderIgnoringArticles(this))
        .apply();

    // refresh icon state
    supportInvalidateOptionsMenu();

    // post event, so all active list fragments can react
    EventBus.getDefault().post(new ListsDistillationSettings.ListsSortOrderChangedEvent());
  }
 private void getPreferences() {
   mSorting = DisplaySettings.getSeasonSortOrder(getActivity());
 }
  @TargetApi(android.os.Build.VERSION_CODES.KITKAT)
  @Override
  protected void onHandleIntent(Intent intent) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    /*
     * Handle a possible delete intent.
     */
    if (handleDeleteIntent(this, intent)) {
      return;
    }

    /*
     * Unschedule notification service wake-ups for disabled notifications
     * and non-supporters.
     */
    if (!NotificationSettings.isNotificationsEnabled(this) || !Utils.hasAccessToX(this)) {
      // cancel any pending alarm
      AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      Intent i = new Intent(this, OnAlarmReceiver.class);
      PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
      am.cancel(pi);

      resetLastEpisodeAirtime(prefs);

      return;
    }

    long wakeUpTime = 0;

    /*
     * Get pool of episodes which air from 12 hours ago until eternity which
     * match the users settings.
     */
    StringBuilder selection = new StringBuilder(SELECTION);
    final long fakeNow = Utils.getFakeCurrentTime(prefs);
    boolean isFavsOnly = NotificationSettings.isNotifyAboutFavoritesOnly(this);
    if (isFavsOnly) {
      selection.append(Shows.SELECTION_FAVORITES);
    }
    boolean isNoSpecials = DisplaySettings.isHidingSpecials(this);
    if (isNoSpecials) {
      selection.append(Episodes.SELECTION_NOSPECIALS);
    }
    final Cursor upcomingEpisodes =
        getContentResolver()
            .query(
                Episodes.CONTENT_URI_WITHSHOW,
                PROJECTION,
                selection.toString(),
                new String[] {String.valueOf(fakeNow - 12 * DateUtils.HOUR_IN_MILLIS)},
                SORTING);

    if (upcomingEpisodes != null) {
      int notificationThreshold = NotificationSettings.getLatestToIncludeTreshold(this);
      if (DEBUG) {
        // a week, for debugging (use only one show to get single
        // episode notifications)
        notificationThreshold = 10080;
        // notify again for same episodes
        resetLastEpisodeAirtime(prefs);
      }
      final long latestTimeToInclude = fakeNow + DateUtils.MINUTE_IN_MILLIS * notificationThreshold;
      final long nextTimePlanned = NotificationSettings.getNextToNotifyAbout(this);
      final long nextWakeUpPlanned =
          Utils.convertToFakeTime(nextTimePlanned, prefs, false)
              - DateUtils.MINUTE_IN_MILLIS * notificationThreshold;

      /*
       * Set to -1 as on first run nextTimePlanned will be 0. This assures
       * we still see notifications of upcoming episodes then.
       */
      int newEpisodesAvailable = -1;

      // Check if we did wake up earlier than planned
      if (System.currentTimeMillis() < nextWakeUpPlanned) {
        newEpisodesAvailable = 0;
        long latestTimeNotified = NotificationSettings.getLastNotified(this);

        // Check if there are any earlier episodes to notify about
        while (upcomingEpisodes.moveToNext()) {
          final long airtime = upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS);
          if (airtime < nextTimePlanned) {
            if (airtime > latestTimeNotified) {
              /**
               * This will not get new episodes which would have aired the same time as the last one
               * we notified about. Sad, but the best we can do right now.
               */
              newEpisodesAvailable = 1;
              break;
            }
          } else {
            break;
          }
        }
      }

      if (newEpisodesAvailable == 0) {
        // Go to sleep, wake up as planned
        wakeUpTime = nextWakeUpPlanned;
      } else {
        // Get episodes which are within the notification threshold
        // (user set) and not yet cleared
        int count = 0;
        final List<Integer> notifyPositions = Lists.newArrayList();
        final long latestTimeCleared = NotificationSettings.getLastCleared(this);

        upcomingEpisodes.moveToPosition(-1);
        while (upcomingEpisodes.moveToNext()) {
          final long airtime = upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS);
          if (airtime <= latestTimeToInclude) {
            count++;
            /*
             * Only add those after the last one the user cleared.
             * At most those of the last 24 hours (see query above).
             */
            if (airtime > latestTimeCleared) {
              notifyPositions.add(count);
            }
          } else {
            // Too far into the future, stop!
            break;
          }
        }

        // Notify if we found any episodes
        if (notifyPositions.size() > 0) {
          // store latest air time of all episodes we notified about
          upcomingEpisodes.moveToPosition(notifyPositions.get(notifyPositions.size() - 1));
          long latestAirtime = upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS);
          if (!AndroidUtils.isHoneycombOrHigher()) {
            /*
             * Everything below HC does not have delete intents, so
             * we just never notify about the same episode twice.
             */
            prefs.edit().putLong(NotificationSettings.KEY_LAST_CLEARED, latestAirtime).commit();
          }
          prefs.edit().putLong(NotificationSettings.KEY_LAST_NOTIFIED, latestAirtime).commit();

          onNotify(prefs, upcomingEpisodes, count, latestAirtime);
        }

        /*
         * Plan next episode to notify about, calc wake-up alarm as
         * early as user wants.
         */
        upcomingEpisodes.moveToPosition(-1);
        while (upcomingEpisodes.moveToNext()) {
          final long airtime = upcomingEpisodes.getLong(NotificationQuery.FIRSTAIREDMS);
          if (airtime > latestTimeToInclude) {
            // store next episode we plan to notify about
            prefs.edit().putLong(NotificationSettings.KEY_NEXT_TO_NOTIFY, airtime).commit();

            // convert it to actual device time for setting the
            // alarm
            wakeUpTime =
                Utils.convertToFakeTime(airtime, prefs, false)
                    - DateUtils.MINUTE_IN_MILLIS * notificationThreshold;

            break;
          }
        }
      }

      upcomingEpisodes.close();
    }

    // Set a default wake-up time if there are no future episodes for now
    if (wakeUpTime <= 0) {
      wakeUpTime = System.currentTimeMillis() + 6 * DateUtils.HOUR_IN_MILLIS;
    }

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    if (AndroidUtils.isKitKatOrHigher()) {
      am.setExact(AlarmManager.RTC_WAKEUP, wakeUpTime, pi);
    } else {
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, pi);
    }
  }