2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.ui
.activity
;
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
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import com
.owncloud
.android
.providers
.UsersAndGroupsSearchProvider
;
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
.operations
.CreateShareWithShareeOperation
;
41 import com
.owncloud
.android
.operations
.UnshareOperation
;
42 import com
.owncloud
.android
.ui
.fragment
.SearchFragment
;
43 import com
.owncloud
.android
.ui
.fragment
.ShareFileFragment
;
44 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
45 import com
.owncloud
.android
.utils
.GetShareWithUsersAsyncTask
;
47 import java
.util
.ArrayList
;
50 * Activity for sharing files
53 public class ShareActivity
extends FileActivity
54 implements GetShareWithUsersAsyncTask
.OnGetSharesWithUsersTaskListener
,
55 ShareFileFragment
.OnShareFragmentInteractionListener
,
56 SearchFragment
.OnSearchFragmentInteractionListener
{
58 private static final String TAG
= ShareActivity
.class.getSimpleName();
60 private static final String TAG_SHARE_FRAGMENT
= "SHARE_FRAGMENT";
61 private static final String TAG_SEARCH_FRAGMENT
= "SEARCH_USER_AND_GROUPS_FRAGMENT";
63 private static final String DIALOG_WAIT_LOAD_DATA
= "DIALOG_WAIT_LOAD_DATA";
65 private ShareFileFragment mShareFileFragment
;
66 private SearchFragment mSearchFragment
;
69 protected void onCreate(Bundle savedInstanceState
) {
70 super.onCreate(savedInstanceState
);
73 setContentView(R
.layout
.share_activity
);
75 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
77 if (savedInstanceState
!= null
) {
79 mShareFileFragment
= (ShareFileFragment
) getSupportFragmentManager().
80 getFragment(savedInstanceState
, TAG_SHARE_FRAGMENT
);
81 mSearchFragment
= (SearchFragment
) getSupportFragmentManager().
82 getFragment(savedInstanceState
, TAG_SEARCH_FRAGMENT
);
84 if (mShareFileFragment
!= null
){
85 ft
.replace(R
.id
.share_fragment_container
, mShareFileFragment
, TAG_SHARE_FRAGMENT
);
87 if (mSearchFragment
!= null
){
88 ft
.hide(mShareFileFragment
);
89 ft
.add(R
.id
.share_fragment_container
, mSearchFragment
, TAG_SEARCH_FRAGMENT
);
96 mShareFileFragment
= ShareFileFragment
.newInstance(getFile(), getAccount());
97 ft
.replace(R
.id
.share_fragment_container
, mShareFileFragment
, TAG_SHARE_FRAGMENT
);
100 mSearchFragment
= null
;
103 handleIntent(getIntent());
110 protected void onNewIntent(Intent intent
) {
112 handleIntent(intent
);
116 private void handleIntent(Intent intent
) {
117 // Verify the action and get the query
118 if (Intent
.ACTION_SEARCH
.equals(intent
.getAction())) {
119 String query
= intent
.getStringExtra(SearchManager
.QUERY
);
120 Log_OC
.w(TAG
, "Ignored Intent requesting to query for " + query
);
122 } else if (UsersAndGroupsSearchProvider
.ACTION_SHARE_WITH
.equals(intent
.getAction())) {
123 Uri data
= intent
.getData();
125 data
.getLastPathSegment(),
126 UsersAndGroupsSearchProvider
.DATA_GROUP
.equals(data
.getAuthority())
130 Log_OC
.wtf(TAG
, "Unexpected intent " + intent
.toString());
134 private void doShareWith(String shareeName
, boolean isGroup
) {
135 getFileOperationsHelper().shareFileWithSharee(
138 (isGroup ? ShareType
.GROUP
: ShareType
.USER
)
143 protected void onSaveInstanceState(Bundle outState
) {
144 super.onSaveInstanceState(outState
);
145 //Save the fragment's instance
146 getSupportFragmentManager().putFragment(outState
, TAG_SHARE_FRAGMENT
, mShareFileFragment
);
147 if (mSearchFragment
!= null
) {
148 getSupportFragmentManager().putFragment(outState
, TAG_SEARCH_FRAGMENT
, mSearchFragment
);
154 public void showSearchUsersAndGroups(ArrayList
<OCShare
> shares
) {
155 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
156 mSearchFragment
= SearchFragment
.newInstance(getFile(), getAccount(), shares
);
157 ft
.hide(mShareFileFragment
);
158 ft
.add(R
.id
.share_fragment_container
, mSearchFragment
, TAG_SEARCH_FRAGMENT
);
159 ft
.addToBackStack(TAG_SEARCH_FRAGMENT
);
164 // Call to Unshare operation
165 public void unshareWith(OCShare share
){
166 OCFile file
= getFile();
167 getFileOperationsHelper().unshareFileWithUserOrGroup(file
, share
.getShareType(), share
.getShareWith());
171 * Get users and groups from the server to fill in the "share with" list
174 public void refreshUsersOrGroupsListFromServer(){
176 showLoadingDialog(getString(R
.string
.common_loading
));
177 // Get Users and Groups
178 GetShareWithUsersAsyncTask getTask
= new GetShareWithUsersAsyncTask(this);
179 Object
[] params
= { getFile(), getAccount(), getStorageManager()};
180 getTask
.execute(params
);
184 public void onBackPressed() {
185 super.onBackPressed();
186 if (mSearchFragment
!= null
){
187 mSearchFragment
= null
;
188 getSupportFragmentManager().popBackStackImmediate();
189 mShareFileFragment
.refreshUsersOrGroupsListFromDB();
194 * Updates the view associated to the activity after the finish of some operation over files
195 * in the current account.
197 * @param operation Removal operation performed.
198 * @param result Result of the removal.
201 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
202 super.onRemoteOperationFinish(operation
, result
);
203 if (operation
instanceof UnshareOperation
||
204 operation
instanceof CreateShareWithShareeOperation
) {
206 if (result
.isSuccess()) {
207 refreshUsersInLists();
208 if (operation
instanceof CreateShareWithShareeOperation
) {
210 getIntent().setAction(null
);
215 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
220 /*} else if (operation instanceof GetSharesForFileOperation) {
221 onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result);*/
226 public void onGetDataShareWithFinish(RemoteOperationResult result
) {
228 dismissLoadingDialog();
229 if (result
!= null
&& result
.isSuccess()) {
230 Log_OC
.d(TAG
, "Get Data Share With finishes sucessfully");
231 } // else, ignore and use pre-cached shares in database
233 // Data is on Database
234 refreshUsersInLists();
237 private void refreshUsersInLists(){
238 if (mShareFileFragment
!= null
){
239 mShareFileFragment
.refreshUsersOrGroupsListFromDB();
241 if (mSearchFragment
!= null
) {
242 mSearchFragment
.refreshUsersOrGroupsListFromDB();