ShareActivity.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.activity;
  22. import android.app.SearchManager;
  23. import android.content.Intent;
  24. import android.net.Uri;
  25. import android.os.Bundle;
  26. import android.support.v4.app.DialogFragment;
  27. import android.support.v4.app.Fragment;
  28. import android.support.v4.app.FragmentTransaction;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.lib.common.utils.Log_OC;
  31. import com.owncloud.android.operations.CreateShareViaLinkOperation;
  32. import com.owncloud.android.operations.GetSharesForFileOperation;
  33. import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
  34. import com.owncloud.android.lib.common.operations.RemoteOperation;
  35. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  36. import com.owncloud.android.datamodel.OCFile;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.lib.resources.shares.ShareType;
  39. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  40. import com.owncloud.android.ui.fragment.SearchShareesFragment;
  41. import com.owncloud.android.ui.fragment.ShareFileFragment;
  42. import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
  43. import org.apache.http.protocol.HTTP;
  44. /**
  45. * Activity for sharing files
  46. */
  47. public class ShareActivity extends FileActivity
  48. implements ShareFileFragment.OnShareFragmentInteractionListener,
  49. SearchShareesFragment.OnSearchFragmentInteractionListener {
  50. private static final String TAG = ShareActivity.class.getSimpleName();
  51. private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
  52. private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
  53. /** Tag for dialog */
  54. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.share_activity);
  59. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  60. if (savedInstanceState == null) {
  61. // Add Share fragment on first creation
  62. Fragment fragment = ShareFileFragment.newInstance(getFile(), getAccount());
  63. ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT);
  64. ft.commit();
  65. }
  66. }
  67. protected void onAccountSet(boolean stateWasRecovered) {
  68. super.onAccountSet(stateWasRecovered);
  69. // Load data into the list
  70. Log_OC.d(TAG, "Refreshing lists on account set");
  71. refreshSharesFromStorageManager();
  72. // Request for a refresh of the data through the server (starts an Async Task)
  73. refreshUsersOrGroupsListFromServer();
  74. }
  75. @Override
  76. protected void onNewIntent(Intent intent) {
  77. // Verify the action and get the query
  78. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  79. String query = intent.getStringExtra(SearchManager.QUERY);
  80. Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
  81. } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
  82. Uri data = intent.getData();
  83. String dataString = intent.getDataString();
  84. String shareWith = dataString.substring(dataString.lastIndexOf('/') + 1);
  85. doShareWith(
  86. shareWith,
  87. UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
  88. );
  89. } else {
  90. Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
  91. }
  92. }
  93. private void doShareWith(String shareeName, boolean isGroup) {
  94. getFileOperationsHelper().shareFileWithSharee(
  95. getFile(),
  96. shareeName,
  97. (isGroup ? ShareType.GROUP : ShareType.USER)
  98. );
  99. }
  100. @Override
  101. public void showSearchUsersAndGroups() {
  102. // replace ShareFragment with SearchFragment on demand
  103. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  104. Fragment searchFragment = SearchShareesFragment.newInstance(getFile(), getAccount());
  105. ft.replace(R.id.share_fragment_container, searchFragment, TAG_SEARCH_FRAGMENT);
  106. ft.addToBackStack(null); // BACK button will recover the ShareFragment
  107. ft.commit();
  108. }
  109. @Override
  110. // Call to Unshare operation
  111. public void unshareWith(OCShare share) {
  112. OCFile file = getFile();
  113. getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
  114. }
  115. /**
  116. * Get users and groups from the server to fill in the "share with" list
  117. */
  118. @Override
  119. public void refreshUsersOrGroupsListFromServer() {
  120. // Show loading
  121. showLoadingDialog(getString(R.string.common_loading));
  122. // Get Users and Groups
  123. GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
  124. Object[] params = {getFile(), getAccount(), getStorageManager()};
  125. getTask.execute(params);
  126. }
  127. /**
  128. * Updates the view associated to the activity after the finish of some operation over files
  129. * in the current account.
  130. *
  131. * @param operation Removal operation performed.
  132. * @param result Result of the removal.
  133. */
  134. @Override
  135. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  136. super.onRemoteOperationFinish(operation, result);
  137. if (result.isSuccess() ||
  138. (operation instanceof GetSharesForFileOperation &&
  139. result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
  140. )
  141. ) {
  142. Log_OC.d(TAG, "Refreshing view on successful operation or finished refresh");
  143. refreshSharesFromStorageManager();
  144. }
  145. if (operation instanceof CreateShareViaLinkOperation && result.isSuccess()) {
  146. // Send link to the app
  147. String link = ((OCShare) (result.getData().get(0))).getShareLink();
  148. Log_OC.d(TAG, "Share link = " + link);
  149. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  150. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  151. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  152. String[] packagesToExclude = new String[]{getPackageName()};
  153. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude);
  154. chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  155. }
  156. }
  157. /**
  158. * Updates the view, reading data from {@link com.owncloud.android.datamodel.FileDataStorageManager}
  159. */
  160. private void refreshSharesFromStorageManager() {
  161. ShareFileFragment shareFileFragment = getShareFileFragment();
  162. if (shareFileFragment != null
  163. && shareFileFragment.isAdded()) { // only if added to the view hierarchy!!
  164. shareFileFragment.refreshCapabilitiesFromDB();
  165. shareFileFragment.refreshUsersOrGroupsListFromDB();
  166. shareFileFragment.refreshPublicShareFromDB();
  167. }
  168. SearchShareesFragment searchShareesFragment = getSearchFragment();
  169. if (searchShareesFragment != null &&
  170. searchShareesFragment.isAdded()) { // only if added to the view hierarchy!!
  171. searchShareesFragment.refreshUsersOrGroupsListFromDB();
  172. }
  173. }
  174. /**
  175. * Shortcut to get access to the {@link ShareFileFragment} instance, if any
  176. *
  177. * @return A {@link ShareFileFragment} instance, or null
  178. */
  179. private ShareFileFragment getShareFileFragment() {
  180. return (ShareFileFragment) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT);
  181. }
  182. /**
  183. * Shortcut to get access to the {@link SearchShareesFragment} instance, if any
  184. *
  185. * @return A {@link SearchShareesFragment} instance, or null
  186. */
  187. private SearchShareesFragment getSearchFragment() {
  188. return (SearchShareesFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT);
  189. }
  190. }