MainApp.java 4.9 KB

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