Skip to content

Commit

Permalink
Version 3
Browse files Browse the repository at this point in the history
Replaced blocked phone number toasts with notifications
  • Loading branch information
StefanIlchev committed Aug 29, 2022
1 parent 1bd6544 commit e70e364
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 14 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ allprojects {
mavenLocal()
maven { url 'https://jitpack.io' }
}

tasks.withType(JavaCompile) {
options.deprecation = true
}
}

android {
Expand All @@ -38,7 +42,7 @@ android {
defaultConfig {
minSdk 28
targetSdk compileSdk
versionCode 2
versionCode 3
versionName "$versionCode"
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
android:label="@string/app_name"
android:supportsRtl="true">

<activity
android:name=".MainActivity"
Expand Down Expand Up @@ -34,4 +35,7 @@

<!-- end phone calls -->
<uses-permission-sdk-23 android:name="android.permission.ANSWER_PHONE_CALLS" />

<!-- notify blocked calls -->
<uses-permission-sdk-23 android:name="android.permission.POST_NOTIFICATIONS" />
</manifest>
58 changes: 49 additions & 9 deletions src/main/java/ilchev/stefan/callblocker/CallReceiver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ilchev.stefan.callblocker;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -10,25 +14,60 @@
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings({"deprecation", "RedundantSuppression"})
public class CallReceiver extends BroadcastReceiver {

private static final String TAG = "CallReceiver";

private static void endCall(Context context, String phoneNumber) {
private static int NOTIFICATION_ID = 0;

@SuppressWarnings({"deprecation", "RedundantSuppression"})
private static String getIncomingNumber(Intent intent) {
return intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
}

@SuppressWarnings({"deprecation", "RedundantSuppression"})
private static boolean endCall(Context context) {
if (context.checkSelfPermission(Manifest.permission.ANSWER_PHONE_CALLS) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, Manifest.permission.ANSWER_PHONE_CALLS, Toast.LENGTH_LONG).show();
}
var telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if (telecomManager != null && telecomManager.endCall()) {
var text = context.getString(R.string.blocked_phone_number, phoneNumber);
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
var telecomManager = context.getSystemService(TelecomManager.class);
return telecomManager != null && telecomManager.endCall();
}

private static void notifyBlockedCall(Context context, String phoneNumber) {
var manager = context.getSystemService(NotificationManager.class);
if (manager == null) {
return;
}
var id = NOTIFICATION_ID = NOTIFICATION_ID % Integer.MAX_VALUE + 1;
var applicationInfo = context.getApplicationInfo();
var applicationIcon = applicationInfo.icon;
var applicationLabel = context.getPackageManager().getApplicationLabel(applicationInfo);
var text = context.getString(R.string.blocked_phone_number, phoneNumber);
var intent = PendingIntent.getActivity(
context,
id,
new Intent(Intent.ACTION_MAIN, null, context, MainActivity.class)
.addCategory(Intent.CATEGORY_LAUNCHER),
PendingIntent.FLAG_IMMUTABLE);
var builder = new Notification.Builder(context, BuildConfig.APPLICATION_ID)
.setSmallIcon(applicationIcon)
.setContentTitle(applicationLabel)
.setContentText(text)
.setContentIntent(intent);
if (id == 1) {
var channel = new NotificationChannel(
BuildConfig.APPLICATION_ID,
applicationLabel,
NotificationManager.IMPORTANCE_MIN);
manager.createNotificationChannel(channel);
}
manager.notify(id, builder.build());
}

@Override
public void onReceive(Context context, Intent intent) {
var phoneNumber = intent != null ? intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) : null;
var phoneNumber = intent != null ? getIncomingNumber(intent) : null;
if (context == null || phoneNumber == null ||
!TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction()) ||
!TelephonyManager.EXTRA_STATE_RINGING.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))) {
Expand All @@ -37,8 +76,9 @@ public void onReceive(Context context, Intent intent) {
var sharedPreferences = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
try {
var callBlocker = new CallBlocker(sharedPreferences);
if (callBlocker.isBlocked(phoneNumber)) {
endCall(context, phoneNumber);
if (callBlocker.isBlocked(phoneNumber) &&
endCall(context)) {
notifyBlockedCall(context, phoneNumber);
}
} catch (Throwable t) {
Log.w(TAG, t);
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/ilchev/stefan/callblocker/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ilchev.stefan.callblocker;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
Expand All @@ -11,12 +15,19 @@
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.function.Function;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";

@SuppressLint("InlinedApi")
private static final String POST_NOTIFICATIONS =
Manifest.permission.POST_NOTIFICATIONS;

private static int toBlockId(Boolean value) {
return value == null ? R.id.block_none
: value ? R.id.block_matches
Expand All @@ -29,6 +40,15 @@ private static Boolean toBoolean(int value) {
: null;
}

@SuppressWarnings({"deprecation", "RedundantSuppression"})
private PackageInfo getPackageInfo(int flags) throws PackageManager.NameNotFoundException {
var packageManager = getPackageManager();
var packageName = getPackageName();
return Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
? packageManager.getPackageInfo(packageName, flags)
: packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags));
}

private void updateContent(int sourceId, Function<CallBlocker, Boolean> function) {
var sharedPreferences = getSharedPreferences(BuildConfig.APPLICATION_ID, MODE_PRIVATE);
String error = null;
Expand Down Expand Up @@ -103,9 +123,16 @@ private void initContent() {

private void requestRequestedPermissions() {
try {
var packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
if (packageInfo.requestedPermissions != null && packageInfo.requestedPermissions.length > 0) {
requestPermissions(packageInfo.requestedPermissions, 0);
var packageInfo = getPackageInfo(PackageManager.GET_PERMISSIONS);
var set = packageInfo.requestedPermissions != null
? new HashSet<>(Arrays.asList(packageInfo.requestedPermissions))
: Collections.<String>emptySet();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
set.remove(POST_NOTIFICATIONS);
}
if (!set.isEmpty()) {
var permissions = set.toArray(new String[]{});
requestPermissions(permissions, 0);
}
} catch (Throwable t) {
Log.w(TAG, t);
Expand Down
13 changes: 13 additions & 0 deletions src/main/res/drawable/ic_launcher_monochrome.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
1 change: 1 addition & 0 deletions src/main/res/mipmap/ic_launcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_monochrome" />
</adaptive-icon>

0 comments on commit e70e364

Please sign in to comment.