From 3e162b88cec2d2873e04e00c98f0356b52ea4cb0 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 25 Oct 2022 22:21:47 +0200 Subject: [PATCH] Whitelist now works with local numbers too Fix bug #1 --- src/.idea/deploymentTargetDropDown.xml | 2 +- src/app/build.gradle | 10 ++++---- .../com/xfarrow/locatemydevice/Utils.java | 18 ++++++++++++- .../locatemydevice/WhitelistDbHandler.java | 25 ++++++++++++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/.idea/deploymentTargetDropDown.xml b/src/.idea/deploymentTargetDropDown.xml index 28cc420..d452729 100644 --- a/src/.idea/deploymentTargetDropDown.xml +++ b/src/.idea/deploymentTargetDropDown.xml @@ -12,6 +12,6 @@ - + \ No newline at end of file diff --git a/src/app/build.gradle b/src/app/build.gradle index 0510dc8..18da622 100644 --- a/src/app/build.gradle +++ b/src/app/build.gradle @@ -3,14 +3,14 @@ plugins { } android { - compileSdk 32 + compileSdk 33 defaultConfig { applicationId "com.xfarrow.locatemydevice" minSdk 28 - targetSdk 32 + targetSdk 33 versionCode 2 - versionName "1.0" + versionName "1.0.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -31,9 +31,9 @@ android { } dependencies { - implementation 'androidx.appcompat:appcompat:1.5.1' - implementation 'com.google.android.material:material:1.6.1' + implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'com.googlecode.libphonenumber:libphonenumber:8.12.57' } \ No newline at end of file diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java index bdb88b8..a9f3d96 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java @@ -2,8 +2,12 @@ package com.xfarrow.locatemydevice; import android.telephony.SmsManager; +import androidx.annotation.Nullable; + +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber; import java.util.ArrayList; -import java.util.Arrays; import java.util.Locale; public class Utils { @@ -43,4 +47,16 @@ public class Utils { .append("https://www.openstreetmap.org/?mlat=").append(latitude).append("&mlon=").append(longitude).toString(); } + @Nullable + public static String extractCountryCodeFromPhoneNumber(String phoneNumber){ + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + Phonenumber.PhoneNumber numberProto; + try { + numberProto = phoneUtil.parse(phoneNumber, ""); + } catch (NumberParseException e) { + return null; + } + return String.valueOf(numberProto.getCountryCode()); + } + } diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java index 64f7a42..ade85dd 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java @@ -71,9 +71,32 @@ public class WhitelistDbHandler extends SQLiteOpenHelper { return array_list; } + /* + * Checks if a contact is already in the database. + * + * If a phoneNumber with an international country code is provided, checks if exists the same + * number without the country code. This is very useful if the user stores in the whitelist a + * number without the international country code because it'll be checked against the SMS sender's + * number obtained from the Broadcast, always containing an international country code. + */ public boolean isContactPresent(String phoneNo){ SQLiteDatabase db = this.getWritableDatabase(); - String query = "Select * from " + TABLE_CONTACTS + " where " + KEY_PH_NO + " = " + "\"" + phoneNo + "\""; + // SELECT KEY_PH_NO FROM TABLE_CONTACTS WHERE KEY_PH_NO = "phoneNo" OR KEY_PH_NO = "phoneNumberWithoutPrefix" + String query = String.format("SELECT %s FROM %s WHERE %s = \"%s\"", + KEY_PH_NO, + TABLE_CONTACTS, + KEY_PH_NO, + phoneNo + ); + + String countryCode = Utils.extractCountryCodeFromPhoneNumber(phoneNo); + if(countryCode != null){ + String phoneNumberWithoutPrefix = phoneNo.replace("+", "").replace(countryCode, ""); + query += String.format(" OR %s = \"%s\"", + KEY_PH_NO, + phoneNumberWithoutPrefix); + } + Cursor cursor = db.rawQuery(query, null); int count = cursor.getCount(); cursor.close();