Hide UI elements about public share when this is disabled in the server
[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.DialogFragment;
29 import android.support.v4.app.Fragment;
30 import android.support.v4.app.FragmentTransaction;
31
32 import com.owncloud.android.R;
33 import com.owncloud.android.lib.common.utils.Log_OC;
34 import com.owncloud.android.operations.CreateShareViaLinkOperation;
35 import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
36
37 import com.owncloud.android.lib.common.operations.RemoteOperation;
38 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.lib.resources.shares.OCShare;
41 import com.owncloud.android.lib.resources.shares.ShareType;
42 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
43 import com.owncloud.android.ui.fragment.SearchShareesFragment;
44 import com.owncloud.android.ui.fragment.ShareFileFragment;
45 import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
46
47 import org.apache.http.protocol.HTTP;
48
49
50 /**
51 * Activity for sharing files
52 */
53
54 public class ShareActivity extends FileActivity
55 implements ShareFileFragment.OnShareFragmentInteractionListener,
56 SearchShareesFragment.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 /** Tag for dialog */
64 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
65
66 @Override
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69
70 setContentView(R.layout.share_activity);
71
72 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
73
74 if (savedInstanceState == null) {
75 // Add Share fragment on first creation
76 Fragment fragment = ShareFileFragment.newInstance(getFile(), getAccount());
77 ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT);
78 ft.commit();
79 }
80
81 }
82
83 protected void onAccountSet(boolean stateWasRecovered) {
84 super.onAccountSet(stateWasRecovered);
85
86 // Load data into the list
87 Log_OC.d(TAG, "Refreshing lists on account set");
88 refreshSharesFromStorageManager();
89
90 // Request for a refresh of the data through the server (starts an Async Task)
91 refreshUsersOrGroupsListFromServer();
92 }
93
94
95 @Override
96 protected void onNewIntent(Intent intent) {
97 // Verify the action and get the query
98 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
99 String query = intent.getStringExtra(SearchManager.QUERY);
100 Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
101
102 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
103 Uri data = intent.getData();
104 doShareWith(
105 data.getLastPathSegment(),
106 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
107 );
108
109 } else {
110 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
111 }
112 }
113
114 private void doShareWith(String shareeName, boolean isGroup) {
115 getFileOperationsHelper().shareFileWithSharee(
116 getFile(),
117 shareeName,
118 (isGroup ? ShareType.GROUP : ShareType.USER)
119 );
120 }
121
122 @Override
123 public void showSearchUsersAndGroups() {
124 // replace ShareFragment with SearchFragment on demand
125 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
126 Fragment searchFragment = SearchShareesFragment.newInstance(getFile(), getAccount());
127 ft.replace(R.id.share_fragment_container, searchFragment, TAG_SEARCH_FRAGMENT);
128 ft.addToBackStack(null); // BACK button will recover the ShareFragment
129 ft.commit();
130 }
131
132 @Override
133 // Call to Unshare operation
134 public void unshareWith(OCShare share) {
135 OCFile file = getFile();
136 getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
137 }
138
139 /**
140 * Get users and groups from the server to fill in the "share with" list
141 */
142 @Override
143 public void refreshUsersOrGroupsListFromServer() {
144 // Show loading
145 showLoadingDialog(getString(R.string.common_loading));
146 // Get Users and Groups
147 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
148 Object[] params = {getFile(), getAccount(), getStorageManager()};
149 getTask.execute(params);
150 }
151
152 /**
153 * Updates the view associated to the activity after the finish of some operation over files
154 * in the current account.
155 *
156 * @param operation Removal operation performed.
157 * @param result Result of the removal.
158 */
159 @Override
160 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
161 super.onRemoteOperationFinish(operation, result);
162
163 if (result.isSuccess()) {
164 Log_OC.d(TAG, "Refreshing view on successful operation");
165 refreshSharesFromStorageManager();
166
167 if (operation instanceof CreateShareViaLinkOperation) {
168 // Send link to the app
169 String link = ((OCShare) (result.getData().get(0))).getShareLink();
170 Log_OC.d(TAG, "Share link = " + link);
171
172 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
173 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
174 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
175 String[] packagesToExclude = new String[]{getPackageName()};
176 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude);
177 chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
178 }
179 }
180 }
181
182
183 /**
184 * Updates the view, reading data from {@link com.owncloud.android.datamodel.FileDataStorageManager}
185 */
186 private void refreshSharesFromStorageManager() {
187
188 ShareFileFragment shareFileFragment = getShareFileFragment();
189 if (shareFileFragment != null
190 && shareFileFragment.isAdded()) { // only if added to the view hierarchy!!
191 shareFileFragment.refreshCapabilitiesFromDB();
192 shareFileFragment.refreshUsersOrGroupsListFromDB();
193 shareFileFragment.refreshPublicShareFromDB();
194 }
195
196 SearchShareesFragment searchShareesFragment = getSearchFragment();
197 if (searchShareesFragment != null &&
198 searchShareesFragment.isAdded()) { // only if added to the view hierarchy!!
199 searchShareesFragment.refreshUsersOrGroupsListFromDB();
200 }
201 }
202
203 /**
204 * Shortcut to get access to the {@link ShareFileFragment} instance, if any
205 *
206 * @return A {@link ShareFileFragment} instance, or null
207 */
208 private ShareFileFragment getShareFileFragment() {
209 return (ShareFileFragment) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT);
210 }
211
212 /**
213 * Shortcut to get access to the {@link SearchShareesFragment} instance, if any
214 *
215 * @return A {@link SearchShareesFragment} instance, or null
216 */
217 private SearchShareesFragment getSearchFragment() {
218 return (SearchShareesFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT);
219 }
220
221 }