Skip to content

Commit 640126d

Browse files
committed
Migrate from FtpNotification to FtpServerNotification.kt
To complete the registry base approach
1 parent f3a702a commit 640126d

9 files changed

Lines changed: 40 additions & 281 deletions

File tree

app/src/main/java/com/amaze/filemanager/application/AppConfig.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
import com.amaze.filemanager.filesystem.HybridFile;
4545
import com.amaze.filemanager.filesystem.ssh.CustomSshJConfig;
4646
import com.amaze.filemanager.ftpserver.FtpServerProvider;
47+
import com.amaze.filemanager.ftpserver.ui.FtpServerNotification;
4748
import com.amaze.filemanager.server.ServerRegistry;
49+
import com.amaze.filemanager.ui.activities.MainActivity;
4850
import com.amaze.filemanager.ui.fragments.FtpServerFragment;
4951
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants;
50-
import com.amaze.filemanager.ui.notifications.FtpNotificationAdapter;
52+
import com.amaze.filemanager.ui.notifications.NotificationConstants;
5153
import com.amaze.filemanager.ui.provider.UtilitiesProvider;
5254
import com.amaze.filemanager.utils.NetworkUtil;
5355
import com.amaze.filemanager.utils.ScreenUtils;
@@ -57,6 +59,7 @@
5759
import android.app.Activity;
5860
import android.app.Application;
5961
import android.content.Context;
62+
import android.content.Intent;
6063
import android.content.SharedPreferences;
6164
import android.os.Environment;
6265
import android.os.StrictMode;
@@ -73,6 +76,7 @@
7376
import io.reactivex.schedulers.Schedulers;
7477
import jcifs.Config;
7578
import jcifs.smb.SmbException;
79+
import kotlin.jvm.functions.Function1;
7680

7781
public class AppConfig extends GlideApplication {
7882

@@ -121,15 +125,22 @@ public void onCreate() {
121125

122126
// Register FtpServerProvider into ServerRegistry so FTP is discoverable via the registry.
123127
// Using application context (this) here is safe — no Activity leak.
128+
// Extract the lambda once to share between FtpServerNotification and FtpServerProvider.
129+
Function1<Context, String> getLocalAddress =
130+
ctx -> {
131+
InetAddress addr = NetworkUtil.getLocalInetAddress(ctx, false);
132+
return addr != null ? addr.getHostAddress() : null;
133+
};
124134
ServerRegistry.INSTANCE.register(
125135
new FtpServerProvider(
126136
this,
127137
FtpServerFragment::new,
128-
new FtpNotificationAdapter(),
129-
ctx -> {
130-
InetAddress addr = NetworkUtil.getLocalInetAddress(ctx, false);
131-
return addr != null ? addr.getHostAddress() : null;
132-
}));
138+
new FtpServerNotification(
139+
NotificationConstants.FTP_ID,
140+
NotificationConstants.CHANNEL_FTP_ID,
141+
new Intent(this, MainActivity.class),
142+
getLocalAddress),
143+
getLocalAddress));
133144
}
134145

135146
@Override

app/src/main/java/com/amaze/filemanager/asynchronous/services/ftp/AppFtpService.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import com.amaze.filemanager.BuildConfig
2727
import com.amaze.filemanager.R
2828
import com.amaze.filemanager.ftpserver.commands.AVBL
2929
import com.amaze.filemanager.ftpserver.service.FtpServerService
30+
import com.amaze.filemanager.server.ServerRegistry
31+
import com.amaze.filemanager.server.ServerType
3032
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_ROOTMODE
31-
import com.amaze.filemanager.ui.notifications.FtpNotification
32-
import com.amaze.filemanager.ui.notifications.NotificationConstants
3333
import com.amaze.filemanager.utils.PasswordUtil
3434
import org.slf4j.Logger
3535
import org.slf4j.LoggerFactory
@@ -44,16 +44,19 @@ import java.security.GeneralSecurityException
4444
* password decryption, and error messages.
4545
*/
4646
class AppFtpService : FtpServerService() {
47-
override fun getNotificationId(): Int = NotificationConstants.FTP_ID
47+
private val serverNotification
48+
get() = ServerRegistry.getProvider(ServerType.FTP)!!.getNotification()
4849

49-
override fun getNotificationChannelId(): String = NotificationConstants.CHANNEL_FTP_ID
50+
override fun getNotificationId(): Int = serverNotification.getNotificationId()
51+
52+
override fun getNotificationChannelId(): String = serverNotification.getChannelId()
5053

5154
override fun createStartingNotification(noStopButton: Boolean): Notification {
52-
return FtpNotification.startNotification(applicationContext, noStopButton)
55+
return serverNotification.createStartingNotification(applicationContext, noStopButton)
5356
}
5457

5558
override fun updateRunningNotification(noStopButton: Boolean) {
56-
FtpNotification.updateNotification(applicationContext, noStopButton)
59+
serverNotification.updateRunningNotification(applicationContext, noStopButton)
5760
}
5861

5962
override fun getKeyStoreInputStream(): InputStream? {

app/src/main/java/com/amaze/filemanager/ui/fragments/FtpServerFragment.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ import com.amaze.filemanager.ftpserver.service.FtpEventBus
7979
import com.amaze.filemanager.ftpserver.service.FtpPreferences
8080
import com.amaze.filemanager.ftpserver.service.FtpServerEngine
8181
import com.amaze.filemanager.ftpserver.service.FtpServerEvent
82+
import com.amaze.filemanager.server.ServerRegistry
83+
import com.amaze.filemanager.server.ServerType
8284
import com.amaze.filemanager.ui.activities.MainActivity
83-
import com.amaze.filemanager.ui.notifications.FtpNotification
8485
import com.amaze.filemanager.ui.runIfDocumentsUIExists
8586
import com.amaze.filemanager.ui.theme.AppTheme
8687
import com.amaze.filemanager.utils.NetworkUtil.getLocalInetAddress
@@ -418,10 +419,12 @@ class FtpServerFragment : Fragment(R.layout.fragment_ftp) {
418419

419420
url.text = spannedStatusUrl
420421
ftpBtn.text = resources.getString(R.string.stop_ftp).uppercase()
421-
FtpNotification.updateNotification(
422-
context,
423-
FtpServerEvent.StartedFromTile == signal,
424-
)
422+
ServerRegistry.getProvider(ServerType.FTP)
423+
?.getNotification()
424+
?.updateRunningNotification(
425+
context ?: return,
426+
FtpServerEvent.StartedFromTile == signal,
427+
)
425428
}
426429
FtpServerEvent.FailedToStart -> {
427430
statusText.text = spannedStatusNotRunning

app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotification.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotificationAdapter.kt

Lines changed: 0 additions & 55 deletions
This file was deleted.

app/src/main/java/com/amaze/filemanager/ui/notifications/NotificationConstants.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class NotificationConstants {
4545
public static final int FAILED_ID = 6;
4646
public static final int WAIT_ID = 7;
4747

48-
public static final int TYPE_NORMAL = 0, TYPE_FTP = 1;
48+
public static final int TYPE_NORMAL = 0;
4949

5050
public static final String CHANNEL_NORMAL_ID = "normalChannel";
5151
public static final String CHANNEL_FTP_ID = "ftpChannel";
@@ -60,9 +60,6 @@ public static void setMetadata(
6060
case TYPE_NORMAL:
6161
createNormalChannel(context);
6262
break;
63-
case TYPE_FTP:
64-
createFtpChannel(context);
65-
break;
6663
default:
6764
throw new IllegalArgumentException("Unrecognized type:" + type);
6865
}
@@ -76,41 +73,12 @@ public static void setMetadata(
7673
notification.setPriority(Notification.PRIORITY_MIN);
7774
}
7875
break;
79-
case TYPE_FTP:
80-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
81-
notification.setCategory(Notification.CATEGORY_SERVICE);
82-
notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
83-
}
84-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
85-
notification.setPriority(Notification.PRIORITY_MAX);
86-
}
87-
break;
8876
default:
8977
throw new IllegalArgumentException("Unrecognized type:" + type);
9078
}
9179
}
9280
}
9381

94-
/**
95-
* You CANNOT call this from android < O. THis channel is set so it doesn't bother the user, but
96-
* it has importance.
97-
*/
98-
@RequiresApi(api = Build.VERSION_CODES.O)
99-
private static void createFtpChannel(Context context) {
100-
NotificationManager mNotificationManager =
101-
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
102-
if (mNotificationManager.getNotificationChannel(CHANNEL_FTP_ID) == null) {
103-
NotificationChannel mChannel =
104-
new NotificationChannel(
105-
CHANNEL_FTP_ID,
106-
context.getString(R.string.channel_name_ftp),
107-
NotificationManager.IMPORTANCE_HIGH);
108-
// Configure the notification channel.
109-
mChannel.setDescription(context.getString(R.string.channel_description_ftp));
110-
mNotificationManager.createNotificationChannel(mChannel);
111-
}
112-
}
113-
11482
/**
11583
* You CANNOT call this from android < O. THis channel is set so it doesn't bother the user, with
11684
* the lowest importance.

0 commit comments

Comments
 (0)