Skip to content

Commit

Permalink
Removing the blocked call from the Call log
Browse files Browse the repository at this point in the history
  • Loading branch information
kaliturin committed May 13, 2017
1 parent 6208a4a commit a015028
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.kaliturin.blacklist"
minSdkVersion 9
targetSdkVersion 25
versionCode 11
versionName "1.2.6"
versionCode 12
versionName "1.2.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kaliturin.blacklist"
android:versionCode="11"
android:versionName="1.2.6">
android:versionCode="12"
android:versionName="1.2.7">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void onClick(View v) {
Settings.BLOCK_PRIVATE_CALLS);
adapter.addCheckbox(R.string.Journal, R.string.Write_calls_to_journal,
Settings.WRITE_CALLS_JOURNAL);
adapter.addCheckbox(R.string.Call_log, R.string.Remove_from_call_log,
Settings.REMOVE_FROM_CALL_LOG);

// calls notifications settings
adapter.addTitle(R.string.Calls_blocking_notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.kaliturin.blacklist.utils.DatabaseAccessHelper;
import com.kaliturin.blacklist.utils.DatabaseAccessHelper.Contact;
import com.kaliturin.blacklist.utils.Notifications;
import com.kaliturin.blacklist.utils.Settings;

/**
* SMS/Call blocking events processing service
Expand Down Expand Up @@ -65,10 +66,16 @@ private void processEvent(Context context, @NonNull String number, String name,
// write to the journal
writeToJournal(context, number, name, body);

// notify the user
// if no body - there was a call
if (body == null) {
// notify the user
Notifications.onCallBlocked(context, name);
// remove the last call from the log
if(Settings.getBooleanValue(context, Settings.REMOVE_FROM_CALL_LOG)) {
removeFromCallLog(context, number);
}
} else {
// notify the user
Notifications.onSmsBlocked(context, name);
}
}
Expand All @@ -86,6 +93,18 @@ private void writeToJournal(Context context, String number, String name, String
}
}

// Removes passed number from the Call log
private void removeFromCallLog(Context context, String number) {
// wait for the call be written to the Call log
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
// and then remove it
ContactsAccessHelper db = ContactsAccessHelper.getInstance(context);
db.deleteLastRecordFromCallLog(context, number, 10000);
}

// Starts the service
public static void start(Context context, @NonNull String number, String name, String body) {
Intent intent = new Intent(context, BlockEventProcessService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import android.database.CursorWrapper;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.*;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
Expand Down Expand Up @@ -300,6 +301,7 @@ private ContactNumberCursorWrapper getContactNumbers(long contactId) {
private static final Uri URI_CONTENT_SMS = Uri.parse("content://sms");
private static final Uri URI_CONTENT_SMS_INBOX = Uri.parse("content://sms/inbox");
private static final Uri URI_CONTENT_SMS_CONVERSATIONS = Uri.parse("content://sms/conversations");
private static final Uri URI_CONTENT_CALLS = Uri.parse("content://call_log/calls");

// SMS data columns
public static final String ID = "_id";
Expand Down Expand Up @@ -444,22 +446,17 @@ public Contact getContact() {
@Nullable
private ContactFromCallsCursorWrapper getContactsFromCallsLog(@Nullable String filter) {
filter = (filter == null ? "%%" : "%" + filter + "%");
Cursor cursor = null;
// try/catch is required because of Calls.CONTENT_URI
try {
// filter by name or by number
cursor = contentResolver.query(
Calls.CONTENT_URI,
new String[]{Calls._ID, Calls.NUMBER, Calls.CACHED_NAME},
Calls.NUMBER + " IS NOT NULL AND (" +
Calls.CACHED_NAME + " IS NULL AND " +
Calls.NUMBER + " LIKE ? OR " +
Calls.CACHED_NAME + " LIKE ? )",
new String[]{filter, filter},
Calls.DATE + " DESC");
} catch (SecurityException e) {
Log.w(TAG, e);
}

// filter by name or by number
Cursor cursor = contentResolver.query(
URI_CONTENT_CALLS,
new String[]{Calls._ID, Calls.NUMBER, Calls.CACHED_NAME},
Calls.NUMBER + " IS NOT NULL AND (" +
Calls.CACHED_NAME + " IS NULL AND " +
Calls.NUMBER + " LIKE ? OR " +
Calls.CACHED_NAME + " LIKE ? )",
new String[]{filter, filter},
Calls.DATE + " DESC");

if (validate(cursor)) {
cursor.moveToFirst();
Expand Down Expand Up @@ -516,6 +513,66 @@ public Contact getContact() {
}
}

// Deletes last Call log record that was written since "duration" time
public boolean deleteLastRecordFromCallLog(Context context, String number, long duration) {
if (!Permissions.isGranted(context, Permissions.WRITE_CALL_LOG)) {
return false;
}

// get id of the last record has been written since duration ago
long id = getLastRecordIdFromCallLog(context, number, duration);
if(id < 0) {
return false;
}

// delete record from log by id
int count = contentResolver.delete(
URI_CONTENT_CALLS,
ID + " = ? ",
new String[]{String.valueOf(id)});

return (count > 0);
}

// Returns last call record from the Call log that was written since "duration" time
private long getLastRecordIdFromCallLog(Context context, String number, long duration) {
if (!Permissions.isGranted(context, Permissions.READ_CALL_LOG)) {
return -1;
}

// We should not search call just by number because it can be not normalized.
// Therefore select all records have been written since passed time duration,
// normalize every number and then compare.
long time = System.currentTimeMillis() - duration;

Cursor cursor = contentResolver.query(
URI_CONTENT_CALLS,
new String[]{Calls._ID, Calls.NUMBER},
Calls.NUMBER + " IS NOT NULL AND " +
Calls.DATE + " > ? ",
new String[]{String.valueOf(time)},
Calls.DATE + " DESC");

long id = -1;
if (validate(cursor)) {
cursor.moveToFirst();
final int ID = cursor.getColumnIndex(Calls._ID);
final int NUMBER = cursor.getColumnIndex(Calls.NUMBER);
// get first equal
do {
String _number = cursor.getString(NUMBER);
_number = normalizePhoneNumber(_number);
if(_number.equals(number)) {
id = cursor.getLong(ID);
break;
}
} while (cursor.moveToNext());
cursor.close();
}

return id;
}

//-------------------------------------------------------------------------------------

// SMS conversation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class Permissions {
public static final String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";
public static final String READ_CONTACTS = "android.permission.READ_CONTACTS";
public static final String READ_CALL_LOG = "android.permission.READ_CALL_LOG";
public static final String WRITE_CALL_LOG = "android.permission.WRITE_CALL_LOG";
public static final String VIBRATE = "android.permission.VIBRATE";

private static String[] PERMISSIONS = new String[]{
Expand All @@ -65,6 +66,7 @@ public class Permissions {
READ_PHONE_STATE,
READ_CONTACTS,
READ_CALL_LOG,
WRITE_CALL_LOG,
VIBRATE
};

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/kaliturin/blacklist/utils/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class Settings {
public static final String GO_TO_JOURNAL_AT_START = "GO_TO_JOURNAL_AT_START";
public static final String DEFAULT_SMS_APP_NATIVE_PACKAGE = "DEFAULT_SMS_APP_NATIVE_PACKAGE";
public static final String DONT_EXIT_ON_BACK_PRESSED = "DONT_EXIT_ON_BACK_PRESSED";
public static final String REMOVE_FROM_CALL_LOG = "REMOVE_FROM_CALL_LOG";

private static final String TRUE = "TRUE";
private static final String FALSE = "FALSE";
Expand Down Expand Up @@ -129,6 +130,7 @@ public static synchronized void initDefaults(Context context) {
map.put(UI_THEME_LIGHT, FALSE);
map.put(GO_TO_JOURNAL_AT_START, FALSE);
map.put(DONT_EXIT_ON_BACK_PRESSED, FALSE);
map.put(REMOVE_FROM_CALL_LOG, FALSE);

if (!Permissions.isGranted(context, Permissions.WRITE_EXTERNAL_STORAGE)) {
settingsMap = map;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="Block_calls_not_from_SMS_list">Блокировать звонки с номеров не из списка СМС</string>
<string name="Block_calls_from_private">Блокировать звонки со скрытых номеров</string>
<string name="Write_calls_to_journal">Вести Журнал заблокированных звонков</string>
<string name="Remove_from_call_log">Удалять заблокированные звонки из Списка вызовов</string>
<string name="Notify_in_status_bar_blocked_call">Уведомлять в строке состояния о заблокированном звонке</string>
<string name="Notify_with_sound_blocked_call">Уведомлять звуком о заблокированном звонке</string>
<string name="Notify_with_vibration_blocked_call">Уведомлять вибрацией о заблокированном звонке</string>
Expand Down Expand Up @@ -64,6 +65,7 @@
<string name="Settings">Настройки</string>
<string name="Information">Информация</string>
<string name="Exit">Выход</string>
<string name="Call_log">Список вызовов</string>
<string name="Messaging">Сообщения</string>
<string name="Select_all">Выбрать все</string>
<string name="SELECT_ALL">ВЫБРАТЬ ВСЕ</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="Block_calls_not_from_SMS_list">Block calls from numbers that are not in the SMS list</string>
<string name="Block_calls_from_private">Block calls from private numbers</string>
<string name="Write_calls_to_journal">Write blocked calls to the Event log</string>
<string name="Remove_from_call_log">Remove blocked calls from the Call log</string>
<string name="Notify_in_status_bar_blocked_call">Notify in status bar about blocked call</string>
<string name="Notify_with_sound_blocked_call">Notify with sound about blocked call</string>
<string name="Notify_with_vibration_blocked_call">Notify with vibration about blocked call</string>
Expand Down Expand Up @@ -64,6 +65,7 @@
<string name="Settings">Settings</string>
<string name="Information">Information</string>
<string name="Exit">Exit</string>
<string name="Call_log">Call log</string>
<string name="Messaging">Messaging</string>
<string name="Select_all">Select all</string>
<string name="SELECT_ALL">SELECT ALL</string>
Expand Down

0 comments on commit a015028

Please sign in to comment.