SharePasswordDialogFragment.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * ownCloud Android client application
  3. * @author masensio
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.dialog;
  20. import android.support.v7.app.AlertDialog;
  21. import android.app.Dialog;
  22. import android.content.DialogInterface;
  23. import android.os.Bundle;
  24. import android.support.v4.app.DialogFragment;
  25. import android.view.LayoutInflater;
  26. import android.view.View;
  27. import android.view.WindowManager;
  28. import android.widget.EditText;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.ui.activity.FileActivity;
  34. /**
  35. * Dialog to input the password for sharing a file/folder.
  36. *
  37. * Triggers the share when the password is introduced.
  38. */
  39. public class SharePasswordDialogFragment extends DialogFragment
  40. implements DialogInterface.OnClickListener {
  41. private static final String ARG_FILE = "FILE";
  42. private static final String ARG_CREATE_SHARE = "CREATE_SHARE";
  43. public static final String PASSWORD_FRAGMENT = "PASSWORD_FRAGMENT";
  44. private OCFile mFile;
  45. private boolean mCreateShare;
  46. /**
  47. * Public factory method to create new SharePasswordDialogFragment instances.
  48. *
  49. * @param file OCFile bound to the public share that which password will be set or updated
  50. * @param createShare When 'true', the request for password will be followed by the creation of a new
  51. * public link; when 'false', a public share is assumed to exist, and the password
  52. * is bound to it.
  53. * @return Dialog ready to show.
  54. */
  55. public static SharePasswordDialogFragment newInstance(OCFile file, boolean createShare) {
  56. SharePasswordDialogFragment frag = new SharePasswordDialogFragment();
  57. Bundle args = new Bundle();
  58. args.putParcelable(ARG_FILE, file);
  59. args.putBoolean(ARG_CREATE_SHARE, createShare);
  60. frag.setArguments(args);
  61. return frag;
  62. }
  63. @Override
  64. public Dialog onCreateDialog(Bundle savedInstanceState) {
  65. mFile = getArguments().getParcelable(ARG_FILE);
  66. mCreateShare = getArguments().getBoolean(ARG_CREATE_SHARE, false);
  67. // Inflate the layout for the dialog
  68. LayoutInflater inflater = getActivity().getLayoutInflater();
  69. View v = inflater.inflate(R.layout.password_dialog, null);
  70. // Setup layout
  71. EditText inputText = ((EditText)v.findViewById(R.id.share_password));
  72. inputText.setText("");
  73. inputText.requestFocus();
  74. // Build the dialog
  75. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
  76. R.style.Theme_ownCloud_Dialog_NoButtonBarStyle);
  77. builder.setView(v)
  78. .setPositiveButton(R.string.common_ok, this)
  79. .setNegativeButton(R.string.common_cancel, this)
  80. .setTitle(R.string.share_link_password_title);
  81. Dialog d = builder.create();
  82. d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  83. return d;
  84. }
  85. @Override
  86. public void onClick(DialogInterface dialog, int which) {
  87. if (which == AlertDialog.BUTTON_POSITIVE) {
  88. String password =
  89. ((TextView)(getDialog().findViewById(R.id.share_password)))
  90. .getText().toString();
  91. if (password.length() <= 0) {
  92. Toast.makeText(
  93. getActivity(),
  94. R.string.share_link_empty_password,
  95. Toast.LENGTH_LONG).show();
  96. return;
  97. }
  98. if (mCreateShare) {
  99. // Share the file
  100. ((FileActivity) getActivity()).getFileOperationsHelper().
  101. shareFileViaLink(mFile, password);
  102. } else {
  103. // updat existing link
  104. ((FileActivity) getActivity()).getFileOperationsHelper().
  105. setPasswordToShareViaLink(mFile, password);
  106. }
  107. }
  108. }
  109. }