7d98d7a43a41544e77eb5c9d61ceda3d3f7b3314
[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 ShareFileFragment.OnShareFragmentInteractionListener,
55 SearchFragment.OnSearchFragmentInteractionListener {
56
57 private static final String TAG = ShareActivity.class.getSimpleName();
58
59 private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
60 private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
61
62 private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
63
64 private ShareFileFragment mShareFileFragment;
65 private SearchFragment mSearchFragment;
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 onAccountSet(false);
71
72 setContentView(R.layout.share_activity);
73
74 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
75
76 if (savedInstanceState != null) {
77
78 mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
79 getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
80 mSearchFragment = (SearchFragment) getSupportFragmentManager().
81 getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
82
83 if (mShareFileFragment != null){
84 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
85
86 if (mSearchFragment != null){
87 ft.hide(mShareFileFragment);
88 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
89 }
90 ft.commit();
91 }
92
93 } else {
94 // Add Share fragment
95 mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount());
96 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
97 ft.commit();
98
99 mSearchFragment = null;
100 }
101
102 handleIntent(getIntent());
103
104
105 }
106
107
108 @Override
109 protected void onNewIntent(Intent intent) {
110 setIntent(intent);
111 handleIntent(intent);
112 }
113
114
115 private void handleIntent(Intent intent) {
116 // Verify the action and get the query
117 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
118 String query = intent.getStringExtra(SearchManager.QUERY);
119 Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
120
121 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
122 Uri data = intent.getData();
123 doShareWith(
124 data.getLastPathSegment(),
125 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
126 );
127
128 } else {
129 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
130 }
131 }
132
133 private void doShareWith(String shareeName, boolean isGroup) {
134 getFileOperationsHelper().shareFileWithSharee(
135 getFile(),
136 shareeName,
137 (isGroup ? ShareType.GROUP : ShareType.USER)
138 );
139 }
140
141 @Override
142 protected void onSaveInstanceState(Bundle outState) {
143 super.onSaveInstanceState(outState);
144 //Save the fragment's instance
145 getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment);
146 if (mSearchFragment != null) {
147 getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment);
148 }
149
150 }
151
152 @Override
153 public void showSearchUsersAndGroups(ArrayList<OCShare> shares) {
154 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
155 mSearchFragment = SearchFragment.newInstance(getFile(), getAccount(), shares);
156 ft.hide(mShareFileFragment);
157 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
158 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
159 ft.commit();
160 }
161
162 @Override
163 // Call to Unshare operation
164 public void unshareWith(OCShare share){
165 OCFile file = getFile();
166 getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
167 }
168
169 /**
170 * Get users and groups from the server to fill in the "share with" list
171 */
172 @Override
173 public void refreshUsersOrGroupsListFromServer(){
174 // Show loading
175 showLoadingDialog(getString(R.string.common_loading));
176 // Get Users and Groups
177 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
178 Object[] params = { getFile(), getAccount(), getStorageManager()};
179 getTask.execute(params);
180 }
181
182 @Override
183 public void onBackPressed() {
184 super.onBackPressed();
185 if (mSearchFragment != null){
186 mSearchFragment = null;
187 getSupportFragmentManager().popBackStackImmediate();
188 mShareFileFragment.refreshUsersOrGroupsListFromDB();
189 }
190 }
191
192 /**
193 * Updates the view associated to the activity after the finish of some operation over files
194 * in the current account.
195 *
196 * @param operation Removal operation performed.
197 * @param result Result of the removal.
198 */
199 @Override
200 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
201 super.onRemoteOperationFinish(operation, result);
202
203 if (result.isSuccess()) {
204 refreshUsersInLists();
205 if (operation instanceof CreateShareWithShareeOperation) {
206 // Clean action
207 getIntent().setAction(null);
208 }
209 }
210
211 }
212
213 private void refreshUsersInLists(){
214 if (mShareFileFragment != null){
215 mShareFileFragment.refreshUsersOrGroupsListFromDB();
216 }
217 if (mSearchFragment != null) {
218 mSearchFragment.refreshUsersOrGroupsListFromDB();
219 }
220 }
221
222 }