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