2  *   ownCloud Android client application 
   5  *   @author David A. Velasco 
   6  *   Copyright (C) 2015 ownCloud Inc. 
   8  *   This program is free software: you can redistribute it and/or modify 
   9  *   it under the terms of the GNU General Public License version 2, 
  10  *   as published by the Free Software Foundation. 
  12  *   This program is distributed in the hope that it will be useful, 
  13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15  *   GNU General Public License for more details. 
  17  *   You should have received a copy of the GNU General Public License 
  18  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  21 package com
.owncloud
.android
; 
  23 import android
.app
.Activity
; 
  24 import android
.app
.Application
; 
  25 import android
.content
.Context
; 
  26 import android
.content
.pm
.PackageInfo
; 
  27 import android
.content
.pm
.PackageManager
; 
  28 import android
.os
.Build
; 
  29 import android
.os
.Bundle
; 
  31 import com
.owncloud
.android
.authentication
.PassCodeManager
; 
  32 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
; 
  33 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
; 
  34 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
.Policy
; 
  35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  39  * Main Application of the project 
  41  * Contains methods to build the "static" strings. These strings were before constants in different 
  44 public class MainApp 
extends Application 
{ 
  46     private static final String TAG 
= MainApp
.class.getSimpleName(); 
  48     private static final String AUTH_ON 
= "on"; 
  50     @SuppressWarnings("unused") 
  51     private static final String POLICY_SINGLE_SESSION_PER_ACCOUNT 
= "single session per account"; 
  52     @SuppressWarnings("unused") 
  53     private static final String POLICY_ALWAYS_NEW_CLIENT 
= "always new client"; 
  55     private static Context mContext
; 
  57     // TODO Enable when "On Device" is recovered? 
  59     // private static boolean mOnlyOnDevice = false; 
  62     public void onCreate(){ 
  64         MainApp
.mContext 
= getApplicationContext(); 
  66         boolean isSamlAuth 
= AUTH_ON
.equals(getString(R
.string
.auth_method_saml_web_sso
)); 
  68         OwnCloudClientManagerFactory
.setUserAgent(getUserAgent()); 
  70             OwnCloudClientManagerFactory
.setDefaultPolicy(Policy
.SINGLE_SESSION_PER_ACCOUNT
); 
  72             OwnCloudClientManagerFactory
.setDefaultPolicy(Policy
.ALWAYS_NEW_CLIENT
); 
  75         // initialise thumbnails cache on background thread 
  76         new ThumbnailsCacheManager
.InitDiskCacheTask().execute(); 
  78         if (BuildConfig
.DEBUG
) { 
  80             String dataFolder 
= getDataFolder(); 
  82             // Set folder for store logs 
  83             Log_OC
.setLogDataFolder(dataFolder
); 
  85             Log_OC
.startLogging(); 
  86             Log_OC
.d("Debug", "start logging"); 
  89         // register global protection with pass code 
  90         if (Build
.VERSION
.SDK_INT 
>= Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
) { 
  91             this.registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() { 
  94                 public void onActivityCreated(Activity activity
, Bundle savedInstanceState
) { 
  95                     Log_OC
.d(activity
.getClass().getSimpleName(),  "onCreate(Bundle) starting" ); 
  96                     PassCodeManager
.getPassCodeManager().onActivityCreated(activity
); 
 100                 public void onActivityStarted(Activity activity
) { 
 101                     Log_OC
.d(activity
.getClass().getSimpleName(),  "onStart() starting" ); 
 102                     PassCodeManager
.getPassCodeManager().onActivityStarted(activity
); 
 106                 public void onActivityResumed(Activity activity
) { 
 107                     Log_OC
.d(activity
.getClass().getSimpleName(), "onResume() starting" ); 
 111                 public void onActivityPaused(Activity activity
) { 
 112                     Log_OC
.d(activity
.getClass().getSimpleName(), "onPause() ending"); 
 116                 public void onActivityStopped(Activity activity
) { 
 117                     Log_OC
.d(activity
.getClass().getSimpleName(), "onStop() ending" ); 
 118                     PassCodeManager
.getPassCodeManager().onActivityStopped(activity
); 
 122                 public void onActivitySaveInstanceState(Activity activity
, Bundle outState
) { 
 123                     Log_OC
.d(activity
.getClass().getSimpleName(), "onSaveInstanceState(Bundle) starting" ); 
 127                 public void onActivityDestroyed(Activity activity
) { 
 128                     Log_OC
.d(activity
.getClass().getSimpleName(), "onDestroy() ending" ); 
 134     public static Context 
getAppContext() { 
 135         return MainApp
.mContext
; 
 138     // Methods to obtain Strings referring app_name  
 139     //   From AccountAuthenticator  
 140     //   public static final String ACCOUNT_TYPE = "owncloud";     
 141     public static String 
getAccountType() { 
 142         return getAppContext().getResources().getString(R
.string
.account_type
); 
 145     //  From AccountAuthenticator  
 146     //  public static final String AUTHORITY = "org.owncloud"; 
 147     public static String 
getAuthority() { 
 148         return getAppContext().getResources().getString(R
.string
.authority
); 
 151     //  From AccountAuthenticator 
 152     //  public static final String AUTH_TOKEN_TYPE = "org.owncloud"; 
 153     public static String 
getAuthTokenType() { 
 154         return getAppContext().getResources().getString(R
.string
.authority
); 
 158     //  public static final String DB_FILE = "owncloud.db"; 
 159     public static String 
getDBFile() { 
 160         return getAppContext().getResources().getString(R
.string
.db_file
); 
 164     //  private final String mDatabaseName = "ownCloud"; 
 165     public static String 
getDBName() { 
 166         return getAppContext().getResources().getString(R
.string
.db_name
); 
 170     public static String 
getDataFolder() { 
 171         return getAppContext().getResources().getString(R
.string
.data_folder
); 
 175     public static String 
getLogName() { 
 176         return getAppContext().getResources().getString(R
.string
.log_name
); 
 179     // TODO Enable when "On Device" is recovered ? 
 180 //    public static void showOnlyFilesOnDevice(boolean state){ 
 181 //        mOnlyOnDevice = state; 
 184 //    public static boolean getOnlyOnDevice(){ 
 185 //        return mOnlyOnDevice; 
 189     public static String 
getUserAgent() { 
 190         String appString 
= getAppContext().getResources().getString(R
.string
.user_agent
); 
 191         String packageName 
= getAppContext().getPackageName(); 
 194         PackageInfo pInfo 
= null
; 
 196             pInfo 
= getAppContext().getPackageManager().getPackageInfo(packageName
, 0); 
 198                 version 
= pInfo
.versionName
; 
 200         } catch (PackageManager
.NameNotFoundException e
) { 
 201             Log_OC
.e(TAG
, "Trying to get packageName", e
.getCause()); 
 204         // Mozilla/5.0 (Android) ownCloud-android/1.7.0 
 205         String userAgent 
= String
.format(appString
, version
);