Android Open Source - npr-android-app News Topic Activity






From Project

Back to project page npr-android-app.

License

The source code is released under:

Apache License

If you think the Android project npr-android-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright 2009 Google Inc.
////from   w  ww .j  a  v a 2 s .c o  m
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.npr.android.news;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import org.npr.android.util.Tracker;
import org.npr.android.util.Tracker.ActivityMeasurement;
import org.npr.api.ApiConstants;
import org.npr.api.Program;
import org.npr.api.StoryGrouping;
import org.npr.api.Topic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsTopicActivity extends PlayerActivity implements
    OnItemClickListener {

  public static enum TopicType {
    TOPICS(R.string.msg_main_subactivity_topics),
    PROGRAMS(R.string.msg_main_subactivity_programs),
    ;
    private int title;

    private TopicType(int title) {
      this.title = title;
    }
    
  }

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case 0:
          listView.setAdapter(listAdapter);
          break;
      }
    }
  };

  private ListAdapter listAdapter;
  private ListView listView;
  private Thread listInitThread;
  private TopicType topic = null;

  @SuppressWarnings("unchecked")
  private int constructList() {
    int type = getIntent().getIntExtra(Constants.EXTRA_SUBACTIVITY_ID, -1);
    List<? extends StoryGrouping> groupings = null;
    switch (topic) {
      case TOPICS:
        groupings =
            Topic.factory.downloadStoryGroupings(30);
        listAdapter = new NewsTopicAdapter<Topic>((List<Topic>) groupings); 
        break;
      case PROGRAMS:
        groupings =
            Program.factory.downloadStoryGroupings(30);
        listAdapter = new NewsTopicAdapter<Program>((List<Program>) groupings); 
        break;
    }
    int message = 0;

    if (groupings == null) {
      message = 1;
    }
    return message;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    int type = getIntent().getIntExtra(Constants.EXTRA_SUBACTIVITY_ID, -1);
    switch (type) {
      case R.string.msg_main_subactivity_topics:
        topic = TopicType.TOPICS;
        break;
      case R.string.msg_main_subactivity_programs:
        topic = TopicType.PROGRAMS;
        break;
    }
    super.onCreate(savedInstanceState);
    ViewGroup container = (ViewGroup) findViewById(R.id.Content);
    ViewGroup.inflate(this, R.layout.news_topics, container);
    listView = (ListView) findViewById(R.id.ListView01);
    listView.setOnItemClickListener(this);

    initializeList();
  }

  private void initializeList() {
    listInitThread = new Thread(new Runnable() {
      public void run() {
        int result = NewsTopicActivity.this.constructList();
        handler.sendEmptyMessage(result);
      }
    });
    listInitThread.start();
  }

  @Override
  public CharSequence getMainTitle() {
    return getString(topic.title);
  }

  @Override
  public void trackNow() {
    StringBuilder pageName = new StringBuilder("News");
    Tracker.instance(getApplication()).trackPage(
        new ActivityMeasurement(pageName.toString(), "News"));
  }

  @Override
  public boolean isRefreshable(){
    return true;
  }

  @Override
  public void refresh(){
    initializeList();
  }
  
  private class NewsTopicAdapter<T extends StoryGrouping> extends
      ArrayAdapter<T> {
    public NewsTopicAdapter(List<T> groupings) {
      super(NewsTopicActivity.this, android.R.layout.simple_list_item_1,
          android.R.id.text1, groupings);
    }
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {
    int type = getIntent().getIntExtra(Constants.EXTRA_SUBACTIVITY_ID, -1);
    StoryGrouping item = (StoryGrouping) parent.getItemAtPosition(position);

    String grouping = getString(type);
    String description = item.getTitle();
    String topicId = item.getId();
    Map<String, String> params = new HashMap<String, String>();
    params.put(ApiConstants.PARAM_ID, topicId);
    params.put(ApiConstants.PARAM_FIELDS, ApiConstants.STORY_FIELDS);
    params.put(ApiConstants.PARAM_SORT, "assigned");
    if (topic == TopicType.PROGRAMS){ 
      params.put(ApiConstants.PARAM_DATE, "current");
    }

    String url =
        ApiConstants.instance().createUrl(ApiConstants.STORY_PATH, params);

    Intent i = new Intent(this, NewsListActivity.class);
    i.putExtra(Constants.EXTRA_QUERY_URL, url);
    i.putExtra(Constants.EXTRA_DESCRIPTION, description);
    i.putExtra(Constants.EXTRA_GROUPING, grouping);
    i.putExtra(Constants.EXTRA_SIZE, 10);
    startActivityWithoutAnimation(i);
  }
}




Java Source Code List

org.npr.android.news.AboutActivity.java
org.npr.android.news.Constants.java
org.npr.android.news.DownloadDrawable.java
org.npr.android.news.EditPreferences.java
org.npr.android.news.ListenView.java
org.npr.android.news.Main.java
org.npr.android.news.NewsListActivity.java
org.npr.android.news.NewsListAdapter.java
org.npr.android.news.NewsStoryActivity.java
org.npr.android.news.NewsTopicActivity.java
org.npr.android.news.PlaybackService.java
org.npr.android.news.PlayerActivity.java
org.npr.android.news.PlaylistActivity.java
org.npr.android.news.PodcastActivity.java
org.npr.android.news.Refreshable.java
org.npr.android.news.SearchActivity.java
org.npr.android.news.SearchResultsActivity.java
org.npr.android.news.StationDetailsActivity.java
org.npr.android.news.StationListActivity.java
org.npr.android.news.StationListAdapter.java
org.npr.android.news.StationSearchActivity.java
org.npr.android.news.StreamProxy.java
org.npr.android.news.Trackable.java
org.npr.android.util.Eula.java
org.npr.android.util.FileUtils.java
org.npr.android.util.M3uParser.java
org.npr.android.util.NodeUtils.java
org.npr.android.util.PlaylistEntry.java
org.npr.android.util.PlaylistParser.java
org.npr.android.util.PlaylistProvider.java
org.npr.android.util.PlsParser.java
org.npr.android.util.Tracker.java
org.npr.android.util.TypefaceCache.java
org.npr.api.ApiConstants.java
org.npr.api.ApiElement.java
org.npr.api.Client.java
org.npr.api.IterableNodeList.java
org.npr.api.Podcast.java
org.npr.api.Program.java
org.npr.api.Station.java
org.npr.api.StoryGrouping.java
org.npr.api.Story.java
org.npr.api.Topic.java