session-android/src/org/thoughtcrime/securesms/database/CanonicalAddressDatabase.java

167 lines
5.2 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
2011-12-20 18:20:44 +00:00
public class CanonicalAddressDatabase {
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";
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);";
private static final String[] ID_PROJECTION = {ID_COLUMN};
private static final String SELECTION = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)";
2011-12-20 18:20:44 +00:00
private static final Object lock = new Object();
2011-12-20 18:20:44 +00:00
private static CanonicalAddressDatabase instance;
private final DatabaseHelper databaseHelper;
private final HashMap<String,Long> addressCache = new HashMap<String,Long>();
private final Map<String,String> idCache = Collections.synchronizedMap(new HashMap<String,String>());
2011-12-20 18:20:44 +00:00
public static CanonicalAddressDatabase getInstance(Context context) {
synchronized (lock) {
if (instance == null)
instance = new CanonicalAddressDatabase(context);
2011-12-20 18:20:44 +00:00
return instance;
}
}
private CanonicalAddressDatabase(Context context) {
databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
2011-12-20 18:20:44 +00:00
public String getAddressFromId(String id) {
if (id == null || id.trim().equals("")) return "Anonymous";
String cachedAddress = idCache.get(id);
2011-12-20 18:20:44 +00:00
if (cachedAddress != null)
return cachedAddress;
2011-12-20 18:20:44 +00:00
Cursor cursor = null;
2011-12-20 18:20:44 +00:00
try {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
cursor = db.query(TABLE, null, ID_COLUMN + " = ?", new String[] {id+""}, null, null, null);
2011-12-20 18:20:44 +00:00
if (!cursor.moveToFirst())
return "Anonymous";
2011-12-20 18:20:44 +00:00
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
2011-12-20 18:20:44 +00:00
if (address == null || address.trim().equals("")) {
return "Anonymous";
2011-12-20 18:20:44 +00:00
} else {
idCache.put(id, address);
return address;
}
2011-12-20 18:20:44 +00:00
} finally {
if (cursor != null)
2012-10-01 02:56:29 +00:00
cursor.close();
2011-12-20 18:20:44 +00:00
}
}
2011-12-20 18:20:44 +00:00
public void close() {
databaseHelper.close();
instance = null;
}
2011-12-20 18:20:44 +00:00
public long getCanonicalAddress(String address) {
long canonicalAddress;
2011-12-20 18:20:44 +00:00
if ((canonicalAddress = getCanonicalAddressFromCache(address)) != -1)
return canonicalAddress;
2011-12-20 18:20:44 +00:00
canonicalAddress = getCanonicalAddressFromDatabase(address);
addressCache.put(address, canonicalAddress);
2011-12-20 18:20:44 +00:00
return canonicalAddress;
}
2011-12-20 18:20:44 +00:00
public List<Long> getCanonicalAddresses(List<String> addresses) {
List<Long> addressList = new LinkedList<Long>();
2011-12-20 18:20:44 +00:00
for (String address : addresses) {
addressList.add(getCanonicalAddress(address));
}
2011-12-20 18:20:44 +00:00
return addressList;
}
2011-12-20 18:20:44 +00:00
private long getCanonicalAddressFromCache(String address) {
if (addressCache.containsKey(address))
2012-09-09 23:10:46 +00:00
return Long.valueOf(addressCache.get(address));
2011-12-20 18:20:44 +00:00
return -1L;
}
2011-12-20 18:20:44 +00:00
private long getCanonicalAddressFromDatabase(String address) {
Cursor cursor = null;
try {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String[] selectionArguments = new String[] {address};
cursor = db.query(TABLE, ID_PROJECTION, SELECTION, selectionArguments, null, null, null);
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
ContentValues contentValues = new ContentValues(1);
contentValues.put(ADDRESS_COLUMN, address);
2011-12-20 18:20:44 +00:00
return db.insert(TABLE, ADDRESS_COLUMN, contentValues);
2011-12-20 18:20:44 +00:00
}
return cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
} finally {
if (cursor != null) {
cursor.close();
2011-12-20 18:20:44 +00:00
}
}
2011-12-20 18:20:44 +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
}
2011-12-20 18:20:44 +00:00
}
}