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
.Fragment
;
29 import android
.support
.v4
.app
.FragmentTransaction
;
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
.ui
.fragment
.SearchShareesFragment
;
41 import com
.owncloud
.android
.ui
.fragment
.ShareFileFragment
;
42 import com
.owncloud
.android
.utils
.GetShareWithUsersAsyncTask
;
46 * Activity for sharing files
49 public class ShareActivity
extends FileActivity
50 implements ShareFileFragment
.OnShareFragmentInteractionListener
,
51 SearchShareesFragment
.OnSearchFragmentInteractionListener
{
53 private static final String TAG
= ShareActivity
.class.getSimpleName();
55 private static final String TAG_SHARE_FRAGMENT
= "SHARE_FRAGMENT";
56 private static final String TAG_SEARCH_FRAGMENT
= "SEARCH_USER_AND_GROUPS_FRAGMENT";
60 protected void onCreate(Bundle savedInstanceState
) {
61 super.onCreate(savedInstanceState
);
63 setContentView(R
.layout
.share_activity
);
65 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
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
);
76 protected void onAccountSet(boolean stateWasRecovered
) {
77 super.onAccountSet(stateWasRecovered
);
79 // Load data into the list
80 Log_OC
.d(TAG
, "Refreshing lists on account set");
81 refreshUsersInLists();
83 // Request for a refresh of the data through the server (starts an Async Task)
84 refreshUsersOrGroupsListFromServer();
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
);
95 } else if (UsersAndGroupsSearchProvider
.ACTION_SHARE_WITH
.equals(intent
.getAction())) {
96 Uri data
= intent
.getData();
98 data
.getLastPathSegment(),
99 UsersAndGroupsSearchProvider
.DATA_GROUP
.equals(data
.getAuthority())
103 Log_OC
.wtf(TAG
, "Unexpected intent " + intent
.toString());
107 private void doShareWith(String shareeName
, boolean isGroup
) {
108 getFileOperationsHelper().shareFileWithSharee(
111 (isGroup ? ShareType
.GROUP
: ShareType
.USER
)
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
126 // Call to Unshare operation
127 public void unshareWith(OCShare share
) {
128 OCFile file
= getFile();
129 getFileOperationsHelper().unshareFileWithUserOrGroup(file
, share
.getShareType(), share
.getShareWith());
133 * Get users and groups from the server to fill in the "share with" list
136 public void refreshUsersOrGroupsListFromServer() {
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
);
146 * Updates the view associated to the activity after the finish of some operation over files
147 * in the current account.
149 * @param operation Removal operation performed.
150 * @param result Result of the removal.
153 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
154 super.onRemoteOperationFinish(operation
, result
);
156 if (result
.isSuccess()) {
157 Log_OC
.d(TAG
, "Refreshing lists on successful sync");
158 refreshUsersInLists();
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();
171 SearchShareesFragment searchShareesFragment
= getSearchFragment();
172 if (searchShareesFragment
!= null
) {
173 if (searchShareesFragment
.isAdded()) { // only if added to the view hierarchy!!
174 searchShareesFragment
.refreshUsersOrGroupsListFromDB();
180 * Shortcut to get access to the {@link ShareFileFragment} instance, if any
182 * @return A {@link ShareFileFragment} instance, or null
184 private ShareFileFragment
getShareFileFragment() {
185 return (ShareFileFragment
) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT
);
189 * Shortcut to get access to the {@link SearchShareesFragment} instance, if any
191 * @return A {@link SearchShareesFragment} instance, or null
193 private SearchShareesFragment
getSearchFragment() {
194 return (SearchShareesFragment
) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT
);