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