MainApp.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Application;
  23. import android.content.Context;
  24. import android.content.pm.PackageInfo;
  25. import android.content.pm.PackageManager;
  26. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  27. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  28. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
  29. import com.owncloud.android.lib.common.utils.Log_OC;
  30. /**
  31. * Main Application of the project
  32. *
  33. * Contains methods to build the "static" strings. These strings were before constants in different
  34. * classes
  35. */
  36. public class MainApp extends Application {
  37. private static final String AUTH_ON = "on";
  38. @SuppressWarnings("unused")
  39. private static final String POLICY_SINGLE_SESSION_PER_ACCOUNT = "single session per account";
  40. @SuppressWarnings("unused")
  41. private static final String POLICY_ALWAYS_NEW_CLIENT = "always new client";
  42. private static Context mContext;
  43. public void onCreate(){
  44. super.onCreate();
  45. MainApp.mContext = getApplicationContext();
  46. boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
  47. OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
  48. if (isSamlAuth) {
  49. OwnCloudClientManagerFactory.setDefaultPolicy(Policy.SINGLE_SESSION_PER_ACCOUNT);
  50. } else {
  51. OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
  52. }
  53. // initialise thumbnails cache on background thread
  54. new ThumbnailsCacheManager.InitDiskCacheTask().execute();
  55. if (BuildConfig.DEBUG) {
  56. String dataFolder = getDataFolder();
  57. // Set folder for store logs
  58. Log_OC.setLogDataFolder(dataFolder);
  59. Log_OC.startLogging();
  60. Log_OC.d("Debug", "start logging");
  61. }
  62. }
  63. public static Context getAppContext() {
  64. return MainApp.mContext;
  65. }
  66. // Methods to obtain Strings referring app_name
  67. // From AccountAuthenticator
  68. // public static final String ACCOUNT_TYPE = "owncloud";
  69. public static String getAccountType() {
  70. return getAppContext().getResources().getString(R.string.account_type);
  71. }
  72. // From AccountAuthenticator
  73. // public static final String AUTHORITY = "org.owncloud";
  74. public static String getAuthority() {
  75. return getAppContext().getResources().getString(R.string.authority);
  76. }
  77. // From AccountAuthenticator
  78. // public static final String AUTH_TOKEN_TYPE = "org.owncloud";
  79. public static String getAuthTokenType() {
  80. return getAppContext().getResources().getString(R.string.authority);
  81. }
  82. // From ProviderMeta
  83. // public static final String DB_FILE = "owncloud.db";
  84. public static String getDBFile() {
  85. return getAppContext().getResources().getString(R.string.db_file);
  86. }
  87. // From ProviderMeta
  88. // private final String mDatabaseName = "ownCloud";
  89. public static String getDBName() {
  90. return getAppContext().getResources().getString(R.string.db_name);
  91. }
  92. // data_folder
  93. public static String getDataFolder() {
  94. return getAppContext().getResources().getString(R.string.data_folder);
  95. }
  96. // log_name
  97. public static String getLogName() {
  98. return getAppContext().getResources().getString(R.string.log_name);
  99. }
  100. // user agent
  101. public static String getUserAgent() {
  102. String appString = getAppContext().getResources().getString(R.string.user_agent);
  103. String packageName = getAppContext().getPackageName();
  104. PackageInfo pInfo = null;
  105. try {
  106. pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
  107. } catch (PackageManager.NameNotFoundException e) {
  108. }
  109. String version = "";
  110. if (pInfo != null) {
  111. version = pInfo.versionName;
  112. }
  113. // Mozilla/5.0 (Android) ownCloud /1.7.0
  114. String userAgent = "Mozilla/5.0 "+ appString + "/" + version;
  115. return userAgent;
  116. }
  117. }