Skip to content

Commit

Permalink
Refactor ImportService
Browse files Browse the repository at this point in the history
  • Loading branch information
zHd4 committed Jan 29, 2025
1 parent bbd341f commit b43ed1f
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions app/src/main/java/app/notesr/service/android/ImportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import app.notesr.R;
import app.notesr.service.ServiceHandler;
import app.notesr.service.data.importer.MainImportService;
import app.notesr.service.data.importer.ImportResult;

Expand All @@ -28,7 +29,8 @@ public class ImportService extends Service implements Runnable {
private static final String CHANNEL_ID = "ImportChannel";
private static final int LOOP_DELAY = 100;

private MainImportService mainImportService;
private final ServiceHandler<MainImportService> mainImportServiceServiceHandler =
new ServiceHandler<>();

@Override
public void onCreate() {
Expand All @@ -39,8 +41,8 @@ public void onCreate() {
@Override
public void run() {
try {
waitForImportService();
mainImportService.start();
mainImportServiceServiceHandler.waitForService();
mainImportServiceServiceHandler.getService().start();
broadcastLoop();
} catch (InterruptedException e) {
Log.e(TAG, "Thread interrupted", e);
Expand All @@ -51,12 +53,14 @@ public void run() {
}

private void broadcastLoop() throws InterruptedException {
while (mainImportService.getResult() == ImportResult.NONE) {
sendBroadcastData(mainImportService.getStatus(), ImportResult.NONE);
MainImportService service = mainImportServiceServiceHandler.getService();

while (service.getResult() == ImportResult.NONE) {
sendBroadcastData(service.getStatus(), ImportResult.NONE);
Thread.sleep(LOOP_DELAY);
}

sendBroadcastData(mainImportService.getStatus(), mainImportService.getResult());
sendBroadcastData(service.getStatus(), service.getResult());
}

private void sendBroadcastData(String status, ImportResult result) {
Expand All @@ -67,12 +71,6 @@ private void sendBroadcastData(String status, ImportResult result) {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

private void waitForImportService() throws InterruptedException {
while (mainImportService == null) {
Thread.sleep(LOOP_DELAY);
}
}

private FileInputStream getFileStream(Uri uri) {
try {
return (FileInputStream) getContentResolver().openInputStream(uri);
Expand All @@ -89,7 +87,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Uri sourceUri = intent.getData();
FileInputStream sourceStream = getFileStream(sourceUri);

mainImportService = new MainImportService(context, sourceStream);
MainImportService service = new MainImportService(context, sourceStream);
mainImportServiceServiceHandler.setService(service);

String channelName = getResources().getString(R.string.importing);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName,
Expand Down

0 comments on commit b43ed1f

Please sign in to comment.