3078bf86d83daa4dcfa60189bd15ae1172da9958
[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.GetShareWithUsersAsyncTask;
45
46 import java.util.ArrayList;
47
48 /**
49 * Activity for sharing files
50 */
51
52 public class ShareActivity extends FileActivity
53 implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener,
54 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
71 setContentView(R.layout.share_activity);
72
73 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
74
75 if (savedInstanceState != null) {
76
77 mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
78 getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
79 mSearchFragment = (SearchFragment) getSupportFragmentManager().
80 getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
81
82 if (mShareFileFragment != null){
83 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
84
85 if (mSearchFragment != null){
86 ft.hide(mShareFileFragment);
87 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
88 ft.addToBackStack(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 onAccountSet(false);
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 if (isGroup) {
135 Toast.makeText(this, "You want to SHARE with GROUP [" + shareeName + "]", Toast.LENGTH_SHORT).show();
136 } else {
137 Toast.makeText(this, "You want to SHARE with USER [" + shareeName + "]", Toast.LENGTH_SHORT).show();
138 }
139 getFileOperationsHelper().shareFileWithSharee(
140 getFile(),
141 shareeName,
142 (isGroup ? ShareType.GROUP : ShareType.USER)
143 );
144 }
145
146 @Override
147 protected void onSaveInstanceState(Bundle outState) {
148 super.onSaveInstanceState(outState);
149 //Save the fragment's instance
150 getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment);
151 if (mSearchFragment != null) {
152 getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment);
153 }
154
155 }
156
157 @Override
158 public void showSearchUsersAndGroups(ArrayList<OCShare> shares) {
159 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
160 mSearchFragment = SearchFragment.newInstance(getFile(), getAccount(), shares);
161 ft.hide(mShareFileFragment);
162 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
163 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
164 ft.commit();
165 }
166
167 @Override
168 // Call to Unshare operation
169 public void unshareWith(OCShare share){
170 OCFile file = getFile();
171 getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
172 }
173
174 /**
175 * Get users and groups from the server to fill in the "share with" list
176 */
177 @Override
178 public void refreshUsersOrGroupsListFromServer(){
179 // Show loading
180 showLoadingDialog(getString(R.string.common_loading));
181 // Get Users and Groups
182 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
183 Object[] params = { getFile(), getAccount(), getStorageManager()};
184 getTask.execute(params);
185 }
186
187 @Override
188 public void onBackPressed() {
189 super.onBackPressed();
190 if (mSearchFragment != null){
191 getSupportFragmentManager().popBackStackImmediate();
192 mSearchFragment = null;
193 mShareFileFragment.refreshUsersOrGroupsListFromDB();
194 }
195 }
196
197 /**
198 * Updates the view associated to the activity after the finish of some operation over files
199 * in the current account.
200 *
201 * @param operation Removal operation performed.
202 * @param result Result of the removal.
203 */
204 @Override
205 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
206 super.onRemoteOperationFinish(operation, result);
207 if (operation instanceof UnshareOperation) {
208 refreshUsersInLists();
209 } else if(operation instanceof CreateShareWithShareeOperation){
210 refreshUsersInLists();
211 }
212 }
213
214 @Override
215 public void onGetDataShareWithFinish(RemoteOperationResult result) {
216 // Remove loading
217 dismissLoadingDialog();
218 if (result != null && result.isSuccess()) {
219 Log_OC.d(TAG, "Get Data Share With finishes sucessfully");
220
221 } else {
222 Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_SHORT).show();
223 }
224
225 // Data is on Database
226 refreshUsersInLists();
227 }
228
229 private void refreshUsersInLists(){
230 if (mShareFileFragment != null){
231 mShareFileFragment.refreshUsersOrGroupsListFromDB();
232 }
233 if (mSearchFragment != null) {
234 mSearchFragment.refreshUsersOrGroupsListFromDB();
235 }
236 }
237
238 }