Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 479124ea authored by alperozturk's avatar alperozturk
Browse files

add SharePasswordDialogFragment

parent 843fda23
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.lib.resources.status.NextcloudVersion;

import java.util.ArrayList;
import java.util.List;
@@ -48,6 +49,7 @@ import it.niedermann.owncloud.notes.share.adapter.ShareeListAdapter;
import it.niedermann.owncloud.notes.share.dialog.FileDetailSharingMenuBottomSheetDialog;
import it.niedermann.owncloud.notes.share.dialog.QuickSharingPermissionsBottomSheetDialog;
import it.niedermann.owncloud.notes.share.dialog.ShareLinkToDialog;
import it.niedermann.owncloud.notes.share.dialog.SharePasswordDialogFragment;
import it.niedermann.owncloud.notes.share.listener.FileDetailsSharingMenuBottomSheetActions;
import it.niedermann.owncloud.notes.share.listener.ShareeListAdapterListener;
import it.niedermann.owncloud.notes.share.model.UsersAndGroupsSearchConfig;
@@ -431,7 +433,7 @@ public class NoteShareFragment extends Fragment implements ShareeListAdapterList
     * @param askForPassword if true, password is optional
     */
    public void requestPasswordForShareViaLink(boolean createShare, boolean askForPassword) {
        SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file,
        SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(note,
                createShare,
                askForPassword);
        dialog.show(getChildFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
+217 −0
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.share.dialog


import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.owncloud.android.lib.resources.shares.OCShare
import it.niedermann.owncloud.notes.R
import it.niedermann.owncloud.notes.branding.BrandedSnackbar
import it.niedermann.owncloud.notes.databinding.PasswordDialogBinding
import it.niedermann.owncloud.notes.persistence.entity.Note
import it.niedermann.owncloud.notes.shared.util.KeyboardUtils
import it.niedermann.owncloud.notes.shared.util.extensions.getParcelableArgument
import it.niedermann.owncloud.notes.shared.util.extensions.getSerializableArgument

/**
 * Dialog to input the password for sharing a file/folder.
 *
 *
 * Triggers the share when the password is introduced.
 */
class SharePasswordDialogFragment : DialogFragment() {

    var keyboardUtils: KeyboardUtils? = null

    private var binding: PasswordDialogBinding? = null
    private var note: Note? = null
    private var share: OCShare? = null
    private var createShare = false
    private var askForPassword = false

    override fun onStart() {
        super.onStart()

        val alertDialog = dialog as AlertDialog?

        if (alertDialog != null) {
            val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as MaterialButton?
            if (positiveButton != null) {
                // viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(positiveButton)
                positiveButton.setOnClickListener {
                    val sharePassword = binding?.sharePassword?.text

                    if (sharePassword != null) {
                        val password = sharePassword.toString()
                        if (!askForPassword && TextUtils.isEmpty(password)) {
                            BrandedSnackbar.make(
                                binding!!.root,
                                getString(R.string.share_link_empty_password),
                                Snackbar.LENGTH_LONG
                            )
                                .show()
                            return@setOnClickListener
                        }
                        if (share == null) {
                            setPassword(createShare, note, password)
                        } else {
                            setPassword(share!!, password)
                        }
                    }

                    alertDialog.dismiss()
                }
            }

            val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as MaterialButton?
            if (negativeButton != null) {
                //viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(negativeButton)
            }

            val neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL) as MaterialButton?
            if (neutralButton != null) {
                // val warningColorId = ContextCompat.getColor(requireContext(), R.color.highlight_textColor_Warning)
                //viewThemeUtils?.platform?.colorTextButtons(warningColorId, neutralButton)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        keyboardUtils?.showKeyboardForEditText(binding!!.sharePassword)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        note = requireArguments().getSerializableArgument(ARG_FILE, Note::class.java)
        share = requireArguments().getParcelableArgument(ARG_SHARE, OCShare::class.java)

        createShare = requireArguments().getBoolean(ARG_CREATE_SHARE, false)
        askForPassword = requireArguments().getBoolean(ARG_ASK_FOR_PASSWORD, false)

        // Inflate the layout for the dialog
        val inflater = requireActivity().layoutInflater
        binding = PasswordDialogBinding.inflate(inflater, null, false)

        // Setup layout
        binding?.sharePassword?.setText(R.string.empty)
       // viewThemeUtils?.material?.colorTextInputLayout(binding!!.sharePasswordContainer)

        val neutralButtonTextId: Int
        val title: Int
        if (askForPassword) {
            title = R.string.share_link_optional_password_title
            neutralButtonTextId = R.string.common_skip
        } else {
            title = R.string.share_link_password_title
            neutralButtonTextId = R.string.common_cancel
        }

        // Build the dialog
        val builder = MaterialAlertDialogBuilder(requireContext())
        builder.setView(binding!!.root)
            .setPositiveButton(R.string.common_ok, null)
            .setNegativeButton(R.string.common_delete) { _: DialogInterface?, _: Int -> callSetPassword() }
            .setNeutralButton(neutralButtonTextId) { _: DialogInterface?, _: Int ->
                if (askForPassword) {
                    callSetPassword()
                }
            }
            .setTitle(title)

        // viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireContext(), builder)

        return builder.create()
    }

    private fun callSetPassword() {
        if (share == null) {
            setPassword(createShare, note, null)
        } else {
            setPassword(share!!, null)
        }
    }

    private fun setPassword(createShare: Boolean, note: Note?, password: String?) {
        val fileOperationsHelper = (requireActivity() as FileActivity).fileOperationsHelper ?: return
        if (createShare) {
            fileOperationsHelper.shareFileViaPublicShare(note, password)
        } else {
            fileOperationsHelper.setPasswordToShare(share, password)
        }
    }

    private fun setPassword(share: OCShare, password: String?) {
        val fileOperationsHelper = (requireActivity() as FileActivity).fileOperationsHelper ?: return
        fileOperationsHelper.setPasswordToShare(share, password)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }

    companion object {
        private const val ARG_FILE = "FILE"
        private const val ARG_SHARE = "SHARE"
        private const val ARG_CREATE_SHARE = "CREATE_SHARE"
        private const val ARG_ASK_FOR_PASSWORD = "ASK_FOR_PASSWORD"
        const val PASSWORD_FRAGMENT = "PASSWORD_FRAGMENT"

        /**
         * Public factory method to create new SharePasswordDialogFragment instances.
         *
         * @param file        OCFile bound to the public share that which
         * password will be set or updated
         * @param createShare When 'true', the request for password will be
         * followed by the creation of a new public link
         * when 'false', a public share is assumed to exist, and the password is bound to it.
         * @return Dialog ready to show.
         */
        @JvmStatic
        fun newInstance(note: Note?, createShare: Boolean, askForPassword: Boolean): SharePasswordDialogFragment {
            val frag = SharePasswordDialogFragment()
            val args = Bundle()
            args.putSerializable(ARG_FILE, note)
            args.putBoolean(ARG_CREATE_SHARE, createShare)
            args.putBoolean(ARG_ASK_FOR_PASSWORD, askForPassword)
            frag.arguments = args
            return frag
        }

        /**
         * Public factory method to create new SharePasswordDialogFragment instances.
         *
         * @param share OCFile bound to the public share that which password will be set or updated
         * @return Dialog ready to show.
         */
        @JvmStatic
        fun newInstance(share: OCShare?, askForPassword: Boolean): SharePasswordDialogFragment {
            val frag = SharePasswordDialogFragment()
            val args = Bundle()
            args.putParcelable(ARG_SHARE, share)
            args.putBoolean(ARG_ASK_FOR_PASSWORD, askForPassword)
            frag.arguments = args
            return frag
        }

        /**
         * Public factory method to create new SharePasswordDialogFragment instances.
         *
         * @param share OCFile bound to the public share that which password will be set or updated
         * @return Dialog ready to show.
         */
        fun newInstance(share: OCShare?): SharePasswordDialogFragment {
            val frag = SharePasswordDialogFragment()
            val args = Bundle()
            args.putParcelable(ARG_SHARE, share)
            frag.arguments = args
            return frag
        }
    }
}
+32 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Nextcloud - Android Client
  ~
  ~ SPDX-FileCopyrightText: 2021 Andy Scherzinger <info@andy-scherzinger>
  ~ SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/spacer_2x">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/share_password_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_password">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/share_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="password"
            android:ems="10"
            android:gravity="top"
            android:inputType="textPassword">

        </com.google.android.material.textfield.TextInputEditText>

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
+10 −0
Original line number Diff line number Diff line
@@ -61,6 +61,16 @@
    <string name="note_share_fragment_could_not_retrieve_url">Could not retrieve URL</string>
    <string name="note_share_fragment_subject_shared_with_you">\"%1$s\" has been shared with you</string>
    <string name="contact_no_permission">Contact permission is required.</string>
    <string name="share_link_empty_password">You must enter a password</string>
    <string name="hint_password">Password</string>
    <string name="share_link_optional_password_title">Enter an optional password</string>
    <string name="common_skip">Skip</string>
    <string name="common_cancel">Cancel</string>
    <string name="empty" translatable="false" />
    <string name="share_link_password_title">Enter a password</string>
    <string name="common_ok">OK</string>
    <string name="common_delete">Delete</string>

    <string name="email_pick_failed">Failed to pick email address.</string>
    <string name="failed_update_ui">Failed to update UI</string>
    <string name="file_detail_sharing_fragment_no_contact_app_message">No app available to select contacts</string>