|
| 1 | +package ca.pkay.rcloneexplorer.Dialogs; |
| 2 | + |
| 3 | +import android.app.Dialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.annotation.Nullable; |
| 9 | +import android.support.design.widget.TextInputLayout; |
| 10 | +import android.support.v4.app.DialogFragment; |
| 11 | +import android.support.v4.app.FragmentActivity; |
| 12 | +import android.support.v7.app.AlertDialog; |
| 13 | +import android.view.LayoutInflater; |
| 14 | +import android.view.View; |
| 15 | +import android.widget.CheckBox; |
| 16 | +import android.widget.EditText; |
| 17 | +import android.widget.RadioGroup; |
| 18 | + |
| 19 | +import ca.pkay.rcloneexplorer.R; |
| 20 | +import ca.pkay.rcloneexplorer.Rclone; |
| 21 | + |
| 22 | +public class ServeDialog extends DialogFragment { |
| 23 | + |
| 24 | + private Context context; |
| 25 | + private boolean isDarkTheme; |
| 26 | + private Callback callback; |
| 27 | + private RadioGroup protocol; |
| 28 | + private CheckBox allowRemoteAccess; |
| 29 | + private EditText user; |
| 30 | + private EditText password; |
| 31 | + |
| 32 | + public interface Callback { |
| 33 | + void onServeOptionsSelected(int protocol, boolean allowRemoteAccess, String user, String password); |
| 34 | + } |
| 35 | + |
| 36 | + @NonNull |
| 37 | + @Override |
| 38 | + public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 39 | + if (getParentFragment() != null) { |
| 40 | + callback = (Callback) getParentFragment(); |
| 41 | + } |
| 42 | + |
| 43 | + if (savedInstanceState != null) { |
| 44 | + isDarkTheme = savedInstanceState.getBoolean("isDarkTheme"); |
| 45 | + } |
| 46 | + |
| 47 | + AlertDialog.Builder builder; |
| 48 | + if (isDarkTheme) { |
| 49 | + builder = new AlertDialog.Builder(context, R.style.DarkDialogTheme); |
| 50 | + } else { |
| 51 | + builder = new AlertDialog.Builder(context); |
| 52 | + } |
| 53 | + |
| 54 | + LayoutInflater layoutInflater = ((FragmentActivity)context).getLayoutInflater(); |
| 55 | + View view = layoutInflater.inflate(R.layout.dialog_serve, null); |
| 56 | + |
| 57 | + protocol = view.findViewById(R.id.radio_group_protocol); |
| 58 | + allowRemoteAccess = view.findViewById(R.id.checkbox_allow_remote_access); |
| 59 | + user = view.findViewById(R.id.edit_text_user); |
| 60 | + password = view.findViewById(R.id.edit_text_password); |
| 61 | + |
| 62 | + ((TextInputLayout) view.findViewById(R.id.text_input_layout_user)).setHint("Username"); |
| 63 | + ((TextInputLayout) view.findViewById(R.id.text_input_layout_password)).setHint("Password"); |
| 64 | + |
| 65 | + builder.setTitle(R.string.serve_dialog_title); |
| 66 | + builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 67 | + @Override |
| 68 | + public void onClick(DialogInterface dialog, int which) { |
| 69 | + sendCallback(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + builder.setNegativeButton(R.string.cancel, null); |
| 74 | + |
| 75 | + builder.setView(view); |
| 76 | + |
| 77 | + return builder.show(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onSaveInstanceState(Bundle outState) { |
| 82 | + super.onSaveInstanceState(outState); |
| 83 | + outState.putBoolean("isDarkTheme", isDarkTheme); |
| 84 | + outState.putInt("protocol", protocol.getCheckedRadioButtonId()); |
| 85 | + outState.putBoolean("allowRemoteAccess", allowRemoteAccess.isChecked()); |
| 86 | + if (!user.getText().toString().trim().isEmpty()) { |
| 87 | + outState.putString("user", user.getText().toString()); |
| 88 | + } |
| 89 | + if (!password.getText().toString().trim().isEmpty()) { |
| 90 | + outState.putString("password", password.getText().toString()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onViewStateRestored(@Nullable Bundle savedInstanceState) { |
| 96 | + super.onViewStateRestored(savedInstanceState); |
| 97 | + if (savedInstanceState == null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + allowRemoteAccess.setChecked(savedInstanceState.getBoolean("allowRemoteAccess", false)); |
| 102 | + String savedUser = savedInstanceState.getString("user"); |
| 103 | + if (savedUser != null) { |
| 104 | + user.setText(savedUser); |
| 105 | + } |
| 106 | + |
| 107 | + String savedPassword = savedInstanceState.getString("password"); |
| 108 | + if (savedPassword != null) { |
| 109 | + password.setText(savedPassword); |
| 110 | + } |
| 111 | + |
| 112 | + int savedProtocol = savedInstanceState.getInt("protocol", -1); |
| 113 | + if (savedProtocol == R.id.radio_http || savedProtocol == R.id.radio_webdav) { |
| 114 | + protocol.check(savedProtocol); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onAttach(Context context) { |
| 120 | + super.onAttach(context); |
| 121 | + this.context = context; |
| 122 | + |
| 123 | + if (context instanceof Callback) { |
| 124 | + callback = (Callback) context; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public ServeDialog setDarkTheme(boolean isDarkTheme) { |
| 129 | + this.isDarkTheme = isDarkTheme; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + private void sendCallback() { |
| 134 | + int selectedProtocolId = protocol.getCheckedRadioButtonId(); |
| 135 | + int selectedProtocol; |
| 136 | + switch (selectedProtocolId) { |
| 137 | + case R.id.radio_webdav: |
| 138 | + selectedProtocol = Rclone.SERVE_PROTOCOL_WEBDAV; |
| 139 | + break; |
| 140 | + case R.id.radio_http: |
| 141 | + default: |
| 142 | + selectedProtocol = Rclone.SERVE_PROTOCOL_HTTP; |
| 143 | + break; |
| 144 | + } |
| 145 | + |
| 146 | + callback.onServeOptionsSelected(selectedProtocol, allowRemoteAccess.isChecked(), user.getText().toString(), password.getText().toString()); |
| 147 | + } |
| 148 | +} |
0 commit comments