Ormlite query takes time in Android - java

I am working in Android application in which I am using ormlite. I am taking my phone book contacts and saving them in my local database, but the problem is that it is taking too much time like for almost 1500 contact it is taking almost 70 seconds.
I searched for the Bulk insert in ormlite, but I can't figure it out how to implement it in my following code.
public static void loadLocalPhoneBookSample(Context ctx) {
try{
ContentResolver contentRes = ctx.getContentResolver();
Cursor cur = null;
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
cur = contentRes.query(ContactsContract.Contacts.CONTENT_URI, PROJECTIONS, selection, null, Phone.DISPLAY_NAME + " ASC");
context = ctx;
if (cur.getCount() > 0) {
// create DB object
MUrgencyDBHelper db = new MUrgencyDBHelper(ctx);
RuntimeExceptionDao<ContactLocal, ?> contactDAO = db.getContactLocalIntDataDao();
UpdateBuilder<ContactLocal, ?> updateDAO = contactDAO.updateBuilder();
try {
updateDAO.updateColumnValue("isUseless", true);
updateDAO.update();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// db.writeUnlock();
}
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** Phone Numbers **/
Cursor pCur = contentRes.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatedNo = number.replaceAll("\\s+", "").replace("+", "00").replace("-", "").trim();
try {
QueryBuilder<ContactLocal, ?> query = contactDAO.queryBuilder();
query.where().eq("mFormatedNumber", number);
ContactLocal contact = query.queryForFirst();
boolean addContact = false, alreadyUpdated = true;
if (contact == null) {
addContact = true;
contact = new ContactLocal();
contact.setFirstName(displayName.trim());
contact.setLastName(displayName.trim());
contact.setContactNumber(formatedNo);
}
// check if this contact was already updated before
if (contact.getContactNumber() == null || contact.getContactNumber().length() == 0) {
contact.setContFirstLastNo(number, displayName, displayName, number);
alreadyUpdated = false;
}
contact.setUseless(false);
// if not updated already, Create/Update
if (addContact) {
contactDAO.create(contact);
} else
contactDAO.update(contact);
}
}
pCur.close();
}
}
}

the problem is that it is taking too much time like for almost 1500 contact it is taking almost 70 seconds
#CarloB has the right answer in terms of doing the mass creates inside the dao. callBatchTasks(...) method. Here's the docs on that subject:
http://ormlite.com/docs/batch
To make things a bit faster, you could also go through and record all of the mFormatedNumber in another List and then query for them using an IN query. Use a raw in query to get back the mFormatedNumber that are already in the database:
results = dao.queryRaw(
"SELECT mFormatedNumber from Contact WHERE mFormatedNumber IN ?",
mFormatedNumberList);
For using raw queries with ORMLite, see:
http://ormlite.com/docs/raw-queries
So then you would make one query to see which of the contacts need to be created and then do all of the inserts from within a batch transaction.
Otherwise you are doing ~3000 synchronous database transactions and 40/sec on an Android device is unfortunately pretty typical.

Here is my revised version (might need a few syntax changes)
public static void loadLocalPhoneBookSample(Context ctx) {
try {
ContentResolver contentRes = ctx.getContentResolver();
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Cursor cur = contentRes.query(ContactsContract.Contacts.CONTENT_URI, PROJECTIONS, selection, null, Phone.DISPLAY_NAME + " ASC");
context = ctx;
if (cur.getCount() > 0) {
// create DB object
MUrgencyDBHelper db = new MUrgencyDBHelper(ctx);
RuntimeExceptionDao<ContactLocal, ?> contactDAO = db.getContactLocalIntDataDao();
UpdateBuilder<ContactLocal, ?> updateDAO = contactDAO.updateBuilder();
try {
updateDAO.updateColumnValue("isUseless", true);
updateDAO.update();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// db.writeUnlock();
}
ArrayList<ContactLocal> contacts = new ArrayList<>();
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** Phone Numbers **/
Cursor pCur = contentRes.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatedNo = number.replaceAll("\\s+", "").replace("+", "00").replace("-", "").trim();
try {
QueryBuilder<ContactLocal, ?> query = contactDAO.queryBuilder();
query.where().eq("mFormatedNumber", number);
ContactLocal contact = query.queryForFirst();
if (contact == null) {
contact = new ContactLocal();
contact.setFirstName(displayName.trim());
contact.setLastName(displayName.trim());
contact.setContactNumber(formatedNo);
}
contact.setUseless(false);
contacts.add(contact);
}
}
pCur.close();
}
contactDao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
for (ContactLocal contact : contacts) {
contactDAO.createOrUpdate(contact);
}
}
});
}
}
The main optimization is to use callBatchTasks. From the ormlite documentation:
Databases by default commit changes after every SQL operation. This method disables this "auto-commit" behavior so a number of changes can be made faster and then committed all at once.
By creating an ArrayList and keeping track of the changes, you can use callBatchTasks to create/update at the end all in one shot.
Also I noticed that alreadyUpdated was never accessed, so it's safe to remove.
Also Dao has a createOrUpdate method which is the same as the addContact if statement you had before.

Related

Get Contact Name through contact number not work in Android 9.0 Pie

I want to get the name for incoming call number from contact list through the following method. it work on all android version but it give null in android 9.0 pie.
private String getContactName(String number, Context context) {
String contactName = "";
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER,
ContactsContract.PhoneLookup.HAS_PHONE_NUMBER };
Uri contactUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
Cursor cursor = context.getContentResolver().query(contactUri,
projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return contactName.equals("") ? number : contactName;
}
i also try a new method but it also not working and give me null value. i get this code from google developer documentation. whats wrong with my code or please help me on which method i can get contact name for a number.
public String onCreateIncomingConnection (String s) {
// Get the telephone number from the incoming request URI.
String phoneNumber = s;
String displayName = "Unknown caller";
boolean isCallerInWorkProfile = false;
// Look up contact details for the caller in the personal and work profiles.
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = getContentResolver().query(
lookupUri,
new String[]{
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.CUSTOM_RINGTONE
},
null,
null,
null);
// Use the first contact found and check if they're from the work profile.
if (cursor != null) {
try {
if (cursor.moveToFirst() == true) {
displayName = cursor.getString(1);
isCallerInWorkProfile =
ContactsContract.Contacts.isEnterpriseContactId(cursor.getLong(0));
}
} finally {
cursor.close();
}
}
// Return a configured connection object for the incoming call.
// MyConnection connection = new MyConnection();
// connection.setCallerDisplayName(displayName, TelecomManager.PRESENTATION_ALLOWED);
//
// Our app's activity uses this value to decide whether to show a work badge.
// connection.setIsCallerInWorkProfile(isCallerInWorkProfile);
// Configure the connection further ...
return displayName;
}

how to perform a search on work profile contacts

I am facing one problem and not getting solution on internet.
I am able to list all user profile contacts but its not showing contacts from work profile.
please refer to below links for detail about work profile
https://support.google.com/work/android/answer/6191949?hl=en
https://support.google.com/work/android/answer/7029561?hl=en
`
private static final String[] PROJECTION =
{
Contacts._ID,
Contacts.LOOKUP_KEY,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME
};
private static final String SELECTION =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
Contacts.DISPLAY_NAME + " LIKE ?";
#Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
/*
* Makes search string into pattern and
* stores it in the selection array
*/
mSelectionArgs[0] = "%" + mSearchString + "%";
// Starts the query
return new CursorLoader(
getActivity(),
Contacts.CONTENT_URI,
PROJECTION,
SELECTION,
mSelectionArgs,
null
);
}
`
For example: i have a contact with name "todd" in normal profile on the other hand i have a "tom" in contact under my work profile. Now in native message app during compose it shows todd and tomm both. i want same to happen in my implementation.
How should i get work profile contacts?
refer to the code below that solved my problem
private static final String[] PROJECTION_ENTERPRISE = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.DATA1,
ContactsContract.CommonDataKinds.Phone.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL
};
#RequiresApi(api = Build.VERSION_CODES.N)
private Cursor getEnterpriseContact(String searchString, String[] cols, ContactSearchType mContactSearchType, String digits, String sortOrder) {
// Get the ContentResolver
ContentResolver cr = mContext.getContentResolver();
// Get the Cursor of all the contacts
Uri phoneUri = ContactsContract.CommonDataKinds.Phone.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
Uri phoneUriWithSearch = Uri.withAppendedPath(phoneUri, Uri.encode(searchString));
Cursor pCursor = cr.query(phoneUriWithSearch, cols, null, null, sortOrder);
Cursor workCur = null;
if (mContactSearchType != ContactSearchType.CONTACT_WITH_PHONE_NO) {
Uri emailUri = ContactsContract.CommonDataKinds.Email.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
Uri emailUriWithSearch = Uri.withAppendedPath(emailUri, Uri.encode(searchString));
Cursor eCursor = cr.query(emailUriWithSearch, cols, null, null, sortOrder);
workCur = new MergeCursor(new Cursor[]{pCursor, eCursor});
} else {
workCur=pCur;
}
return workCur;
}

compare a string with a database column's values

I am developing an app in which i have a profiles list which is stored in a database containing too many profiles,the problem is that when i save a new profile, the app must have to check the profile is already exist or not....how to do that
mSaveProfile.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
CProfileDataSource m_profileDataSource = new CProfileDataSource(CAddNewProfileActivity.this);
_profile = mProfileName.getText().toString();
if (_profile.matches(""))
{
Toast.makeText(CAddNewProfileActivity.this, "You did not enter a profileName", Toast.LENGTH_SHORT).show();
return;
} else if(_profile.equalsIgnoreCase(getProfileName(CAddNewProfileActivity.this)))
{
Toast.makeText(CAddNewProfileActivity.this, "Profile already exists", Toast.LENGTH_SHORT).show();
return;
}
else
{
userProfile.setProfileName(_profile);
m_profileDataSource.addProfile(new CUserProfile(CAddNewProfileActivity.this, userProfile.getProfileName(), userProfile.getBrightness(), userProfile.getSleepTime(), m_n8SensorState, userProfile.getOptimization()));
Toast.makeText(CAddNewProfileActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
finish();
}
});
and the following function is only checking the last entered profile not the whole list ..
public String getProfileName(CAddNewProfileActivity cAddNewProfileActivity){
String profile=null;
CProfileDataSource profileDataSource =new CProfileDataSource(cAddNewProfileActivity);
List<CUserProfile> profileName=profileDataSource.getAllProfiles();
for(CUserProfile cp:profileName){
profile=cp.getProfileName();
}
return profile;
}
Below sample method explains how to check whether the profile already exists or not. This is sample one you had to change the values, table name and where conditions according to your.
public void saveOrUpdateProfile(String profileId, String name, String emailId) {
boolean UPDATE_FLAG = false;
SQLiteDatabase sdb = this.getWritableDatabase();
String selectQuery = "SELECT * FROM tableName WHERE profileId = '" + profileId + "';";
Cursor cursor = sdb.rawQuery(selectQuery, null);
try {
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
UPDATE_FLAG = true;
}
}
ContentValues values = new ContentValues();
values. ("name", name);
values. ("email_id", emailId);
if (UPDATE_FLAG) {
sdb.update(tableName, values, "profileId = ?", new String[]{profileId});
} else {
sdb.insert(tableName, null, values);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
}
public long getProfilesRecordCount(String profileName)
{
SQLiteDatabase db=this.openReadable();
long rawNum=DatabaseUtils.queryNumEntries(db,TABLE_NAME,"Profile_Name=?", new String[]{profileName});
return rawNum;
}
rawNum should not greater than 0 ...if it is then String already exists.

Android select only one mobile number from contact list

I am developing an android app, in which I am taking contacts from contact list and showing in listview, when I display mobile number if user has two mobile number saved in a contact, then in listview his name is repeated and second mobile number is shown below his first name, so if two mobile number is saved for a particular number I need to select only first number, so what change I need to do in following code, if anybody know please help.
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
ArrayList<Contact> contacts = new ArrayList<Contact>();
while (c.moveToNext())
{
int type = c.getInt(c.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_MOBILE)
{
Contact con = new Contact(c.getString(c
.getColumnIndex(Contacts.DISPLAY_NAME)), c.getString(c
.getColumnIndex(Phone.NUMBER)));
contacts.add(con);
}
}
public void readContacts() {
Cursor contactsCursor =getActivity().getContentResolver().query(ContactsContract
.Contacts.CONTENT_URI, null, null, null,ContactsContract.Contacts.DISPLAY_NAME);
mContactsMetaDataList = new ArrayList<>();
try {
Set<String> idSet = new HashSet<>();
if (contactsCursor.getCount() > 0) {
while (contactsCursor.moveToNext()) {
String id =contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts._ID));
String name =contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
idSet.add(name);
if (!idSet.contains(name)){
//add contact to your list here
}
}
}
}
}finally {
if (contactsCursor != null) {
contactsCursor.close();
}
}
}

Using method from one class into class

I have two classes:
MyActivity.Java
DatabaseHandler.Java
I would like to take the returning String from the method "getCurrentCar" and put it inside MyActivity.java
DatabaseHandler.Java:
public class DatabaseHandler extends SQLiteOpenHelper {
public Car getCurrentCar() {
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
Car car = null;
try {
if (cursor.moveToFirst()) {
car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
}
}
finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
}
return car;
}
}
I have attempted already, but the parameters are asking for Context.
//DatabaseHandler handler = new DatabaseHandler(contexthere);
DatabaseHandler handler = new DatabaseHandler();
handler.getCurrentCar();
I would like to know how to take that return and put it inside my MyActivity.java so I can then use a BitmapFactory to recreate the image.
Thanks
Context means current runtime :
protected void onCreate(Bundle savedInstanceState) {
...........
...........
// you should instantiate 'DatabaseHandler' here
DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context
..........
..........
// insert the rows
db.createCar(new Car("Sesame street A","23423","anImage1"));
db.createCar(new Car("Sesame street B","43543","anImage2"));
// get the rows which you mean string
for(Car cars : db.getCurrentCar()){
String rows= "id : "+ cars.getID()+ " address : "+getAddress() + "postcode : "+getPostcode()+" image : "+getImage());
}
}
Just adapt .getID(),getAddress(),getPostcode(),getImage() based on the 'getter' methods name in your Car class, as I might write them wrongly
** Codes above I write based on the file that you shared by a link. (your file missing Car class), but I just predict that it should be like above :

Categories

Resources