Whitelist now works with local numbers too

Fix bug #1
This commit is contained in:
Alessandro Ferro 2022-10-25 22:21:47 +02:00
parent c8b21bf327
commit 3e162b88ce
4 changed files with 47 additions and 8 deletions

View File

@ -12,6 +12,6 @@
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-09-29T21:08:01.717115Z" />
<timeTargetWasSelectedWithDropDown value="2022-10-25T19:37:19.859988Z" />
</component>
</project>

View File

@ -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'
}

View File

@ -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());
}
}

View File

@ -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();