Merge branch 'material_buttons' of https://github.com/owncloud/android into material_fab
[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.Fragment;
29 import android.support.v4.app.FragmentTransaction;
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.ui.fragment.SearchShareesFragment;
41 import com.owncloud.android.ui.fragment.ShareFileFragment;
42 import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
43
44
45 /**
46 * Activity for sharing files
47 */
48
49 public class ShareActivity extends FileActivity
50 implements ShareFileFragment.OnShareFragmentInteractionListener,
51 SearchShareesFragment.OnSearchFragmentInteractionListener {
52
53 private static final String TAG = ShareActivity.class.getSimpleName();
54
55 private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
56 private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
57
58
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 setContentView(R.layout.share_activity);
64
65 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
66
67 if (savedInstanceState == null) {
68 // Add Share fragment on first creation
69 Fragment fragment = ShareFileFragment.newInstance(getFile(), getAccount());
70 ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT);
71 ft.commit();
72 }
73
74 }
75
76 protected void onAccountSet(boolean stateWasRecovered) {
77 super.onAccountSet(stateWasRecovered);
78
79 // Load data into the list
80 Log_OC.d(TAG, "Refreshing lists on account set");
81 refreshUsersInLists();
82
83 // Request for a refresh of the data through the server (starts an Async Task)
84 refreshUsersOrGroupsListFromServer();
85 }
86
87
88 @Override
89 protected void onNewIntent(Intent intent) {
90 // Verify the action and get the query
91 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
92 String query = intent.getStringExtra(SearchManager.QUERY);
93 Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
94
95 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
96 Uri data = intent.getData();
97 doShareWith(
98 data.getLastPathSegment(),
99 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
100 );
101
102 } else {
103 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
104 }
105 }
106
107 private void doShareWith(String shareeName, boolean isGroup) {
108 getFileOperationsHelper().shareFileWithSharee(
109 getFile(),
110 shareeName,
111 (isGroup ? ShareType.GROUP : ShareType.USER)
112 );
113 }
114
115 @Override
116 public void showSearchUsersAndGroups() {
117 // replace ShareFragment with SearchFragment on demand
118 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
119 Fragment searchFragment = SearchShareesFragment.newInstance(getFile(), getAccount());
120 ft.replace(R.id.share_fragment_container, searchFragment, TAG_SEARCH_FRAGMENT);
121 ft.addToBackStack(null); // BACK button will recover the ShareFragment
122 ft.commit();
123 }
124
125 @Override
126 // Call to Unshare operation
127 public void unshareWith(OCShare share) {
128 OCFile file = getFile();
129 getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
130 }
131
132 /**
133 * Get users and groups from the server to fill in the "share with" list
134 */
135 @Override
136 public void refreshUsersOrGroupsListFromServer() {
137 // Show loading
138 showLoadingDialog(getString(R.string.common_loading));
139 // Get Users and Groups
140 GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this);
141 Object[] params = {getFile(), getAccount(), getStorageManager()};
142 getTask.execute(params);
143 }
144
145 /**
146 * Updates the view associated to the activity after the finish of some operation over files
147 * in the current account.
148 *
149 * @param operation Removal operation performed.
150 * @param result Result of the removal.
151 */
152 @Override
153 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
154 super.onRemoteOperationFinish(operation, result);
155
156 if (result.isSuccess()) {
157 Log_OC.d(TAG, "Refreshing lists on successful sync");
158 refreshUsersInLists();
159 }
160
161 }
162
163 private void refreshUsersInLists() {
164 ShareFileFragment shareFileFragment = getShareFileFragment();
165 if (shareFileFragment != null) { // only if added to the view hierarchy!!
166 if (shareFileFragment.isAdded()) {
167 shareFileFragment.refreshUsersOrGroupsListFromDB();
168 }
169 }
170
171 SearchShareesFragment searchShareesFragment = getSearchFragment();
172 if (searchShareesFragment != null) {
173 if (searchShareesFragment.isAdded()) { // only if added to the view hierarchy!!
174 searchShareesFragment.refreshUsersOrGroupsListFromDB();
175 }
176 }
177 }
178
179 /**
180 * Shortcut to get access to the {@link ShareFileFragment} instance, if any
181 *
182 * @return A {@link ShareFileFragment} instance, or null
183 */
184 private ShareFileFragment getShareFileFragment() {
185 return (ShareFileFragment) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT);
186 }
187
188 /**
189 * Shortcut to get access to the {@link SearchShareesFragment} instance, if any
190 *
191 * @return A {@link SearchShareesFragment} instance, or null
192 */
193 private SearchShareesFragment getSearchFragment() {
194 return (SearchShareesFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT);
195 }
196
197 }