瀏覽代碼

Show users/groups in Search Fragment list

masensio 9 年之前
父節點
當前提交
671fef617b

+ 4 - 5
res/layout/share_user_item.xml

@@ -29,7 +29,7 @@
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:layout_weight="0.7"
+            android:layout_weight="0.9"
             android:textAppearance="?android:attr/textAppearanceMedium"
             android:text="@string/username"
             android:id="@+id/userOrGroupName"
@@ -39,13 +39,12 @@
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
-            android:layout_weight="0.3"
+            android:layout_weight="0.1"
             android:id="@+id/unshareButton"
             android:src="@drawable/ic_cancel"
-            android:layout_marginRight="4dp"
+            android:layout_marginRight="8dp"
             android:layout_marginLeft="4dp"
-            android:layout_gravity="center_horizontal"
-            android:visibility="gone"/>
+            android:layout_gravity="center_horizontal"/>
     </LinearLayout>
 
     <View

+ 51 - 20
src/com/owncloud/android/ui/activity/ShareActivity.java

@@ -34,18 +34,24 @@ 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.operations.GetSharesForFileOperation;
+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.GetShareWithUsersAsyncTask;
+
+import java.util.ArrayList;
 
 /**
  * Activity for sharing files
  */
 
 public class ShareActivity extends FileActivity
-        implements ShareFileFragment.OnShareFragmentInteractionListener,
+        implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener,
+        ShareFileFragment.OnShareFragmentInteractionListener,
         SearchFragment.OnSearchFragmentInteractionListener {
 
     private static final String TAG = ShareActivity.class.getSimpleName();
@@ -94,6 +100,8 @@ public class ShareActivity extends FileActivity
         }
 
         handleIntent(getIntent());
+
+        onAccountSet(false);
     }
 
 
@@ -131,7 +139,7 @@ public class ShareActivity extends FileActivity
         getFileOperationsHelper().shareFileWithSharee(
                 getFile(),
                 shareeName,
-                (isGroup ? ShareType.GROUP : ShareType.USER )
+                (isGroup ? ShareType.GROUP : ShareType.USER)
         );
     }
 
@@ -146,17 +154,36 @@ 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);
         ft.commit();
     }
 
+    @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();
@@ -178,30 +205,34 @@ public class ShareActivity extends FileActivity
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
         super.onRemoteOperationFinish(operation, result);
         if (operation instanceof UnshareOperation) {
-            if (mShareFileFragment != null){
-                mShareFileFragment.refreshUsersOrGroupsListFromDB();
-            }
-        } else if (operation instanceof GetSharesForFileOperation) {
-            onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result);
+            refreshUsersInLists();
+        } else if(operation instanceof CreateShareWithShareeOperation){
+            refreshUsersInLists();
         }
-
     }
 
-    private  void onGetSharesForFileOperationFinish(GetSharesForFileOperation operation, RemoteOperationResult result){
+    @Override
+    public void onGetDataShareWithFinish(RemoteOperationResult result) {
+        // Remove loading
         dismissLoadingDialog();
+        if (result != null && result.isSuccess()) {
+            Log_OC.d(TAG, "Get Data Share With finishes sucessfully");
 
-        if (!result.isSuccess()) {
-            Toast.makeText(getApplicationContext(), result.getLogMessage(), Toast.LENGTH_LONG).show();
+        } else {
+            Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_SHORT).show();
         }
 
-        // Show Shares
+        // Data is on Database
+        refreshUsersInLists();
+    }
+
+    private void refreshUsersInLists(){
         if (mShareFileFragment != null){
             mShareFileFragment.refreshUsersOrGroupsListFromDB();
         }
+        if (mSearchFragment != null) {
+            mSearchFragment.refreshUsersOrGroupsListFromDB();
+        }
     }
 
-    @Override
-    public void onSearchFragmentInteraction(Uri uri) {
-
-    }
 }

+ 15 - 3
src/com/owncloud/android/ui/adapter/ShareUserListAdapter.java

@@ -41,11 +41,14 @@ public class ShareUserListAdapter extends ArrayAdapter {
 
     private Context mContext;
     private ArrayList<OCShare> mShares;
+    private ShareUserAdapterListener mListener;
 
-    public ShareUserListAdapter(Context context, int resource, ArrayList<OCShare>shares) {
+    public ShareUserListAdapter(Context context, int resource, ArrayList<OCShare>shares,
+                                ShareUserAdapterListener listener) {
         super(context, resource);
         mContext= context;
         mShares = shares;
+        mListener = listener;
     }
 
     @Override
@@ -79,13 +82,22 @@ public class ShareUserListAdapter extends ArrayAdapter {
             }
             userName.setText(name);
 
-            ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
-            unshareButton.setVisibility(View.GONE);
+            final ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
+            unshareButton.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    mListener.unshareButtonPressed(mShares.get(position));
+                }
+            });
 
         }
         return view;
     }
 
+    public interface ShareUserAdapterListener {
+        void unshareButtonPressed(OCShare share);
+    }
+
 
 
 }

+ 57 - 10
src/com/owncloud/android/ui/fragment/SearchFragment.java

@@ -25,17 +25,22 @@ import android.accounts.Account;
 import android.app.Activity;
 import android.app.SearchManager;
 import android.content.Context;
-import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ListView;
 import android.widget.SearchView;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.ui.activity.ShareActivity;
+import com.owncloud.android.ui.adapter.ShareUserListAdapter;
+
+import java.util.ArrayList;
 
 /**
  * Fragment for Searching users and groups
@@ -47,16 +52,19 @@ import com.owncloud.android.lib.common.utils.Log_OC;
  * Use the {@link SearchFragment#newInstance} factory method to
  * create an instance of this fragment.
  */
-public class SearchFragment extends Fragment {
+public class SearchFragment extends Fragment implements ShareUserListAdapter.ShareUserAdapterListener {
     private static final String TAG = SearchFragment.class.getSimpleName();
 
     // the fragment initialization parameters
     private static final String ARG_FILE = "FILE";
     private static final String ARG_ACCOUNT = "ACCOUNT";
+    private static final String ARG_SHARES = "SHARES";
 
     // Parameters
     private OCFile mFile;
     private Account mAccount;
+    private ArrayList<OCShare> mShares;
+    private ShareUserListAdapter mUserGroupsAdapter = null;
 
     private OnSearchFragmentInteractionListener mListener;
 
@@ -65,14 +73,16 @@ public class SearchFragment extends Fragment {
      *
      * @param fileToShare   An {@link OCFile} to show in the fragment
      * @param account       An ownCloud account
+     * @param
      * @return A new instance of fragment SearchFragment.
      */
     // TODO: Rename and change types and number of parameters
-    public static SearchFragment newInstance(OCFile fileToShare, Account account) {
+    public static SearchFragment newInstance(OCFile fileToShare, Account account, ArrayList<OCShare> shares) {
         SearchFragment fragment = new SearchFragment();
         Bundle args = new Bundle();
         args.putParcelable(ARG_FILE, fileToShare);
         args.putParcelable(ARG_ACCOUNT, account);
+        args.putParcelableArrayList(ARG_SHARES, shares);
         fragment.setArguments(args);
         return fragment;
     }
@@ -87,6 +97,7 @@ public class SearchFragment extends Fragment {
         if (getArguments() != null) {
             mFile = getArguments().getParcelable(ARG_FILE);
             mAccount = getArguments().getParcelable(ARG_ACCOUNT);
+            mShares = getArguments().getParcelableArrayList(ARG_SHARES);
         }
 
     }
@@ -121,13 +132,44 @@ public class SearchFragment extends Fragment {
             }
         });
 
+        // Show data: Fill in list of users and groups
+        ListView usersList = (ListView) view.findViewById(R.id.searchUsersListView);
+        mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
+                R.layout.share_user_item, mShares, this);
+        if (mShares.size() > 0) {
+            usersList.setVisibility(View.VISIBLE);
+            usersList.setAdapter(mUserGroupsAdapter);
+        }
+
         return view;
     }
 
-    // TODO: Rename method, update argument and hook method into UI event
-    public void onButtonPressed(Uri uri) {
-        if (mListener != null) {
-            mListener.onSearchFragmentInteraction(uri);
+    /**
+     * Get users and groups fromn the DB to fill in the "share with" list
+     */
+    public void refreshUsersOrGroupsListFromDB (){
+        // Get Users and Groups
+        mShares = ((ShareActivity) mListener).getStorageManager().getSharesWithForAFile(mFile.getRemotePath(),
+                mAccount.name);
+
+        // Update list of users/groups
+        updateListOfUserGroups();
+    }
+
+    private void updateListOfUserGroups() {
+        // Update list of users/groups
+        mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
+                R.layout.share_user_item, mShares, this);
+
+        // Show data
+        ListView usersList = (ListView) getView().findViewById(R.id.searchUsersListView);
+
+        if (mShares.size() > 0) {
+            usersList.setVisibility(View.VISIBLE);
+            usersList.setAdapter(mUserGroupsAdapter);
+
+        } else {
+            usersList.setVisibility(View.GONE);
         }
     }
 
@@ -148,7 +190,13 @@ public class SearchFragment extends Fragment {
         mListener = null;
     }
 
-    // TODO: review if it is necessary
+    @Override
+    public void unshareButtonPressed(OCShare share) {
+        // Unshare
+        mListener.unshareWith(share);
+        Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
+    }
+
     /**
      * This interface must be implemented by activities that contain this
      * fragment to allow an interaction in this fragment to be communicated
@@ -160,8 +208,7 @@ public class SearchFragment extends Fragment {
      * >Communicating with Other Fragments</a> for more information.
      */
     public interface OnSearchFragmentInteractionListener {
-        // TODO: Update argument type and name
-        public void onSearchFragmentInteraction(Uri uri);
+        void unshareWith(OCShare share);
     }
 
 }

+ 12 - 82
src/com/owncloud/android/ui/fragment/ShareFileFragment.java

@@ -28,25 +28,19 @@ import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.AdapterView;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
-import android.widget.Toast;
 
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.OCShare;
-import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.ShareActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
 import com.owncloud.android.utils.DisplayUtils;
-import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
 import java.util.ArrayList;
@@ -62,7 +56,7 @@ import java.util.ArrayList;
  * create an instance of this fragment.
  */
 public class ShareFileFragment extends Fragment
-        implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener {
+        implements ShareUserListAdapter.ShareUserAdapterListener{
 
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
@@ -79,8 +73,6 @@ public class ShareFileFragment extends Fragment
 
     private OnShareFragmentInteractionListener mListener;
 
-    private FileDataStorageManager mFileDataStorageManager;
-
     /**
      * Public factory method to create new ShareFileFragment instances.
      *
@@ -107,7 +99,6 @@ public class ShareFileFragment extends Fragment
         if (getArguments() != null) {
             mFile = getArguments().getParcelable(ARG_FILE);
             mAccount = getArguments().getParcelable(ARG_ACCOUNT);
-            mFileDataStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
         }
     }
 
@@ -147,7 +138,7 @@ public class ShareFileFragment extends Fragment
             @Override
             public void onClick(View view) {
                 // Show Search Fragment
-                mListener.showSearchUsersAndGroups();
+                mListener.showSearchUsersAndGroups(mShares);
             }
         });
 
@@ -159,7 +150,7 @@ public class ShareFileFragment extends Fragment
         super.onActivityCreated(savedInstanceState);
 
         // Load data to the list (start process with an Async Task)
-        refreshUsersOrGroupsListFromServer();
+        mListener.refreshUsersOrGroupsListFromServer();
     }
 
     @Override
@@ -184,7 +175,7 @@ public class ShareFileFragment extends Fragment
      */
     public void refreshUsersOrGroupsListFromDB (){
         // Get Users and Groups
-        mShares = mFileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(),
+        mShares = ((ShareActivity) mListener).getStorageManager().getSharesWithForAFile(mFile.getRemotePath(),
                 mAccount.name);
 
         // Update list of users/groups
@@ -194,7 +185,7 @@ public class ShareFileFragment extends Fragment
     private void updateListOfUserGroups() {
         // Update list of users/groups
         mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
-                R.layout.share_user_item, mShares);
+                R.layout.share_user_item, mShares, this);
 
         // Show data
         TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
@@ -205,83 +196,20 @@ public class ShareFileFragment extends Fragment
             usersList.setVisibility(View.VISIBLE);
             usersList.setAdapter(mUserGroupsAdapter);
 
-            // Add unshare options
-            registerLongClickListener(usersList);
-
         } else {
             noShares.setVisibility(View.VISIBLE);
             usersList.setVisibility(View.GONE);
         }
     }
 
-    /**
-     * Get users and groups from the server to fill in the "share with" list
-     */
-    public void refreshUsersOrGroupsListFromServer(){
-        // Show loading
-        ((ShareActivity) getActivity()).showLoadingDialog(getString(R.string.common_loading));
-        // Get Users and Groups
-        GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
-        Object[] params = { mFile, mAccount, mFileDataStorageManager};
-        getTask.execute(params);
-    }
-
-    private void registerLongClickListener(final ListView listView) {
-        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
-            @Override
-            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
-                                           long id) {
-                // Show unshare button
-                ImageView unshareButton = (ImageView) view.findViewById(R.id.unshareButton);
-                if (unshareButton.getVisibility() == View.GONE) {
-                    unshareButton.setVisibility(View.VISIBLE);
-                    unshareButton.setOnClickListener(new View.OnClickListener() {
-                        @Override
-                        public void onClick(View v) {
-                            // Unshare
-                            unshareWith(mShares.get(position));
-                            Log_OC.d(TAG, "Unshare - " +
-                                    mShares.get(position).getSharedWithDisplayName());
-                        }
-                    });
-
-                } else {
-                    unshareButton.setVisibility(View.GONE);
-                }
-                view.setAlpha(0);
-                view.animate().alpha(1).setDuration(500).start();
-                return false;
-            }
-        });
-    }
-
-    // Call to Unshare operation
-    private void unshareWith(OCShare share){
-        OCFile file = ((FileActivity) getActivity()).getFile();
-
-        ((FileActivity) getActivity()).getFileOperationsHelper().
-                unshareFileWithUserOrGroup(
-                        file, share.getShareType(), share.getShareWith()
-                );
-    }
-
     @Override
-    public void onGetDataShareWithFinish(RemoteOperationResult result) {
-        // Remove loading
-        ((ShareActivity) getActivity()).dismissLoadingDialog();
-        if (result != null && result.isSuccess()) {
-            Log_OC.d(TAG, "Get Data Share With finishes sucessfully");
-
-        } else {
-            Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
-        }
-
-        // Data is on Database
-        refreshUsersOrGroupsListFromDB();
+    public void unshareButtonPressed(OCShare share) {
+        // Unshare
+        mListener.unshareWith(share);
+        Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
     }
 
 
-    // TODO: review if it is necessary
     /**
      * This interface must be implemented by activities that contain this
      * fragment to allow an interaction in this fragment to be communicated
@@ -293,7 +221,9 @@ public class ShareFileFragment extends Fragment
      * >Communicating with Other Fragments</a> for more information.
      */
     public interface OnShareFragmentInteractionListener {
-        void showSearchUsersAndGroups();
+        void showSearchUsersAndGroups(ArrayList<OCShare> shares);
+        void refreshUsersOrGroupsListFromServer();
+        void unshareWith(OCShare share);
     }
 
 }