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
.GetShareWithUsersAsyncTask
;
46 import java
.util
.ArrayList
;
49 * Activity for sharing files
52 public class ShareActivity
extends FileActivity
53 implements GetShareWithUsersAsyncTask
.OnGetSharesWithUsersTaskListener
,
54 ShareFileFragment
.OnShareFragmentInteractionListener
,
55 SearchFragment
.OnSearchFragmentInteractionListener
{
57 private static final String TAG
= ShareActivity
.class.getSimpleName();
59 private static final String TAG_SHARE_FRAGMENT
= "SHARE_FRAGMENT";
60 private static final String TAG_SEARCH_FRAGMENT
= "SEARCH_USER_AND_GROUPS_FRAGMENT";
62 private static final String DIALOG_WAIT_LOAD_DATA
= "DIALOG_WAIT_LOAD_DATA";
64 private ShareFileFragment mShareFileFragment
;
65 private SearchFragment mSearchFragment
;
68 protected void onCreate(Bundle savedInstanceState
) {
69 super.onCreate(savedInstanceState
);
72 setContentView(R
.layout
.share_activity
);
74 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
76 if (savedInstanceState
!= null
) {
78 mShareFileFragment
= (ShareFileFragment
) getSupportFragmentManager().
79 getFragment(savedInstanceState
, TAG_SHARE_FRAGMENT
);
80 mSearchFragment
= (SearchFragment
) getSupportFragmentManager().
81 getFragment(savedInstanceState
, TAG_SEARCH_FRAGMENT
);
83 if (mShareFileFragment
!= null
){
84 ft
.replace(R
.id
.share_fragment_container
, mShareFileFragment
, TAG_SHARE_FRAGMENT
);
86 if (mSearchFragment
!= null
){
87 ft
.hide(mShareFileFragment
);
88 ft
.add(R
.id
.share_fragment_container
, mSearchFragment
, TAG_SEARCH_FRAGMENT
);
95 mShareFileFragment
= ShareFileFragment
.newInstance(getFile(), getAccount());
96 ft
.replace(R
.id
.share_fragment_container
, mShareFileFragment
, TAG_SHARE_FRAGMENT
);
99 mSearchFragment
= null
;
102 handleIntent(getIntent());
109 protected void onNewIntent(Intent intent
) {
111 handleIntent(intent
);
115 private void handleIntent(Intent intent
) {
116 // Verify the action and get the query
117 if (Intent
.ACTION_SEARCH
.equals(intent
.getAction())) {
118 String query
= intent
.getStringExtra(SearchManager
.QUERY
);
119 Log_OC
.w(TAG
, "Ignored Intent requesting to query for " + query
);
121 } else if (UsersAndGroupsSearchProvider
.ACTION_SHARE_WITH
.equals(intent
.getAction())) {
122 Uri data
= intent
.getData();
124 data
.getLastPathSegment(),
125 UsersAndGroupsSearchProvider
.DATA_GROUP
.equals(data
.getAuthority())
129 Log_OC
.wtf(TAG
, "Unexpected intent " + intent
.toString());
133 private void doShareWith(String shareeName
, boolean isGroup
) {
135 Log_OC
.d(TAG
, "You want to SHARE with GROUP [" + shareeName
+ "]");
137 Log_OC
.d(TAG
, "You want to SHARE with USER [" + shareeName
+ "]");
139 getFileOperationsHelper().shareFileWithSharee(
142 (isGroup ? ShareType
.GROUP
: ShareType
.USER
)
147 protected void onSaveInstanceState(Bundle outState
) {
148 super.onSaveInstanceState(outState
);
149 //Save the fragment's instance
150 getSupportFragmentManager().putFragment(outState
, TAG_SHARE_FRAGMENT
, mShareFileFragment
);
151 if (mSearchFragment
!= null
) {
152 getSupportFragmentManager().putFragment(outState
, TAG_SEARCH_FRAGMENT
, mSearchFragment
);
158 public void showSearchUsersAndGroups(ArrayList
<OCShare
> shares
) {
159 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
160 mSearchFragment
= SearchFragment
.newInstance(getFile(), getAccount(), shares
);
161 ft
.hide(mShareFileFragment
);
162 ft
.add(R
.id
.share_fragment_container
, mSearchFragment
, TAG_SEARCH_FRAGMENT
);
163 ft
.addToBackStack(TAG_SEARCH_FRAGMENT
);
168 // Call to Unshare operation
169 public void unshareWith(OCShare share
){
170 OCFile file
= getFile();
171 getFileOperationsHelper().unshareFileWithUserOrGroup(file
, share
.getShareType(), share
.getShareWith());
175 * Get users and groups from the server to fill in the "share with" list
178 public void refreshUsersOrGroupsListFromServer(){
180 showLoadingDialog(getString(R
.string
.common_loading
));
181 // Get Users and Groups
182 GetShareWithUsersAsyncTask getTask
= new GetShareWithUsersAsyncTask(this);
183 Object
[] params
= { getFile(), getAccount(), getStorageManager()};
184 getTask
.execute(params
);
188 public void onBackPressed() {
189 super.onBackPressed();
190 if (mSearchFragment
!= null
){
191 mSearchFragment
= null
;
192 getSupportFragmentManager().popBackStackImmediate();
193 mShareFileFragment
.refreshUsersOrGroupsListFromDB();
198 * Updates the view associated to the activity after the finish of some operation over files
199 * in the current account.
201 * @param operation Removal operation performed.
202 * @param result Result of the removal.
205 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
206 super.onRemoteOperationFinish(operation
, result
);
207 if (operation
instanceof UnshareOperation
) {
208 refreshUsersInLists();
209 } else if(operation
instanceof CreateShareWithShareeOperation
){
210 refreshUsersInLists();
212 getIntent().setAction(null
);
217 public void onGetDataShareWithFinish(RemoteOperationResult result
) {
219 dismissLoadingDialog();
220 if (result
!= null
&& result
.isSuccess()) {
221 Log_OC
.d(TAG
, "Get Data Share With finishes sucessfully");
224 Toast
.makeText(this, result
.getLogMessage(), Toast
.LENGTH_SHORT
).show();
227 // Data is on Database
228 refreshUsersInLists();
231 private void refreshUsersInLists(){
232 if (mShareFileFragment
!= null
){
233 mShareFileFragment
.refreshUsersOrGroupsListFromDB();
235 if (mSearchFragment
!= null
) {
236 mSearchFragment
.refreshUsersOrGroupsListFromDB();