Skip to content

Commit 9986d76

Browse files
committed
Support stoping and starting again the ftp server at runtime.
1 parent 29a752a commit 9986d76

File tree

6 files changed

+80
-22
lines changed

6 files changed

+80
-22
lines changed

app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ dependencies
4545
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
4646
implementation 'androidx.appcompat:appcompat:1.2.0'
4747
implementation 'com.github.hxcan:upgrademanager:2023.5.8'
48-
implementation 'com.github.hxcan:ftpserver:2023.9.8'
49-
implementation 'com.github.hxcan:feedback:2023.6.35'
48+
// implementation 'com.github.hxcan:ftpserver:2023.9.8'
49+
implementation 'com.github.hxcan:feedback:2023.10.24'
5050

51-
// implementation 'com.github.hxcan:ftpserver:0e976e4'
51+
implementation 'com.github.hxcan:ftpserver:3b21c14'
5252

5353
implementation 'com.github.hxcan:rotatingactiveuser:2021.11.30'
5454
implementation 'com.upokecenter:cbor:4.2.0'

app/src/main/java/com/stupidbeauty/builtinftp/BuiltinFtpServer.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,22 @@ public BuiltinFtpServer(Context context)
119119

120120
private Context context; //!< Context.
121121

122+
/**
123+
* Stop the ftp server.
124+
*/
125+
public void stop()
126+
{
127+
ftpServer.stop();
128+
129+
ftpServer = null;
130+
ftpServerErrorListener = null;
131+
} // public void stop()
132+
122133
public void start()
123134
{
124-
ftpServerErrorListener=new FtpServerErrorListener(this);
135+
ftpServerErrorListener = new FtpServerErrorListener(this);
125136

126137
ftpServer = new FtpServer("0.0.0.0", port, context, allowActiveMode, ftpServerErrorListener, ip);
127-
// ftpServer.setErrorListener(ftpServerErrorListener); // Set error listner. Chen xin.
128-
Log.d(TAG, "start, rootDirectory: " + Environment.getExternalStorageDirectory()); // Debug.
129138

130139
ftpServer.setRootDirectory(Environment.getExternalStorageDirectory()); // Set the root directory.
131140
ftpServer.setAutoDetectIp(true); // Auto detect ip.

app/src/main/java/com/stupidbeauty/builtinftp/demo/LauncherActivity.java

+40-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.stupidbeauty.farmingbookapp.PreferenceManagerUtil;
2121
import com.stupidbeauty.hxlauncher.application.HxLauncherApplication;
2222
import com.stupidbeauty.ftpserver.lib.DocumentTreeBrowseRequest;
23-
// import butterknife.Bind;
23+
import android.widget.Button;
2424
import butterknife.ButterKnife;
2525
import android.os.Debug;
2626
import butterknife.OnCheckedChanged;
@@ -64,6 +64,7 @@
6464
import android.annotation.SuppressLint;
6565
import android.app.Activity;
6666
import android.widget.CheckBox;
67+
import android.widget.ImageView;
6768
import com.stupidbeauty.farmingbookapp.PreferenceManagerUtil;
6869
import com.stupidbeauty.hxlauncher.application.HxLauncherApplication;
6970
import butterknife.BindView;
@@ -74,24 +75,22 @@ public class LauncherActivity extends Activity
7475
private static final String TAG="LauncherActivity"; //!< 输出调试信息时使用的标记
7576
private VoiceUi voiceUi=null; //!< 语音交互对象。
7677
private Timer timerObj = null; //!< 用于报告下载完毕的定时器。
77-
7878
private ActiveUserReportManager activeUserReportManager=null; //!< 活跃用户统计管理器。陈欣。
7979
private BuiltinFtpServer builtinFtpServer=null; //!< The builtin ftp server.
80-
80+
@BindView(R.id.stopServerlButton) Button stopServerlButton; //!< the stop server button.
81+
@BindView(R.id.startServerlButton) Button startServerlButton; //!< the start server button.
8182
@BindView(R.id.statustextView) TextView statustextView; //!< Label to show status text.
83+
@BindView(R.id.maskUrlLineImagecon) ImageView maskUrlLineImagecon; //!< The mask on the url line.
8284
@BindView(R.id.availableSpaceView) TextView availableSpaceView; //!< 可用空间。
8385
@BindView(R.id.allowAnonymousetei) CheckBox allowAnonymousetei; //!< Allow anonymous check box.
8486
@BindView(R.id.userNamePassWordayout) RelativeLayout userNamePassWordayout; //!< User name pass word layout.
8587

8688
@OnClick(R.id.shareIcon)
8789
public void shareViaText()
8890
{
89-
Log.d(TAG, "gotoLoginActivity, 119."); //Debug.
9091
Intent launchIntent=new Intent(this, SettingsActivity.class); // 启动意图。
9192

9293
startActivity(launchIntent); //启动活动。
93-
94-
Log.d(TAG, "gotoLoginActivity, 122."); //Debug.
9594
} // public void shareViaText()
9695

9796
@OnClick(R.id.feedbackIcon)
@@ -101,6 +100,30 @@ public void showFeedbackUit()
101100

102101
feedback.showFeedbackUi(); // Show feedback ui.
103102
} // public void shareViaText()
103+
104+
@OnClick(R.id.stopServerlButton)
105+
public void stopServerlButton()
106+
{
107+
builtinFtpServer.stop(); // Stop the ftp server.
108+
109+
stopServerlButton.setVisibility(View.INVISIBLE); // Hide the stop server button.
110+
startServerlButton.setVisibility(View.VISIBLE); // Show the start server button.
111+
maskUrlLineImagecon.setVisibility(View.VISIBLE); // Mask the url line.
112+
113+
stopPersistantNotification(); // Stop the persistent notification.
114+
} // public void stopServerlButton()
115+
116+
@OnClick(R.id.startServerlButton)
117+
public void startServerlButton()
118+
{
119+
builtinFtpServer.start(); // Start the ftp server.
120+
121+
stopServerlButton.setVisibility(View.VISIBLE); // Hide the stop server button.
122+
startServerlButton.setVisibility(View.INVISIBLE); // Show the start server button.
123+
maskUrlLineImagecon.setVisibility(View.INVISIBLE); // Mask the url line.
124+
125+
startTimeCheckService(); // Show persistent notification.
126+
} // public void stopServerlButton()
104127

105128
@OnClick(R.id.copyUrlButton)
106129
public void copyUrlButton()
@@ -114,8 +137,6 @@ public void copyUrlButton()
114137

115138
String downloadFinished = getResources().getString(R.string.urlCopiedged); // Load the text content.
116139

117-
Log.d(TAG, "notifyDownloadFinish, text: " + downloadFinished); // Debug.
118-
119140
voiceUi.say(downloadFinished); // 发声。
120141
} // public void copyUrlButton()
121142

@@ -147,11 +168,8 @@ protected void onCreate(Bundle savedInstanceState)
147168

148169
voiceUi=new VoiceUi(this); // 创建语音交互对象。
149170

150-
// WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
151-
// String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
152-
153171
HxLauncherApplication hxLauncherApplication= HxLauncherApplication.getInstance() ; // 获取应用程序实例。
154-
builtinFtpServer=hxLauncherApplication.getBuiltinFtpServer(); // 获取FTP服务器实例对象。
172+
builtinFtpServer = hxLauncherApplication.getBuiltinFtpServer(); // 获取FTP服务器实例对象。
155173

156174
showFtpUrl(); // Show ftp url.
157175

@@ -200,7 +218,6 @@ public void toggleAllowAnonymouse(boolean isChecked)
200218
builtinFtpServer.setAllowAnonymous(isChecked); // SEt whether aloow anonymous.
201219

202220
toggleUserNamePassWordVisibility(isChecked); // Toggle user name pass word visibility.
203-
204221
} //public void toggleBuiltinShortcuts()
205222

206223
/**
@@ -212,6 +229,16 @@ private void startTimeCheckService()
212229

213230
startService(serviceIntent); //启动服务。
214231
} //private void startTimeCheckService()
232+
233+
/**
234+
* Stop the persistent notification.
235+
*/
236+
private void stopPersistantNotification()
237+
{
238+
Intent serviceIntent = new Intent(this, DownloadNotificationService.class); //创建意图。
239+
240+
stopService(serviceIntent); // Stop the service.
241+
} // private void stopPersistantNotification()
215242

216243
@Override
217244
/**
Binary file not shown.

app/src/main/res/layout/launcher_activity.xml

+23-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,34 @@
3535
android:layout_alignParentTop="true"
3636
android:focusable="true"
3737
android:scaleType="centerCrop"
38-
android:visibility="visible" />
38+
android:visibility="visible"
39+
/>
40+
<Button android:id="@+id/startServerlButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/feedbackIcon"
41+
android:layout_centerHorizontal="true" android:textColor="@color/yellow" android:textSize="28sp" android:text="@string/startServerl"
42+
android:visibility="invisible"
43+
/>
44+
<Button android:id="@+id/stopServerlButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/startServerlButton"
45+
android:layout_centerHorizontal="true" android:textColor="@color/yellow" android:textSize="28sp" android:text="@string/stopServerl" />
3946
<CheckBox android:id="@+id/allowAnonymousetei" android:text="@string/allowAnonymousetei" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/allowUnsafeDeletei" android:checked="true" />
4047
<CheckBox android:id="@+id/allowUnsafeDeletei" android:text="@string/allowUnsafeDeletei" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/statustextView" android:checked="true" />
4148
<TextView android:id="@+id/statustextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="false"
4249
android:layout_alignParentLeft="false" android:layout_centerVertical="true" android:textColor="@color/yellow" android:textSize="28sp"
4350
tools:visibility="visible" />
51+
<ImageView
52+
android:id="@+id/maskUrlLineImagecon"
53+
android:layout_width="wrap_content"
54+
android:layout_height="wrap_content"
55+
android:alpha="0.48"
56+
android:background="@drawable/z00various2016028giftwrapvarity700400"
57+
android:clickable="true"
58+
android:contentDescription="@string/backgroundImage"
59+
android:layout_alignRight="@id/statustextView"
60+
android:layout_alignTop="@id/statustextView"
61+
android:layout_alignLeft="@id/statustextView"
62+
android:layout_alignBottom="@id/statustextView"
63+
android:focusable="true"
64+
android:scaleType="centerCrop"
65+
android:visibility="invisible" />
4466
<Button android:id="@+id/copyUrlButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/statustextView"
4567
android:layout_centerHorizontal="true" android:textColor="@color/yellow" android:textSize="28sp" android:text="@string/copyUrl" />
4668
<RelativeLayout android:id="@+id/userNamePassWordayout" android:layout_width="wrap_content" android:layout_height="wrap_content"

app/src/main/res/values/strings.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
<string name="grantManageAllFilesPermission">Grant manage all files permission</string>
3838
<string name="stopWeb">Stop</string>
3939
<string name="forwardWeb">Forward</string>
40-
<string name="register">Register</string>
40+
<string name="startServerl">Start server</string>
4141
<string name="enterPassword">Enter password</string>
4242
<string name="forgotPassword">Forgot password?</string>
4343
<string name="apn_setting_username">You can set a custom root directory for the ftp server. Start by pressing the button below.</string>
4444
<string name="firstStepEnableLanIme">Auto start</string>
45-
<string name="about_info_coverage">Coverage</string>
45+
<string name="stopServerl">Stop server</string>
4646
<string name="unlink_not_right">Not right</string>
4747
<string name="about_help_feedback_title">Help And Feedback</string>
4848
<string name="urlCopiedged">Url copied</string>

0 commit comments

Comments
 (0)