Separate notification settings and call handling

This commit is contained in:
xynngh 2020-05-06 14:52:39 +04:00
parent ef4b018e87
commit e22aa92525
4 changed files with 57 additions and 33 deletions

View File

@ -8,12 +8,16 @@ public class App extends Application {
private static App instance;
private static Settings settings;
@Override
public void onCreate() {
super.onCreate();
instance = this;
settings = new Settings(this);
EventBus.builder()
.throwSubscriberException(BuildConfig.DEBUG)
.sendNoSubscriberEvent(false)
@ -29,4 +33,8 @@ public class App extends Application {
return instance;
}
public static Settings getSettings() {
return settings;
}
}

View File

@ -2,10 +2,8 @@ package dummydomain.yetanothercallblocker;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
@ -28,20 +26,6 @@ public class CallReceiver extends BroadcastReceiver {
private static boolean isOnCall; // TODO: proper handling
public static boolean isEnabled(Context context) {
return context.getPackageManager()
.getComponentEnabledSetting(new ComponentName(context, CallReceiver.class))
!= PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
public static void setEnabled(Context context, boolean enable) {
context.getPackageManager().setComponentEnabledSetting(
new ComponentName(context, CallReceiver.class),
enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
@Override
public void onReceive(Context context, Intent intent) {
if (!TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction())) return;
@ -56,11 +40,17 @@ public class CallReceiver extends BroadcastReceiver {
} else if (TelephonyManager.EXTRA_STATE_RINGING.equals(telephonyExtraState)) {
if (incomingNumber == null) return;
Settings settings = App.getSettings();
boolean blockCalls = settings.getBlockCalls();
boolean showNotifications = settings.getIncomingCallNotifications();
if (blockCalls || showNotifications) {
NumberInfo numberInfo = DatabaseSingleton.getNumberInfo(incomingNumber);
boolean blocked = false;
if (!isOnCall && numberInfo.rating == NumberInfo.Rating.NEGATIVE
&& new Settings(context).getBlockCalls()) {
&& blockCalls) {
blocked = rejectCall(context);
if (blocked) {
@ -68,7 +58,10 @@ public class CallReceiver extends BroadcastReceiver {
}
}
if (!blocked) NotificationHelper.showIncomingCallNotification(context, numberInfo);
if (!blocked && showNotifications) {
NotificationHelper.showIncomingCallNotification(context, numberInfo);
}
}
} else if(TelephonyManager.EXTRA_STATE_IDLE.equals(telephonyExtraState)) {
isOnCall = false;
NotificationHelper.hideIncomingCallNotification(context, incomingNumber);
@ -86,7 +79,7 @@ public class CallReceiver extends BroadcastReceiver {
return true;
} catch (Exception e) {
LOG.warn("Error while rejecting call on API 26+", e);
LOG.warn("Error while rejecting call on API 28+", e);
}
}

View File

@ -47,15 +47,17 @@ public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView = findViewById(R.id.callLogList);
recyclerView.setAdapter(callLogAdapter);
Settings settings = App.getSettings();
SwitchCompat notificationsSwitch = findViewById(R.id.notificationsEnabledSwitch);
notificationsSwitch.setChecked(CallReceiver.isEnabled(this));
notificationsSwitch.setChecked(settings.getIncomingCallNotifications());
notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked)
-> CallReceiver.setEnabled(MainActivity.this, isChecked));
-> settings.setIncomingCallNotifications(isChecked));
SwitchCompat blockCallsSwitch = findViewById(R.id.blockCallsSwitch);
blockCallsSwitch.setChecked(new Settings(this).getBlockCalls());
blockCallsSwitch.setChecked(settings.getBlockCalls());
blockCallsSwitch.setOnCheckedChangeListener((buttonView, isChecked)
-> new Settings(this).setBlockCalls(isChecked));
-> settings.setBlockCalls(isChecked));
UpdateScheduler updateScheduler = UpdateScheduler.get(this);
SwitchCompat autoUpdateSwitch = findViewById(R.id.autoUpdateEnabledSwitch);

View File

@ -5,20 +5,41 @@ import android.content.SharedPreferences;
public class Settings {
private static final String PREF_INCOMING_CALL_NOTIFICATIONS = "incomingCallNotifications";
private static final String PREF_BLOCK_CALLS = "blockCalls";
private final SharedPreferences pref;
public Settings(Context context) {
Settings(Context context) {
pref = context.getSharedPreferences("yacb_preferences", Context.MODE_PRIVATE);
}
public boolean getIncomingCallNotifications() {
return getBoolean(PREF_INCOMING_CALL_NOTIFICATIONS, true);
}
public void setIncomingCallNotifications(boolean show) {
setBoolean(PREF_INCOMING_CALL_NOTIFICATIONS, show);
}
public boolean getBlockCalls() {
return pref.getBoolean(PREF_BLOCK_CALLS, false);
return getBoolean(PREF_BLOCK_CALLS);
}
public void setBlockCalls(boolean block) {
pref.edit().putBoolean(PREF_BLOCK_CALLS, block).apply();
setBoolean(PREF_BLOCK_CALLS, block);
}
public boolean getBoolean(String key) {
return getBoolean(key, false);
}
public boolean getBoolean(String key, boolean defValue) {
return pref.getBoolean(key, defValue);
}
private void setBoolean(String key, boolean value) {
pref.edit().putBoolean(key, value).apply();
}
}