Skip to content

Commit 5485145

Browse files
committed
Support setting default user name and password
1 parent 9c89e28 commit 5485145

File tree

10 files changed

+207
-56
lines changed

10 files changed

+207
-56
lines changed

app/build.gradle

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
apply plugin: 'com.android.application'
22

3-
android {
4-
compileOptions {
3+
android
4+
{
5+
compileOptions
6+
{
57
sourceCompatibility JavaVersion.VERSION_1_8
68
targetCompatibility JavaVersion.VERSION_1_8
79
}
810

911
compileSdkVersion 29
1012

11-
defaultConfig {
13+
defaultConfig
14+
{
1215
buildToolsVersion = "30.0.3"
1316
applicationId "com.stupidbeauty.hxftpserver"
1417
minSdkVersion 21
1518
targetSdkVersion 29
16-
versionCode 4563
17-
versionName "4563.2022.4.6"
19+
versionCode 4580
20+
versionName "4580.2022.6.17"
1821
}
1922

20-
buildTypes {
21-
release {
23+
buildTypes
24+
{
25+
release
26+
{
2227
minifyEnabled false
2328
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2429
}
@@ -27,12 +32,13 @@ android {
2732
return void
2833
}
2934

30-
dependencies {
35+
dependencies
36+
{
3137
compile 'com.jakewharton:butterknife:7.0.1'
3238
annotationProcessor 'com.jakewharton:butterknife:7.0.1'
3339
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
3440
implementation 'androidx.appcompat:appcompat:1.2.0'
35-
implementation 'com.github.hxcan:ftpserver:2022.6.8'
41+
implementation 'com.github.hxcan:ftpserver:2022.6.23'
3642
compile 'com.github.hxcan:rotatingactiveuser:2021.11.30'
3743
compile 'com.upokecenter:cbor:4.2.0'
3844
implementation 'com.github.hxcan:voiceui:2022.5.15'

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

+45-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.os.AsyncTask;
55
import com.stupidbeauty.ftpserver.lib.FtpServer;
6+
import com.stupidbeauty.ftpserver.lib.UserManager;
67
import java.net.BindException;
78
import android.os.Environment;
89
import android.os.LocaleList;
@@ -18,6 +19,7 @@
1819

1920
public class BuiltinFtpServer
2021
{
22+
private boolean allowAnonymous=true; //!< Whether to allow anonymous.
2123
private static final String TAG="BuiltinFtpServer"; //!< 输出调试信息时使用的标记
2224
private ErrorListener errorListener=null; //!< Error listener.
2325
private EventListener eventListener=null; //!< Event listener.
@@ -59,9 +61,19 @@ public void setAllowActiveMode(boolean allowActiveMode)
5961
{
6062
this.allowActiveMode=allowActiveMode;
6163
} //private void setAllowActiveMode(allowActiveMode)
64+
65+
/**
66+
* Set whether to allow anonymous.
67+
*/
68+
public void setAllowAnonymous(boolean allowAnonymous)
69+
{
70+
this.allowAnonymous=allowAnonymous;
71+
72+
assessSetUserManager(); // Set user mnager.
73+
} // public void setAllowAnonymous(bool allowAnonymous)
6274

6375
/**
64-
* 获取实际的端口。
76+
* Query the actual port.
6577
*/
6678
public int getActualPort()
6779
{
@@ -82,16 +94,39 @@ public BuiltinFtpServer(Context context)
8294
this.context = context;
8395
}
8496

85-
private Context context; //!< Context.
97+
private Context context; //!< Context.
8698

87-
public void start()
88-
{
89-
ftpServerErrorListener=new FtpServerErrorListener(this);
99+
public void start()
100+
{
101+
ftpServerErrorListener=new FtpServerErrorListener(this);
90102

91-
ftpServer = new FtpServer("0.0.0.0", port, context, allowActiveMode);
92-
ftpServer.setErrorListener(ftpServerErrorListener); // Set error listner. Chen xin.
93-
Log.d(TAG, "start, rootDirectory: " + Environment.getExternalStorageDirectory()); // Debug.
103+
ftpServer = new FtpServer("0.0.0.0", port, context, allowActiveMode);
104+
ftpServer.setErrorListener(ftpServerErrorListener); // Set error listner. Chen xin.
105+
Log.d(TAG, "start, rootDirectory: " + Environment.getExternalStorageDirectory()); // Debug.
94106

95-
ftpServer.setRootDirectory(Environment.getExternalStorageDirectory()); // 设置根目录。
96-
}
107+
ftpServer.setRootDirectory(Environment.getExternalStorageDirectory()); // Set the root directory.
108+
109+
assessSetUserManager(); // Assess set user manager.
110+
111+
}
112+
113+
/**
114+
* Assess set user manager.
115+
*/
116+
private void assessSetUserManager()
117+
{
118+
UserManager userManager=null; // Create user manager.
119+
120+
if (!allowAnonymous) // Not allow anonymous
121+
{
122+
userManager=new UserManager(); // Create user manager.
123+
124+
userManager.addUser("stupidbeauty", "ftpserver");
125+
} // if (!allowAnonymous) // Not allow anonymous
126+
127+
if (ftpServer!=null)
128+
{
129+
ftpServer.setUserManager(userManager); // Set user manager.
130+
} // if (ftpServer!=null)
131+
} // private void assessSetUserManager()
97132
}

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

+73-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.stupidbeauty.builtinftp.demo;
22

3+
import com.stupidbeauty.farmingbookapp.PreferenceManagerUtil;
4+
import com.stupidbeauty.hxlauncher.application.HxLauncherApplication;
5+
import butterknife.Bind;
6+
import butterknife.ButterKnife;
7+
import android.os.Debug;
8+
import butterknife.OnCheckedChanged;
9+
import butterknife.OnClick;
310
import android.util.Log;
411
import java.util.Date;
512
import java.time.format.DateTimeFormatter;
@@ -38,6 +45,11 @@
3845
import android.Manifest;
3946
import android.annotation.SuppressLint;
4047
import android.app.Activity;
48+
import android.widget.CheckBox;
49+
import com.stupidbeauty.farmingbookapp.PreferenceManagerUtil;
50+
import com.stupidbeauty.hxlauncher.application.HxLauncherApplication;
51+
import butterknife.Bind;
52+
import android.widget.RelativeLayout;
4153

4254
public class LauncherActivity extends Activity
4355
{
@@ -50,6 +62,8 @@ public class LauncherActivity extends Activity
5062

5163
@Bind(R.id.statustextView) TextView statustextView; //!< Label to show status text.
5264
@Bind(R.id.availableSpaceView) TextView availableSpaceView; //!< 可用空间。
65+
@Bind(R.id.allowAnonymousetei) CheckBox allowAnonymousetei; //!< Allow anonymous check box.
66+
@Bind(R.id.userNamePassWordayout) RelativeLayout userNamePassWordayout; //!< User name pass word layout.
5367

5468
@OnClick(R.id.copyUrlButton)
5569
public void copyUrlButton()
@@ -91,26 +105,69 @@ protected void onCreate(Bundle savedInstanceState)
91105
initializeEventListener(); // 初始化事件监听器。
92106

93107
startTimeCheckService(); // 启动下载通知服务。陈欣。
108+
109+
loadSettings(); // Load settings.
94110
} //protected void onCreate(Bundle savedInstanceState)
111+
112+
/**
113+
* 载入选项。
114+
*/
115+
private void loadSettings()
116+
{
117+
boolean builtinShortcutsVisible= PreferenceManagerUtil.getAllowAnonymous(); //内置快捷方式是否可见。
95118

96-
/**
97-
* 启动时间检查服务。
98-
*/
99-
private void startTimeCheckService()
100-
{
101-
Intent serviceIntent = new Intent(this, DownloadNotificationService.class); //创建意图。
119+
allowAnonymousetei.setChecked(builtinShortcutsVisible); //切换是否选中。
120+
121+
toggleUserNamePassWordVisibility(builtinShortcutsVisible); // Toggle user name pass word visibility.
122+
} //private void loadPreference()
123+
124+
/**
125+
* Toggle user name pass word visibility.
126+
*/
127+
private void toggleUserNamePassWordVisibility(boolean isChecked)
128+
{
129+
if (isChecked) // Allow anonymous
130+
{
131+
userNamePassWordayout.setVisibility(View.INVISIBLE); // Set the visibility of user name pass word layout.
132+
} //isChecked
133+
else // Not allow anonymous
134+
{
135+
userNamePassWordayout.setVisibility(View.VISIBLE); // Set the visibility of user name pass word layout.
136+
}
137+
} // private void toggleUserNamePassWordVisibility(boolean isChecked)
138+
139+
/**
140+
* Toggle, whether allow anonymouse.
141+
*/
142+
@OnCheckedChanged(R.id.allowAnonymousetei)
143+
public void toggleAllowAnonymouse(boolean isChecked)
144+
{
145+
PreferenceManagerUtil.setAllowAnonymous(isChecked); //保存选项。
146+
147+
builtinFtpServer.setAllowAnonymous(isChecked); // SEt whether aloow anonymous.
148+
149+
toggleUserNamePassWordVisibility(isChecked); // Toggle user name pass word visibility.
150+
151+
} //public void toggleBuiltinShortcuts()
152+
153+
/**
154+
* 启动时间检查服务。
155+
*/
156+
private void startTimeCheckService()
157+
{
158+
Intent serviceIntent = new Intent(this, DownloadNotificationService.class); //创建意图。
102159

103-
startService(serviceIntent); //启动服务。
104-
} //private void startTimeCheckService()
160+
startService(serviceIntent); //启动服务。
161+
} //private void startTimeCheckService()
105162

106-
@Override
107-
/**
108-
* 活动重新处于活跃状态。
109-
*/
110-
protected void onResume()
111-
{
112-
long startTimestamp=System.currentTimeMillis(); // 记录开始时间戳。
113-
super.onResume(); //超类继续工作。
163+
@Override
164+
/**
165+
* 活动重新处于活跃状态。
166+
*/
167+
protected void onResume()
168+
{
169+
long startTimestamp=System.currentTimeMillis(); // 记录开始时间戳。
170+
super.onResume(); //超类继续工作。
114171

115172
refreshAvailableSpace(); // 刷新可用空间数量。
116173

@@ -129,7 +186,6 @@ private void createActiveUserReportManager()
129186
activeUserReportManager.startReportActiveUser(); // 开始报告活跃用户。
130187
} //if (activeUserReportManager==null)
131188
} //private void createActiveUserReportManager()
132-
133189

134190
/**
135191
* 初始化事件监听器。

app/src/main/java/com/stupidbeauty/farmingbookapp/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class Constants
1414
*/
1515
public final class Common
1616
{
17-
/** 控制业务tabs 的显示 */
1817
public static final String CLIENT_ID = "CLIENT_ID"; //!<客户端编号。
1918
public static final String BuitinShortcutsVisible = "com.stupidbeauty.hxlauncher.builtinShortcutsVisible"; //!<内置快捷方式是否可见。
19+
public static final String AllowAnonymous = "com.stupidbeauty.hxlauncher.allowAnonymous"; //!< Whether to allow anonymous.
2020
public static final String UseHiveLayout="com.stupidbeauty.hxlauncher.useHiveLayout"; //!<是否使用蜂窝布局
2121
public static final String VoicePackageMapVersion="com.stupidbeauty.hxlauncher.voicePackageMapVersion"; //!<语音命中应用数据版本号
2222
public static final String VoiceShortCutMapVersion="com.stupidbeauty.hxlauncher.voiceShortCutMapVersion"; //!<语音命中快捷方式数据版本号

app/src/main/java/com/stupidbeauty/farmingbookapp/PreferenceManagerUtil.java

+28-11
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public static void setUseHiveLayout(Boolean isChecked)
116116
*/
117117
public static void setBuiltinShortcutsVisible(Boolean hasInit)
118118
{
119-
Context ct = HxLauncherApplication.getAppContext(); //获取应用程序上下文。
120-
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct); //获取共享配置对象。
121-
sp.edit().putBoolean(Constants.Common.BuitinShortcutsVisible, hasInit).commit(); //保存。
119+
Context ct = HxLauncherApplication.getAppContext(); //获取应用程序上下文。
120+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct); //获取共享配置对象。
121+
sp.edit().putBoolean(Constants.Common.BuitinShortcutsVisible, hasInit).commit(); //保存。
122122
} //public static void saveHasFAQInit(Boolean hasInit)s
123123

124124
/**
@@ -127,22 +127,39 @@ public static void setBuiltinShortcutsVisible(Boolean hasInit)
127127
*/
128128
public static boolean isHiveLayout()
129129
{
130-
Context ct = HxLauncherApplication.getAppContext();
131-
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct);
132-
return sp.getBoolean(Constants.Common.UseHiveLayout, false);
133-
130+
Context ct = HxLauncherApplication.getAppContext();
131+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct);
132+
return sp.getBoolean(Constants.Common.UseHiveLayout, false);
134133
} //public static boolean isHiveLayout()
135134

136135
/**
137136
* 是否显示内置快捷方式。陈欣
138137
*/
139138
public static boolean isBuiltinShortcutsVisible()
140139
{
141-
Context ct = HxLauncherApplication.getAppContext();
142-
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct);
143-
return sp.getBoolean(Constants.Common.BuitinShortcutsVisible, true);
140+
Context ct = HxLauncherApplication.getAppContext();
141+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct);
142+
return sp.getBoolean(Constants.Common.BuitinShortcutsVisible, true);
144143
}
145144

145+
/**
146+
* Set whether to allow anonymous.
147+
* @param hasInit Whether to allow anonymous.
148+
*/
149+
public static void setAllowAnonymous(Boolean hasInit)
150+
{
151+
Context ct = HxLauncherApplication.getAppContext(); //获取应用程序上下文。
152+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct); //获取共享配置对象。
153+
sp.edit().putBoolean(Constants.Common.AllowAnonymous, hasInit).commit(); //保存。
154+
} //public static void saveHasFAQInit(Boolean hasInit)s
146155

147-
156+
/**
157+
* wHETHER to allow anonymous
158+
*/
159+
public static boolean getAllowAnonymous()
160+
{
161+
Context ct = HxLauncherApplication.getAppContext();
162+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ct);
163+
return sp.getBoolean(Constants.Common.AllowAnonymous, true);
164+
}
148165
}

app/src/main/java/com/stupidbeauty/hxlauncher/activity/FakeLauncherActivity.java

-8
This file was deleted.

app/src/main/java/com/stupidbeauty/hxlauncher/application/HxLauncherApplication.java

+3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ public void onCreate()
9797

9898
mContext = getApplicationContext(); //获取应用程序上下文。
9999

100+
boolean allowAnonymous=PreferenceManagerUtil.getAllowAnonymous(); // Get settings, whether allow anonymous.
101+
100102
int actualPort=chooseRandomPort(); // Choose a random port.
101103
builtinFtpServer.setPort(actualPort); // Set the port.
102104
builtinFtpServer.setAllowActiveMode(false); // Do not allow active mode.
105+
builtinFtpServer.setAllowAnonymous(allowAnonymous); // Whether allow anonymous.
103106
builtinFtpServer.start(); // Start the builtin ftp server.
104107
} //public void onCreate()
105108

0 commit comments

Comments
 (0)