2 * ownCloud Android client application
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
.fragment
;
23 import android
.accounts
.Account
;
24 import android
.accounts
.AuthenticatorException
;
25 import android
.accounts
.OperationCanceledException
;
26 import android
.app
.Activity
;
27 import android
.graphics
.Bitmap
;
28 import android
.net
.Uri
;
29 import android
.os
.Bundle
;
30 import android
.support
.v4
.app
.Fragment
;
31 import android
.view
.LayoutInflater
;
32 import android
.view
.View
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.Button
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ListView
;
37 import android
.widget
.TextView
;
38 import android
.widget
.Toast
;
40 import com
.owncloud
.android
.MainApp
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
45 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
46 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
47 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
48 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
49 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
50 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
51 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
52 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
53 import com
.owncloud
.android
.operations
.GetSharesForFileOperation
;
54 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
55 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
56 import com
.owncloud
.android
.ui
.adapter
.LocalFileListAdapter
;
57 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
58 import com
.owncloud
.android
.utils
.CopyTmpFileAsyncTask
;
59 import com
.owncloud
.android
.utils
.DisplayUtils
;
60 import com
.owncloud
.android
.utils
.GetShareWithUserAsyncTask
;
61 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
63 import java
.io
.IOException
;
64 import java
.util
.ArrayList
;
67 * Fragment for Sharing a file with users
69 * A simple {@link Fragment} subclass.
70 * Activities that contain this fragment must implement the
71 * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
72 * to handle interaction events.
73 * Use the {@link ShareFileFragment#newInstance} factory method to
74 * create an instance of this fragment.
76 public class ShareFileFragment
extends Fragment
77 implements GetShareWithUserAsyncTask
.OnGetSharesWithUserTaskListener
{
78 private static final String TAG
= ShareFileFragment
.class.getSimpleName();
80 // the fragment initialization parameters
81 private static final String ARG_FILE
= "FILE";
82 private static final String ARG_ACCOUNT
= "ACCOUNT";
86 private Account mAccount
;
88 private ArrayList
<OCShare
> mShares
;
89 private ShareUserListAdapter mUserGroupsAdapter
= null
;
91 private OnShareFragmentInteractionListener mListener
;
94 * Public factory method to create new ShareFileFragment instances.
96 * @param fileToShare An {@link OCFile} to show in the fragment
97 * @param account An ownCloud account
98 * @return A new instance of fragment ShareFileFragment.
100 public static ShareFileFragment
newInstance(OCFile fileToShare
, Account account
) {
101 ShareFileFragment fragment
= new ShareFileFragment();
102 Bundle args
= new Bundle();
103 args
.putParcelable(ARG_FILE
, fileToShare
);
104 args
.putParcelable(ARG_ACCOUNT
, account
);
105 fragment
.setArguments(args
);
109 public ShareFileFragment() {
110 // Required empty public constructor
114 public void onCreate(Bundle savedInstanceState
) {
115 super.onCreate(savedInstanceState
);
116 if (getArguments() != null
) {
117 mFile
= getArguments().getParcelable(ARG_FILE
);
118 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
123 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
124 Bundle savedInstanceState
) {
125 // Inflate the layout for this fragment
126 View view
= inflater
.inflate(R
.layout
.share_file_layout
, container
, false
);
130 ImageView icon
= (ImageView
) view
.findViewById(R
.id
.shareFileIcon
);
131 icon
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mFile
.getMimetype(),
132 mFile
.getFileName()));
133 if (mFile
.isImage()) {
134 String remoteId
= String
.valueOf(mFile
.getRemoteId());
135 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(remoteId
);
136 if (thumbnail
!= null
){
137 icon
.setImageBitmap(thumbnail
);
141 TextView filename
= (TextView
) view
.findViewById(R
.id
.shareFileName
);
142 filename
.setText(mFile
.getFileName());
144 TextView size
= (TextView
) view
.findViewById(R
.id
.shareFileSize
);
145 if (mFile
.isFolder()){
146 size
.setVisibility(View
.GONE
);
148 size
.setText(DisplayUtils
.bytesToHumanReadable(mFile
.getFileLength()));
151 // List of share with users
152 TextView noShares
= (TextView
) view
.findViewById(R
.id
.shareNoUsers
);
154 // TODO: Get shares from DB and show
158 Button addUserGroupButton
= (Button
)
159 view
.findViewById(R
.id
.addUserButton
);
160 addUserGroupButton
.setOnClickListener(new View
.OnClickListener() {
162 public void onClick(View view
) {
163 // Show Search Fragment
164 mListener
.showSearchUsersAndGroups();
172 public void onActivityCreated(Bundle savedInstanceState
) {
173 super.onActivityCreated(savedInstanceState
);
178 // TODO: Rename method, update argument and hook method into UI event
179 public void onButtonPressed(Uri uri
) {
180 if (mListener
!= null
) {
181 mListener
.onShareFragmentInteraction(uri
);
186 public void onAttach(Activity activity
) {
187 super.onAttach(activity
);
189 mListener
= (OnShareFragmentInteractionListener
) activity
;
190 } catch (ClassCastException e
) {
191 throw new ClassCastException(activity
.toString()
192 + " must implement OnShareFragmentInteractionListener");
197 public void onDetach() {
202 // Get users and groups to fill the "share with" list
203 private void getShares(OCFile file
){
204 mShares
= new ArrayList
<>();
206 RemoteOperationResult result
= null
;
209 // TODO: Activate loading
210 // ( (ShareActivity) getActivity()).showWaitingLoadDialog();
211 // Get Users and Groups
212 GetShareWithUserAsyncTask getTask
= new GetShareWithUserAsyncTask(this);
213 FileDataStorageManager fileDataStorageManager
=
214 new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
215 mShares
= fileDataStorageManager
.getSharesWithForAFile(mFile
.getRemotePath(), mAccount
.name
);
217 // Object[] params = { mFile, mAccount, fileDataStorageManager};
218 // getTask.execute(params);
221 // ((ShareActivity) getActivity()).dismissWaitingLoadDialog();
223 // Update list of users/groups
224 updateListOfUserGroups();
228 public void onGetDataShareWithFinish(RemoteOperationResult result
) {
230 ((ShareActivity
) getActivity()).dismissWaitingLoadDialog();
231 if (result
!= null
&& result
.isSuccess()) {
232 // update local database
233 for(Object obj
: result
.getData()) {
234 if ( ((OCShare
) obj
).getShareType() == ShareType
.USER
||
235 ((OCShare
) obj
).getShareType() == ShareType
.GROUP
){
236 mShares
.add((OCShare
) obj
);
240 // Update list of users/groups
241 updateListOfUserGroups();
244 Toast
.makeText(getActivity(), result
.getLogMessage(), Toast
.LENGTH_SHORT
).show();
249 private void updateListOfUserGroups(){
250 // Update list of users/groups
251 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
252 R
.layout
.share_user_item
, mShares
);
255 TextView noShares
= (TextView
) getView().findViewById(R
.id
.shareNoUsers
);
256 ListView usersList
= (ListView
) getView().findViewById(R
.id
.shareUsersList
);
258 if (mShares
.size() > 0) {
259 noShares
.setVisibility(View
.GONE
);
260 usersList
.setVisibility(View
.VISIBLE
);
261 usersList
.setAdapter(mUserGroupsAdapter
);
264 noShares
.setVisibility(View
.VISIBLE
);
265 usersList
.setVisibility(View
.GONE
);
269 // TODO: review if it is necessary
271 * This interface must be implemented by activities that contain this
272 * fragment to allow an interaction in this fragment to be communicated
273 * to the activity and potentially other fragments contained in that
276 * See the Android Training lesson <a href=
277 * "http://developer.android.com/training/basics/fragments/communicating.html"
278 * >Communicating with Other Fragments</a> for more information.
280 public interface OnShareFragmentInteractionListener
{
281 void showSearchUsersAndGroups();
283 void onShareFragmentInteraction(Uri uri
);