123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- /**
- * ownCloud Android client application
- *
- * @author masensio
- * @author David A. Velasco
- * Copyright (C) 2015 ownCloud Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package com.owncloud.android.ui.fragment;
- import android.accounts.Account;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.Switch;
- import android.widget.TextView;
- import com.owncloud.android.R;
- import com.owncloud.android.datamodel.OCFile;
- import com.owncloud.android.lib.common.operations.RemoteOperationResult;
- import com.owncloud.android.lib.common.utils.Log_OC;
- import com.owncloud.android.lib.resources.shares.OCShare;
- import com.owncloud.android.lib.resources.shares.SharePermissionsBuilder;
- import com.owncloud.android.ui.activity.FileActivity;
- public class EditShareFragment extends Fragment {
- private static final String TAG = EditShareFragment.class.getSimpleName();
- /** The fragment initialization parameters */
- private static final String ARG_SHARE = "SHARE";
- private static final String ARG_FILE = "FILE";
- private static final String ARG_ACCOUNT = "ACCOUNT";
- /** Ids of CheckBoxes depending on R.id.canEdit CheckBox */
- private static final int sSubordinateCheckBoxIds[] = {
- R.id.canEditCreateCheckBox,
- R.id.canEditChangeCheckBox,
- R.id.canEditDeleteCheckBox
- };
- /** Share to show & edit, received as a parameter in construction time */
- private OCShare mShare;
- /** File bound to mShare, received as a parameter in construction time */
- private OCFile mFile;
- /** OC account holding the shared file, received as a parameter in construction time */
- private Account mAccount;
- /** Listener for changes on privilege checkboxes */
- private CompoundButton.OnCheckedChangeListener mOnPrivilegeChangeListener;
- /**
- * Public factory method to create new EditShareFragment instances.
- *
- * @param shareToEdit An {@link OCShare} to show and edit in the fragment
- * @param sharedFile The {@link OCFile} bound to 'shareToEdit'
- * @param account The ownCloud account holding 'sharedFile'
- * @return A new instance of fragment EditShareFragment.
- */
- public static EditShareFragment newInstance(OCShare shareToEdit, OCFile sharedFile, Account account) {
- EditShareFragment fragment = new EditShareFragment();
- Bundle args = new Bundle();
- args.putParcelable(ARG_SHARE, shareToEdit);
- args.putParcelable(ARG_FILE, sharedFile);
- args.putParcelable(ARG_ACCOUNT, account);
- fragment.setArguments(args);
- return fragment;
- }
- /**
- * Required empty public constructor
- */
- public EditShareFragment() {
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log_OC.d(TAG, "onCreate");
- if (getArguments() != null) {
- mShare = getArguments().getParcelable(ARG_SHARE);
- mFile = getArguments().getParcelable(ARG_FILE);
- mAccount = getArguments().getParcelable(ARG_ACCOUNT);
- }
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- Log_OC.d(TAG, "onActivityCreated");
- getActivity().setTitle(mShare.getSharedWithDisplayName());
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- Log_OC.d(TAG, "onCreateView");
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.edit_share_layout, container, false);
- // Setup layout
- initPrivileges(view);
- initUnshareButton(view);
- initDoneButton(view);
- return view;
- }
- /**
- * Binds listener for user actions to enable or disable a privilege on the edited share
- * to the views receiving the user events.
- *
- * @param editShareView Root view in the fragment.
- */
- private void initPrivileges(View editShareView) {
- boolean setListener = false;
- if (mOnPrivilegeChangeListener == null) {
- mOnPrivilegeChangeListener = new OnPrivilegeChangeListener();
- setListener = true;
- }
- int sharePermissions = mShare.getPermissions();
- CompoundButton compound;
- compound = (CompoundButton) editShareView.findViewById(R.id.canShareSwitch);
- compound.setChecked((sharePermissions & OCShare.SHARE_PERMISSION_FLAG) > 0);
- if (setListener) {
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- compound = (CompoundButton) editShareView.findViewById(R.id.canEditSwitch);
- int anyUpdatePermission =
- OCShare.CREATE_PERMISSION_FLAG |
- OCShare.UPDATE_PERMISSION_FLAG |
- OCShare.DELETE_PERMISSION_FLAG
- ;
- boolean canEdit = (sharePermissions & anyUpdatePermission) > 0;
- compound.setChecked(canEdit);
- if (setListener) {
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- if (mFile.isFolder()) {
- compound = (CompoundButton) editShareView.findViewById(R.id.canEditCreateCheckBox);
- if (canEdit) {
- compound.setVisibility(View.VISIBLE);
- compound.setChecked((sharePermissions & OCShare.CREATE_PERMISSION_FLAG) > 0);
- }
- if (setListener) {
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- compound = (CompoundButton) editShareView.findViewById(R.id.canEditChangeCheckBox);
- if (canEdit) {
- compound.setVisibility(View.VISIBLE);
- compound.setChecked((sharePermissions & OCShare.UPDATE_PERMISSION_FLAG) > 0);
- }
- if (setListener) {
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- compound = (CompoundButton) editShareView.findViewById(R.id.canEditDeleteCheckBox);
- if (canEdit) {
- compound.setVisibility(View.VISIBLE);
- compound.setChecked((sharePermissions & OCShare.DELETE_PERMISSION_FLAG) > 0);
- }
- if (setListener) {
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- } // else, trust in visibility GONE in R.layout.edit_share_layout
- }
- /**
- * Called when an operation to update share permissions finished.
- * s
- * @param result Result of the update operation
- */
- public void onUpdateSharePermissionsFinished(RemoteOperationResult result) {
- if (result.isSuccess()) {
- getFragmentManager().popBackStack();
- } else {
- if (getView() != null) {
- initPrivileges(getView());
- }
- }
- }
- /**
- * Listener for user actions that enable or disable a privilege
- */
- private class OnPrivilegeChangeListener
- implements CompoundButton.OnCheckedChangeListener {
- /**
- * Called by every {@link Switch} and {@link CheckBox} in the fragment to update
- * the state of its associated permission.
- *
- * @param compound {@link CompoundButton} toggled by the user
- * @param isChecked New switch state.
- */
- @Override
- public void onCheckedChanged(CompoundButton compound, boolean isChecked) {
- if (!isResumed()) {
- // very important, setCheched(...) is called automatically during
- // Fragment recreation on device rotations
- return;
- }
- /// else, getView() cannot be NULL
- CompoundButton subordinate;
- switch(compound.getId()) {
- case R.id.canShareSwitch:
- Log_OC.v(TAG, "canShareCheckBox toggled to " + isChecked);
- /// TODO?
- // option 1: direct update approach
- /*
- ((FileActivity) getActivity()).getFileOperationsHelper().
- setPrivilegeToShare(mFile, mShare, privilege, isChecked);
- */
- // option 2: nothing?
- break;
- case R.id.canEditSwitch:
- Log_OC.v(TAG, "canEditCheckBox toggled to " + isChecked);
- /// sync subordinate CheckBoxes
- if (mFile.isFolder()) {
- if (isChecked) {
- for (int i = 0; i < sSubordinateCheckBoxIds.length; i++) {
- //noinspection ConstantConditions, prevented in the method beginning
- subordinate = (CompoundButton) getView().findViewById(sSubordinateCheckBoxIds[i]);
- subordinate.setVisibility(View.VISIBLE);
- if (!subordinate.isChecked()) {
- toggleDisablingListener(subordinate);
- }
- }
- } else {
- for (int i = 0; i < sSubordinateCheckBoxIds.length; i++) {
- //noinspection ConstantConditions, prevented in the method beginning
- subordinate = (CompoundButton) getView().findViewById(sSubordinateCheckBoxIds[i]);
- subordinate.setVisibility(View.GONE);
- if (subordinate.isChecked()) {
- toggleDisablingListener(subordinate);
- }
- }
- }
- }
- /// TODO - anything else?; only if modification-on-change approach is taken
- break;
- case R.id.canEditCreateCheckBox:
- Log_OC.v(TAG, "canEditCreateCheckBox toggled to " + isChecked);
- syncCanEditSwitch(compound, isChecked);
- /// TODO - anything else?; only if modification-on-change approach is taken
- break;
- case R.id.canEditChangeCheckBox:
- Log_OC.v(TAG, "canEditChangeCheckBox toggled to " + isChecked);
- syncCanEditSwitch(compound, isChecked);
- /// TODO - anything else?; only if modification-on-change approach is taken
- break;
- case R.id.canEditDeleteCheckBox:
- Log_OC.v(TAG, "canEditDeleteCheckBox toggled to " + isChecked);
- syncCanEditSwitch(compound, isChecked);
- /// TODO - anything else?; only if modification-on-change approach is taken
- break;
- }
- // undo the toggle to grant the view will be correct if any intermediate dialog is cancelled or
- // the create/delete operation fails
- // ONLY if direct update approach is followed
- /*
- checkBoxView.setOnCheckedChangeListener(null);
- checkBoxView.toggle();
- checkBoxView.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
- */
- }
- /**
- * Sync value of "can edit" {@link Switch} according to a change in one of its subordinate checkboxes.
- *
- * If all the subordinates are disabled, "can edit" has to be disabled.
- *
- * If any subordinate is enabled, "can edit" has to be enabled.
- *
- * @param subordinateCheckBoxView Subordinate {@link CheckBox} that was changed.
- * @param isChecked 'true' iif subordinateCheckBoxView was checked.
- */
- private void syncCanEditSwitch(View subordinateCheckBoxView, boolean isChecked) {
- CompoundButton canEditCompound = (CompoundButton) getView().findViewById(R.id.canEditSwitch);
- if (isChecked) {
- if (!canEditCompound.isChecked()) {
- toggleDisablingListener(canEditCompound);
- }
- } else {
- boolean allDisabled = true;
- for (int i=0; allDisabled && i<sSubordinateCheckBoxIds.length; i++) {
- allDisabled &=
- sSubordinateCheckBoxIds[i] == subordinateCheckBoxView.getId() ||
- !((CheckBox) getView().findViewById(sSubordinateCheckBoxIds[i])).isChecked()
- ;
- }
- if (canEditCompound.isChecked() && allDisabled) {
- toggleDisablingListener(canEditCompound);
- for (int i=0; i<sSubordinateCheckBoxIds.length; i++) {
- getView().findViewById(sSubordinateCheckBoxIds[i]).setVisibility(View.GONE);
- }
- }
- }
- }
- /**
- * Toggle value of received {@link CompoundButton} granting that its change listener is not called.
- *
- * @param compound {@link CompoundButton} (switch or checkBox) to toggle without reporting to
- * the change listener
- */
- private void toggleDisablingListener(CompoundButton compound) {
- compound.setOnCheckedChangeListener(null);
- compound.toggle();
- compound.setOnCheckedChangeListener(mOnPrivilegeChangeListener);
- }
- }
- /**
- * Binds listener for user interactions on the 'unshare' button with the button itself.
- *
- * @param editShareView Root view in the fragment.
- */
- private void initUnshareButton(View editShareView) {
- TextView unshareButton = (TextView) editShareView.findViewById(R.id.unshareButton);
- unshareButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ((FileActivity) getActivity()).getFileOperationsHelper().
- unshareFileWithUserOrGroup(
- mFile,
- mShare.getShareType(),
- mShare.getShareWith()
- )
- ;
- }
- });
- }
- /**
- * Binds listener for user interactions on the 'done' button with the button itself.
- *
- * @param editShareView Root view in the fragment.
- */
- private void initDoneButton(View editShareView) {
- TextView doneButton = (TextView) editShareView.findViewById(R.id.doneButton);
- doneButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- SharePermissionsBuilder spb = new SharePermissionsBuilder();
- spb.setSharePermission(getCanShareSwitch().isChecked());
- if (mFile.isFolder()) {
- spb.setUpdatePermission(getCanEditChangeCheckBox().isChecked())
- .setCreatePermission(getCanEditCreateCheckBox().isChecked())
- .setDeletePermission(getCanEditDeleteCheckBox().isChecked());
- } else {
- spb.setUpdatePermission(getCanEditSwitch().isChecked());
- }
- int permissions = spb.build();
- ((FileActivity) getActivity()).getFileOperationsHelper().
- setPermissionsToShare(
- mShare,
- permissions
- );
- }
- });
- }
- /**
- * Shortcut to access {@link Switch} R.id.canShareSwitch
- *
- * @return {@link Switch} R.id.canShareCheckBox or null if called before
- * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
- */
- private Switch getCanShareSwitch() {
- return (Switch) getView().findViewById(R.id.canShareSwitch);
- }
- /**
- * Shortcut to access {@link Switch} R.id.canEditSwitch
- *
- * @return {@link Switch} R.id.canEditSwitch or null if called before
- * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
- */
- private Switch getCanEditSwitch() {
- return (Switch) getView().findViewById(R.id.canEditSwitch);
- }
- /**
- * Shortcut to access {@link CheckBox} R.id.canEditCreateCheckBox
- *
- * @return {@link CheckBox} R.id.canEditCreateCheckBox or null if called before
- * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
- */
- private CheckBox getCanEditCreateCheckBox() {
- return (CheckBox) getView().findViewById(R.id.canEditCreateCheckBox);
- }
- /**
- * Shortcut to access {@link CheckBox} R.id.canEditChangeCheckBox
- *
- * @return {@link CheckBox} R.id.canEditChangeCheckBox or null if called before
- * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
- */
- private CheckBox getCanEditChangeCheckBox() {
- return (CheckBox) getView().findViewById(R.id.canEditChangeCheckBox);
- }
- /**
- * Shortcut to access {@link CheckBox} R.id.canEditDeleteCheckBox
- *
- * @return {@link CheckBox} R.id.canEditDeleteCheckBox or null if called before
- * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
- */
- private CheckBox getCanEditDeleteCheckBox() {
- return (CheckBox) getView().findViewById(R.id.canEditDeleteCheckBox);
- }
- }
|