2012-09-08 03:03:23 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-08 03:03:23 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-08 03:03:23 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2015-07-27 17:49:14 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-04-24 23:40:54 +00:00
|
|
|
import android.telephony.PhoneNumberUtils;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.util.Log;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-11-13 18:49:11 +00:00
|
|
|
import com.google.i18n.phonenumbers.NumberParseException;
|
|
|
|
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
|
|
|
import com.google.i18n.phonenumbers.Phonenumber;
|
|
|
|
import com.google.i18n.phonenumbers.ShortNumberInfo;
|
|
|
|
|
2014-04-24 23:40:54 +00:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2015-11-14 23:17:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ShortCodeUtil;
|
2015-10-26 18:03:08 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-04-24 23:40:54 +00:00
|
|
|
import org.thoughtcrime.securesms.util.VisibleForTesting;
|
2015-10-26 18:03:08 +00:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
|
|
|
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
2014-04-24 23:40:54 +00:00
|
|
|
|
2012-09-08 03:03:23 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2014-04-24 23:40:54 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public class CanonicalAddressDatabase {
|
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
private static final String TAG = CanonicalAddressDatabase.class.getSimpleName();
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final int DATABASE_VERSION = 1;
|
|
|
|
private static final String DATABASE_NAME = "canonical_address.db";
|
|
|
|
private static final String TABLE = "canonical_addresses";
|
|
|
|
private static final String ID_COLUMN = "_id";
|
|
|
|
private static final String ADDRESS_COLUMN = "address";
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE + " (" + ID_COLUMN + " integer PRIMARY KEY, " + ADDRESS_COLUMN + " TEXT NOT NULL);";
|
2014-04-24 23:40:54 +00:00
|
|
|
private static final String SELECTION_NUMBER = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)";
|
|
|
|
private static final String SELECTION_OTHER = ADDRESS_COLUMN + " = ? COLLATE NOCASE";
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static CanonicalAddressDatabase instance;
|
2014-04-24 23:40:54 +00:00
|
|
|
private DatabaseHelper databaseHelper;
|
2015-10-26 18:03:08 +00:00
|
|
|
private final Context context;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
private final Map<String, Long> addressCache = new ConcurrentHashMap<>();
|
|
|
|
private final Map<Long, String> idCache = new ConcurrentHashMap<>();
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
public synchronized static CanonicalAddressDatabase getInstance(Context context) {
|
|
|
|
if (instance == null)
|
|
|
|
instance = new CanonicalAddressDatabase(context.getApplicationContext());
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
return instance;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private CanonicalAddressDatabase(Context context) {
|
2015-10-26 18:03:08 +00:00
|
|
|
this.context = context;
|
|
|
|
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
fillCache();
|
|
|
|
}
|
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
public void reset(Context context) {
|
|
|
|
DatabaseHelper old = this.databaseHelper;
|
2015-05-21 09:32:38 +00:00
|
|
|
this.databaseHelper = new DatabaseHelper(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
|
2013-06-25 04:02:30 +00:00
|
|
|
old.close();
|
|
|
|
fillCache();
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private void fillCache() {
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
cursor = db.query(TABLE, null, null, null, null, null, null);
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2014-04-24 23:40:54 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
|
2012-12-24 16:40:37 +00:00
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
|
|
|
|
|
|
|
|
if (address == null || address.trim().length() == 0)
|
|
|
|
address = "Anonymous";
|
|
|
|
|
2014-04-24 23:40:54 +00:00
|
|
|
idCache.put(id, address);
|
2012-12-24 16:40:37 +00:00
|
|
|
addressCache.put(address, id);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-07-27 17:49:14 +00:00
|
|
|
public @NonNull String getAddressFromId(long id) {
|
2012-09-08 03:03:23 +00:00
|
|
|
String cachedAddress = idCache.get(id);
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cachedAddress != null)
|
|
|
|
return cachedAddress;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2015-10-26 18:03:08 +00:00
|
|
|
Log.w(TAG, "Hitting DB on query [ID].");
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
cursor = db.query(TABLE, null, ID_COLUMN + " = ?", new String[] {id+""}, null, null, null);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (!cursor.moveToFirst())
|
2012-09-08 03:03:23 +00:00
|
|
|
return "Anonymous";
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (address == null || address.trim().equals("")) {
|
2012-09-08 03:03:23 +00:00
|
|
|
return "Anonymous";
|
2011-12-20 18:20:44 +00:00
|
|
|
} else {
|
2012-09-08 03:03:23 +00:00
|
|
|
idCache.put(id, address);
|
|
|
|
return address;
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
2012-09-08 03:03:23 +00:00
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void close() {
|
|
|
|
databaseHelper.close();
|
|
|
|
instance = null;
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
public long getCanonicalAddressId(@NonNull String address) {
|
|
|
|
try {
|
|
|
|
long canonicalAddressId;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
if (isNumberAddress(address) && TextSecurePreferences.isPushRegistered(context)) {
|
2015-11-13 18:49:11 +00:00
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
|
2015-11-14 23:17:05 +00:00
|
|
|
if (!ShortCodeUtil.isShortCode(localNumber, address)) {
|
2015-11-13 18:49:11 +00:00
|
|
|
address = PhoneNumberFormatter.formatNumber(address, localNumber);
|
|
|
|
}
|
2015-10-26 18:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1) {
|
|
|
|
return canonicalAddressId;
|
|
|
|
}
|
|
|
|
|
|
|
|
canonicalAddressId = getCanonicalAddressIdFromDatabase(address);
|
|
|
|
|
|
|
|
idCache.put(canonicalAddressId, address);
|
|
|
|
addressCache.put(address, canonicalAddressId);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
return canonicalAddressId;
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
public @NonNull List<Long> getCanonicalAddressIds(@NonNull List<String> addresses) {
|
|
|
|
List<Long> addressList = new LinkedList<>();
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (String address : addresses) {
|
2014-04-24 23:40:54 +00:00
|
|
|
addressList.add(getCanonicalAddressId(address));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return addressList;
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private long getCanonicalAddressFromCache(String address) {
|
2014-04-24 23:40:54 +00:00
|
|
|
Long cachedAddress = addressCache.get(address);
|
|
|
|
return cachedAddress == null ? -1L : cachedAddress;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-26 18:03:08 +00:00
|
|
|
private long getCanonicalAddressIdFromDatabase(@NonNull String address) {
|
|
|
|
Log.w(TAG, "Hitting DB on query [ADDRESS]");
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2015-10-26 18:03:08 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2015-10-26 18:03:08 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
String[] selectionArguments = new String[]{address};
|
|
|
|
boolean isNumber = isNumberAddress(address);
|
|
|
|
|
|
|
|
cursor = db.query(TABLE, null, isNumber ? SELECTION_NUMBER : SELECTION_OTHER,
|
|
|
|
selectionArguments, null, null, null);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
|
2012-09-08 03:03:23 +00:00
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(ADDRESS_COLUMN, address);
|
|
|
|
return db.insert(TABLE, ADDRESS_COLUMN, contentValues);
|
2014-04-24 23:40:54 +00:00
|
|
|
} else {
|
2015-10-26 18:03:08 +00:00
|
|
|
long canonicalId = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
|
|
|
|
String oldAddress = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
|
|
|
|
|
|
|
|
if (!address.equals(oldAddress)) {
|
2014-04-24 23:40:54 +00:00
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(ADDRESS_COLUMN, address);
|
|
|
|
db.update(TABLE, contentValues, ID_COLUMN + " = ?", new String[]{canonicalId+""});
|
|
|
|
|
|
|
|
addressCache.remove(oldAddress);
|
|
|
|
}
|
2015-10-26 18:03:08 +00:00
|
|
|
|
2014-04-24 23:40:54 +00:00
|
|
|
return canonicalId;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
2012-09-08 03:03:23 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
}
|
2014-04-24 23:40:54 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-04-24 23:40:54 +00:00
|
|
|
@VisibleForTesting
|
2015-10-26 18:03:08 +00:00
|
|
|
static boolean isNumberAddress(@NonNull String number) {
|
|
|
|
if (number.contains("@")) return false;
|
|
|
|
if (GroupUtil.isEncodedGroup(number)) return false;
|
2014-04-24 23:40:54 +00:00
|
|
|
|
|
|
|
final String networkNumber = PhoneNumberUtils.extractNetworkPortion(number);
|
2015-10-26 18:03:08 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(networkNumber)) return false;
|
|
|
|
if (networkNumber.length() < 3) return false;
|
2014-04-24 23:40:54 +00:00
|
|
|
|
|
|
|
return PhoneNumberUtils.isWellFormedSmsAddress(number);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static class DatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
|
|
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
|
|
|
|
super(context, name, factory, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2011-12-20 18:20:44 +00:00
|
|
|
db.execSQL(DATABASE_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|