Merge branch 'share_link__new_share' into share_link__unshare_file
authormasensio <masensio@solidgear.es>
Wed, 5 Feb 2014 10:25:39 +0000 (11:25 +0100)
committermasensio <masensio@solidgear.es>
Wed, 5 Feb 2014 10:25:39 +0000 (11:25 +0100)
1  2 
res/values/strings.xml
src/com/owncloud/android/datamodel/FileDataStorageManager.java
src/com/owncloud/android/files/FileOperationsHelper.java
src/com/owncloud/android/operations/GetSharesOperation.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java
src/com/owncloud/android/ui/fragment/OCFileListFragment.java
src/com/owncloud/android/ui/preview/PreviewImageFragment.java
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

Simple merge
@@@ -1099,19 -1100,30 +1100,44 @@@ public class FileDataStorageManager 
          }
          
      } 
 -
 -
 +    
 +    public void removeShare(OCShare share){
 +        Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
 +        String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
 +        String [] whereArgs = new String[]{mAccount.name, share.getPath()};
 +        if (getContentProviderClient() != null) {
 +            try {
 +                getContentProviderClient().delete(share_uri, where, whereArgs);
 +            } catch (RemoteException e) {
 +                e.printStackTrace();
 +            }
 +        } else {
 +            getContentResolver().delete(share_uri, where, whereArgs); 
 +        }
 +    }
++    
+     public void saveSharesDB(ArrayList<OCShare> shares) {
+         saveShares(shares);
+         ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
+         for (OCShare share : shares) {
+             // Get the path
+             String path = share.getPath();
+             if (share.isDirectory()) {
+                 path = path + FileUtils.PATH_SEPARATOR;
+             }           
+             // Update OCFile with data from share: ShareByLink  ¿and publicLink?
+             OCFile file = getFileByPath(path);
+             if (file != null) {
+                 if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
+                     file.setShareByLink(true);
+                     sharedFiles.add(file);
+                 }
+             } 
+         }
+         
+         updateSharedFiles(sharedFiles);
+     }
  }
index 0000000,ab8395c..1c0db87
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,143 +1,165 @@@
+ /* ownCloud Android client application
+  *   Copyright (C) 2012-2014 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,
+  *   as published by the Free Software Foundation.
+  *
+  *   This program is distributed in the hope that it will be useful,
+  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  *   GNU General Public License for more details.
+  *
+  *   You should have received a copy of the GNU General Public License
+  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  *
+  */
+ package com.owncloud.android.files;
+ import org.apache.http.protocol.HTTP;
+ import android.accounts.AccountManager;
+ import android.content.Intent;
+ import android.net.Uri;
+ import android.support.v4.app.DialogFragment;
+ import android.webkit.MimeTypeMap;
+ import android.widget.Toast;
+ import com.owncloud.android.R;
+ import com.owncloud.android.datamodel.OCFile;
+ import com.owncloud.android.lib.accounts.OwnCloudAccount;
+ import com.owncloud.android.lib.network.webdav.WebdavUtils;
+ import com.owncloud.android.lib.operations.common.ShareType;
+ import com.owncloud.android.operations.CreateShareOperation;
++import com.owncloud.android.operations.UnshareLinkOperation;
+ import com.owncloud.android.ui.activity.FileActivity;
+ import com.owncloud.android.ui.dialog.ActivityChooserDialog;
+ import com.owncloud.android.utils.Log_OC;
+ /**
+  * 
+  * @author masensio
+  * @author David A. Velasco
+  */
+ public class FileOperationsHelper {
+     private static final String TAG = FileOperationsHelper.class.getName();
+     
+     private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; 
+     
+     public void openFile(OCFile file, FileActivity callerActivity) {
+         if (file != null) {
+             String storagePath = file.getStoragePath();
+             String encodedStoragePath = WebdavUtils.encodePath(storagePath);
+             
+             Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
+             intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
+             intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+             
+             Intent intentForGuessedMimeType = null;
+             if (storagePath.lastIndexOf('.') >= 0) {
+                 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
+                 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
+                     intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
+                     intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
+                     intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+                 }
+             }
+             
+             Intent chooserIntent = null;
+             if (intentForGuessedMimeType != null) {
+                 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
+             } else {
+                 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
+             }
+             
+             callerActivity.startActivity(chooserIntent);
+             
+         } else {
+             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
+         }
+     }
+     
+     public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
+         
+         if (isSharedSupported(callerActivity)) {
+             if (file != null) {
+                 String link = "https://fake.url";
+                 Intent intent = createShareWithLinkIntent(link);
+                 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
+                 DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
+                 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
+                 
+             } else {
+                 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
+             }
+             
+         } else {
+             // Show a Message
+             Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
+             t.show();
+         }
+     }
+     
+     
+     public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
+         
+         if (file != null) {
+             callerActivity.showLoadingDialog();
+             CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent);
+             createShare.execute(callerActivity.getStorageManager(), 
+                                 callerActivity, 
+                                 callerActivity.getRemoteOperationListener(), 
+                                 callerActivity.getHandler(), 
+                                 callerActivity);
+             
+         } else {
+             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
+         }
+     }
+     
+     
+     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; 
+     }
+     
+     
+     /**
+      *  @return 'True' if the server supports the Share API
+      */
+     public boolean isSharedSupported(FileActivity callerActivity) {
+         if (callerActivity.getAccount() != null) {
+             AccountManager accountManager = AccountManager.get(callerActivity);
+             return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
+         }
+         return false;
+     }
++    
++    public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
++        
++        if (isSharedSupported(callerActivity)) {
++            // Unshare the file
++            UnshareLinkOperation unshare = new UnshareLinkOperation(file);
++            unshare.execute(callerActivity.getStorageManager(), 
++                    callerActivity, 
++                    callerActivity.getRemoteOperationListener(), 
++                    callerActivity.getHandler(), 
++                    callerActivity);
++         
++            callerActivity.showLoadingDialog();
++            
++        } else {
++            // Show a Message
++            Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
++            t.show();
++            
++        }
++    }
+ }
@@@ -1353,25 -1303,21 +1304,36 @@@ OCFileListFragment.ContainerActivity, F
  
          } else if (operation instanceof CreateFolderOperation) {
              onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
 -        
 +            
+         } else if (operation instanceof CreateShareOperation) {
+             onCreateShareOperationFinish((CreateShareOperation) operation, result);
 -        }
++            
 +        } else if (operation instanceof UnshareLinkOperation) {
 +            onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
-         }
++        
++        } 
+         
      }
  
 +    private void onUnshareLinkOperationFinish(UnshareLinkOperation operation, RemoteOperationResult result) {
 +        if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
 +            // Show a Message
 +            Toast t = Toast.makeText(this, getString(R.string.share_link_file_no_exist), Toast.LENGTH_LONG);
 +            t.show();
 +        }
 +
 +        refeshListOfFilesFragment();
 +
 +        dismissLoadingDialog();
 +    }
  
+     private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) {
+         if (result.isSuccess()) {
+             refeshListOfFilesFragment();
+         }
+     }
+     
      /**
       * Updates the view associated to the activity after the finish of an operation trying to remove a 
       * file. 
          
          mRefreshSharesInProgress = true;
      }
-     public void unshareFileWithLink(OCFile file) {
-         
-         if (isSharedSupported()) {
-             // Unshare the file
-             UnshareLinkOperation unshare = new UnshareLinkOperation(file);
-             unshare.execute(getStorageManager(), this, this, mHandler, this);
-          
-             showLoadingDialog();
-             
-         } else {
-             // Show a Message
-             Toast t = Toast.makeText(this, getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
-             t.show();
-             
-         }
-     }
 +    
 +    
++
  
  }
@@@ -283,11 -282,12 +282,17 @@@ public class OCFileListFragment extend
      public boolean onContextItemSelected (MenuItem item) {
          AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();        
          mTargetFile = (OCFile) mAdapter.getItem(info.position);
--        switch (item.getItemId()) {
++        switch (item.getItemId()) {                
+             case R.id.action_share_file: {
+                 FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity();
+                 activity.getFileOperationsHelper().shareFileWithLink(mTargetFile, activity);
+                 return true;
+             }
 +            case R.id.action_unshare_file: {
-                 mContainerActivity.unshareFileWithLink(mTargetFile);
++                FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity();
++                activity.getFileOperationsHelper().unshareFileWithLink(mTargetFile, activity);
 +                return true;
 +            }
              case R.id.action_rename_file: {
                  String fileName = mTargetFile.getFileName();
                  int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf(".");
           * @param file
           */
          public void onBrowsedDownTo(OCFile folder);
--        
-         public void unshareFileWithLink(OCFile mTargetFile);
 +
          public void startDownloadForPreview(OCFile file);
  
          public void startMediaPreview(OCFile file, int i, boolean b);
@@@ -329,16 -328,16 +333,24 @@@ public class PreviewMediaFragment exten
                  return false;
          }
      }
 +    
 +    /**
 +     * {@inheritDoc}
 +     */
 +    @Override
 +    public void onPrepareOptionsMenu(Menu menu) {
 +        super.onPrepareOptionsMenu(menu);
 +    }
  
      
+     private void shareFileWithLink() {
+         stopPreview(false);
+         FileActivity activity = (FileActivity)((FileFragment.ContainerActivity)getActivity());
+         activity.getFileOperationsHelper().shareFileWithLink(getFile(), activity);
+         
+     }
      private void seeDetails() {
          stopPreview(false);
          ((FileFragment.ContainerActivity)getActivity()).showDetails(getFile());