diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java index 2241482..c9bb33f 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java @@ -1,8 +1,10 @@ package dummydomain.yetanothercallblocker; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.telephony.TelephonyManager; import org.slf4j.Logger; @@ -17,6 +19,20 @@ public class CallReceiver extends BroadcastReceiver { private static final Logger LOG = LoggerFactory.getLogger(CallReceiver.class); + 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); + } + // TODO: handle in-call calls @Override diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java index 3604edf..7484308 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java @@ -3,6 +3,7 @@ package dummydomain.yetanothercallblocker; import android.content.Intent; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.SwitchCompat; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -14,6 +15,11 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + SwitchCompat notificationsSwitch = findViewById(R.id.notificationsEnabledSwitch); + notificationsSwitch.setChecked(CallReceiver.isEnabled(this)); + notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked) + -> CallReceiver.setEnabled(MainActivity.this, isChecked)); + PermissionHelper.checkPermissions(this); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5dd2444..df152fa 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,15 @@ - - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3fb0416..ab46306 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,6 +40,9 @@ Safe nonprofit Reviews + + Incoming call notifications enabled + Open debug screen Debug