Android Open Source - mobile2-android Course Activity






From Project

Back to project page mobile2-android.

License

The source code is released under:

Apache License

If you think the Android project mobile2-android 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

package com.ecollege.android;
/*from   w  ww .j  av a 2  s .com*/
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import roboguice.inject.InjectExtra;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ecollege.android.activities.ECollegeActivity;
import com.ecollege.android.activities.ECollegeListActivity;
import com.ecollege.android.adapter.UberAdapter;
import com.ecollege.android.adapter.UberItem;
import com.ecollege.api.ECollegeClient;
import com.ecollege.api.model.Announcement;
import com.ecollege.api.model.Course;
import com.ecollege.api.model.User;
import com.ecollege.api.services.courses.FetchAnnouncementsForCourse;
import com.ecollege.api.services.courses.FetchInstructorsForCourse;
import com.google.inject.Inject;

public class CourseActivity extends ECollegeListActivity {
  
  public static final String ANNOUNCEMENT_LIST_EXTRA = "ANNOUNCEMENT_LIST_EXTRA";
  
  @Inject ECollegeApplication app;
  @Inject SharedPreferences prefs;
  @InjectView(R.id.course_title_text) TextView courseTitleText;
  @InjectView(R.id.course_code_text) TextView courseCodeText;
  @InjectView(R.id.instructor_text) TextView instructorText;
  @InjectView(R.id.announcement_title) TextView announcementTitle;
  @InjectView(R.id.announcement_description) TextView announcementDescription;
  @InjectView(R.id.instructor_loading_indicator) ProgressBar instructorLoadingIndicator;
  @InjectView(R.id.announcement_loading_indicator) ProgressBar announcementsLoadingIndicator;
  @InjectView(R.id.announcement_view) LinearLayout announcementView;
  @InjectResource(R.array.course_menu_items) String[] courseMenuItems;
  @InjectExtra(CoursesActivity.COURSE_EXTRA) Course course;
  protected ECollegeClient client;
  protected LayoutInflater viewInflater;
  private CourseMenuAdapter courseMenuAdapter;
  private List<User> instructors;
  private List<Announcement> announcements;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.course);
    
    viewInflater = getLayoutInflater();
    client = app.getClient();
    displayCourse();
    createMenu();
    
    announcementView.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        if (announcements != null && announcements.size() > 0) {
            Intent i = new Intent(CourseActivity.this,AnnouncementActivity.class);
            i.putExtra(AnnouncementActivity.COURSE_EXTRA, course);
            i.putExtra(AnnouncementActivity.ANNOUNCEMENT_EXTRA, announcements.get(0));
            i.putExtra(AnnouncementActivity.FINISH_ON_CLICK_ALL_ANNOUNCEMENTS_EXTRA, false);      
            startActivity(i);
        }
      }
    });
    
    loadAndDisplayInstructorsForCourse();
    loadAndDisplayAnnouncementsForCourse();
  }
  
  @Override protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Class<? extends ECollegeActivity> destinationClass;
    String detailExtraKey = null;
    Serializable detailExtraValue = null;
    switch (position) {
    case 0:
      destinationClass = CourseAnnouncementsActivity.class;
      detailExtraKey = ANNOUNCEMENT_LIST_EXTRA;
      detailExtraValue = (Serializable) announcements;
      break;
    case 1:
      destinationClass = CourseGradebookActivity.class;
      break;
    case 2:
      destinationClass = CoursePeopleActivity.class;
      break;
    default:
      return;
    }
    Intent intent = new Intent(this, destinationClass);
    intent.putExtra(CoursesActivity.COURSE_EXTRA, course);
    if (detailExtraKey != null && detailExtraValue != null) {
      intent.putExtra(detailExtraKey, detailExtraValue);
    }
    startActivity(intent);
  }
  
  private void displayCourse() {
    courseTitleText.setText(Html.fromHtml(course.getTitle()));
    courseCodeText.setText(Html.fromHtml(course.getDisplayCourseCode()));
  }

  private void createMenu() {
    courseMenuAdapter = new CourseMenuAdapter(this);
    courseMenuAdapter.updateItems(courseMenuItems);
    setListAdapter(courseMenuAdapter);
  }

  private void displayInstructorList() {
    showInstructorLoadingProgress(false);
    if (!instructors.isEmpty()) {
      Iterator<User> i = instructors.listIterator();
      StringBuffer sb = new StringBuffer("");
      User instructor;
      while (i.hasNext()) {
        instructor = i.next();
        sb.append(instructor.getFirstName() + " " + instructor.getLastName());
        if (i.hasNext()) sb.append(", ");
      }
      instructorText.setText(sb.toString());
    } else {
      instructorText.setText(R.string.no_instructor);
    }
  }

  private void displayFirstAnnouncement() {
    showAnnouncementLoadingProgress(false);
    if (!announcements.isEmpty()) {
      Announcement firstAnnouncement = announcements.get(0);
      announcementTitle.setText(firstAnnouncement.getSubject());
      announcementDescription.setText(firstAnnouncement.getRawText());
    } else {
      announcementTitle.setText(R.string.no_announcements);
      announcementDescription.setText(null);
    }
  }

  private void displayAnnouncementCount() {
    courseMenuAdapter.notifyDataSetChanged();
  }

  private void loadAndDisplayInstructorsForCourse() {
    showInstructorLoadingProgress(true);
    buildService(new FetchInstructorsForCourse(course)).execute();
  }

  private void loadAndDisplayAnnouncementsForCourse() {
    showAnnouncementLoadingProgress(true);
    buildService(new FetchAnnouncementsForCourse(course, true)).execute();
  }
  
  private void showAnnouncementLoadingProgress(boolean loading) {
    if (loading) {
      announcementsLoadingIndicator.setVisibility(View.VISIBLE);
      announcementTitle.setVisibility(View.GONE);
      announcementDescription.setVisibility(View.GONE);
    } else {
      announcementsLoadingIndicator.setVisibility(View.GONE);
      announcementTitle.setVisibility(View.VISIBLE);
      announcementDescription.setVisibility(View.VISIBLE);
    }
  }

  private void showInstructorLoadingProgress(boolean loading) {
    if (loading) {
      instructorLoadingIndicator.setVisibility(View.VISIBLE);
      instructorText.setVisibility(View.INVISIBLE);
    } else {
      instructorLoadingIndicator.setVisibility(View.GONE);
      instructorText.setVisibility(View.VISIBLE);
    }
  }

  public void onServiceCallSuccess(FetchInstructorsForCourse service) {
    instructors = service.getResult();
    displayInstructorList();
  }
  
  public void onServiceCallSuccess(FetchAnnouncementsForCourse service) {
    // stop progress
    announcements = service.getResult();
    displayFirstAnnouncement();
    displayAnnouncementCount();
  }
  
  protected class CourseMenuItemViewHolder {
    public TextView title;
    public TextView unreadCountText;
  }
  
  protected class CourseMenuAdapter extends UberAdapter<String> {

    public CourseMenuAdapter(Context context) {
      super(context,false,false,false);
    }

    @Override
    protected View getDataItemView(View convertView, ViewGroup parent,
        UberItem<String> item) {
      CourseMenuItemViewHolder holder;
      if (convertView == null) {
        holder = new CourseMenuItemViewHolder();
        convertView = viewInflater.inflate(R.layout.course_menu_item, null);
        holder.title = (TextView) convertView.findViewById(R.id.course_title);
        holder.unreadCountText = (TextView) convertView.findViewById(R.id.unread_count_text);
        convertView.setTag(holder);
      } else {
        holder = (CourseMenuItemViewHolder) convertView.getTag();
      }
      holder.title.setText(item.getDataItem());
      // first position should be "Announcements"
//      if (position == 0 && announcements != null && announcements.size() > 0) {
//        holder.unreadCountText.setVisibility(View.VISIBLE);
//        holder.unreadCountText.setText(announcements.size() + "");
//      } else {
//        holder.unreadCountText.setVisibility(View.GONE);
//        holder.unreadCountText.setText(null);
//      }
      return convertView;
    }
    
  }
}




Java Source Code List

com.ecollege.android.AnnouncementActivity.java
com.ecollege.android.CourseActivity.java
com.ecollege.android.CourseAnnouncementsActivity.java
com.ecollege.android.CourseDiscussionsActivity.java
com.ecollege.android.CourseGradebookActivity.java
com.ecollege.android.CoursePeopleActivity.java
com.ecollege.android.CourseThreadActivity.java
com.ecollege.android.CoursesActivity.java
com.ecollege.android.DiscussionsActivity.java
com.ecollege.android.DropboxMessageActivity.java
com.ecollege.android.ECollegeApplication.java
com.ecollege.android.GradeActivity.java
com.ecollege.android.HomeActivity.java
com.ecollege.android.HtmlContentActivity.java
com.ecollege.android.LoginActivity.java
com.ecollege.android.MainActivity.java
com.ecollege.android.PersonActivity.java
com.ecollege.android.ProfileActivity.java
com.ecollege.android.SingleSignonActivity.java
com.ecollege.android.SplashActivity.java
com.ecollege.android.UserDiscussionActivity.java
com.ecollege.android.UserResponseActivity.java
com.ecollege.android.UserTopicActivity.java
com.ecollege.android.activities.ECollegeActivityHelper.java
com.ecollege.android.activities.ECollegeActivity.java
com.ecollege.android.activities.ECollegeDefaultActivity.java
com.ecollege.android.activities.ECollegeListActivity.java
com.ecollege.android.activities.ECollegeTabActivity.java
com.ecollege.android.adapter.ActivityFeedAdapter.java
com.ecollege.android.adapter.GroupedAdapter.java
com.ecollege.android.adapter.LoadMoreAdapter.java
com.ecollege.android.adapter.ParentAdapterObserver.java
com.ecollege.android.adapter.ResponseAdapter.java
com.ecollege.android.adapter.TopicsAdapter.java
com.ecollege.android.adapter.UberAdapter.java
com.ecollege.android.adapter.UberItem.java
com.ecollege.android.adapter.UpcomingEventsAdapter.java
com.ecollege.android.adapter.WaitingForApiAdapter.java
com.ecollege.android.errors.ECollegeAlertException.java
com.ecollege.android.errors.ECollegeException.java
com.ecollege.android.errors.ECollegePromptException.java
com.ecollege.android.errors.ECollegePromptRetryException.java
com.ecollege.android.tasks.ECollegeAsyncTask.java
com.ecollege.android.tasks.ServiceCallTask.java
com.ecollege.android.tasks.TaskPostProcessor.java
com.ecollege.android.util.CacheConfiguration.java
com.ecollege.android.util.DateTimeUtil.java
com.ecollege.android.util.FileCacheManager.java
com.ecollege.android.util.VolatileCacheManager.java
com.ecollege.android.view.HeaderView.java
com.ecollege.android.view.helpers.ResponseCountViewHelper.java