8cbc04e418473d6c07e70a3289f49e220390e23c
[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.UnshareOperation;
38 import com.owncloud.android.ui.fragment.SearchFragment;
39 import com.owncloud.android.ui.fragment.ShareFileFragment;
40
41 /**
42 * Activity for sharing files
43 */
44
45 public class ShareActivity extends FileActivity
46 implements ShareFileFragment.OnShareFragmentInteractionListener,
47 SearchFragment.OnSearchFragmentInteractionListener {
48
49 private static final String TAG = ShareActivity.class.getSimpleName();
50
51 private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
52 private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
53
54 private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA";
55
56 private ShareFileFragment mShareFileFragment;
57 private SearchFragment mSearchFragment;
58
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 setContentView(R.layout.share_activity);
63
64 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
65
66 if (savedInstanceState != null) {
67
68 mShareFileFragment = (ShareFileFragment) getSupportFragmentManager().
69 getFragment(savedInstanceState, TAG_SHARE_FRAGMENT);
70 mSearchFragment = (SearchFragment) getSupportFragmentManager().
71 getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT);
72
73 if (mShareFileFragment != null){
74 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
75
76 if (mSearchFragment != null){
77 ft.hide(mShareFileFragment);
78 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
79 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
80 }
81 ft.commit();
82 }
83
84 } else {
85 // Add Share fragment
86 mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount());
87 ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT);
88 ft.commit();
89
90 mSearchFragment = null;
91 }
92
93 handleIntent(getIntent());
94 }
95
96
97 @Override
98 protected void onNewIntent(Intent intent) {
99 setIntent(intent);
100 handleIntent(intent);
101 }
102
103
104 private void handleIntent(Intent intent) {
105 // Verify the action and get the query
106 if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
107 String query = intent.getStringExtra(SearchManager.QUERY);
108 doMySearch(query);
109
110 } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) {
111 Uri data = intent.getData();
112 doShareWith(
113 data.getLastPathSegment(),
114 UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority())
115 );
116
117 } else {
118 Log_OC.wtf(TAG, "Unexpected intent " + intent.toString());
119 }
120 }
121
122 private void doMySearch(String query) {
123 // TODO implement , or prevent that search may be sent without choosing from the suggestions list
124 Toast.makeText(this, "You want to search for [" + query + "]", Toast.LENGTH_SHORT).show();
125 }
126
127 private void doShareWith(String username, boolean isGroup) {
128 // TODO implement
129 if (isGroup) {
130 Toast.makeText(this, "You want to SHARE with GROUP [" + username + "]", Toast.LENGTH_SHORT).show();
131
132 } else {
133 Toast.makeText(this, "You want to SHARE with USER [" + username + "]", Toast.LENGTH_SHORT).show();
134 }
135 }
136
137 @Override
138 protected void onSaveInstanceState(Bundle outState) {
139 super.onSaveInstanceState(outState);
140 //Save the fragment's instance
141 getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment);
142 if (mSearchFragment != null) {
143 getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment);
144 }
145
146 }
147
148 @Override
149 public void showSearchUsersAndGroups() {
150 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
151 mSearchFragment = SearchFragment.newInstance(getFile(), getAccount());
152 ft.hide(mShareFileFragment);
153 ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT);
154 ft.addToBackStack(TAG_SEARCH_FRAGMENT);
155 ft.commit();
156 }
157
158 @Override
159 public void onBackPressed() {
160 super.onBackPressed();
161 if (mSearchFragment != null){
162 getSupportFragmentManager().popBackStackImmediate();
163 mSearchFragment = null;
164 }
165 }
166
167 /**
168 * Updates the view associated to the activity after the finish of some operation over files
169 * in the current account.
170 *
171 * @param operation Removal operation performed.
172 * @param result Result of the removal.
173 */
174 @Override
175 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
176 super.onRemoteOperationFinish(operation, result);
177 if (operation instanceof UnshareOperation) {
178 if (mShareFileFragment != null){
179 mShareFileFragment.refreshUsersOrGroupsList();
180 }
181 }
182
183 }
184
185 @Override
186 public void onShareFragmentInteraction(Uri uri) {
187
188 }
189
190 @Override
191 public void onSearchFragmentInteraction(Uri uri) {
192
193 }
194 }