Share the file with a user or group when selected on the list of suggestions
[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.operations.GetSharesForFileOperation;
38 import com.owncloud.android.lib.resources.shares.ShareType;
39 import com.owncloud.android.operations.UnshareOperation;
40 import com.owncloud.android.ui.fragment.SearchFragment;
41 import com.owncloud.android.ui.fragment.ShareFileFragment;
42
43 /**
44 * Activity for sharing files
45 */
46
47 public class ShareActivity extends FileActivity
48 implements ShareFileFragment.OnShareFragmentInteractionListener,
49 SearchFragment.OnSearchFragmentInteractionListener {
50
51 private static final String TAG = ShareActivity.class.getSimpleName();
52
53 private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
54 private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
55
56 private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
57
58 private ShareFileFragment mShareFileFragment;
59 private SearchFragment mSearchFragment;
60
61 @Override
62 protected void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64
65 setContentView(R.layout.share_activity);
66
67 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
68
69 if (savedInstanceState != null) {
70
71 mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
72 getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
73 mSearchFragment = (SearchFragment) getSupportFragmentManager().
74 getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
75
76 if (mShareFileFragment != null){
77 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
78
79 if (mSearchFragment != null){
80 ft.hide(mShareFileFragment);
81 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
82 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
83 }
84 ft.commit();
85 }
86
87 } else {
88 // Add Share fragment
89 mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount());
90 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
91 ft.commit();
92
93 mSearchFragment = null;
94 }
95
96 handleIntent(getIntent());
97 }
98
99
100 @Override
101 protected void onNewIntent(Intent intent) {
102 setIntent(intent);
103 handleIntent(intent);
104 }
105
106
107 private void handleIntent(Intent intent) {
108 // Verify the action and get the query
109 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
110 String query = intent.getStringExtra(SearchManager.QUERY);
111 Log_OC.w(TAG, "Ignored Intent requesting to query for " + query);
112
113 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
114 Uri data = intent.getData();
115 doShareWith(
116 data.getLastPathSegment(),
117 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
118 );
119
120 } else {
121 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
122 }
123 }
124
125 private void doShareWith(String shareeName, boolean isGroup) {
126 if (isGroup) {
127 Toast.makeText(this, "You want to SHARE with GROUP [" + shareeName + "]", Toast.LENGTH_SHORT).show();
128 } else {
129 Toast.makeText(this, "You want to SHARE with USER [" + shareeName + "]", Toast.LENGTH_SHORT).show();
130 }
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
150 @Override
151 public void showSearchUsersAndGroups() {
152 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
153 mSearchFragment = SearchFragment.newInstance(getFile(), getAccount());
154 ft.hide(mShareFileFragment);
155 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
156 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
157 ft.commit();
158 }
159
160 @Override
161 public void onBackPressed() {
162 super.onBackPressed();
163 if (mSearchFragment != null){
164 getSupportFragmentManager().popBackStackImmediate();
165 mSearchFragment = null;
166 mShareFileFragment.refreshUsersOrGroupsList();
167 }
168 }
169
170 /**
171 * Updates the view associated to the activity after the finish of some operation over files
172 * in the current account.
173 *
174 * @param operation Removal operation performed.
175 * @param result Result of the removal.
176 */
177 @Override
178 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
179 super.onRemoteOperationFinish(operation, result);
180 if (operation instanceof UnshareOperation) {
181 if (mShareFileFragment != null){
182 mShareFileFragment.refreshUsersOrGroupsListFromDB();
183 }
184 } else if (operation instanceof GetSharesForFileOperation) {
185 onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result);
186 }
187
188 }
189
190 private void onGetSharesForFileOperationFinish(GetSharesForFileOperation operation, RemoteOperationResult result){
191 dismissLoadingDialog();
192
193 if (!result.isSuccess()) {
194 Toast.makeText(getApplicationContext(), result.getLogMessage(), Toast.LENGTH_LONG).show();
195 }
196
197 // Show Shares
198 if (mShareFileFragment != null){
199 mShareFileFragment.refreshUsersOrGroupsListFromDB();
200 }
201 }
202
203 @Override
204 public void onSearchFragmentInteraction(Uri uri) {
205
206 }
207 }