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