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