Android Open Source - ExampleApp App Context






From Project

Back to project page ExampleApp.

License

The source code is released under:

Copyright (c) 2014, Altinn All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redis...

If you think the Android project ExampleApp 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.altinn.apps.fisher;
//from w w w.j  a  v a 2s .co m
/**
 * This is a class which will first comes in picture when application starts.
 * So here we can have some global functionality which is accessible through out 
 * all the activities in the live session of the application.
 * 
 */


import java.util.Date;

import android.app.Application;
import android.content.Context;
import android.graphics.Point;
import android.os.CountDownTimer;
import android.view.Display;
import android.view.WindowManager;

import com.altinn.apps.fisher.common.AppConstants;
import com.altinn.apps.fisher.common.IStatusMessage;
import com.altinn.apps.fisher.net.tasks.RefreshTokenTask;
import com.altinn.apps.fisher.ui.screen.BaseActivity;
import com.altinn.apps.fisher.utils.PreferenceUtils;
import com.altinn.apps.fisher.utils.Utils.Log;




public class AppContext extends Application {
  
  private static AppContext mInstance;
  private static BaseActivity mActiveActivity;
  private boolean mDisplayStatusInfo;
  private IStatusMessage mStatusMessage;
  private boolean isLoginExpired;
  public static boolean TEST_MODE = false;//Ideal case keep this variable false , For testing if we want to test only UI, in non - network state in that case we can make this variable true
  
    private CountDownTimer mRefreshTokenTimer;
    private RefreshTokenTask mRefreshTokenTask;
    
    private int mMenuWidth;
  
    /*
     * Here we are starting scheduler for refresh token
     * (non-Javadoc)
     * @see android.app.Application#onCreate()
     */
  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
    
  
  }
  
  /**
   * This method make this class accessible throughout the application environment
   * @return
   */
  public static AppContext getInstance(){
    return mInstance;
  }

  
  /**
   * On Resume of BaseActivity we are setting Active Activity-State by this method
   * @param baseActivity
   */
  public void setActiveActivity(BaseActivity baseActivity) {
    mActiveActivity = baseActivity;
    
  }
  
  /**
   * This method give us reference of current active activity
   * @return
   */
  public BaseActivity getActiveActivity() {
    return mActiveActivity;
    
  }
  
  /**
   * This method called from activity which is at the bottom of activity stack.
   * On exit we are canceling the timer task.
   */
  public void exitApp(){
    //Process.killProcess(Process.myPid());
    if( mRefreshTokenTimer != null){
      mRefreshTokenTimer.cancel();
    }
  }

  /**
   * This method indicates weather status message is visible or not
   * @return mDisplayStatusInfo
   */
  public boolean isDisplayStatusVisible() {
    return mDisplayStatusInfo;
  }

  /**
   * This method makes status message visible on any of the active screen which is a sub class of 
   * BaseActivity
   * @param mDisplayStatusInfo
   * @param msg
   */
  public void setDisplayStatus(boolean mDisplayStatusInfo, IStatusMessage msg) {
    this.mDisplayStatusInfo = mDisplayStatusInfo;
    this.mStatusMessage = msg;
    if(mActiveActivity != null){
      mActiveActivity.refreshStatus();
    }
  }
  
  /**
   * This method returns instance of currently active status message
   * @return mStatusMessage
   */
  public IStatusMessage getStatusMessage(){
    return mStatusMessage;
  }
  
  /**
   * Using this method we can set/reset login status
   * in case of login expired we can call browser interface for login,
   * And after successful login we can call 'callwebservice' method of respective activity
   * @param loginStatus
   */
  public void setLoginExpired(boolean loginStatus){
    isLoginExpired = loginStatus;
  }
  
  /**
   * This method indicates login status of the user
   * @return
   */
  public boolean isLoginExpired(){
    return isLoginExpired;
  }
  
  
  
  /**
   * This method start refresh token timer task,
   * this will invoked in every 7+ minutes
   * if conditions are in favor it will try to refresh a token.
   * Usually in server configuration, token will expire in 30 mins.
   * If web service called after half of the time (>15 mins) then new token will be issued.
   * 
   * Here we are checking 'isRefreshNeeded() = >15 mins' this condition is in favor to refresh the token.
   * During this task we are just trying to access a web service(dummy), so that we get a refreshed token, and  user remains logged in
   *  
   */
  public void scheduleRefreshTimer(){
    if( mRefreshTokenTimer != null){
      mRefreshTokenTimer.cancel();
    }
    
    mRefreshTokenTimer = new CountDownTimer(Integer.MAX_VALUE, AppConstants.REFRESH_TIMER) {
      public void onTick(long millisUntilFinished) {
        Log.d("scheduleRefreshTimer#onTick(): ", ""+(new Date(System.currentTimeMillis())).toString());
        if(isRefreshNeeded()){    
          startRefreshTask();
        }
      }
      public void onFinish() {
        Log.d("scheduleRefreshTimer#onFinish(): ",""+(new Date(System.currentTimeMillis())).toString());
        
      }
    };
    mRefreshTokenTimer.start();
  }  
  
  /**
   * When ever we get a refreshed token, then that time get recorded in preferences, to get the next refresh time.
   * So this method gives indicates that do we really need to refresh a token
   * @return
   */
  public boolean isRefreshNeeded(){
    return ( System.currentTimeMillis() - PreferenceUtils.getInstance().getCookieUpdateTime())>AppConstants.REFRESH_AVAILABLE_TIME;
  }
  
  /**
   * This method returns true if token is stale by 30+ mins.
   * @return
   */
  public boolean isTokenExpired(){
    return (System.currentTimeMillis() -  PreferenceUtils.getInstance().getCookieUpdateTime())  >AppConstants.TOKEN_EXPIRE_TIME_MILLIES;
  }
  
  
  /**
   * Start a refresh token action refer AppContext.scheduleRefreshTimer()
   */
  public void startRefreshTask(){          
      if(mRefreshTokenTask != null){
        mRefreshTokenTask.cancel(true);
      }
      mRefreshTokenTask = new RefreshTokenTask();
      mRefreshTokenTask.execute();
      Log.d("executeRefreshTask(): ",""+(new Date(System.currentTimeMillis())).toString());
    
  
  }
  
  public int getMenuWidth(){
    if(mMenuWidth == 0){
      WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
      
      mMenuWidth = (int)(Math.min(width, height)*0.80);//80% screen width
      
    }
    return mMenuWidth;
  }
  


}




Java Source Code List

com.altinn.apps.fisher.AppContext.java
com.altinn.apps.fisher.CacheManager.java
com.altinn.apps.fisher.common.AppConstants.java
com.altinn.apps.fisher.common.IStatusMessage.java
com.altinn.apps.fisher.common.MenuItem.java
com.altinn.apps.fisher.common.StatusMessage.java
com.altinn.apps.fisher.db.DataBaseHelper.java
com.altinn.apps.fisher.db.FactoryDBHelper.java
com.altinn.apps.fisher.db.FishCategoryDBHelper.java
com.altinn.apps.fisher.db.FormDBHelper.java
com.altinn.apps.fisher.db.IDBHelper.java
com.altinn.apps.fisher.db.RegsDBHelper.java
com.altinn.apps.fisher.db.VesselDBHelper.java
com.altinn.apps.fisher.gps.CLocationProvider.java
com.altinn.apps.fisher.gps.ILocationUpdateListner.java
com.altinn.apps.fisher.models.CaughtInfoData.java
com.altinn.apps.fisher.models.InfoData.java
com.altinn.apps.fisher.models.ReportInfoData.java
com.altinn.apps.fisher.models.UserProfile.java
com.altinn.apps.fisher.net.AbstractWorkerTask.java
com.altinn.apps.fisher.net.CookieHelper.java
com.altinn.apps.fisher.net.IParser.java
com.altinn.apps.fisher.net.JSParser.java
com.altinn.apps.fisher.net.ParseManager.java
com.altinn.apps.fisher.net.TaskNotifier.java
com.altinn.apps.fisher.net.jsobj.AttachmentObj.java
com.altinn.apps.fisher.net.jsobj.FormObj.java
com.altinn.apps.fisher.net.jsobj.JSConstants.java
com.altinn.apps.fisher.net.jsobj.JsonObj.java
com.altinn.apps.fisher.net.jsobj.LinkItemObj.java
com.altinn.apps.fisher.net.jsobj.LinkObj.java
com.altinn.apps.fisher.net.jsobj.MessageObj.java
com.altinn.apps.fisher.net.jsobj.MessagesEmbedded.java
com.altinn.apps.fisher.net.jsobj.OrganisationObj.java
com.altinn.apps.fisher.net.tasks.LoginTask.java
com.altinn.apps.fisher.net.tasks.RefreshTokenTask.java
com.altinn.apps.fisher.net.tasks.SendReportTask.java
com.altinn.apps.fisher.net.tasks.UserProfileTask.java
com.altinn.apps.fisher.settings.FactoryDetails.java
com.altinn.apps.fisher.settings.FishDetails.java
com.altinn.apps.fisher.settings.SettingItem.java
com.altinn.apps.fisher.settings.VesselsDetails.java
com.altinn.apps.fisher.ui.component.DurationTimePickDialog.java
com.altinn.apps.fisher.ui.component.RAutoCompleteTextView.java
com.altinn.apps.fisher.ui.component.RButton.java
com.altinn.apps.fisher.ui.component.REditText.java
com.altinn.apps.fisher.ui.component.RTextView.java
com.altinn.apps.fisher.ui.screen.BaseActivity.java
com.altinn.apps.fisher.ui.screen.BrowserActivity.java
com.altinn.apps.fisher.ui.screen.FactoryDetailsActivity.java
com.altinn.apps.fisher.ui.screen.HomeActivity.java
com.altinn.apps.fisher.ui.screen.InformationActivity.java
com.altinn.apps.fisher.ui.screen.MenuNavigationActivity.java
com.altinn.apps.fisher.ui.screen.ReportActivity.java
com.altinn.apps.fisher.ui.screen.ReportReceivedFishActivity.java
com.altinn.apps.fisher.ui.screen.ReportSendDetailActivity.java
com.altinn.apps.fisher.ui.screen.SplashActivity.java
com.altinn.apps.fisher.ui.screen.UserProfileActivity.java
com.altinn.apps.fisher.utils.PreferenceUtils.java
com.altinn.apps.fisher.utils.Utils.java
net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java