579443320a10bcfd4445b2af9fa3b45a391fea68
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / ShareActivity.java
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
22 package com.owncloud.android.ui.activity;
23
24 import android.app.SearchManager;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.support.v4.app.FragmentTransaction;
29 import android.widget.Toast;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.lib.common.utils.Log_OC;
33 import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
34
35 import com.owncloud.android.lib.common.operations.RemoteOperation;
36 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.lib.resources.shares.OCShare;
39 import com.owncloud.android.lib.resources.shares.ShareType;
40 import com.owncloud.android.operations.CreateShareWithShareeOperation;
41 import com.owncloud.android.operations.UnshareOperation;
42 import com.owncloud.android.ui.fragment.SearchFragment;
43 import com.owncloud.android.ui.fragment.ShareFileFragment;
44 import com.owncloud.android.utils.ErrorMessageAdapter;
45 import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
46
47 import java.util.ArrayList;
48
49 /**
50 * Activity for sharing files
51 */
52
53 public class ShareActivity extends FileActivity
54 implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener,
55 ShareFileFragment.OnShareFragmentInteractionListener,
56 SearchFragment.OnSearchFragmentInteractionListener {
57
58 private static final String TAG = ShareActivity.class.getSimpleName();
59
60 private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
61 private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
62
63 private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
64
65 private ShareFileFragment mShareFileFragment;
66 private SearchFragment mSearchFragment;
67
68 @Override
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 onAccountSet(false);
72
73 setContentView(R.layout.share_activity);
74
75 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
76
77 if (savedInstanceState != null) {
78
79 mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
80 getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
81 mSearchFragment = (SearchFragment) getSupportFragmentManager().
82 getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
83
84 if (mShareFileFragment != null){
85 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
86
87 if (mSearchFragment != null){
88 ft.hide(mShareFileFragment);
89 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
90 }
91 ft.commit();
92 }
93
94 } else {
95 // Add Share fragment
96 mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount());
97 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
98 ft.commit();
99
100 mSearchFragment = null;
101 }
102
103 handleIntent(getIntent());
104
105
106 }
107
108
109 @Override
110 protected void onNewIntent(Intent intent) {
111 setIntent(intent);
112 handleIntent(intent);
113 }
114
115
116 private void handleIntent(Intent intent) {
117 // Verify the action and get the query
118 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
119 String query = intent.getStringExtra(SearchManager.QUERY);
120 Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
121
122 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
123 Uri data = intent.getData();
124 doShareWith(
125 data.getLastPathSegment(),
126 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
127 );
128
129 } else {
130 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
131 }
132 }
133
134 private void doShareWith(String shareeName, boolean isGroup) {
135 getFileOperationsHelper().shareFileWithSharee(
136 getFile(),
137 shareeName,
138 (isGroup ? ShareType.GROUP : ShareType.USER)
139 );
140 }
141
142 @Override
143 protected void onSaveInstanceState(Bundle outState) {
144 super.onSaveInstanceState(outState);
145 //Save the fragment's instance
146 getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment);
147 if (mSearchFragment != null) {
148 getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment);
149 }
150
151 }
152
153 @Override
154 public void showSearchUsersAndGroups(ArrayList<OCShare> shares) {
155 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
156 mSearchFragment = SearchFragment.newInstance(getFile(), getAccount(), shares);
157 ft.hide(mShareFileFragment);
158 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
159 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
160 ft.commit();
161 }
162
163 @Override
164 // Call to Unshare operation
165 public void unshareWith(OCShare share){
166 OCFile file = getFile();
167 getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
168 }
169
170 /**
171 * Get users and groups from the server to fill in the "share with" list
172 */
173 @Override
174 public void refreshUsersOrGroupsListFromServer(){
175 // Show loading
176 showLoadingDialog(getString(R.string.common_loading));
177 // Get Users and Groups
178 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
179 Object[] params = { getFile(), getAccount(), getStorageManager()};
180 getTask.execute(params);
181 }
182
183 @Override
184 public void onBackPressed() {
185 super.onBackPressed();
186 if (mSearchFragment != null){
187 mSearchFragment = null;
188 getSupportFragmentManager().popBackStackImmediate();
189 mShareFileFragment.refreshUsersOrGroupsListFromDB();
190 }
191 }
192
193 /**
194 * Updates the view associated to the activity after the finish of some operation over files
195 * in the current account.
196 *
197 * @param operation Removal operation performed.
198 * @param result Result of the removal.
199 */
200 @Override
201 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
202 super.onRemoteOperationFinish(operation, result);
203 if (operation instanceof UnshareOperation ||
204 operation instanceof CreateShareWithShareeOperation) {
205
206 if (result.isSuccess()) {
207 refreshUsersInLists();
208 if (operation instanceof CreateShareWithShareeOperation) {
209 // Clean action
210 getIntent().setAction(null);
211 }
212 } else {
213 Toast.makeText(
214 this,
215 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
216 Toast.LENGTH_LONG
217 ).show();
218 }
219
220 /*} else if (operation instanceof GetSharesForFileOperation) {
221 onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result);*/
222 }
223 }
224
225 @Override
226 public void onGetDataShareWithFinish(RemoteOperationResult result) {
227 // Remove loading
228 dismissLoadingDialog();
229 if (result != null && result.isSuccess()) {
230 Log_OC.d(TAG, "Get Data Share With finishes sucessfully");
231 } // else, ignore and use pre-cached shares in database
232
233 // Data is on Database
234 refreshUsersInLists();
235 }
236
237 private void refreshUsersInLists(){
238 if (mShareFileFragment != null){
239 mShareFileFragment.refreshUsersOrGroupsListFromDB();
240 }
241 if (mSearchFragment != null) {
242 mSearchFragment.refreshUsersOrGroupsListFromDB();
243 }
244 }
245
246 }