X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/c49cc06d3de203308ef7726338aff4a93be89cdd..b5ef1698bd57b124a15db7207e5469b63d8e7ee0:/src/com/owncloud/android/ui/activity/ShareActivity.java diff --git a/src/com/owncloud/android/ui/activity/ShareActivity.java b/src/com/owncloud/android/ui/activity/ShareActivity.java index 03d3b833..d4b964db 100644 --- a/src/com/owncloud/android/ui/activity/ShareActivity.java +++ b/src/com/owncloud/android/ui/activity/ShareActivity.java @@ -25,19 +25,28 @@ import android.app.SearchManager; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; -import android.widget.Toast; import com.owncloud.android.R; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.operations.CreateShareViaLinkOperation; +import com.owncloud.android.operations.GetSharesForFileOperation; import com.owncloud.android.providers.UsersAndGroupsSearchProvider; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.operations.GetSharesForFileOperation; -import com.owncloud.android.operations.UnshareOperation; -import com.owncloud.android.ui.fragment.SearchFragment; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.resources.shares.OCShare; +import com.owncloud.android.lib.resources.shares.ShareType; +import com.owncloud.android.ui.dialog.ShareLinkToDialog; +import com.owncloud.android.ui.fragment.SearchShareesFragment; import com.owncloud.android.ui.fragment.ShareFileFragment; +import com.owncloud.android.utils.GetShareWithUsersAsyncTask; + +import org.apache.http.protocol.HTTP; + /** * Activity for sharing files @@ -45,17 +54,15 @@ import com.owncloud.android.ui.fragment.ShareFileFragment; public class ShareActivity extends FileActivity implements ShareFileFragment.OnShareFragmentInteractionListener, - SearchFragment.OnSearchFragmentInteractionListener { + SearchShareesFragment.OnSearchFragmentInteractionListener { private static final String TAG = ShareActivity.class.getSimpleName(); private static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT"; private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT"; - private static final String DIALOG_WAIT_LOAD_DATA = "DIALOG_WAIT_LOAD_DATA"; - - private ShareFileFragment mShareFileFragment; - private SearchFragment mSearchFragment; + /** Tag for dialog */ + private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -65,54 +72,40 @@ public class ShareActivity extends FileActivity FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - if (savedInstanceState != null) { - - mShareFileFragment = (ShareFileFragment) getSupportFragmentManager(). - getFragment(savedInstanceState, TAG_SHARE_FRAGMENT); - mSearchFragment = (SearchFragment) getSupportFragmentManager(). - getFragment(savedInstanceState, TAG_SEARCH_FRAGMENT); - - if (mShareFileFragment != null){ - ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT); - - if (mSearchFragment != null){ - ft.hide(mShareFileFragment); - ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT); - ft.addToBackStack(TAG_SEARCH_FRAGMENT); - } - ft.commit(); - } - - } else { - // Add Share fragment - mShareFileFragment = ShareFileFragment.newInstance(getFile(), getAccount()); - ft.replace(R.id.share_fragment_container, mShareFileFragment, TAG_SHARE_FRAGMENT); + if (savedInstanceState == null) { + // Add Share fragment on first creation + Fragment fragment = ShareFileFragment.newInstance(getFile(), getAccount()); + ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT); ft.commit(); - - mSearchFragment = null; } - handleIntent(getIntent()); } + protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); - @Override - protected void onNewIntent(Intent intent) { - setIntent(intent); - handleIntent(intent); + // Load data into the list + Log_OC.d(TAG, "Refreshing lists on account set"); + refreshSharesFromStorageManager(); + + // Request for a refresh of the data through the server (starts an Async Task) + refreshUsersOrGroupsListFromServer(); } - private void handleIntent(Intent intent) { + @Override + protected void onNewIntent(Intent intent) { // Verify the action and get the query if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); - doMySearch(query); + Log_OC.w(TAG, "Ignored Intent requesting to query for " + query); } else if (UsersAndGroupsSearchProvider.ACTION_SHARE_WITH.equals(intent.getAction())) { Uri data = intent.getData(); + String dataString = intent.getDataString(); + String shareWith = dataString.substring(dataString.lastIndexOf('/') + 1); doShareWith( - data.getLastPathSegment(), + shareWith, UsersAndGroupsSearchProvider.DATA_GROUP.equals(data.getAuthority()) ); @@ -121,50 +114,42 @@ public class ShareActivity extends FileActivity } } - private void doMySearch(String query) { - // TODO implement , or prevent that search may be sent without choosing from the suggestions list - Toast.makeText(this, "You want to search for [" + query + "]", Toast.LENGTH_SHORT).show(); - } - - private void doShareWith(String username, boolean isGroup) { - // TODO implement - if (isGroup) { - Toast.makeText(this, "You want to SHARE with GROUP [" + username + "]", Toast.LENGTH_SHORT).show(); - - } else { - Toast.makeText(this, "You want to SHARE with USER [" + username + "]", Toast.LENGTH_SHORT).show(); - } + private void doShareWith(String shareeName, boolean isGroup) { + getFileOperationsHelper().shareFileWithSharee( + getFile(), + shareeName, + (isGroup ? ShareType.GROUP : ShareType.USER) + ); } @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - //Save the fragment's instance - getSupportFragmentManager().putFragment(outState, TAG_SHARE_FRAGMENT, mShareFileFragment); - if (mSearchFragment != null) { - getSupportFragmentManager().putFragment(outState, TAG_SEARCH_FRAGMENT, mSearchFragment); - } - - } - - - @Override public void showSearchUsersAndGroups() { + // replace ShareFragment with SearchFragment on demand FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - mSearchFragment = SearchFragment.newInstance(getFile(), getAccount()); - ft.hide(mShareFileFragment); - ft.add(R.id.share_fragment_container, mSearchFragment, TAG_SEARCH_FRAGMENT); - ft.addToBackStack(TAG_SEARCH_FRAGMENT); + Fragment searchFragment = SearchShareesFragment.newInstance(getFile(), getAccount()); + ft.replace(R.id.share_fragment_container, searchFragment, TAG_SEARCH_FRAGMENT); + ft.addToBackStack(null); // BACK button will recover the ShareFragment ft.commit(); } @Override - public void onBackPressed() { - super.onBackPressed(); - if (mSearchFragment != null){ - getSupportFragmentManager().popBackStackImmediate(); - mSearchFragment = null; - } + // Call to Unshare operation + public void unshareWith(OCShare share) { + OCFile file = getFile(); + getFileOperationsHelper().unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith()); + } + + /** + * Get users and groups from the server to fill in the "share with" list + */ + @Override + public void refreshUsersOrGroupsListFromServer() { + // Show loading + showLoadingDialog(getString(R.string.common_loading)); + // Get Users and Groups + GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this); + Object[] params = {getFile(), getAccount(), getStorageManager()}; + getTask.execute(params); } /** @@ -177,31 +162,68 @@ public class ShareActivity extends FileActivity @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { super.onRemoteOperationFinish(operation, result); - if (operation instanceof UnshareOperation) { - if (mShareFileFragment != null){ - mShareFileFragment.refreshUsersOrGroupsListFromDB(); - } - } else if (operation instanceof GetSharesForFileOperation) { - onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result); + + if (result.isSuccess() || + (operation instanceof GetSharesForFileOperation && + result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND + ) + ) { + Log_OC.d(TAG, "Refreshing view on successful operation or finished refresh"); + refreshSharesFromStorageManager(); + } + + if (operation instanceof CreateShareViaLinkOperation) { + // Send link to the app + String link = ((OCShare) (result.getData().get(0))).getShareLink(); + Log_OC.d(TAG, "Share link = " + link); + + Intent intentToShareLink = new Intent(Intent.ACTION_SEND); + intentToShareLink.putExtra(Intent.EXTRA_TEXT, link); + intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE); + String[] packagesToExclude = new String[]{getPackageName()}; + DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude); + chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); } } - private void onGetSharesForFileOperationFinish(GetSharesForFileOperation operation, RemoteOperationResult result){ - dismissLoadingDialog(); - if (!result.isSuccess()) { - Toast.makeText(getApplicationContext(), result.getLogMessage(), Toast.LENGTH_LONG).show(); + /** + * Updates the view, reading data from {@link com.owncloud.android.datamodel.FileDataStorageManager} + */ + private void refreshSharesFromStorageManager() { + + ShareFileFragment shareFileFragment = getShareFileFragment(); + if (shareFileFragment != null + && shareFileFragment.isAdded()) { // only if added to the view hierarchy!! + shareFileFragment.refreshCapabilitiesFromDB(); + shareFileFragment.refreshUsersOrGroupsListFromDB(); + shareFileFragment.refreshPublicShareFromDB(); } - // Show Shares - if (mShareFileFragment != null){ - mShareFileFragment.refreshUsersOrGroupsListFromDB(); + SearchShareesFragment searchShareesFragment = getSearchFragment(); + if (searchShareesFragment != null && + searchShareesFragment.isAdded()) { // only if added to the view hierarchy!! + searchShareesFragment.refreshUsersOrGroupsListFromDB(); } } - @Override - public void onSearchFragmentInteraction(Uri uri) { + /** + * Shortcut to get access to the {@link ShareFileFragment} instance, if any + * + * @return A {@link ShareFileFragment} instance, or null + */ + private ShareFileFragment getShareFileFragment() { + return (ShareFileFragment) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT); + } + /** + * Shortcut to get access to the {@link SearchShareesFragment} instance, if any + * + * @return A {@link SearchShareesFragment} instance, or null + */ + private SearchShareesFragment getSearchFragment() { + return (SearchShareesFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT); } + }