コード例 #1
0
ファイル: AddActivity.java プロジェクト: Ertyguy/ContTent
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      ViewHolder holder;

      if (convertView == null) {
        convertView =
            inflater.inflate(
                (getItemViewType(position) == 0
                    ? R.layout.item_youtube_search
                    : R.layout.item_twitter_search),
                parent,
                false);
        holder = new ViewHolder();
        holder.image_iv = (ImageView) convertView.findViewById(R.id.thumbnail_iv);
        holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
        holder.screenName_tv = (TextView) convertView.findViewById(R.id.screen_name_tv);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      if (searchMode == Var.SEARCH_YOUTUBE) {
        YoutubeChannel feed = (YoutubeChannel) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_youtube_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getName());
      }

      if (searchMode == Var.SEARCH_TWITTER) {
        TwitterFeed feed = (TwitterFeed) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_twitter_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getDisplayName());
        holder.screenName_tv.setText(feed.getName());
      }

      if (searchMode == Var.SEARCH_YT_CHANNEL) {
        YoutubeFeed feed = (YoutubeFeed) getItem(position);
        holder.image_iv.setImageResource(R.drawable.ic_youtube_icon);
        if (feed.getThumbnail() != null)
          imageLoader.DisplayImage(feed.getThumbnail(), holder.image_iv);

        holder.name_tv.setText(feed.getName());
      }

      return convertView;
    }
コード例 #2
0
ファイル: AddActivity.java プロジェクト: Ertyguy/ContTent
    @Override
    protected String doInBackground(Void... params) {
      searchBusy = true;
      try {

        searchChannel.getYoutubeFeeds().clear();

        // TODO check the internet connection here
        String channelUrl =
            "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id="
                + searchChannel.getFeedId()
                + "&fields=items%2FcontentDetails&key="
                + Var.DEVELOPER_KEY;
        String playlists = "";
        String channel = Var.HTTPGet(channelUrl);

        JSONObject res = new JSONObject(channel);
        if (Var.isJsonArray(res, "items")) {
          JSONArray items = res.getJSONArray("items");
          if (items.length() > 0) {
            JSONObject item = items.getJSONObject(0);
            if (Var.isJsonObject(item, "contentDetails")) {
              JSONObject contentDetails = item.getJSONObject(("contentDetails"));
              if (Var.isJsonObject(contentDetails, "relatedPlaylists")) { // Channel Name
                JSONObject relatedPlaylists = contentDetails.getJSONObject("relatedPlaylists");
                if (Var.isJsonString(relatedPlaylists, "uploads"))
                  playlists +=
                      (Var.isEmpty(playlists) ? "" : ",") + relatedPlaylists.getString("uploads");
                if (Var.isJsonString(relatedPlaylists, "likes"))
                  playlists +=
                      (Var.isEmpty(playlists) ? "" : ",") + relatedPlaylists.getString("likes");
                if (Var.isJsonString(relatedPlaylists, "favorites"))
                  playlists +=
                      (Var.isEmpty(playlists) ? "" : ",") + relatedPlaylists.getString("favorites");
              }
            }
          }
        }

        String playlistUrl =
            "https://www.googleapis.com/youtube/v3/playlists?part=snippet&id="
                + URLEncoder.encode(playlists, "UTF-8")
                + "&fields=items(id%2Csnippet)&key="
                + Var.DEVELOPER_KEY;
        String playlist = Var.HTTPGet(playlistUrl);

        JSONObject play = new JSONObject(playlist);
        if (Var.isJsonArray(play, "items")) {
          JSONArray items = play.getJSONArray("items");
          for (int i = 0; i < items.length(); i++) {
            JSONObject item = items.getJSONObject(i);

            if (Var.isJsonString(item, "id")) { // Feed Id
              YoutubeFeed feed = new YoutubeFeed(item.getString("id"));
              feed.setType(Var.TYPE_YOUTUBE_PLAYLIST);
              feed.setChannelHandle(searchChannel.getFeedId());

              if (Var.isJsonObject(item, "snippet")) { // Feed Name
                JSONObject snippet = item.getJSONObject("snippet");
                if (Var.isJsonString(snippet, "title")) {
                  if (snippet.getString("title").startsWith("Upload")) feed.setName("Uploads");
                  else feed.setName(snippet.getString("title"));
                }

                if (Var.isJsonObject(snippet, "thumbnails")) { // Feed Thumbnail
                  JSONObject thumbnails = snippet.getJSONObject("thumbnails");
                  if (Var.isJsonObject(thumbnails, "default")) {
                    JSONObject def = thumbnails.getJSONObject("default");
                    if (Var.isJsonString(def, "url")) {
                      feed.setThumbnail(def.getString("url"));
                    }
                  }
                }
              }

              searchChannel.getYoutubeFeeds().add(feed);

              Log.d(TAG, "youtube feed added " + feed.getName());
            }
          }

          YoutubeFeed activityFeed = new YoutubeFeed();
          activityFeed.setChannelHandle(searchChannel.getFeedId());
          searchChannel.getYoutubeFeeds().add(activityFeed); // Activity
        }

      } catch (Throwable t) {
        t.printStackTrace();
      }
      return null;
    }