SharePasswordDialogFragment.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.content.Intent;
  24. import android.os.Bundle;
  25. import android.support.v4.app.DialogFragment;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.WindowManager;
  29. import android.widget.EditText;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.datamodel.OCFile;
  34. import com.owncloud.android.ui.activity.FileActivity;
  35. /**
  36. * Dialog to input the password for sharing a file/folder.
  37. *
  38. * Triggers the share when the password is introduced.
  39. */
  40. public class SharePasswordDialogFragment extends DialogFragment
  41. implements DialogInterface.OnClickListener {
  42. private static final String ARG_FILE = "FILE";
  43. private static final String ARG_SEND_INTENT = "SEND_INTENT";
  44. public static final String PASSWORD_FRAGMENT = "PASSWORD_FRAGMENT";
  45. private OCFile mFile;
  46. private Intent mSendIntent;
  47. /**
  48. * Public factory method to create new SharePasswordDialogFragment instances.
  49. *
  50. * @param file
  51. * @param sendIntent
  52. * @return Dialog ready to show.
  53. */
  54. public static SharePasswordDialogFragment newInstance(OCFile file, Intent sendIntent) {
  55. SharePasswordDialogFragment frag = new SharePasswordDialogFragment();
  56. Bundle args = new Bundle();
  57. args.putParcelable(ARG_FILE, file);
  58. args.putParcelable(ARG_SEND_INTENT, sendIntent);
  59. frag.setArguments(args);
  60. return frag;
  61. }
  62. @Override
  63. public Dialog onCreateDialog(Bundle savedInstanceState) {
  64. mFile = getArguments().getParcelable(ARG_FILE);
  65. mSendIntent = getArguments().getParcelable(ARG_SEND_INTENT);
  66. // Inflate the layout for the dialog
  67. LayoutInflater inflater = getActivity().getLayoutInflater();
  68. View v = inflater.inflate(R.layout.password_dialog, null);
  69. // Setup layout
  70. EditText inputText = ((EditText)v.findViewById(R.id.share_password));
  71. inputText.setText("");
  72. inputText.requestFocus();
  73. // Build the dialog
  74. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  75. builder.setView(v)
  76. .setPositiveButton(R.string.common_ok, this)
  77. .setNegativeButton(R.string.common_cancel, this)
  78. .setTitle(R.string.share_link_password_title);
  79. Dialog d = builder.create();
  80. d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  81. return d;
  82. }
  83. @Override
  84. public void onClick(DialogInterface dialog, int which) {
  85. if (which == AlertDialog.BUTTON_POSITIVE) {
  86. // Enable the flag "Share again"
  87. ((FileActivity) getActivity()).setTryShareAgain(true);
  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. // Share the file
  99. ((FileActivity)getActivity()).getFileOperationsHelper()
  100. .shareFileWithLinkToApp(mFile, password, mSendIntent);
  101. } else {
  102. // Disable the flag "Share again"
  103. ((FileActivity) getActivity()).setTryShareAgain(false);
  104. }
  105. }
  106. }