ShareActivity.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.FragmentTransaction;
  27. import android.widget.Toast;
  28. import com.owncloud.android.R;
  29. import com.owncloud.android.lib.common.utils.Log_OC;
  30. import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
  31. import com.owncloud.android.lib.common.operations.RemoteOperation;
  32. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  33. import com.owncloud.android.operations.GetSharesForFileOperation;
  34. import com.owncloud.android.lib.resources.shares.ShareType;
  35. import com.owncloud.android.operations.UnshareOperation;
  36. import com.owncloud.android.ui.fragment.SearchFragment;
  37. import com.owncloud.android.ui.fragment.ShareFileFragment;
  38. /**
  39. * Activity for sharing files
  40. */
  41. public class ShareActivity extends FileActivity
  42. implements ShareFileFragment.OnShareFragmentInteractionListener,
  43. SearchFragment.OnSearchFragmentInteractionListener {
  44. private static final String TAG = ShareActivity.class.getSimpleName();
  45. private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
  46. private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
  47. private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
  48. private ShareFileFragment mShareFileFragment;
  49. private SearchFragment mSearchFragment;
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.share_activity);
  54. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  55. if (savedInstanceState != null) {
  56. mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
  57. getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
  58. mSearchFragment = (SearchFragment) getSupportFragmentManager().
  59. getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
  60. if (mShareFileFragment != null){
  61. ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
  62. if (mSearchFragment != null){
  63. ft.hide(mShareFileFragment);
  64. ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
  65. ft.addToBackStack(TAG_SEARCH_FRAGMENT);
  66. }
  67. ft.commit();
  68. }
  69. } else {
  70. // Add Share fragment
  71. mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount());
  72. ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
  73. ft.commit();
  74. mSearchFragment = null;
  75. }
  76. handleIntent(getIntent());
  77. }
  78. @Override
  79. protected void onNewIntent(Intent intent) {
  80. setIntent(intent);
  81. handleIntent(intent);
  82. }
  83. private void handleIntent(Intent intent) {
  84. // Verify the action and get the query
  85. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  86. String query = intent.getStringExtra(SearchManager.QUERY);
  87. Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
  88. } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
  89. Uri data = intent.getData();
  90. doShareWith(
  91. data.getLastPathSegment(),
  92. UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
  93. );
  94. } else {
  95. Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
  96. }
  97. }
  98. private void doShareWith(String shareeName, boolean isGroup) {
  99. if (isGroup) {
  100. Toast.makeText(this, "You want to SHARE with GROUP [" + shareeName + "]", Toast.LENGTH_SHORT).show();
  101. } else {
  102. Toast.makeText(this, "You want to SHARE with USER [" + shareeName + "]", Toast.LENGTH_SHORT).show();
  103. }
  104. getFileOperationsHelper().shareFileWithSharee(
  105. getFile(),
  106. shareeName,
  107. (isGroup ? ShareType.GROUP : ShareType.USER )
  108. );
  109. }
  110. @Override
  111. protected void onSaveInstanceState(Bundle outState) {
  112. super.onSaveInstanceState(outState);
  113. //Save the fragment's instance
  114. getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment);
  115. if (mSearchFragment != null) {
  116. getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment);
  117. }
  118. }
  119. @Override
  120. public void showSearchUsersAndGroups() {
  121. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  122. mSearchFragment = SearchFragment.newInstance(getFile(), getAccount());
  123. ft.hide(mShareFileFragment);
  124. ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
  125. ft.addToBackStack(TAG_SEARCH_FRAGMENT);
  126. ft.commit();
  127. }
  128. @Override
  129. public void onBackPressed() {
  130. super.onBackPressed();
  131. if (mSearchFragment != null){
  132. getSupportFragmentManager().popBackStackImmediate();
  133. mSearchFragment = null;
  134. mShareFileFragment.refreshUsersOrGroupsListFromDB();
  135. }
  136. }
  137. /**
  138. * Updates the view associated to the activity after the finish of some operation over files
  139. * in the current account.
  140. *
  141. * @param operation Removal operation performed.
  142. * @param result Result of the removal.
  143. */
  144. @Override
  145. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  146. super.onRemoteOperationFinish(operation, result);
  147. if (operation instanceof UnshareOperation) {
  148. if (mShareFileFragment != null){
  149. mShareFileFragment.refreshUsersOrGroupsListFromDB();
  150. }
  151. } else if (operation instanceof GetSharesForFileOperation) {
  152. onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result);
  153. }
  154. }
  155. private void onGetSharesForFileOperationFinish(GetSharesForFileOperation operation, RemoteOperationResult result){
  156. dismissLoadingDialog();
  157. if (!result.isSuccess()) {
  158. Toast.makeText(getApplicationContext(), result.getLogMessage(), Toast.LENGTH_LONG).show();
  159. }
  160. // Show Shares
  161. if (mShareFileFragment != null){
  162. mShareFileFragment.refreshUsersOrGroupsListFromDB();
  163. }
  164. }
  165. @Override
  166. public void onSearchFragmentInteraction(Uri uri) {
  167. }
  168. }