Added filtered chooser dialog for activities that can receive a link
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
index 2e47ce0..07c5b53 100644 (file)
 
 package com.owncloud.android.ui.activity;
 
+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.support.v4.app.DialogFragment;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
 import android.webkit.MimeTypeMap;
+import android.widget.Toast;
 
 import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.owncloud.android.Log_OC;
+import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountAuthenticator;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 
-import eu.alefzero.webdav.WebdavUtils;
+import com.owncloud.android.lib.accounts.OwnCloudAccount;
+import com.owncloud.android.lib.network.webdav.WebdavUtils;
+
+import com.owncloud.android.ui.dialog.ActivityChooserDialog;
+import com.owncloud.android.utils.Log_OC;
+
 
 /**
  * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
@@ -47,8 +55,11 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
     public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
     public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
+    public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
     
-    public static final String TAG = FileActivity.class.getSimpleName(); 
+    public static final String TAG = FileActivity.class.getSimpleName();
+    
+    private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; 
     
     
     /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
@@ -65,6 +76,10 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     
     /** Flag to signal when the value of mAccount was restored from a saved state */ 
     private boolean mAccountWasRestored;
+    
+    /** Flag to signal if the activity is launched by a notification */
+    private boolean mFromNotification;
+    
 
     
     /**
@@ -77,17 +92,19 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-
         Account account;
         if(savedInstanceState != null) {
             account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
             mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
+            mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
         } else {
             account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
             mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
+            mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
         }
 
         setAccount(account, savedInstanceState != null);
+       
     }
 
     
@@ -173,7 +190,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
      */
     private void createFirstAccount() {
         AccountManager am = AccountManager.get(getApplicationContext());
-        am.addAccount(AccountAuthenticator.ACCOUNT_TYPE
+        am.addAccount(MainApp.getAccountType()
                         null,
                         null, 
                         null, 
@@ -191,6 +208,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
         super.onSaveInstanceState(outState);
         outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
         outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
+        outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
     }
     
     
@@ -223,6 +241,12 @@ public abstract class FileActivity extends SherlockFragmentActivity {
         return mAccount;
     }
 
+    /**
+     * @return Value of mFromNotification: True if the Activity is launched by a notification
+     */
+    public boolean fromNotification() {
+        return mFromNotification;
+    }
     
     /**
      * @return  'True' when the Activity is finishing to enforce the setup of a new account.
@@ -233,6 +257,17 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     
     
     /**
+     *  @return 'True' if the server supports the Share API
+     */
+    public boolean isSharedSupported() {
+        if (getAccount() != null) {
+            AccountManager accountManager = AccountManager.get(this);
+            return Boolean.parseBoolean(accountManager.getUserData(getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
+        }
+        return false;
+    }
+
+    /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
      * 
@@ -315,5 +350,57 @@ public abstract class FileActivity extends SherlockFragmentActivity {
             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 - TODO integrate before or after the chooser menu
+                //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 intent = createShareWithLinkIntent(link);
+                String[] packagesToExclude = new String[] { getPackageName() };
+                DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude);
+                chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
+                
+            } 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 createShareWithLinkIntent(String link) {
+        Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
+        intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
+        intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
+        return intentToShareLink; 
+    }
+    
     
 }