Merge pull request #914 from owncloud/accessibility
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SharePasswordDialogFragment.java
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
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.os.Bundle;
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
33 import com.actionbarsherlock.app.SherlockDialogFragment;
34 import com.owncloud.android.R;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.ui.activity.FileActivity;
37
38 /**
39 * Dialog to input the password for sharing a file/folder.
40 *
41 * Triggers the share when the password is introduced.
42 */
43
44 public class SharePasswordDialogFragment extends SherlockDialogFragment
45 implements DialogInterface.OnClickListener {
46
47 private static final String ARG_FILE = "FILE";
48 private static final String ARG_SEND_INTENT = "SEND_INTENT";
49
50 public static final String PASSWORD_FRAGMENT = "PASSWORD_FRAGMENT";
51
52 private OCFile mFile;
53 private Intent mSendIntent;
54
55 /**
56 * Public factory method to create new SharePasswordDialogFragment instances.
57 *
58 * @param file
59 * @param sendIntent
60 * @return Dialog ready to show.
61 */
62 public static SharePasswordDialogFragment newInstance(OCFile file, Intent sendIntent) {
63 SharePasswordDialogFragment frag = new SharePasswordDialogFragment();
64 Bundle args = new Bundle();
65 args.putParcelable(ARG_FILE, file);
66 args.putParcelable(ARG_SEND_INTENT, sendIntent);
67 frag.setArguments(args);
68 return frag;
69 }
70
71 @Override
72 public Dialog onCreateDialog(Bundle savedInstanceState) {
73 mFile = getArguments().getParcelable(ARG_FILE);
74 mSendIntent = getArguments().getParcelable(ARG_SEND_INTENT);
75
76 // Inflate the layout for the dialog
77 LayoutInflater inflater = getActivity().getLayoutInflater();
78 View v = inflater.inflate(R.layout.password_dialog, null);
79
80 // Setup layout
81 EditText inputText = ((EditText)v.findViewById(R.id.share_password));
82 inputText.setText("");
83 inputText.requestFocus();
84
85 // Build the dialog
86 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
87 builder.setView(v)
88 .setPositiveButton(R.string.common_ok, this)
89 .setNegativeButton(R.string.common_cancel, this)
90 .setTitle(R.string.share_link_password_title);
91 Dialog d = builder.create();
92 d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
93 return d;
94 }
95
96
97 @Override
98 public void onClick(DialogInterface dialog, int which) {
99 if (which == AlertDialog.BUTTON_POSITIVE) {
100 // Enable the flag "Share again"
101 ((FileActivity) getSherlockActivity()).setTryShareAgain(true);
102
103 String password =
104 ((TextView)(getDialog().findViewById(R.id.share_password)))
105 .getText().toString();
106
107 if (password.length() <= 0) {
108 Toast.makeText(
109 getActivity(),
110 R.string.share_link_empty_password,
111 Toast.LENGTH_LONG).show();
112 return;
113 }
114
115 // Share the file
116 ((FileActivity)getSherlockActivity()).getFileOperationsHelper()
117 .shareFileWithLinkToApp(mFile, password, mSendIntent);
118
119 } else {
120 // Disable the flag "Share again"
121 ((FileActivity) getSherlockActivity()).setTryShareAgain(false);
122 }
123 }
124 }