Пример #1
0
  public void refreshFeedCounts() {
    final APIClient client = new APIClient(context);
    final APIResponse response = client.get(APIConstants.URL_FEED_COUNTS);
    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {
      final FeedRefreshResponse feedCountUpdate =
          gson.fromJson(response.responseString, FeedRefreshResponse.class);
      for (String feedId : feedCountUpdate.feedCounts.keySet()) {
        Uri feedUri = FeedProvider.FEEDS_URI.buildUpon().appendPath(feedId).build();
        contentResolver.update(
            feedUri, feedCountUpdate.feedCounts.get(feedId).getValues(), null, null);
      }

      for (String socialfeedId : feedCountUpdate.socialfeedCounts.keySet()) {
        String userId = socialfeedId.split(":")[1];
        Uri feedUri = FeedProvider.SOCIAL_FEEDS_URI.buildUpon().appendPath(userId).build();
        contentResolver.update(
            feedUri, feedCountUpdate.socialfeedCounts.get(socialfeedId).getValues(), null, null);
      }
    }
  }
Пример #2
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_itemlist, null);
    ListView itemList = (ListView) v.findViewById(R.id.itemlistfragment_list);

    itemList.setEmptyView(v.findViewById(R.id.empty_view));

    contentResolver = getActivity().getContentResolver();
    Uri storiesUri = FeedProvider.FEED_STORIES_URI.buildUpon().appendPath(feedId).build();
    Cursor storiesCursor =
        contentResolver.query(
            storiesUri,
            null,
            DatabaseConstants.getStorySelectionFromState(currentState),
            null,
            DatabaseConstants.getStorySortOrder(storyOrder));
    Uri feedUri = FeedProvider.FEEDS_URI.buildUpon().appendPath(feedId).build();
    Cursor feedCursor = contentResolver.query(feedUri, null, null, null, null);

    if (feedCursor.getCount() < 1) {
      // This shouldn't happen, but crash reports indicate that it does (very rarely).
      // If we are told to create an item list for a feed, but then can't find that feed ID in the
      // DB,
      // something is very wrong, and we won't be able to recover, so just force the user back to
      // the
      // feed list until we have a better understanding of how to prevent this.
      Log.w(this.getClass().getName(), "Feed not found in DB, can't create item list.");
      getActivity().finish();
      return v;
    }

    feedCursor.moveToFirst();
    Feed feed = Feed.fromCursor(feedCursor);

    String[] groupFrom =
        new String[] {
          DatabaseConstants.STORY_TITLE,
          DatabaseConstants.STORY_AUTHORS,
          DatabaseConstants.STORY_READ,
          DatabaseConstants.STORY_SHORTDATE,
          DatabaseConstants.STORY_INTELLIGENCE_AUTHORS
        };
    int[] groupTo =
        new int[] {
          R.id.row_item_title,
          R.id.row_item_author,
          R.id.row_item_title,
          R.id.row_item_date,
          R.id.row_item_sidebar
        };

    // create the adapter before starting the loader, since the callback updates the adapter
    adapter =
        new FeedItemsAdapter(
            getActivity(),
            feed,
            R.layout.row_item,
            storiesCursor,
            groupFrom,
            groupTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    getLoaderManager().initLoader(ITEMLIST_LOADER, null, this);

    itemList.setOnScrollListener(this);

    adapter.setViewBinder(new FeedItemViewBinder(getActivity()));
    itemList.setAdapter(adapter);
    itemList.setOnItemClickListener(this);
    itemList.setOnCreateContextMenuListener(this);

    return v;
  }
Пример #3
0
  /**
   * Fetch the list of feeds/folders/socials from the backend.
   *
   * @param doUpdateCounts forces a refresh of unread counts. This has a high latency cost and
   *     should not be set if the call is being used to display the UI for the first time, in which
   *     case it is more appropriate to make a separate, additional call to refreshFeedCounts().
   */
  public boolean getFolderFeedMapping(boolean doUpdateCounts) {

    final APIClient client = new APIClient(context);
    final ContentValues params = new ContentValues();
    params.put(APIConstants.PARAMETER_UPDATE_COUNTS, (doUpdateCounts ? "true" : "false"));
    final APIResponse response = client.get(APIConstants.URL_FEEDS, params);
    final FeedFolderResponse feedUpdate = new FeedFolderResponse(response.responseString, gson);

    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {

      // if the response says we aren't logged in, clear the DB and prompt for login. We test this
      // here, since this the first sync call we make on launch if we believe we are cookied.
      if (!feedUpdate.isAuthenticated) {
        PrefsUtils.logout(context);
        return false;
      }

      HashMap<String, Feed> existingFeeds = getExistingFeeds();

      List<ContentValues> feedValues = new ArrayList<ContentValues>();
      for (String newFeedId : feedUpdate.feeds.keySet()) {
        if (existingFeeds.get(newFeedId) == null
            || !feedUpdate.feeds.get(newFeedId).equals(existingFeeds.get(newFeedId))) {
          feedValues.add(feedUpdate.feeds.get(newFeedId).getValues());
        }
      }
      if (feedValues.size() > 0) {
        contentResolver.bulkInsert(
            FeedProvider.FEEDS_URI, feedValues.toArray(new ContentValues[feedValues.size()]));
      }

      for (String olderFeedId : existingFeeds.keySet()) {
        if (feedUpdate.feeds.get(olderFeedId) == null) {
          Uri feedUri = FeedProvider.FEEDS_URI.buildUpon().appendPath(olderFeedId).build();
          contentResolver.delete(feedUri, null, null);
        }
      }

      List<ContentValues> socialFeedValues = new ArrayList<ContentValues>();
      for (final SocialFeed feed : feedUpdate.socialFeeds) {
        socialFeedValues.add(feed.getValues());
      }
      if (socialFeedValues.size() > 0) {
        contentResolver.bulkInsert(
            FeedProvider.SOCIAL_FEEDS_URI,
            socialFeedValues.toArray(new ContentValues[socialFeedValues.size()]));
      }

      Cursor folderCursor = contentResolver.query(FeedProvider.FOLDERS_URI, null, null, null, null);
      folderCursor.moveToFirst();
      HashSet<String> existingFolders = new HashSet<String>();
      while (!folderCursor.isAfterLast()) {
        existingFolders.add(Folder.fromCursor(folderCursor).getName());
        folderCursor.moveToNext();
      }
      folderCursor.close();

      for (final Entry<String, List<Long>> entry : feedUpdate.folders.entrySet()) {
        if (!TextUtils.isEmpty(entry.getKey())) {
          String folderName = entry.getKey().trim();
          if (!existingFolders.contains(folderName) && !TextUtils.isEmpty(folderName)) {
            final ContentValues folderValues = new ContentValues();
            folderValues.put(DatabaseConstants.FOLDER_NAME, folderName);
            contentResolver.insert(FeedProvider.FOLDERS_URI, folderValues);
          }

          for (Long feedId : entry.getValue()) {
            if (!existingFeeds.containsKey(Long.toString(feedId))) {
              ContentValues values = new ContentValues();
              values.put(DatabaseConstants.FEED_FOLDER_FEED_ID, feedId);
              values.put(DatabaseConstants.FEED_FOLDER_FOLDER_NAME, folderName);
              contentResolver.insert(FeedProvider.FEED_FOLDER_MAP_URI, values);
            }
          }
        }
      }

      int starredStoriesCount = feedUpdate.starredCount;
      ContentValues values = new ContentValues();
      values.put(DatabaseConstants.STARRED_STORY_COUNT_COUNT, starredStoriesCount);
      contentResolver.insert(FeedProvider.STARRED_STORIES_COUNT_URI, values);
    }
    return true;
  }