Update call log when call ends

This commit is contained in:
xynngh 2020-05-06 15:26:11 +04:00
parent e22aa92525
commit ace3f05ed7
8 changed files with 62 additions and 23 deletions

View File

@ -17,9 +17,14 @@ import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import dummydomain.yetanothercallblocker.event.CallEndedEvent;
import dummydomain.yetanothercallblocker.event.CallOngoingEvent;
import dummydomain.yetanothercallblocker.event.CallStartedEvent;
import dummydomain.yetanothercallblocker.sia.DatabaseSingleton;
import dummydomain.yetanothercallblocker.sia.model.NumberInfo;
import static dummydomain.yetanothercallblocker.EventUtils.postEvent;
public class CallReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(CallReceiver.class);
@ -37,9 +42,12 @@ public class CallReceiver extends BroadcastReceiver {
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(telephonyExtraState)) {
isOnCall = true;
postEvent(new CallOngoingEvent());
} else if (TelephonyManager.EXTRA_STATE_RINGING.equals(telephonyExtraState)) {
if (incomingNumber == null) return;
postEvent(new CallStartedEvent());
Settings settings = App.getSettings();
boolean blockCalls = settings.getBlockCalls();
@ -55,6 +63,7 @@ public class CallReceiver extends BroadcastReceiver {
if (blocked) {
NotificationHelper.showBlockedCallNotification(context, numberInfo);
postEvent(new CallEndedEvent());
}
}
@ -65,6 +74,7 @@ public class CallReceiver extends BroadcastReceiver {
} else if(TelephonyManager.EXTRA_STATE_IDLE.equals(telephonyExtraState)) {
isOnCall = false;
NotificationHelper.hideIncomingCallNotification(context, incomingNumber);
postEvent(new CallEndedEvent());
}
}

View File

@ -0,0 +1,23 @@
package dummydomain.yetanothercallblocker;
import org.greenrobot.eventbus.EventBus;
public class EventUtils {
public static void postEvent(Object event) {
bus().post(event);
}
public static void postStickyEvent(Object event) {
bus().postSticky(event);
}
public static void removeStickyEvent(Object event) {
bus().removeStickyEvent(event);
}
private static EventBus bus() {
return EventBus.getDefault();
}
}

View File

@ -5,6 +5,7 @@ import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -23,6 +24,7 @@ import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import dummydomain.yetanothercallblocker.event.CallEndedEvent;
import dummydomain.yetanothercallblocker.event.MainDbDownloadFinishedEvent;
import dummydomain.yetanothercallblocker.event.MainDbDownloadingEvent;
import dummydomain.yetanothercallblocker.sia.DatabaseSingleton;
@ -112,6 +114,11 @@ public class MainActivity extends AppCompatActivity {
super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onCallEvent(CallEndedEvent event) {
new Handler(getMainLooper()).postDelayed(this::loadCallLog, 1000);
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onMainDbDownloadFinished(MainDbDownloadFinishedEvent event) {
loadCallLog();

View File

@ -0,0 +1,3 @@
package dummydomain.yetanothercallblocker.event;
public class CallEndedEvent extends CallEvent {}

View File

@ -0,0 +1,3 @@
package dummydomain.yetanothercallblocker.event;
public class CallEvent {}

View File

@ -0,0 +1,3 @@
package dummydomain.yetanothercallblocker.event;
public class CallOngoingEvent extends CallEvent {}

View File

@ -0,0 +1,3 @@
package dummydomain.yetanothercallblocker.event;
public class CallStartedEvent extends CallEvent {}

View File

@ -12,7 +12,6 @@ import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,6 +24,10 @@ import dummydomain.yetanothercallblocker.event.SecondaryDbUpdatingEvent;
import dummydomain.yetanothercallblocker.sia.DatabaseSingleton;
import dummydomain.yetanothercallblocker.sia.model.database.DbManager;
import static dummydomain.yetanothercallblocker.EventUtils.postEvent;
import static dummydomain.yetanothercallblocker.EventUtils.postStickyEvent;
import static dummydomain.yetanothercallblocker.EventUtils.removeStickyEvent;
public class TaskService extends IntentService {
public static final String TASK_DOWNLOAD_MAIN_DB = "download_main_db";
@ -95,45 +98,29 @@ public class TaskService extends IntentService {
private void downloadMainDb() {
MainDbDownloadingEvent sticky = new MainDbDownloadingEvent();
postSticky(sticky);
postStickyEvent(sticky);
try {
DbManager.downloadMainDb();
DatabaseSingleton.getCommunityDatabase().reload();
DatabaseSingleton.getFeaturedDatabase().reload();
} finally {
removeSticky(sticky);
removeStickyEvent(sticky);
}
post(new MainDbDownloadFinishedEvent());
postEvent(new MainDbDownloadFinishedEvent());
}
private void updateSecondaryDb() {
SecondaryDbUpdatingEvent sticky = new SecondaryDbUpdatingEvent();
postSticky(sticky);
postStickyEvent(sticky);
try {
DatabaseSingleton.getCommunityDatabase().updateSecondaryDb();
} finally {
removeSticky(sticky);
removeStickyEvent(sticky);
}
post(new SecondaryDbUpdateFinished());
}
private void post(Object event) {
bus().post(event);
}
private void postSticky(Object event) {
bus().postSticky(event);
}
private void removeSticky(Object event) {
bus().removeStickyEvent(event);
}
private EventBus bus() {
return EventBus.getDefault();
postEvent(new SecondaryDbUpdateFinished());
}
}