Merge branch 'share_link__new_share' into share_link__new_share_menu
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 3 Feb 2014 08:46:26 +0000 (09:46 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 3 Feb 2014 08:46:26 +0000 (09:46 +0100)
1  2 
AndroidManifest.xml
res/values/strings.xml
src/com/owncloud/android/ui/activity/FileActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

diff --combined AndroidManifest.xml
@@@ -54,6 -54,7 +54,6 @@@
              >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
 -
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
                  <category android:name="android.intent.category.DEFAULT" >
                  </category>
  
 -                <data android:mimeType="*/*" >
 +                <data android:mimeType="*/*" android:scheme="content">
                  </data>
 -            </intent-filter>
 +
 +                <data android:mimeType="*/*" android:scheme="file">
 +                </data>
 +                </intent-filter>
          </activity>
          <activity
              android:name=".ui.activity.Preferences"
              </intent-filter>
          </activity>
  
+         <service android:name=".services.OperationsService" />
          <service android:name=".files.services.FileDownloader" />
          <service android:name=".files.services.FileUploader" />
          <service android:name=".media.MediaService" />
diff --combined res/values/strings.xml
      <string name="wait_a_moment">Wait a moment</string>
      <string name="filedisplay_unexpected_bad_get_content">"Unexpected problem ; please select the file from a different app"</string>
      <string name="filedisplay_no_file_selected">No file was selected</string>
 +    <string name="activity_chooser_title">Send link to #8230;</string>
      
      <string name="oauth_check_onoff">Login with oAuth2</string> 
      <string name="oauth_login_connection">Connecting to oAuth2 server…</string>    
      <string name="failed_upload_retry_do_nothing_text">do nothing you are not online for instant upload</string>
        <string name="failed_upload_failure_text">Failure Message: </string>
        <string name="failed_upload_quota_exceeded_text">Please check your server configuration,maybe your quota is exceeded.</string>
+       
+       <string name="share_link_no_support_share_api">Sorry, sharing is not enabled on your server. Please contact your administrator.</string>
+       <string name="share_link_file_no_exist">Unable to share this file or folder. Please, make sure it exists</string>
  </resources>
  
  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.content.pm.PackageManager;
 +import android.content.pm.ResolveInfo;
  import android.net.Uri;
  import android.os.Bundle;
 +import android.os.Parcelable;
  import android.webkit.MimeTypeMap;
++import android.widget.Toast;
  
  import com.actionbarsherlock.app.SherlockFragmentActivity;
  import com.owncloud.android.MainApp;
@@@ -351,77 -343,5 +352,88 @@@ public abstract class FileActivity exte
              Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
          }
      }
-         if (file != null) {
-             
-             //CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1);
-             //createShare.execute(getAccount(), this, this, mHandler, this);
-             
-             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);
 +
 +    /*
 +    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 (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[] {}));
++        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);
++                        }
 +                    }
 +                }
-                 // to show standard message
-                 chooserIntent = Intent.createChooser(null, getString(R.string.action_share_file));
++                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 {
-             startActivity(chooserIntent);
++                Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
 +            }
-             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; 
 +    }
 +    
      
  }
@@@ -45,6 -45,7 +45,7 @@@ import android.provider.MediaStore
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
+ import android.support.v4.content.LocalBroadcastManager;
  import android.util.Log;
  import android.view.View;
  import android.view.ViewGroup;
@@@ -69,17 -70,17 +70,16 @@@ import com.owncloud.android.files.servi
  import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  import com.owncloud.android.operations.CreateFolderOperation;
  
- import com.owncloud.android.operations.GetSharesOperation;
  import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
  import com.owncloud.android.lib.operations.common.RemoteOperation;
  import com.owncloud.android.lib.operations.common.RemoteOperationResult;
 -import com.owncloud.android.lib.operations.common.ShareType;
  import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
+ import com.owncloud.android.operations.CreateShareOperation;
  import com.owncloud.android.operations.RemoveFileOperation;
  import com.owncloud.android.operations.RenameFileOperation;
  import com.owncloud.android.operations.SynchronizeFileOperation;
  import com.owncloud.android.operations.SynchronizeFolderOperation;
+ import com.owncloud.android.services.OperationsService;
  import com.owncloud.android.syncadapter.FileSyncService;
  import com.owncloud.android.ui.dialog.EditNameDialog;
  import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
@@@ -114,6 -115,7 +114,7 @@@ OCFileListFragment.ContainerActivity, F
      private SyncBroadcastReceiver mSyncBroadcastReceiver;
      private UploadFinishReceiver mUploadFinishReceiver;
      private DownloadFinishReceiver mDownloadFinishReceiver;
+     private OperationsServiceReceiver mOperationsServiceReceiver;
      private FileDownloaderBinder mDownloaderBinder = null;
      private FileUploaderBinder mUploaderBinder = null;
      private ServiceConnection mDownloadConnection = null, mUploadConnection = null;
          downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
          mDownloadFinishReceiver = new DownloadFinishReceiver();
          registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
+         
+         // Listen for messages from the OperationsService
+         IntentFilter operationsIntentFilter = new IntentFilter(OperationsService.ACTION_OPERATION_ADDED);
+         operationsIntentFilter.addAction(OperationsService.ACTION_OPERATION_FINISHED);
+         mOperationsServiceReceiver = new OperationsServiceReceiver();
+         LocalBroadcastManager.getInstance(this).registerReceiver(mOperationsServiceReceiver, operationsIntentFilter);
      
          Log_OC.d(TAG, "onResume() end");
      }
              unregisterReceiver(mDownloadFinishReceiver);
              mDownloadFinishReceiver = null;
          }
+         if (mOperationsServiceReceiver != null) {
+             LocalBroadcastManager.getInstance(this).unregisterReceiver(mOperationsServiceReceiver);
+             mOperationsServiceReceiver = null;
+         }
          Log_OC.d(TAG, "onPause() end");
      }
  
              return (accountName != null && getAccount() != null && accountName.equals(getAccount().name));
          }
      }
+     
+     
+     /**
+      * Class waiting for broadcast events from the {@link OperationsService}.
+      * 
+      * Updates the list of files when a get for shares is finished; at this moment the refresh of shares is the only
+      * operation performed in {@link OperationsService}.
+      * 
+      * In the future will handle the progress or finalization of all the operations performed in {@link OperationsService}, 
+      * probably all the operations associated to the app model. 
+      */
+     private class OperationsServiceReceiver extends BroadcastReceiver {
+         @Override
+         public void onReceive(Context context, Intent intent) {
+             if (OperationsService.ACTION_OPERATION_ADDED.equals(intent.getAction())) {
+                 
+             } else if (OperationsService.ACTION_OPERATION_FINISHED.equals(intent.getAction())) {
+                 mRefreshSharesInProgress = false;
+                 
+                 Account account = intent.getParcelableExtra(OperationsService.EXTRA_ACCOUNT);
+                 RemoteOperationResult getSharesResult = (RemoteOperationResult)intent.getSerializableExtra(OperationsService.EXTRA_RESULT);
+                 if (getAccount() != null && account.name.equals(getAccount().name)
+                         && mStorageManager != null
+                         ) {
+                     refeshListOfFilesFragment();
+                 }
+                 if ((getSharesResult != null) &&
+                         RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(getSharesResult.getCode())) {
+                     mLastSslUntrustedServerResult = getSharesResult;
+                     showDialog(DIALOG_SSL_VALIDATOR); 
+                 }
+                 setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress || mSyncInProgress);
+             }
+             
+         }
+             
+     }
  
  
      /**
  
          } else if (operation instanceof CreateFolderOperation) {
              onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
-             
-         } else if (operation instanceof GetSharesOperation) {
-             onGetSharesOperationFinish((GetSharesOperation) operation, result);
+         
+         } else if (operation instanceof CreateShareOperation) {
+             onCreateShareOperation((CreateShareOperation) operation, result);
          }
+         
      }
  
  
-     /** Updates the data about shared files
-      * 
-      * @param operation     Get Shared Files
-      * @param result        Result of the operation
-      */
-     private void onGetSharesOperationFinish(GetSharesOperation operation, RemoteOperationResult result) {
-         // Refresh the filelist with the information
-         refeshListOfFilesFragment();  
+     private void onCreateShareOperation(CreateShareOperation 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();
+         }
          
-         mRefreshSharesInProgress = false;
+         refeshListOfFilesFragment();
          
-         if (!mSyncInProgress) {
-             setSupportProgressBarIndeterminateVisibility(false);
-         }
      }
  
      /**
      
      private void startGetShares() {
          // Get shared files/folders
-         RemoteOperation getShares = new GetSharesOperation(mStorageManager);
-         getShares.execute(getAccount(), this, this, mHandler, this);
+         Intent intent = new Intent(this, OperationsService.class);
+         intent.putExtra(OperationsService.EXTRA_ACCOUNT, getAccount());
+         startService(intent);
          
          mRefreshSharesInProgress = true;
          setSupportProgressBarIndeterminateVisibility(true);
          
      }
  
- //    public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
- //        int childCount = viewGroup.getChildCount();
- //        for (int i = 0; i < childCount; i++) {
- //          View view = viewGroup.getChildAt(i);
- //          view.setEnabled(enabled);
- //          view.setClickable(!enabled);
- //          if (view instanceof ViewGroup) {
- //            enableDisableViewGroup((ViewGroup) view, enabled);
- //          }
- //        }
- //      }
 -
 -    public void shareFileWithLink(OCFile file) {
 -        
 -        if (isSharedSupported()) {
 -            // 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
 -            
 -        } else {
 -            // Show a Message
 -            Toast t = Toast.makeText(this, getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
 -            t.show();
 -            
 -        }
 -    }
 -    
  }
@@@ -55,9 -55,6 +55,6 @@@ import com.owncloud.android.lib.operati
  import com.owncloud.android.lib.operations.common.RemoteOperation;
  import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
- import com.owncloud.android.lib.operations.common.ShareType;
- import com.owncloud.android.lib.operations.remote.CreateShareRemoteOperation;
- import com.owncloud.android.operations.CreateShareOperation;
  import com.owncloud.android.operations.RemoveFileOperation;
  import com.owncloud.android.operations.RenameFileOperation;
  import com.owncloud.android.operations.SynchronizeFileOperation;
@@@ -339,7 -336,7 +336,7 @@@ public class FileDetailFragment extend
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.action_share_file: {
 -                    mContainerActivity.shareFileWithLink(getFile());
 +                mContainerActivity.shareFileWithLink(getFile());
                  return true;
              }
              case R.id.action_open_file_with: {