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