Merge branch 'share_link__new_share' into share_link__new_share_menu
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
index 31e3e86..4f42597 100644 (file)
 
 package com.owncloud.android.ui.activity;
 
 
 package com.owncloud.android.ui.activity;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.protocol.HTTP;
+
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerCallback;
 import android.accounts.AccountManagerFuture;
 import android.accounts.OperationCanceledException;
 import android.content.Intent;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerCallback;
 import android.accounts.AccountManagerFuture;
 import android.accounts.OperationCanceledException;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Bundle;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Parcelable;
 import android.webkit.MimeTypeMap;
 import android.webkit.MimeTypeMap;
+import android.widget.Toast;
 
 import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 
 import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
-import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
+
+import com.owncloud.android.lib.accounts.OwnCloudAccount;
+import com.owncloud.android.lib.network.webdav.WebdavUtils;
+
 import com.owncloud.android.utils.Log_OC;
 
 
 import com.owncloud.android.utils.Log_OC;
 
 
@@ -71,10 +82,6 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
     
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
     
-    /** Flag to signal if the server supports the Share API */
-    private boolean mIsSharedSupported;
-    
-    
 
     
     /**
 
     
     /**
@@ -87,7 +94,6 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-
         Account account;
         if(savedInstanceState != null) {
             account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
         Account account;
         if(savedInstanceState != null) {
             account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
@@ -100,6 +106,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
         }
 
         setAccount(account, savedInstanceState != null);
         }
 
         setAccount(account, savedInstanceState != null);
+       
     }
 
     
     }
 
     
@@ -164,7 +171,6 @@ public abstract class FileActivity extends SherlockFragmentActivity {
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
-        AccountManager accountManager = AccountManager.get(this);
         Account newAccount  = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
         if (newAccount == null) {
             /// no account available: force account creation
         Account newAccount  = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
         if (newAccount == null) {
             /// no account available: force account creation
@@ -178,7 +184,6 @@ public abstract class FileActivity extends SherlockFragmentActivity {
             mAccountWasRestored = (newAccount.equals(mAccount));
             mAccount = newAccount;
         }
             mAccountWasRestored = (newAccount.equals(mAccount));
             mAccount = newAccount;
         }
-        setIsSharedSupported( Boolean.getBoolean(accountManager.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)));
     }
 
 
     }
 
 
@@ -257,14 +262,13 @@ public abstract class FileActivity extends SherlockFragmentActivity {
      *  @return 'True' if the server supports the Share API
      */
     public boolean isSharedSupported() {
      *  @return 'True' if the server supports the Share API
      */
     public boolean isSharedSupported() {
-        return mIsSharedSupported;
+        if (getAccount() != null) {
+            AccountManager accountManager = AccountManager.get(this);
+            return Boolean.parseBoolean(accountManager.getUserData(getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
+        }
+        return false;
     }
 
     }
 
-
-    public void setIsSharedSupported(boolean mIsSharedSupported) {
-        this.mIsSharedSupported = mIsSharedSupported;
-    }
-    
     /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
     /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
@@ -348,5 +352,88 @@ public abstract class FileActivity extends SherlockFragmentActivity {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
         }
     }
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
         }
     }
+
+    /*
+    public void shareFileWithLink(OCFile file) {
+        if (file != null) {
+            
+            Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
+            intentToShareLink.putExtra(Intent.EXTRA_TEXT, "https://fake.url.lolo");
+            intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
+            
+            Intent chooserIntent = Intent.createChooser(intentToShareLink, getString(R.string.action_share_file));
+            startActivity(chooserIntent);
+            
+        } else {
+            Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
+        }
+    }
+    */
+    
+    public void shareFileWithLink(OCFile file) {
+        if (isSharedSupported()) {
+            if (file != null) {
+                
+                // Create the Share
+                //CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1);
+                //createShare.execute(getStorageManager(), this, this, mHandler, this);
+                        
+                // TODO
+                // Get the link --> when the operation is finished
+                        
+                String link = "https://fake.url.lolo";
+                Intent chooserIntent = null;
+                List<Intent> targetedShareIntents = new ArrayList<Intent>();
+                List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(createShareWithLinkIntent(link), PackageManager.MATCH_DEFAULT_ONLY);
+                String myPackageName = getPackageName();
+                if (!resInfo.isEmpty()) {
+                    for (ResolveInfo info : resInfo) {
+                        if (!info.activityInfo.packageName.equalsIgnoreCase(myPackageName)) {
+                            Intent targetedShare = createTargetedShare(link, info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
+                            targetedShareIntents.add(targetedShare);
+                        }
+                    }
+                }
+                if (targetedShareIntents.size() > 0) {
+                    Intent firstTargeted = targetedShareIntents.remove(0);
+                    chooserIntent = Intent.createChooser(firstTargeted, getString(R.string.action_share_file));
+                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));
+                } else {
+                    // to show standard message
+                    chooserIntent = Intent.createChooser(null, getString(R.string.action_share_file));
+                }
+                startActivity(chooserIntent);
+                
+            } else {
+                Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
+            }
+            
+        } else {
+            // Show a Message
+            Toast t = Toast.makeText(this, getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
+            t.show();
+        }
+    }
+    
+    private Intent createTargetedShare(String link, String packageName, String className) {
+        //Intent targetedShare = createShareWithLinkIntent(link);
+        Intent targetedShare=new Intent(Intent.ACTION_MAIN);
+
+        targetedShare.addCategory(Intent.CATEGORY_LAUNCHER);
+        targetedShare.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
+                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);        
+        Log_OC.e("LOLO", "className: " + className + "\npackageName: " + packageName + "\n");
+        targetedShare.setClassName(packageName, className);
+        return targetedShare;
+    }
+
+
+    private Intent createShareWithLinkIntent(String link) {
+        Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
+        intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
+        intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
+        return intentToShareLink; 
+    }
+    
     
 }
     
 }