MainApp.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. *
  20. */
  21. package com.owncloud.android;
  22. import android.app.Activity;
  23. import android.app.Application;
  24. import android.content.ComponentName;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.ServiceConnection;
  28. import android.content.SharedPreferences;
  29. import android.content.pm.PackageInfo;
  30. import android.content.pm.PackageManager;
  31. import android.os.Bundle;
  32. import android.os.Environment;
  33. import android.os.IBinder;
  34. import android.preference.PreferenceManager;
  35. import com.owncloud.android.authentication.PassCodeManager;
  36. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  37. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  38. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
  39. import com.owncloud.android.lib.common.utils.Log_OC;
  40. import com.owncloud.android.services.observer.SyncedFolderObserverService;
  41. import com.owncloud.android.ui.activity.Preferences;
  42. import com.owncloud.android.ui.activity.WhatsNewActivity;
  43. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  44. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  45. /**
  46. * Main Application of the project
  47. *
  48. * Contains methods to build the "static" strings. These strings were before constants in different
  49. * classes
  50. */
  51. public class MainApp extends Application {
  52. private static final String TAG = MainApp.class.getSimpleName();
  53. private static final String AUTH_ON = "on";
  54. @SuppressWarnings("unused")
  55. private static final String POLICY_SINGLE_SESSION_PER_ACCOUNT = "single session per account";
  56. @SuppressWarnings("unused")
  57. private static final String POLICY_ALWAYS_NEW_CLIENT = "always new client";
  58. private static Context mContext;
  59. private static String storagePath;
  60. private static boolean mOnlyOnDevice = false;
  61. private static SyncedFolderObserverService mObserverService;
  62. @SuppressWarnings("unused")
  63. private boolean mBound;
  64. @SuppressFBWarnings("ST") public void onCreate(){
  65. super.onCreate();
  66. MainApp.mContext = getApplicationContext();
  67. SharedPreferences appPrefs =
  68. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  69. MainApp.storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, Environment.
  70. getExternalStorageDirectory().getAbsolutePath());
  71. boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
  72. OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
  73. if (isSamlAuth) {
  74. OwnCloudClientManagerFactory.setDefaultPolicy(Policy.SINGLE_SESSION_PER_ACCOUNT);
  75. } else {
  76. OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
  77. }
  78. // initialise thumbnails cache on background thread
  79. new ThumbnailsCacheManager.InitDiskCacheTask().execute();
  80. if (BuildConfig.DEBUG) {
  81. String dataFolder = getDataFolder();
  82. // Set folder for store logs
  83. Log_OC.setLogDataFolder(dataFolder);
  84. Log_OC.startLogging(MainApp.storagePath);
  85. Log_OC.d("Debug", "start logging");
  86. }
  87. Log_OC.d("SyncedFolderObserverService", "Start service SyncedFolderObserverService");
  88. Intent i = new Intent(this, SyncedFolderObserverService.class);
  89. startService(i);
  90. bindService(i, syncedFolderObserverServiceConnection, Context.BIND_AUTO_CREATE);
  91. // register global protection with pass code
  92. registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
  93. @Override
  94. public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
  95. Log_OC.d(activity.getClass().getSimpleName(), "onCreate(Bundle) starting" );
  96. WhatsNewActivity.runIfNeeded(activity);
  97. PassCodeManager.getPassCodeManager().onActivityCreated(activity);
  98. }
  99. @Override
  100. public void onActivityStarted(Activity activity) {
  101. Log_OC.d(activity.getClass().getSimpleName(), "onStart() starting" );
  102. PassCodeManager.getPassCodeManager().onActivityStarted(activity);
  103. }
  104. @Override
  105. public void onActivityResumed(Activity activity) {
  106. Log_OC.d(activity.getClass().getSimpleName(), "onResume() starting" );
  107. }
  108. @Override
  109. public void onActivityPaused(Activity activity) {
  110. Log_OC.d(activity.getClass().getSimpleName(), "onPause() ending");
  111. }
  112. @Override
  113. public void onActivityStopped(Activity activity) {
  114. Log_OC.d(activity.getClass().getSimpleName(), "onStop() ending" );
  115. PassCodeManager.getPassCodeManager().onActivityStopped(activity);
  116. }
  117. @Override
  118. public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
  119. Log_OC.d(activity.getClass().getSimpleName(), "onSaveInstanceState(Bundle) starting" );
  120. }
  121. @Override
  122. public void onActivityDestroyed(Activity activity) {
  123. Log_OC.d(activity.getClass().getSimpleName(), "onDestroy() ending" );
  124. }
  125. });
  126. }
  127. public static Context getAppContext() {
  128. return MainApp.mContext;
  129. }
  130. public static String getStoragePath(){
  131. return MainApp.storagePath;
  132. }
  133. public static void setStoragePath(String path){
  134. MainApp.storagePath = path;
  135. }
  136. // Methods to obtain Strings referring app_name
  137. // From AccountAuthenticator
  138. // public static final String ACCOUNT_TYPE = "owncloud";
  139. public static String getAccountType() {
  140. return getAppContext().getResources().getString(R.string.account_type);
  141. }
  142. // Non gradle build systems do not provide BuildConfig.VERSION_CODE
  143. // so we must fallback to this method :(
  144. public static int getVersionCode() {
  145. try {
  146. String thisPackageName = getAppContext().getPackageName();
  147. return getAppContext().getPackageManager().getPackageInfo(thisPackageName, 0).versionCode;
  148. } catch (PackageManager.NameNotFoundException e) {
  149. return 0;
  150. }
  151. }
  152. // From AccountAuthenticator
  153. // public static final String AUTHORITY = "org.owncloud";
  154. public static String getAuthority() {
  155. return getAppContext().getResources().getString(R.string.authority);
  156. }
  157. // From AccountAuthenticator
  158. // public static final String AUTH_TOKEN_TYPE = "org.owncloud";
  159. public static String getAuthTokenType() {
  160. return getAppContext().getResources().getString(R.string.authority);
  161. }
  162. // From ProviderMeta
  163. // public static final String DB_FILE = "owncloud.db";
  164. public static String getDBFile() {
  165. return getAppContext().getResources().getString(R.string.db_file);
  166. }
  167. // From ProviderMeta
  168. // private final String mDatabaseName = "ownCloud";
  169. public static String getDBName() {
  170. return getAppContext().getResources().getString(R.string.db_name);
  171. }
  172. /**
  173. * name of data_folder, e.g., "owncloud"
  174. */
  175. public static String getDataFolder() {
  176. return getAppContext().getResources().getString(R.string.data_folder);
  177. }
  178. // log_name
  179. public static String getLogName() {
  180. return getAppContext().getResources().getString(R.string.log_name);
  181. }
  182. public static void showOnlyFilesOnDevice(boolean state){
  183. mOnlyOnDevice = state;
  184. }
  185. public static boolean isOnlyOnDevice(){
  186. return mOnlyOnDevice;
  187. }
  188. public static SyncedFolderObserverService getSyncedFolderObserverService() {
  189. return mObserverService;
  190. }
  191. // user agent
  192. public static String getUserAgent() {
  193. String appString = getAppContext().getResources().getString(R.string.user_agent);
  194. String packageName = getAppContext().getPackageName();
  195. String version = "";
  196. PackageInfo pInfo = null;
  197. try {
  198. pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
  199. if (pInfo != null) {
  200. version = pInfo.versionName;
  201. }
  202. } catch (PackageManager.NameNotFoundException e) {
  203. Log_OC.e(TAG, "Trying to get packageName", e.getCause());
  204. }
  205. // Mozilla/5.0 (Android) ownCloud-android/1.7.0
  206. String userAgent = String.format(appString, version);
  207. return userAgent;
  208. }
  209. /** Defines callbacks for service binding, passed to bindService() */
  210. private ServiceConnection syncedFolderObserverServiceConnection = new ServiceConnection() {
  211. @Override
  212. public void onServiceConnected(ComponentName className, IBinder service) {
  213. SyncedFolderObserverService.SyncedFolderObserverBinder binder = (SyncedFolderObserverService.SyncedFolderObserverBinder) service;
  214. mObserverService = binder.getService();
  215. mBound = true;
  216. }
  217. @Override
  218. public void onServiceDisconnected(ComponentName arg0) {
  219. mBound = false;
  220. }
  221. };
  222. }