Prevent that PIN code is requested when system windows partially overlapping the...
[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.pm.PackageInfo;
28 import android.content.pm.PackageManager;
29 import android.os.Build;
30 import android.os.Bundle;
31
32 import com.owncloud.android.authentication.AuthenticatorActivity;
33 import com.owncloud.android.authentication.PassCodeManager;
34 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
35 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
36 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
37 import com.owncloud.android.lib.common.utils.Log_OC;
38 import com.owncloud.android.ui.activity.PinCodeActivity;
39
40 import java.util.HashSet;
41 import java.util.Set;
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 public void onCreate(){
63 super.onCreate();
64 MainApp.mContext = getApplicationContext();
65
66 boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
67
68 OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
69 if (isSamlAuth) {
70 OwnCloudClientManagerFactory.setDefaultPolicy(Policy.SINGLE_SESSION_PER_ACCOUNT);
71 } else {
72 OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
73 }
74
75 // initialise thumbnails cache on background thread
76 new ThumbnailsCacheManager.InitDiskCacheTask().execute();
77
78 if (BuildConfig.DEBUG) {
79
80 String dataFolder = getDataFolder();
81
82 // Set folder for store logs
83 Log_OC.setLogDataFolder(dataFolder);
84
85 Log_OC.startLogging();
86 Log_OC.d("Debug", "start logging");
87 }
88
89 // register global protection with pass code
90 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
91 this.registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
92
93 @Override
94 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
95 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onCreate(Bundle)" );
96 }
97
98 @Override
99 public void onActivityStarted(Activity activity) {
100 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onStart()" );
101 PassCodeManager.getPassCodeManager().onActivityStarted(activity);
102 }
103
104 @Override
105 public void onActivityResumed(Activity activity) {
106 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onResume()" );
107 }
108
109 @Override
110 public void onActivityPaused(Activity activity) {
111 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onPause()");
112 }
113
114 @Override
115 public void onActivityStopped(Activity activity) {
116 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onStop()" );
117 PassCodeManager.getPassCodeManager().onActivityStopped(activity);
118 }
119
120 @Override
121 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
122 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onSaveInstanceState(Bundle)" );
123
124 }
125
126 @Override
127 public void onActivityDestroyed(Activity activity) {
128 Log_OC.d(TAG, activity.getClass().getSimpleName() + " in onDestroy()" );
129
130 }
131 });
132 }
133 }
134
135 public static Context getAppContext() {
136 return MainApp.mContext;
137 }
138
139 // Methods to obtain Strings referring app_name
140 // From AccountAuthenticator
141 // public static final String ACCOUNT_TYPE = "owncloud";
142 public static String getAccountType() {
143 return getAppContext().getResources().getString(R.string.account_type);
144 }
145
146 // From AccountAuthenticator
147 // public static final String AUTHORITY = "org.owncloud";
148 public static String getAuthority() {
149 return getAppContext().getResources().getString(R.string.authority);
150 }
151
152 // From AccountAuthenticator
153 // public static final String AUTH_TOKEN_TYPE = "org.owncloud";
154 public static String getAuthTokenType() {
155 return getAppContext().getResources().getString(R.string.authority);
156 }
157
158 // From ProviderMeta
159 // public static final String DB_FILE = "owncloud.db";
160 public static String getDBFile() {
161 return getAppContext().getResources().getString(R.string.db_file);
162 }
163
164 // From ProviderMeta
165 // private final String mDatabaseName = "ownCloud";
166 public static String getDBName() {
167 return getAppContext().getResources().getString(R.string.db_name);
168 }
169
170 // data_folder
171 public static String getDataFolder() {
172 return getAppContext().getResources().getString(R.string.data_folder);
173 }
174
175 // log_name
176 public static String getLogName() {
177 return getAppContext().getResources().getString(R.string.log_name);
178 }
179
180 // user agent
181 public static String getUserAgent() {
182 String appString = getAppContext().getResources().getString(R.string.user_agent);
183 String packageName = getAppContext().getPackageName();
184 String version = "";
185
186 PackageInfo pInfo = null;
187 try {
188 pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
189 if (pInfo != null) {
190 version = pInfo.versionName;
191 }
192 } catch (PackageManager.NameNotFoundException e) {
193 Log_OC.e(TAG, "Trying to get packageName", e.getCause());
194 }
195
196 // Mozilla/5.0 (Android) ownCloud-android/1.7.0
197 String userAgent = String.format(appString, version);
198
199 return userAgent;
200 }
201 }