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