Android: performance cost of content provider queries within queries - android

Writing this on the fly, so I apologize for the code sample. This is NOT real code, it's something I wrote in a plain text editor on the fly. No compile checking, couldn't remember all the exact class and method names, etc. It's just a written concept of what I'm trying to do, I'm looking for feedback on the broader concepts.
I'm working on retrieving a list of contacts from the content provider. I want to be able to filter the results based on the contact's account name. the user will be presented with all available accounts, and will select which ones are to be used, and then that will be used in the retrieval method.
The thing is, the account name is in RawContacts, and the rest of the info I want (display name, lookupID) is in Contacts. I know that ContactsContract.Contacts.Entity is the shortcut to access all of this, so this code sample is what I'm planning to do.
Again, this is written on the fly with no IDE or looking up methods or anything. I'm sure my syntax is bad in many places, but this shows the concept I'm trying to do.
private static final URI URI = ContactsContract.Contacts.URI;
private static final String[] FIRST_PROJECTION = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LOOKUP_KEY
};
private String[] acceptedAccountNames = {Accepted Account Names Will Go Here (dynamic)};
private static final String[] SECOND_PROJECTION = new String[]{
ContactsContract.Contacts.Entity.ACCOUNT_NAME //This is whatever the entity -> RawContacts field name would be
};
public List<Contact> loadContacts(Context context){
List<Contact> contacts = new ArrayList<>();
ContentProvider provider = context.getContentProvider();
Cursor contactsCursor = provider.query(URI, FIRST_PROJECTION, null, null);
contactsCursor.movetoFirst();
while(!contactsCursor.isAtLast()){
String name = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
long lookupKey = contactsCursor.getLong(contactsCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri idUri = Uri.makeWithId(URI, lookupKey);
Uri entityUri = Uri.makeWithTableName(idUri, "entity");
Cursor contactEntityCursor = provider.query(entityUri, SECOND_PROJECTION, null, null);
contactEntityCursor.moveToFirst();
String accountName = contactEntityCursor.getString(contactEntityCursor.getColumnIndex(ContactsContract.Contacts.Entity.ACCOUNT_NAME));
if(Arrays.asList(acceptedAccountNames).contains(accountName)){
Contact contact = new Contact(lookupKey, name);
contacts.add(contact);
}
contactsCursor.moveToNext();
}
return contacts;
}
As you can see, I create a cursor while looping over another cursor. I'm essentially creating a new cursor for each contact in the list.
My question is twofold:
1) What would be the performance implications of this? With a large enough list, would this severely hurt app performance?
2) Is there a better way to do this? As in, a way to do this in a single query, getting all the data I'm looking for in the cursor.
Thanks so much in advance.

Related

How do I use jetbrains exposed with android contentResolver/Mediastore

Im trying to make a simple android mediaplayer app that can be controlled from a distance. At the moment I'm trying to fix the issue of sending all the information on artists/albums/songs that are on the phone. At the moment I'm retrieving all the information as such:
private val contentResolver = activity.contentResolver!!
fun getAll():Set<Album>{
val res = mutableSetOf<Album>()
val cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
arrayOf(
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ALBUM_ART,
MediaStore.Audio.Albums.NUMBER_OF_SONGS,
MediaStore.Audio.Albums.ARTIST)
,null,null)
if(cursor!!.moveToFirst())
do {
res.add(Album().apply {
name = cursor.getString(0)
if (!cursor.getString(1).isNullOrEmpty())
albumArtUri = Uri.parse(cursor.getString(1))
songCount = cursor.getInt(2)
artist = Artist().apply {
name = cursor.getString(3)
}
})
cursor.moveToNext()
}while (!cursor.isAfterLast)
cursor.close()
return res
}
Seeing that I'm using a cursor, I thought I was working with a kind of database (SQLite or so) As you can see, this is a lot of code for just a set of objects with little information; the album objects created don't have the songs in them. For this you'd need to start a new query, starting and a new URI. Now I thought I could use an ORM. So I can actually fill the album objects with a list of songs and so on. I decided to try Jetbrains Exposed, typed:
val database = Database.connect(....)
and I'm at a loss, I don't know how to connect to this the database. I can't seem to find any examples on how to start with this.
Exposed is for JDBC. ContentResolver is not using JDBC, and the Cursor is not an object from JDBC. In general, Android does not use JDBC, in apps or at the OS level.

Xamarin.Mobile Contacts API takes up a lot of time while fetching contacts?

I'm using Xamarin.Mobile Component for Android to fetch contacts using the code:
var book = new AddressBook (Activity) {PreferContactAggregation = true};
var contData = data.Data;
var cur = Activity.ManagedQuery (contData, null, null, null, null);
Contact myContact = null;
var lookupKeyList = new List<string> ();
while (cur.MoveToNext ()) {
lookupKeyList.Add (cur.GetString (cur.GetColumnIndexContactsContract.Contacts.InterfaceConsts.LookupKey)));
}
myContact = book.Where (c => c.Id == lookupKeyList [0]).First ();
This code is part of picking a contact from the phone book and receiving the data on OnActivityResult method.
Unfortunately, this code is taking up too much time on some devices and is instantaneous on others. I guess its related to Contact Aggregation but I am not sure. Any pointers?
Sounds like an Android issue, not necessarily a Xamarin issue. Take a look at the question Getting name and email from contact list is very slow. One of the things this answer does is use a Projection in the query to get all the columns in one go.

how to get the saved words in android dictionary

I'm developing an app that can add words to UserDictionary and query from it.
when running the app on tablet device it work fine.
but when running it on non tablet device
and save an auto corrected word to my word list and query for it, it didn't retrieve the words.
so what I should to do to query and select all saved auto corrected word.
// this to add word to dictionary
Uri dic = UserDictionary.Words.CONTENT_URI;
UserDictionary.Words.addWord(this, "word", 100, UserDictionary.Words.LOCALE_TYPE_ALL);
// this to query
Uri dic = UserDictionary.Words.CONTENT_URI;
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(dic, null, null, null, null);
while (cursor.moveToNext()){
String word = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD));
int id = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID));
String app = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.APP_ID));
int frequency = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY));
String locale = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.LOCALE));
Log.i("", "word: "+word+"\nId: "+id+"\nAppID: "+app+"\nfrequency: "+frequency+"\nLocale: "+locale);
}
To add a word, the Javadoc suggests using UserDicrionary.Words.addWord.
To use the spellcheker, above Android 4.0, there's a framework, along with a few examples of its use. Hope these help.

Not able to retrieve the data from database when running it on real device

I m working on an e-learning type of an application where i retrieve the data from the database on list.
It works fine on the emulator,but when i use the APK file of that app on real device,it does not show any data on list, my database is stored in the windows-file explorer-package-data-database-table_name.
I am referring to this site
http://anujarosha.wordpress.com/2011/12/19/how-to-retrieve-data-from-a-sqlite-database-in-android/
Here's a snippet of my code using database
list_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, namelist1());
list1.setAdapter(list_adapter);
}
public List<String> namelist1()
{
// We have to return a List which contains only String values. Lets create a List first
List<String> namelist= new ArrayList<String>();
// First we need to make contact with the database we have created using the DbHelper class
Database_helper_class open_database_helper= new Database_helper_class(this);
// Then we need to get a readable database
SQLiteDatabase sqlitedatabase = open_database_helper.getReadableDatabase();
// We need a a guy to read the database query. Cursor interface will do it for us
//(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Cursor cursor =sqlitedatabase.query(open_database_helper.TABLE_E_LEARNING,null,null,null,null,null,null);
//above query is read all the database column
// Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop
while(cursor.moveToNext()){
String str_name = cursor.getString(cursor.getColumnIndex(Database_helper_class.QUES_COLUMN));
String str_id = cursor.getString(cursor.getColumnIndex(Database_helper_class.ANS_COLUMN));
//double str_gpa = cursor.getDouble(cursor.getColumnIndex(Database_helper_class.GPA_COLUMN));
// Finish reading one raw, now we have to pass them to the POJO
nameclass nameclassobj1=new nameclass();
nameclassobj1.setname(str_name);
nameclassobj1.setid(str_id);
//nameclassobj1.setgpa(str_gpa);
// Lets pass that POJO to our ArrayList which contains undergraduates as type
pojo_namelist.add(nameclassobj1);
// But we need a List of String to display in the ListView also.
//That is why we create "nameList"
namelist.add(str_name);
}
sqlitedatabase.close();
sqlitedatabase.query() returns a cursor which is positioned before the first record. make sure to call moveToFirst() before trying to access any data from it.
verify your database path in DBHelper.class. And after that Write below line before you call while loop.
cursor.moveToFirst();
This will point to your first record and then your while loop will work.
Try it like this:
// Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String str_name = cursor.getString(cursor.getColumnIndex(Database_helper_class.QUES_COLUMN));
String str_id = cursor.getString(cursor.getColumnIndex(Database_helper_class.ANS_COLUMN));
//double str_gpa = cursor.getDouble(cursor.getColumnIndex(Database_helper_class.GPA_COLUMN));
// Finish reading one raw, now we have to pass them to the POJO
nameclass nameclassobj1=new nameclass();
nameclassobj1.setname(str_name);
nameclassobj1.setid(str_id);
//nameclassobj1.setgpa(str_gpa);
// Lets pass that POJO to our ArrayList which contains undergraduates as type
pojo_namelist.add(nameclassobj1);
// But we need a List of String to display in the ListView also.
//That is why we create "nameList"
namelist.add(str_name);
}
Use the isAfterLast()-method to check, if your reached the end of your cursor.
Sorry for late reply, got busy in other application
Actually the data was not saved in the database, that is why i was not getting it on device.
It works now.

Can I extend Androids Contacts database?

I was wondering is it possible to extend the Android Contacts database?
From here - http://d.android.com/reference/android/provider/ContactsContract.html
It says:
ContactsContract defines an extensible
database of contact-related
information
Extensible would suggest to me that I can add in more data to the contacts application outside the normal values such as Name, number, email, work number, home number etc..
However the examples of this page - http://d.android.com/reference/android/provider/ContactsContract.RawContacts.html only show how to insert the standard values like name and not how to add a new field to a contact.
Furthermore a search on the web does not turn up much information on extending the contacts data.
So I was wondering is it even possible or does the extensible refer to some other part of the contacts?
For example I would like to add in an additional field for contacts that have special privileges within my app so when a user looks at the contacts he or she knows what users they can use my app with.
Is this possible?
You can store custom data in the contacts database. However "when a user looks at the contacts he or she knows what users they can use my app with," may not be possible if you are thinking users will be able to see the custom data you inserted while using the built-in Android Contacts application. You would have to display the custom data in your own application.
The javadocs for the ContactsContract.Data class should provide an explanation, as well as the Contacts article.
To use this you'll need to get a raw contact id by querying the RawContacts.
Here some example code that might help you get started...
private void makePowerful(int rawContactId) {
ContentValues values = new ContentValues();
values.put(Privilege.RAW_CONTACT_ID, rawContactId);
values.put(Privilege.MIMETYPE, Privilege.CONTENT_ITEM_TYPE);
values.put(Privilege.PRIVILEGE_LEVEL, Privilege.TYPE_POWERFUL);
Uri uri = getContentResolver().insert(Data.CONTENT_URI, values);
}
public static final class Privilege implements ContactsContract.DataColumnsWithJoins, ContactsContract.CommonDataKinds.CommonColumns {
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/my_app_privilege";
public static final int TYPE_POWERFUL = 1;
public static final int TYPE_WEAK = 2;
public static final String PRIVILEGE_LEVEL = DATA1;
private Privilege() { }
}

Categories

Resources