Android Open Source - mobile2-android E College Application






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   ww  w.j  av  a2s. co  m*/
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import roboguice.application.RoboApplication;
import roboguice.inject.SharedPreferencesName;
import roboguice.util.Ln;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.ecollege.android.errors.ECollegeAlertException;
import com.ecollege.android.errors.ECollegeException;
import com.ecollege.android.errors.ECollegePromptException;
import com.ecollege.android.errors.ECollegePromptRetryException;
import com.ecollege.android.util.FileCacheManager;
import com.ecollege.android.util.VolatileCacheManager;
import com.ecollege.android.view.HeaderView;
import com.ecollege.api.ECollegeClient;
import com.ecollege.api.model.Course;
import com.ecollege.api.model.User;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Module;

public class ECollegeApplication extends RoboApplication implements UncaughtExceptionHandler {
  
  @Inject SharedPreferences prefs;
    final protected VolatileCacheManager volatileCache = new VolatileCacheManager(1000 * 60 * 30); // 30 minute cache
  protected Context lastActiveContext;
    private FileCacheManager serviceCache;
  
  public ECollegeApplication() {
    super();
    lastActiveContext = this;
    Thread.setDefaultUncaughtExceptionHandler(this); //global error handling on UI thread
  }

  private ECollegeClient client;
  public ECollegeClient getClient() {
    if (client == null) {
      client = new ECollegeClient(getString(R.string.client_string), getString(R.string.client_id));
    }
    return client;
  }
  
  public String getSessionIdentifier() {
    String id = getClient().getGrantToken();
    return (id == null) ? "" : id;
  }
  
  @Override
  protected void addApplicationModules(List<Module> modules) {
    modules.add(new Module() {
      public void configure(Binder binder) {
         binder.bindConstant().annotatedWith(SharedPreferencesName.class).to("com.ecollege.android");
        //can make a separate module as needed
      }
    });
  }
    
  public void logout() {
    client = null;
    currentUser = null;
    currentCourseList = null;
    serviceCache = null;
    courseIdMap.clear();
    volatileCache.clear();
    SharedPreferences.Editor editor = prefs.edit();
    editor.remove("grantToken");
    editor.commit(); //change to apply if android 2.2
    Intent i = new Intent(this,SplashActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
  }
    
  public void putObjectInVolatileCache(String key, Object object) {
    volatileCache.put(key + "-" + getSessionIdentifier(), object);
  }
  
  public <CachedT> CachedT getObjectOfTypeFromVolatileCache(String key, Class<CachedT> clazz) {
    CachedT cachedObject = volatileCache.get(key + "-" + getSessionIdentifier(), clazz);
    return cachedObject;
  }
  
  public FileCacheManager getServiceCache() {
    if (serviceCache == null) {
      serviceCache = new FileCacheManager(this, 1000 * 60 * 60); //1 hour cache
      sweepCacheInBackground();
    }
    return serviceCache;
  }  
  
  protected void sweepCacheInBackground() {
    Thread t = new Thread(new Runnable() {
      public void run() {
        try {
          Integer count = serviceCache.removeInvalidEntries();
          Ln.d(String.format("Finished sweeping cache, removed %d files", count));
        } catch (Exception e) {
          Ln.e("Error sweeping the cache", e);
        }
      }
    });
    t.start();
  }

  private int pendingServiceCalls = 0;
  private User currentUser;
  private long currentCourseListLastLoaded;
  private List<Course> currentCourseList;
  final private ConcurrentHashMap<Long, Course> courseIdMap = new ConcurrentHashMap<Long, Course>(8);
  private int nextProgressDialogTitleId = -1;
    private int nextProgressDialogMsgId = -1;
    
    public int getPendingServiceCalls() {
    return pendingServiceCalls;
  }

  public synchronized void incrementPendingServiceCalls() {
    if (pendingServiceCalls == 0) {
      this.pendingServiceCalls++;
      updateHeaderProgress(true);
    } else {
      this.pendingServiceCalls++;
    }
  }
  
  public synchronized void decrementPendingServiceCalls() {
    if (pendingServiceCalls == 1) {
      this.pendingServiceCalls--;  
      updateHeaderProgress(false);
    } else {
      this.pendingServiceCalls--;
    }
  }

  public User getCurrentUser() {
    return currentUser;
  }

  public void setCurrentUser(User currentUser) {
    this.currentUser = currentUser;
  }

  public void setCurrentCourseList(List<Course> currentCourseList) {
    this.currentCourseList = currentCourseList;
    Collections.sort(this.currentCourseList, new Comparator<Course>() {
      public int compare(Course course1, Course course2) {
        assert(course1 != null);
        assert(course2 != null);
        return (course1.getTitle().compareToIgnoreCase(course2.getTitle()));
      }
    });
    for (Course course : currentCourseList) {
      courseIdMap.put(course.getId(), course);
    }
    currentCourseListLastLoaded = System.currentTimeMillis();
  }

  public List<Course> getCurrentCourseList() {
    return currentCourseList;
  }
  
  public void setCurrentCourseListLastLoaded(long currentCourseListLastLoaded) {
    this.currentCourseListLastLoaded = currentCourseListLastLoaded;
  }

  public long getCurrentCourseListLastLoaded() {
    return currentCourseListLastLoaded;
  }

  public Course getCourseById(long id) {
    return courseIdMap.get(id);
  }

  public int getNextProgressDialogTitleId() {
    return nextProgressDialogTitleId;
  }

  public void setNextProgressDialogTitleId(int nextProgressDialogTitleId) {
    this.nextProgressDialogTitleId = nextProgressDialogTitleId;
  }

  public int getNextProgressDialogMsgId() {
    return nextProgressDialogMsgId;
  }

  public void setNextProgressDialogMsgId(int nextProgressDialogMsgId) {
    this.nextProgressDialogMsgId = nextProgressDialogMsgId;
  }

    private HashMap<String, WeakReference<Context>> contextObjects = new HashMap<String, WeakReference<Context>>();

    public synchronized Context getActiveContext(String className) {
        WeakReference<Context> ref = contextObjects.get(className);
        if (ref == null) {
            return null;
        }

        final Context c = ref.get();
        if (c == null) // If the WeakReference is no longer valid, ensure it is removed.
            contextObjects.remove(className);

        return c;
    }

    public synchronized Context getActiveContext() {
      return lastActiveContext;
    }
    
    public synchronized void setActiveContext(String className, Context context) {
      if (!(context instanceof ECollegeApplication)) {
        lastActiveContext = context;
      }
        WeakReference<Context> ref = new WeakReference<Context>(context);
        this.contextObjects.put(className, ref);
    }

    public synchronized void resetActiveContext(String className) {
        contextObjects.remove(className);
    }  
  
  private List<WeakReference<HeaderView>> registeredHeaderViews = new ArrayList<WeakReference<HeaderView>>();
  
  public synchronized void updateHeaderProgress(boolean showProgress) {
    for (int i=registeredHeaderViews.size()-1;i>=0;i--) {
      HeaderView hv = registeredHeaderViews.get(i).get();
      if (hv == null) {
        registeredHeaderViews.remove(i);        
      } else {
        hv.setProgressVisibility(showProgress);
      }
    }
  }
  
  public synchronized void registerHeaderView(HeaderView hv) {
    WeakReference<HeaderView> ref = new WeakReference<HeaderView>(hv);
    registeredHeaderViews.add(ref);
    hv.setProgressVisibility(pendingServiceCalls > 0);
  }
  
  public synchronized void unregisterHeaderView(HeaderView hv) {
    for (int i=registeredHeaderViews.size()-1;i>=0;i--) {
      if (registeredHeaderViews.get(i).get() == hv) {
        registeredHeaderViews.remove(i);
      }
    }
  }
  
  public void reportError(Throwable source) {
    ECollegeException ex;
    
    if (source instanceof ECollegeException){
      ex = (ECollegeException)source;
    } else {
      ex = new ECollegePromptException(lastActiveContext, source);
    }
    
    if (ex.getSource() != null) {
      Ln.e(ex.getSource());
    }
    
    if (ex instanceof ECollegeAlertException) {
      Toast.makeText(this,ex.getErrorMessageId(),5000).show();
    } else if (ex instanceof ECollegePromptException) {
      ((ECollegePromptException)ex).showErrorDialog();
    } else if (ex instanceof ECollegePromptRetryException) {
      ((ECollegePromptRetryException)ex).showErrorDialog();
    }
  }

  public void uncaughtException(Thread thread, Throwable ex) {
    reportError(ex);
  }
  
  
}




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