Merge remote-tracking branch 'remotes/upstream/externalSD2' into beta
[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.Activity;
24 import android.app.Application;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.content.pm.PackageInfo;
29 import android.content.pm.PackageManager;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.os.Environment;
33 import android.preference.PreferenceManager;
34
35 import com.owncloud.android.authentication.PassCodeManager;
36 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
37 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
38 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
39 import com.owncloud.android.lib.common.utils.Log_OC;
40 import com.owncloud.android.utils.ExceptionHandler;
41
42
43 /**
44 * Main Application of the project
45 *
46 * Contains methods to build the "static" strings. These strings were before constants in different
47 * classes
48 */
49 public class MainApp extends Application {
50
51 private static final String TAG = MainApp.class.getSimpleName();
52
53 private static final String AUTH_ON = "on";
54
55 @SuppressWarnings("unused")
56 private static final String POLICY_SINGLE_SESSION_PER_ACCOUNT = "single session per account";
57 @SuppressWarnings("unused")
58 private static final String POLICY_ALWAYS_NEW_CLIENT = "always new client";
59
60 private static Context mContext;
61
62 private static String storagePath;
63
64 private static boolean mOnlyOnDevice = false;
65
66
67 public void onCreate(){
68 super.onCreate();
69 MainApp.mContext = getApplicationContext();
70
71 // Setup handler for uncaught exceptions.
72 Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
73
74
75 SharedPreferences appPrefs =
76 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
77 MainApp.storagePath = appPrefs.getString("storage_path", Environment.
78 getExternalStorageDirectory().getAbsolutePath());
79
80 boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
81
82 OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
83 if (isSamlAuth) {
84 OwnCloudClientManagerFactory.setDefaultPolicy(Policy.SINGLE_SESSION_PER_ACCOUNT);
85 } else {
86 OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
87 }
88
89 // initialise thumbnails cache on background thread
90 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
91
92 if (BuildConfig.DEBUG) {
93
94 String dataFolder = getDataFolder();
95
96 // Set folder for store logs
97 Log_OC.setLogDataFolder(dataFolder);
98
99 Log_OC.startLogging(MainApp.storagePath);
100 Log_OC.d("Debug", "start logging");
101 }
102
103 // register global protection with pass code
104 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
105 this.registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
106
107 @Override
108 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
109 Log_OC.d(activity.getClass().getSimpleName(), "onCreate(Bundle) starting" );
110 PassCodeManager.getPassCodeManager().onActivityCreated(activity);
111 }
112
113 @Override
114 public void onActivityStarted(Activity activity) {
115 Log_OC.d(activity.getClass().getSimpleName(), "onStart() starting" );
116 PassCodeManager.getPassCodeManager().onActivityStarted(activity);
117 }
118
119 @Override
120 public void onActivityResumed(Activity activity) {
121 Log_OC.d(activity.getClass().getSimpleName(), "onResume() starting" );
122 }
123
124 @Override
125 public void onActivityPaused(Activity activity) {
126 Log_OC.d(activity.getClass().getSimpleName(), "onPause() ending");
127 }
128
129 @Override
130 public void onActivityStopped(Activity activity) {
131 Log_OC.d(activity.getClass().getSimpleName(), "onStop() ending" );
132 PassCodeManager.getPassCodeManager().onActivityStopped(activity);
133 }
134
135 @Override
136 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
137 Log_OC.d(activity.getClass().getSimpleName(), "onSaveInstanceState(Bundle) starting" );
138 }
139
140 @Override
141 public void onActivityDestroyed(Activity activity) {
142 Log_OC.d(activity.getClass().getSimpleName(), "onDestroy() ending" );
143 }
144 });
145 }
146 }
147
148 public static Context getAppContext() {
149 return MainApp.mContext;
150 }
151
152 public static String getStoragePath(){
153 return MainApp.storagePath;
154 }
155
156 public static void setStoragePath(String path){
157 MainApp.storagePath = path;
158 }
159
160 // Methods to obtain Strings referring app_name
161 // From AccountAuthenticator
162 // public static final String ACCOUNT_TYPE = "owncloud";
163 public static String getAccountType() {
164 return getAppContext().getResources().getString(R.string.account_type);
165 }
166
167 // From AccountAuthenticator
168 // public static final String AUTHORITY = "org.owncloud";
169 public static String getAuthority() {
170 return getAppContext().getResources().getString(R.string.authority);
171 }
172
173 // From AccountAuthenticator
174 // public static final String AUTH_TOKEN_TYPE = "org.owncloud";
175 public static String getAuthTokenType() {
176 return getAppContext().getResources().getString(R.string.authority);
177 }
178
179 // From ProviderMeta
180 // public static final String DB_FILE = "owncloud.db";
181 public static String getDBFile() {
182 return getAppContext().getResources().getString(R.string.db_file);
183 }
184
185 // From ProviderMeta
186 // private final String mDatabaseName = "ownCloud";
187 public static String getDBName() {
188 return getAppContext().getResources().getString(R.string.db_name);
189 }
190
191 // data_folder
192 public static String getDataFolder() {
193 return getAppContext().getResources().getString(R.string.data_folder);
194 }
195
196 // log_name
197 public static String getLogName() {
198 return getAppContext().getResources().getString(R.string.log_name);
199 }
200
201 public static void showOnlyFilesOnDevice(boolean state){
202 mOnlyOnDevice = state;
203 }
204
205 public static boolean getOnlyOnDevice(){
206 return mOnlyOnDevice;
207 }
208
209 // user agent
210 public static String getUserAgent() {
211 String appString = getAppContext().getResources().getString(R.string.user_agent);
212 String packageName = getAppContext().getPackageName();
213 String version = "";
214
215 PackageInfo pInfo = null;
216 try {
217 pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
218 if (pInfo != null) {
219 version = pInfo.versionName;
220 }
221 } catch (PackageManager.NameNotFoundException e) {
222 Log_OC.e(TAG, "Trying to get packageName", e.getCause());
223 }
224
225 // Mozilla/5.0 (Android) ownCloud-android/1.7.0
226 String userAgent = String.format(appString, version);
227
228 return userAgent;
229 }
230 }