Refactor parameter user_agent out of RemoteOperation and children
[pub/Android/ownCloud.git] / src / com / owncloud / android / MainApp.java
index e04239d..bf82c57 100644 (file)
@@ -1,5 +1,9 @@
-/* ownCloud Android client application
- *   Copyright (C) 2012-2013 ownCloud Inc.
+/**
+ *   ownCloud Android client application
+ *
+ *   @author masensio
+ *   @author David A. Velasco
+ *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -18,6 +22,8 @@ package com.owncloud.android;
 
 import android.app.Application;
 import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
 
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@@ -28,9 +34,6 @@ import com.owncloud.android.lib.common.utils.Log_OC;
  * 
  * Contains methods to build the "static" strings. These strings were before constants in different
  * classes
- * 
- * @author masensio
- * @author David A. Velasco
  */
 public class MainApp extends Application {
     
@@ -48,14 +51,15 @@ public class MainApp extends Application {
         MainApp.mContext = getApplicationContext();
         
         boolean isSamlAuth = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
-        
+
+        OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
         if (isSamlAuth) {   
             OwnCloudClientManagerFactory.setDefaultPolicy(Policy.SINGLE_SESSION_PER_ACCOUNT);
             
         } else {
             OwnCloudClientManagerFactory.setDefaultPolicy(Policy.ALWAYS_NEW_CLIENT);
         }
-        
+
         // initialise thumbnails cache on background thread
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();
         
@@ -116,4 +120,24 @@ public class MainApp extends Application {
         return getAppContext().getResources().getString(R.string.log_name);
     }
 
+    // user agent
+    public static String getUserAgent() {
+        String appString = getAppContext().getResources().getString(R.string.user_agent);
+        String packageName = getAppContext().getPackageName();
+        PackageInfo pInfo = null;
+        try {
+            pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
+        } catch (PackageManager.NameNotFoundException e) {
+
+        }
+        String version = "";
+        if (pInfo != null) {
+            version = pInfo.versionName;
+        }
+
+        // Mozilla/5.0 (Android) ownCloud /1.7.0
+        String userAgent = "Mozilla/5.0 "+ appString + "/" + version;
+
+        return userAgent;
+    }
 }