Skip to content

Commit

Permalink
Merge pull request #2031 from marunjar/incoming_calls_in_history
Browse files Browse the repository at this point in the history
implement call screening service
  • Loading branch information
Neamar authored Dec 29, 2022
2 parents 57b034b + 3fbaf6c commit 7cb48f6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<service
android:name=".broadcast.IncomingCallScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>


<service android:name=".dataprovider.AppProvider" />
<service android:name=".dataprovider.ContactsProvider" />
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/fr/neamar/kiss/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.neamar.kiss;

import static android.view.HapticFeedbackConstants.LONG_PRESS;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
Expand Down Expand Up @@ -61,12 +63,9 @@
import fr.neamar.kiss.ui.KeyboardScrollHider;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.ui.SearchEditText;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.Permission;
import fr.neamar.kiss.utils.SystemUiVisibilityHelper;

import static android.view.HapticFeedbackConstants.LONG_PRESS;

public class MainActivity extends Activity implements QueryInterface, KeyboardScrollHider.KeyboardHandler, View.OnTouchListener {

public static final String START_LOAD = "fr.neamar.summon.START_LOAD";
Expand Down Expand Up @@ -366,7 +365,7 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
this.hider.start();

// Enable/disable phone broadcast receiver
PackageManagerUtils.enableComponent(this, IncomingCallHandler.class, prefs.getBoolean("enable-phone-history", false));
IncomingCallHandler.setEnabled(this, prefs.getBoolean("enable-phone-history", false));

// Hide the "X" after the text field, instead displaying the menu button
displayClearOnInput();
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/fr/neamar/kiss/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -47,7 +48,6 @@
import fr.neamar.kiss.preference.PreferenceScreenHelper;
import fr.neamar.kiss.preference.SwitchPreference;
import fr.neamar.kiss.searcher.QuerySearcher;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.Permission;

@SuppressWarnings("FragmentInjection")
Expand Down Expand Up @@ -570,7 +570,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
Permission.askPermission(Permission.PERMISSION_READ_PHONE_STATE, new Permission.PermissionResultListener() {
@Override
public void onGranted() {
PackageManagerUtils.enableComponent(SettingsActivity.this, IncomingCallHandler.class, true);
setPhoneHistoryEnabled(true);
}

@Override
Expand All @@ -582,7 +582,7 @@ public void onDenied() {
}
});
} else {
PackageManagerUtils.enableComponent(this, IncomingCallHandler.class, enabled);
setPhoneHistoryEnabled(enabled);
}
} else if (key.equalsIgnoreCase("primary-color")) {
UIColors.clearPrimaryColorCache(this);
Expand Down Expand Up @@ -614,6 +614,15 @@ public void onDenied() {
}
}

private void setPhoneHistoryEnabled(boolean enabled) {
IncomingCallHandler.setEnabled(this, enabled);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && enabled) {
RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
startActivityForResult(intent, 1);
}
}

@Override
public void onPause() {
super.onPause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import fr.neamar.kiss.DataHandler;
import fr.neamar.kiss.KissApplication;
import fr.neamar.kiss.dataprovider.ContactsProvider;
import fr.neamar.kiss.pojo.ContactsPojo;
import fr.neamar.kiss.utils.PackageManagerUtils;

public class IncomingCallHandler extends BroadcastReceiver {

Expand Down Expand Up @@ -46,4 +48,13 @@ public void onReceive(final Context context, Intent intent) {
Log.e("Phone Receive Error", " " + e);
}
}

public static void setEnabled(Context context, boolean enabled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
PackageManagerUtils.enableComponent(context, IncomingCallHandler.class, false);
} else {
PackageManagerUtils.enableComponent(context, IncomingCallHandler.class, enabled);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.neamar.kiss.broadcast;

import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telecom.Call;
import android.telecom.CallScreeningService;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import fr.neamar.kiss.DataHandler;
import fr.neamar.kiss.KissApplication;
import fr.neamar.kiss.dataprovider.ContactsProvider;
import fr.neamar.kiss.pojo.ContactsPojo;

@RequiresApi(api = Build.VERSION_CODES.N)
public class IncomingCallScreeningService extends CallScreeningService {

@Override
public void onScreenCall(@NonNull Call.Details callDetails) {
respondToCall(callDetails, new CallResponse.Builder().build());

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("enable-phone-history", false) && callDetails.getHandle() != null) {
String phoneNumber = callDetails.getHandle().getSchemeSpecificPart();
if (!TextUtils.isEmpty(phoneNumber)) {
DataHandler dataHandler = KissApplication.getApplication(this).getDataHandler();
ContactsProvider contactsProvider = dataHandler.getContactsProvider();
if (contactsProvider != null) {
ContactsPojo contactPojo = contactsProvider.findByPhone(phoneNumber);
if (contactPojo != null) {
dataHandler.addToHistory(contactPojo.getHistoryId());
}
}
}
}
}
}
9 changes: 4 additions & 5 deletions app/src/main/java/fr/neamar/kiss/utils/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ListIterator;

import androidx.annotation.NonNull;


public class Permission {
public static final int PERMISSION_READ_CONTACTS = 0;
public static final int PERMISSION_CALL_PHONE = 1;
Expand All @@ -21,14 +20,14 @@ public class Permission {
private static final String[] permissions = {
Manifest.permission.READ_CONTACTS,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_PHONE_STATE
};

// Static weak reference to the linked activity, this is sadly required
// to ensure classes requesting permission can access activity.requestPermission()
private static WeakReference<Activity> currentActivity = new WeakReference<>(null);

private static ArrayList<PermissionResultListener> permissionListeners = new ArrayList<>();
private static final ArrayList<PermissionResultListener> permissionListeners = new ArrayList<>();

public static boolean checkPermission(Context context, int permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || context.checkSelfPermission(permissions[permission]) == PackageManager.PERMISSION_GRANTED;
Expand Down

0 comments on commit 7cb48f6

Please sign in to comment.