Fixed error messages related with network in ShareActivity
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / ShareActivity.java
index 8cbc04e..7d98d7a 100644 (file)
@@ -34,9 +34,17 @@ import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
 
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.resources.shares.ShareType;
+import com.owncloud.android.operations.CreateShareWithShareeOperation;
 import com.owncloud.android.operations.UnshareOperation;
 import com.owncloud.android.ui.fragment.SearchFragment;
 import com.owncloud.android.ui.fragment.ShareFileFragment;
+import com.owncloud.android.utils.ErrorMessageAdapter;
+import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
+
+import java.util.ArrayList;
 
 /**
  * Activity for sharing files
@@ -59,6 +67,8 @@ public class ShareActivity extends FileActivity
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        onAccountSet(false);
+
         setContentView(R.layout.share_activity);
 
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
@@ -76,7 +86,6 @@ public class ShareActivity extends FileActivity
                 if (mSearchFragment != null){
                     ft.hide(mShareFileFragment);
                     ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
-                    ft.addToBackStack(TAG_SEARCH_FRAGMENT);
                 }
                 ft.commit();
             }
@@ -91,6 +100,8 @@ public class ShareActivity extends FileActivity
         }
 
         handleIntent(getIntent());
+
+
     }
 
 
@@ -105,7 +116,7 @@ public class ShareActivity extends FileActivity
         // Verify the action and get the query
         if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
             String query = intent.getStringExtra(SearchManager.QUERY);
-            doMySearch(query);
+            Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
 
         } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
             Uri data = intent.getData();
@@ -119,19 +130,12 @@ public class ShareActivity extends FileActivity
         }
     }
 
-    private void doMySearch(String query) {
-        // TODO implement , or prevent that search may be sent without choosing from the suggestions list
-        Toast.makeText(this, "You want to search for [" + query + "]", Toast.LENGTH_SHORT).show();
-    }
-
-    private void doShareWith(String username, boolean isGroup) {
-        // TODO implement
-        if (isGroup) {
-            Toast.makeText(this, "You want to SHARE with GROUP [" + username + "]", Toast.LENGTH_SHORT).show();
-
-        } else {
-            Toast.makeText(this, "You want to SHARE with USER [" + username + "]", Toast.LENGTH_SHORT).show();
-        }
+    private void doShareWith(String shareeName, boolean isGroup) {
+        getFileOperationsHelper().shareFileWithSharee(
+                getFile(),
+                shareeName,
+                (isGroup ? ShareType.GROUP : ShareType.USER)
+        );
     }
 
     @Override
@@ -146,9 +150,9 @@ public class ShareActivity extends FileActivity
     }
 
     @Override
-    public void showSearchUsersAndGroups() {
+    public void showSearchUsersAndGroups(ArrayList<OCShare> shares) {
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
-        mSearchFragment = SearchFragment.newInstance(getFile(), getAccount());
+        mSearchFragment = SearchFragment.newInstance(getFile(), getAccount(), shares);
         ft.hide(mShareFileFragment);
         ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
         ft.addToBackStack(TAG_SEARCH_FRAGMENT);
@@ -156,11 +160,32 @@ public class ShareActivity extends FileActivity
     }
 
     @Override
+    // Call to Unshare operation
+    public void unshareWith(OCShare share){
+        OCFile file = getFile();
+        getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
+    }
+
+    /**
+     * Get users and groups from the server to fill in the "share with" list
+     */
+    @Override
+    public void refreshUsersOrGroupsListFromServer(){
+        // Show loading
+        showLoadingDialog(getString(R.string.common_loading));
+        // Get Users and Groups
+        GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
+        Object[] params = { getFile(), getAccount(), getStorageManager()};
+        getTask.execute(params);
+    }
+
+    @Override
     public void onBackPressed() {
         super.onBackPressed();
         if (mSearchFragment != null){
-            getSupportFragmentManager().popBackStackImmediate();
             mSearchFragment = null;
+            getSupportFragmentManager().popBackStackImmediate();
+            mShareFileFragment.refreshUsersOrGroupsListFromDB();
         }
     }
 
@@ -174,21 +199,24 @@ public class ShareActivity extends FileActivity
     @Override
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
         super.onRemoteOperationFinish(operation, result);
-        if (operation instanceof UnshareOperation) {
-            if (mShareFileFragment != null){
-                mShareFileFragment.refreshUsersOrGroupsList();
+
+        if (result.isSuccess()) {
+            refreshUsersInLists();
+            if (operation instanceof  CreateShareWithShareeOperation) {
+                // Clean action
+                getIntent().setAction(null);
             }
         }
 
     }
 
-    @Override
-    public void onShareFragmentInteraction(Uri uri) {
-
+    private void refreshUsersInLists(){
+        if (mShareFileFragment != null){
+            mShareFileFragment.refreshUsersOrGroupsListFromDB();
+        }
+        if (mSearchFragment != null) {
+            mSearchFragment.refreshUsersOrGroupsListFromDB();
+        }
     }
 
-    @Override
-    public void onSearchFragmentInteraction(Uri uri) {
-
-    }
 }