Эх сурвалжийг харах

Show users/groups in share with view, getting the data from the server

masensio 9 жил өмнө
parent
commit
20b0ec8977

+ 1 - 1
res/layout/list_item.xml

@@ -78,7 +78,7 @@
                 android:ellipsize="middle"
                 android:singleLine="true"
                 android:text="TextView"
-                android:textColor="#303030"
+                android:textColor="@color/textColor"
                 android:textSize="16dip" />
 
             <LinearLayout

+ 0 - 1
res/layout/share_file_layout.xml

@@ -19,7 +19,6 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
     tools:context="com.owncloud.android.ui.fragment.ShareFileFragment">
 
     <LinearLayout

+ 36 - 0
res/layout/share_user_item.xml

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ownCloud Android client application
+  Copyright (C) 2015 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/>.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/username"
+        android:id="@+id/userOrGroupName"
+        android:layout_margin="12dp"
+        android:textColor="@color/textColor"/>
+
+    <View
+        android:layout_width="match_parent"
+        android:layout_height="1dp"
+        android:background="@color/list_divider_background"></View>
+</LinearLayout>

+ 30 - 0
src/com/owncloud/android/ui/activity/ShareActivity.java

@@ -23,12 +23,15 @@ package com.owncloud.android.ui.activity;
 import android.accounts.Account;
 import android.net.Uri;
 import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.ui.dialog.LoadingDialog;
 import com.owncloud.android.ui.fragment.SearchFragment;
 import com.owncloud.android.ui.fragment.ShareFileFragment;
 
@@ -43,6 +46,8 @@ public class ShareActivity extends AppCompatActivity
     private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
     private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
 
+    private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
+
     private Account mAccount;
     private OCFile mFile;
 
@@ -133,4 +138,29 @@ public class ShareActivity extends AppCompatActivity
     public void onSearchFragmentInteraction(Uri uri) {
 
     }
+
+    /**
+     * Show waiting for loading data
+     */
+    public void showWaitingLoadDialog() {
+        // Construct dialog
+        LoadingDialog loading = new LoadingDialog(
+                getResources().getString(R.string.common_loading));
+        FragmentManager fm = getSupportFragmentManager();
+        FragmentTransaction ft = fm.beginTransaction();
+        loading.show(ft, DIALOG_WAIT_LOAD_DATA);
+
+    }
+
+
+    /**
+     * Dismiss waiting for loading data
+     */
+    public void dismissWaitingLoadDialog(){
+        Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_LOAD_DATA);
+        if (frag != null) {
+            LoadingDialog loading = (LoadingDialog) frag;
+            loading.dismiss();
+        }
+    }
 }

+ 87 - 0
src/com/owncloud/android/ui/adapter/ShareUserListAdapter.java

@@ -0,0 +1,87 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   @author masensio
+ *   Copyright (C) 2015 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.ui.adapter;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.BaseAdapter;
+import android.widget.TextView;
+
+import com.owncloud.android.R;
+import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.resources.shares.ShareType;
+
+import java.io.File;
+import java.util.ArrayList;
+
+/**
+ * Adapter to show a user/group in Share With List
+ */
+public class ShareUserListAdapter extends ArrayAdapter {
+
+    private Context mContext;
+    private ArrayList<OCShare> mShares;
+
+    public ShareUserListAdapter(Context context, int resource, ArrayList<OCShare>shares) {
+        super(context, resource);
+        mContext= context;
+        mShares = shares;
+    }
+
+    @Override
+    public int getCount() {
+        return mShares.size();
+    }
+
+    @Override
+    public Object getItem(int position) {
+        return mShares.get(position);
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return 0;
+    }
+
+    @Override
+    public View getView(int position, View convertView, ViewGroup parent) {
+        LayoutInflater inflator = (LayoutInflater) mContext
+                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+//        View rowView = inflater.inflate(R.layout.row_estacion, parent, false);
+        View view = inflator.inflate(R.layout.share_user_item, parent, false);
+
+        if (mShares != null && mShares.size() > position) {
+            OCShare share = mShares.get(position);
+
+            TextView userName = (TextView) view.findViewById(R.id.userOrGroupName);
+            String name = share.getSharedWithDisplayName();
+            if (share.getShareType() == ShareType.GROUP){
+                name = name + "(group)";
+            }
+            userName.setText(name);
+        }
+
+        return view;
+    }
+}

+ 97 - 3
src/com/owncloud/android/ui/fragment/ShareFileFragment.java

@@ -21,6 +21,8 @@
 package com.owncloud.android.ui.fragment;
 
 import android.accounts.Account;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.net.Uri;
@@ -31,14 +33,36 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
+import android.widget.ListView;
 import android.widget.TextView;
+import android.widget.Toast;
 
+import com.owncloud.android.MainApp;
 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.OwnCloudAccount;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
+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.lib.resources.shares.ShareType;
+import com.owncloud.android.operations.GetSharesForFileOperation;
+import com.owncloud.android.ui.activity.FileActivity;
+import com.owncloud.android.ui.activity.ShareActivity;
+import com.owncloud.android.ui.adapter.LocalFileListAdapter;
+import com.owncloud.android.ui.adapter.ShareUserListAdapter;
+import com.owncloud.android.utils.CopyTmpFileAsyncTask;
 import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.utils.GetShareWithUserAsyncTask;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
+import java.io.IOException;
+import java.util.ArrayList;
+
 /**
  * Fragment for Sharing a file with users
  *
@@ -49,7 +73,8 @@ import com.owncloud.android.utils.MimetypeIconUtil;
  * Use the {@link ShareFileFragment#newInstance} factory method to
  * create an instance of this fragment.
  */
-public class ShareFileFragment extends Fragment {
+public class ShareFileFragment extends Fragment
+        implements GetShareWithUserAsyncTask.OnGetSharesWithUserTaskListener{
     private static final String TAG = ShareFileFragment.class.getSimpleName();
 
     // the fragment initialization parameters
@@ -60,6 +85,9 @@ public class ShareFileFragment extends Fragment {
     private OCFile mFile;
     private Account mAccount;
 
+    private ArrayList<OCShare> mShares;
+    private ShareUserListAdapter mUserGroupsAdapter = null;
+
     private OnShareFragmentInteractionListener mListener;
 
     /**
@@ -120,6 +148,12 @@ public class ShareFileFragment extends Fragment {
             size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
         }
 
+        // List of share with users
+        TextView noShares = (TextView) view.findViewById(R.id.shareNoUsers);
+
+        // TODO: Get shares from DB and show
+
+
         //  Add User Button
         Button addUserGroupButton = (Button)
                 view.findViewById(R.id.addUserButton);
@@ -134,6 +168,13 @@ public class ShareFileFragment extends Fragment {
         return view;
     }
 
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+
+        getShares(mFile);
+    }
+
     // TODO: Rename method, update argument and hook method into UI event
     public void onButtonPressed(Uri uri) {
         if (mListener != null) {
@@ -158,6 +199,59 @@ public class ShareFileFragment extends Fragment {
         mListener = null;
     }
 
+    // Get users and groups to fill the "share with" list
+    private void getShares(OCFile file){
+        mShares = new ArrayList<>();
+
+        RemoteOperationResult result = null;
+
+        // Show loading
+        ( (ShareActivity) getActivity()).showWaitingLoadDialog();
+        // Get Users and Groups
+        GetShareWithUserAsyncTask getTask = new GetShareWithUserAsyncTask(this);
+        FileDataStorageManager fileDataStorageManager =
+                new FileDataStorageManager(mAccount, getActivity().getContentResolver());
+        Object[] params = { mFile, mAccount, fileDataStorageManager};
+        getTask.execute(params);
+    }
+
+    @Override
+    public void onGetDataShareWithFinish(RemoteOperationResult result) {
+        // Remove loading
+        ((ShareActivity) getActivity()).dismissWaitingLoadDialog();
+        if (result != null && result.isSuccess()) {
+            // update local database
+            for(Object obj: result.getData()) {
+                if ( ((OCShare) obj).getShareType() == ShareType.USER ||
+                        ((OCShare) obj).getShareType() == ShareType.GROUP ){
+                    mShares.add((OCShare) obj);
+                }
+            }
+
+            // Update list of users/groups
+            mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
+                    R.layout.share_user_item, mShares);
+
+            // Show data
+            TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
+            ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
+
+            if (mShares.size() > 0) {
+                noShares.setVisibility(View.GONE);
+                usersList.setVisibility(View.VISIBLE);
+                usersList.setAdapter(mUserGroupsAdapter);
+
+            } else {
+                noShares.setVisibility(View.VISIBLE);
+                usersList.setVisibility(View.GONE);
+            }
+
+        } else {
+            Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show();
+        }
+
+    }
+
     // TODO: review if it is necessary
     /**
      * This interface must be implemented by activities that contain this
@@ -170,9 +264,9 @@ public class ShareFileFragment extends Fragment {
      * >Communicating with Other Fragments</a> for more information.
      */
     public interface OnShareFragmentInteractionListener {
-        public void showSearchUsersAndGroups();
+        void showSearchUsersAndGroups();
 
-        public void onShareFragmentInteraction(Uri uri);
+        void onShareFragmentInteraction(Uri uri);
     }
 
 }

+ 108 - 0
src/com/owncloud/android/utils/GetShareWithUserAsyncTask.java

@@ -0,0 +1,108 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   @author masensio
+ *   Copyright (C) 2015 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.utils;
+
+import android.accounts.Account;
+import android.os.AsyncTask;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.OwnCloudAccount;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
+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.operations.GetSharesForFileOperation;
+
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+
+/**
+ * Async Task to get the users and groups which a file is shared with
+ */
+public class GetShareWithUserAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
+
+    private final String TAG = GetShareWithUserAsyncTask.class.getSimpleName();
+    private final WeakReference<OnGetSharesWithUserTaskListener> mListener;
+    private ArrayList<OCShare> mShares;
+
+    public ArrayList<OCShare> getShares(){
+        return mShares;
+    }
+
+    public GetShareWithUserAsyncTask(OnGetSharesWithUserTaskListener listener) {
+        mListener = new WeakReference<OnGetSharesWithUserTaskListener>(listener);
+    }
+
+    @Override
+    protected RemoteOperationResult doInBackground(Object... params) {
+
+        RemoteOperationResult result = null;
+
+        if (params != null && params.length == 3) {
+            OCFile file = (OCFile) params[0];
+            Account account = (Account) params[1];
+            FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
+
+            try {
+                // Get shares request
+                GetSharesForFileOperation operation =
+                        new GetSharesForFileOperation(file.getRemotePath(), false, false);
+                OwnCloudAccount ocAccount = new OwnCloudAccount(account,
+                        MainApp.getAppContext());
+                OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
+                        getClientFor(ocAccount, MainApp.getAppContext());
+                result = operation.execute(client, fileDataStorageManager);
+
+            } catch (Exception e) {
+                result = new RemoteOperationResult(e);
+                Log_OC.e(TAG, "Exception while getting shares", e);
+            }
+        } else {
+            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
+        }
+
+        return result;
+    }
+
+    @Override
+    protected void onPostExecute(RemoteOperationResult result) {
+
+        if (result!= null)
+        {
+            OnGetSharesWithUserTaskListener listener = mListener.get();
+            if (listener!= null)
+            {
+                listener.onGetDataShareWithFinish(result);
+            }
+        }
+    }
+
+    /*
+     * Interface to retrieve data from get shares task
+     */
+    public interface OnGetSharesWithUserTaskListener{
+
+        void onGetDataShareWithFinish(RemoteOperationResult result);
+    }
+}