ShareFileFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.fragment;
  22. import android.accounts.Account;
  23. import android.app.Activity;
  24. import android.graphics.Bitmap;
  25. import android.os.Bundle;
  26. import android.support.v4.app.Fragment;
  27. import android.support.v7.widget.AppCompatButton;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.ViewGroup;
  31. import android.widget.Button;
  32. import android.widget.CompoundButton;
  33. import android.widget.ImageView;
  34. import android.widget.ListView;
  35. import android.widget.Switch;
  36. import android.widget.TextView;
  37. import android.widget.Toast;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.authentication.AccountUtils;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  42. import com.owncloud.android.lib.common.utils.Log_OC;
  43. import com.owncloud.android.lib.resources.shares.OCShare;
  44. import com.owncloud.android.lib.resources.shares.ShareType;
  45. import com.owncloud.android.ui.activity.FileActivity;
  46. import com.owncloud.android.ui.adapter.ShareUserListAdapter;
  47. import com.owncloud.android.utils.DisplayUtils;
  48. import com.owncloud.android.utils.MimetypeIconUtil;
  49. import java.util.ArrayList;
  50. /**
  51. * Fragment for Sharing a file with sharees (users or groups) or creating
  52. * a public link.
  53. *
  54. * A simple {@link Fragment} subclass.
  55. *
  56. * Activities that contain this fragment must implement the
  57. * {@link ShareFileFragment.OnShareFragmentInteractionListener} interface
  58. * to handle interaction events.
  59. *
  60. * Use the {@link ShareFileFragment#newInstance} factory method to
  61. * create an instance of this fragment.
  62. */
  63. public class ShareFileFragment extends Fragment
  64. implements ShareUserListAdapter.ShareUserAdapterListener{
  65. private static final String TAG = ShareFileFragment.class.getSimpleName();
  66. // the fragment initialization parameters
  67. private static final String ARG_FILE = "FILE";
  68. private static final String ARG_ACCOUNT = "ACCOUNT";
  69. /** File to share, received as a parameter in construction time */
  70. private OCFile mFile;
  71. /** OC account holding the file to share, received as a parameter in construction time */
  72. private Account mAccount;
  73. /** Reference to parent listener */
  74. private OnShareFragmentInteractionListener mListener;
  75. /** List of private shares bound to the file */
  76. private ArrayList<OCShare> mPrivateShares;
  77. /** Adapter to show private shares */
  78. private ShareUserListAdapter mUserGroupsAdapter = null;
  79. /** Public share bound to the file */
  80. private OCShare mPublicShare;
  81. /** Listener for changes on switch to share / unshare publicly */
  82. private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener;
  83. /**
  84. * Public factory method to create new ShareFileFragment instances.
  85. *
  86. * @param fileToShare An {@link OCFile} to show in the fragment
  87. * @param account An ownCloud account
  88. * @return A new instance of fragment ShareFileFragment.
  89. */
  90. public static ShareFileFragment newInstance(OCFile fileToShare, Account account) {
  91. ShareFileFragment fragment = new ShareFileFragment();
  92. Bundle args = new Bundle();
  93. args.putParcelable(ARG_FILE, fileToShare);
  94. args.putParcelable(ARG_ACCOUNT, account);
  95. fragment.setArguments(args);
  96. return fragment;
  97. }
  98. public ShareFileFragment() {
  99. // Required empty public constructor
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. @Override
  105. public void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. if (getArguments() != null) {
  108. mFile = getArguments().getParcelable(ARG_FILE);
  109. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  110. }
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. @Override
  116. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  117. Bundle savedInstanceState) {
  118. // Inflate the layout for this fragment
  119. View view = inflater.inflate(R.layout.share_file_layout, container, false);
  120. // Setup layout
  121. // Image
  122. ImageView icon = (ImageView) view.findViewById(R.id.shareFileIcon);
  123. icon.setImageResource(MimetypeIconUtil.getFileTypeIconId(mFile.getMimetype(),
  124. mFile.getFileName()));
  125. if (mFile.isImage()) {
  126. String remoteId = String.valueOf(mFile.getRemoteId());
  127. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
  128. if (thumbnail != null) {
  129. icon.setImageBitmap(thumbnail);
  130. }
  131. }
  132. // Name
  133. TextView filename = (TextView) view.findViewById(R.id.shareFileName);
  134. filename.setText(mFile.getFileName());
  135. // Size
  136. TextView size = (TextView) view.findViewById(R.id.shareFileSize);
  137. if (mFile.isFolder()) {
  138. size.setVisibility(View.GONE);
  139. } else {
  140. size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength()));
  141. }
  142. // Add User Button
  143. Button addUserGroupButton = (Button)
  144. view.findViewById(R.id.addUserButton);
  145. addUserGroupButton.setOnClickListener(new View.OnClickListener() {
  146. @Override
  147. public void onClick(View view) {
  148. boolean shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
  149. if (shareWithUsersEnable) {
  150. // Show Search Fragment
  151. mListener.showSearchUsersAndGroups();
  152. } else {
  153. String message = getString(R.string.share_sharee_unavailable);
  154. Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
  155. }
  156. }
  157. });
  158. // Switch to create public share
  159. mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
  160. @Override
  161. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  162. if (isResumed()) {
  163. if (isChecked) {
  164. ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile);
  165. } else {
  166. ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(mFile);
  167. }
  168. } // else, nothing; very important, setCheched(...) is called automatically during Fragment
  169. // recreation on device rotations
  170. }
  171. };
  172. Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
  173. shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
  174. // Switch for expiration date
  175. Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
  176. shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  177. @Override
  178. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  179. if (isChecked) {
  180. // TODO real implementation: update share with expiration date
  181. // show value of expiration date
  182. getExpirationDateValue().setText(R.string.placeholder_timestamp);
  183. } else {
  184. // TODO real implementation: update share without expiration date
  185. // empty value
  186. getExpirationDateValue().setText(R.string.empty);
  187. }
  188. }
  189. });
  190. // Switch for password
  191. Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
  192. shareViaLinkPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  193. @Override
  194. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  195. if (isChecked) {
  196. // TODO real implementation: update share with password
  197. // show
  198. getExpirationPasswordValue().setVisibility(View.VISIBLE);
  199. } else {
  200. // TODO real implementation: update share without password
  201. // empty value
  202. getExpirationPasswordValue().setVisibility(View.INVISIBLE);
  203. }
  204. }
  205. });
  206. return view;
  207. }
  208. @Override
  209. public void onActivityCreated(Bundle savedInstanceState) {
  210. super.onActivityCreated(savedInstanceState);
  211. // Load data into the list of private shares
  212. refreshUsersOrGroupsListFromDB();
  213. // Load data of public share, if exists
  214. refreshPublicShareFromDB();
  215. }
  216. @Override
  217. public void onAttach(Activity activity) {
  218. super.onAttach(activity);
  219. try {
  220. mListener = (OnShareFragmentInteractionListener) activity;
  221. } catch (ClassCastException e) {
  222. throw new ClassCastException(activity.toString()
  223. + " must implement OnShareFragmentInteractionListener");
  224. }
  225. }
  226. @Override
  227. public void onDetach() {
  228. super.onDetach();
  229. mListener = null;
  230. }
  231. /**
  232. * Get users and groups from the DB to fill in the "share with" list
  233. *
  234. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  235. * instance ready to use. If not ready, does nothing.
  236. */
  237. public void refreshUsersOrGroupsListFromDB (){
  238. if (((FileActivity) mListener).getStorageManager() != null) {
  239. // Get Users and Groups
  240. mPrivateShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
  241. mFile.getRemotePath(),
  242. mAccount.name
  243. );
  244. // Update list of users/groups
  245. updateListOfUserGroups();
  246. }
  247. }
  248. private void updateListOfUserGroups() {
  249. // Update list of users/groups
  250. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  251. mUserGroupsAdapter = new ShareUserListAdapter(
  252. getActivity(),
  253. R.layout.share_user_item,
  254. mPrivateShares,
  255. this
  256. );
  257. // Show data
  258. TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers);
  259. ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList);
  260. if (mPrivateShares.size() > 0) {
  261. noShares.setVisibility(View.GONE);
  262. usersList.setVisibility(View.VISIBLE);
  263. usersList.setAdapter(mUserGroupsAdapter);
  264. } else {
  265. noShares.setVisibility(View.VISIBLE);
  266. usersList.setVisibility(View.GONE);
  267. }
  268. }
  269. @Override
  270. public void unshareButtonPressed(OCShare share) {
  271. // Unshare
  272. mListener.unshareWith(share);
  273. Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
  274. }
  275. /**
  276. * Get public link from the DB to fill in the "Share link" section
  277. *
  278. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  279. * instance ready to use. If not ready, does nothing.
  280. */
  281. public void refreshPublicShareFromDB() {
  282. if (((FileActivity) mListener).getStorageManager() != null) {
  283. // Get public share
  284. mPublicShare = ((FileActivity) mListener).getStorageManager().getFirstShareByPathAndType(
  285. mFile.getRemotePath(),
  286. ShareType.PUBLIC_LINK,
  287. ""
  288. );
  289. // Update list of users/groups
  290. updatePublicShareSection();
  291. }
  292. }
  293. /**
  294. * Updates in the UI the section about public share with the information in the current
  295. * public share bound to mFile, if any
  296. */
  297. private void updatePublicShareSection() {
  298. if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
  299. // public share bound -> expand section
  300. Switch shareViaLinkSwitch = getShareViaLinkSwitch();
  301. if (!shareViaLinkSwitch.isChecked()) {
  302. // set null listener before setChecked() to prevent infinite loop of calls
  303. shareViaLinkSwitch.setOnCheckedChangeListener(null);
  304. getShareViaLinkSwitch().setChecked(true);
  305. shareViaLinkSwitch.setOnCheckedChangeListener(
  306. mOnShareViaLinkSwitchCheckedChangeListener
  307. );
  308. }
  309. getExpirationDateSection().setVisibility(View.VISIBLE);
  310. getPasswordSection().setVisibility(View.VISIBLE);
  311. getGetLinkButton().setVisibility(View.VISIBLE);
  312. } else {
  313. // no public share -> collapse section
  314. Switch shareViaLinkSwitch = getShareViaLinkSwitch();
  315. if (shareViaLinkSwitch.isChecked()) {
  316. shareViaLinkSwitch.setOnCheckedChangeListener(null);
  317. getShareViaLinkSwitch().setChecked(false);
  318. shareViaLinkSwitch.setOnCheckedChangeListener(
  319. mOnShareViaLinkSwitchCheckedChangeListener
  320. );
  321. }
  322. getExpirationDateSection().setVisibility(View.GONE);
  323. getPasswordSection().setVisibility(View.GONE);
  324. getGetLinkButton().setVisibility(View.GONE);
  325. }
  326. }
  327. private Switch getShareViaLinkSwitch() {
  328. return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
  329. }
  330. private View getExpirationDateSection() {
  331. return getView().findViewById(R.id.shareViaLinkExpirationSection);
  332. }
  333. private TextView getExpirationDateValue() {
  334. return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
  335. }
  336. private View getPasswordSection() {
  337. return getView().findViewById(R.id.shareViaLinkPasswordSection);
  338. }
  339. private TextView getExpirationPasswordValue() {
  340. return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
  341. }
  342. private AppCompatButton getGetLinkButton() {
  343. return (AppCompatButton) getView().findViewById(R.id.shareViewLinkGetLinkButton);
  344. }
  345. /**
  346. * This interface must be implemented by activities that contain this
  347. * fragment to allow an interaction in this fragment to be communicated
  348. * to the activity and potentially other fragments contained in that
  349. * activity.
  350. * <p/>
  351. * See the Android Training lesson <a href=
  352. * "http://developer.android.com/training/basics/fragments/communicating.html"
  353. * >Communicating with Other Fragments</a> for more information.
  354. */
  355. public interface OnShareFragmentInteractionListener {
  356. void showSearchUsersAndGroups();
  357. void refreshUsersOrGroupsListFromServer();
  358. void unshareWith(OCShare share);
  359. }
  360. }