-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathMFTPSettingActivity.java
More file actions
225 lines (190 loc) · 8.37 KB
/
Copy pathMFTPSettingActivity.java
File metadata and controls
225 lines (190 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package org.qpython.qpy.texteditor;
import android.support.v7.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.quseit.base.QBaseDialog;
import com.quseit.util.NAction;
import com.quseit.util.NUtil;
import org.qpython.qpy.R;
import org.qpython.qpysdk.QPyConstants;
import org.swiftp.Defaults;
import java.io.File;
import java.net.InetAddress;
import java.text.MessageFormat;
public class MFTPSettingActivity extends BaseActivity {
private static final String TAG = "MSettingActivity";
//private static final int SCRIPT_EXEC_CODE = 1235;
BroadcastReceiver ftpServerReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "FTPServerService action received: " + intent.getAction());
TextView running_state = (TextView) findViewById(R.id.ftp_service_value);
Button ftpOpBtn = (Button)findViewById(R.id.ftp_op_btn);
if (intent.getAction().equals(org.swiftp.FTPServerService.ACTION_STARTED)) {
// Fill in the FTP server address
InetAddress address = org.swiftp.FTPServerService.getWifiIp();
if (address == null) {
Log.v(TAG, "Unable to retreive wifi ip address");
running_state.setText(getString(R.string.cant_get_url));
return;
}
String iptext = "ftp://" + address.getHostAddress() + ":"
+ org.swiftp.FTPServerService.getPort() + "/";
Resources resources = getResources();
String summary = resources.getString(R.string.running_summary_started, iptext);
running_state.setText(summary);
ftpOpBtn.setText(getString(R.string.ftp_stop));
} else if (intent.getAction().equals(org.swiftp.FTPServerService.ACTION_STOPPED)) {
running_state.setText("");
running_state.setText(R.string.running_summary_stopped);
ftpOpBtn.setText(getString(R.string.ftp_start));
} else if (intent.getAction().equals(org.swiftp.FTPServerService.ACTION_FAILEDTOSTART)) {
running_state.setText(R.string.running_summary_failed);
ftpOpBtn.setText(getString(R.string.ftp_start));
}
}
};
public static void start(Context context) {
Intent starter = new Intent(context, MFTPSettingActivity.class);
context.startActivity(starter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m_ftp_setting);
setTitle(R.string.swiftp_name);
//initWidgetTabItem(4);
String externalStorage = new File(Environment.getExternalStorageDirectory(), QPyConstants.BASE_PATH).getAbsolutePath();
String frv = MessageFormat.format(getString(R.string.ftp_root), externalStorage);
TextView fr = (TextView)findViewById(R.id.ftp_root_value);
fr.setText(frv);
displayAccount();
//
Button ftpOpBtn = (Button)findViewById(R.id.ftp_op_btn);
if (org.swiftp.FTPServerService.isRunning()) {
ftpOpBtn.setText(getString(R.string.ftp_stop));
} else {
ftpOpBtn.setText(getString(R.string.ftp_start));
}
IntentFilter filter = new IntentFilter();
filter.addAction(org.swiftp.FTPServerService.ACTION_STARTED);
filter.addAction(org.swiftp.FTPServerService.ACTION_STOPPED);
filter.addAction(org.swiftp.FTPServerService.ACTION_FAILEDTOSTART);
registerReceiver(ftpServerReceiver, filter);
// MNApp mnApp = (MNApp) this.getApplication();
//
// QBaseApp.getInstance().addActivity(this);
}
@Override
public void onDestroy() {
Log.v(TAG, "onPause");
super.onDestroy();
Log.v(TAG, "Unregistering the FTPServer actions");
unregisterReceiver(ftpServerReceiver);
}
public void onFTPOp(View v) {
if (org.swiftp.FTPServerService.isRunning()) {
stopServer();
} else {
startServer();
}
}
private void startServer() {
Context context = getApplicationContext();
NAction.setFtpRoot(context, Environment.getExternalStorageDirectory()+"/"+ QPyConstants.BASE_PATH);
Intent serverService = new Intent(context, FTPServerService.class);
if (!org.swiftp.FTPServerService.isRunning()) {
startService(serverService);
}
}
private void stopServer() {
Context context = getApplicationContext();
Intent serverService = new Intent(context, FTPServerService.class);
stopService(serverService);
}
public void displayAccount() {
String ftpPort = NAction.getFtpPort(getApplicationContext());
if (ftpPort.equals("")) {
ftpPort = String.valueOf(Defaults.getPortNumber());
}
String ftpUsername = NAction.getFtpUsername(getApplicationContext());
if (ftpUsername.equals("")) {
ftpUsername = NAction.getCode(getApplicationContext());
}
String ftpPwd = NAction.getFtpPwd(getApplicationContext());
if (ftpPwd.equals("")) {
ftpPwd = NAction.getCode(getApplicationContext());
}
TextView ftpPortValue = (TextView)findViewById(R.id.ftp_port_value);
ftpPortValue.setText(ftpPort);
TextView ftpAccountValue = (TextView)findViewById(R.id.plugin_ftp_account_value);
ftpAccountValue.setText(ftpUsername+" / "+ftpPwd);
}
@SuppressWarnings("deprecation")
public void onFtpAccountSetting(View v) {
String ftpUsername = NAction.getFtpUsername(getApplicationContext());
if (ftpUsername.equals("")) {
ftpUsername = NAction.getCode(getApplicationContext());
}
String ftpPwd = NAction.getFtpPwd(getApplicationContext());
if (ftpPwd.equals("")) {
ftpPwd = NAction.getCode(getApplicationContext());
}
WBase.setTxtDialogParam2(0, R.string.ftp_account_setting, getString(R.string.ftp_username), getString(R.string.ftp_pwd), ftpUsername, ftpPwd,
(dialog, which) -> {
AlertDialog ad = (AlertDialog) dialog;
EditText t1 = (EditText) ad.findViewById(R.id.editText_prompt1);
EditText t2 = (EditText) ad.findViewById(R.id.editText_prompt2);
String username = t1.getText().toString();
String pwd = t2.getText().toString();
if (!username.equals("") && !pwd.equals("")) {
NAction.setFtpUsername(getApplicationContext(), username);
NAction.setFtpPwd(getApplicationContext(), pwd);
} else {
Toast.makeText(getApplicationContext(), R.string.err_need_input, Toast.LENGTH_SHORT).show();
}
displayAccount();
},null);
showDialog(QBaseDialog.DIALOG_TEXT_ENTRY2+dialogIndex);
dialogIndex++;
}
@SuppressWarnings("deprecation")
public void onFtpPortSetting(View v) {
final TextView port = (TextView)findViewById(R.id.ftp_port_value);
String portVal = port.getText().toString();
WBase.setTxtDialogParam(0, R.string.ftp_port, portVal,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog ad = (AlertDialog) dialog;
EditText t = (EditText) ad.findViewById(R.id.editText_prompt);
String content = t.getText().toString();
if (!content.equals("")) {
if (NUtil.isInt(content)) {
NAction.setFtpPort(getApplicationContext(), content);
displayAccount();
} else {
Toast.makeText(getApplicationContext(), R.string.err_need_int, Toast.LENGTH_SHORT).show();
}
} else {
NAction.setProxyPort(getApplicationContext(), "");
port.setText("");
}
}
},null);
showDialog(QBaseDialog.DIALOG_TEXT_ENTRY+dialogIndex);
dialogIndex++;
}
}