1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.util; 18 19 import android.accounts.Account; 20 import android.content.ContentUris; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.Intent.ShortcutIconResource; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.provider.ContactsContract; 27 import android.provider.ContactsContract.RawContacts; 28 import android.text.TextUtils; 29 30 import com.android.tv.settings.R; 31 32 /** 33 * Utility functions for retrieving account pictures. 34 */ 35 public final class AccountImageHelper { 36 37 static final String[] CONTACT_PROJECTION_DATA = new String[] { 38 ContactsContract.Data._ID, 39 ContactsContract.Data.CONTACT_ID, 40 ContactsContract.Data.RAW_CONTACT_ID, 41 ContactsContract.Data.LOOKUP_KEY, 42 ContactsContract.Data.PHOTO_URI, 43 ContactsContract.Data.PHOTO_FILE_ID 44 }; 45 static final String CONTACT_SELECTION = 46 ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE ?"; 47 48 /** 49 * Non instantiable. 50 */ 51 private AccountImageHelper() { 52 } 53 54 /** 55 * Tries to retrieve the Picture for the provided account, from the Contacts database. 56 */ 57 public static String getAccountPictureUri(Context context, Account account) { 58 // Look up this account in the contacts database. 59 60 String[] selectionArgs = new String[] { 61 account.name }; 62 Cursor c = null; 63 long contactId = -1; 64 String lookupKey = null; 65 String photoUri = null; 66 int photoFileId = 0; 67 long rawContactId = 0; 68 try { 69 c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 70 CONTACT_PROJECTION_DATA, CONTACT_SELECTION, selectionArgs, null); 71 if (c.moveToNext()) { 72 contactId = c.getLong(1); 73 rawContactId = c.getLong(2); 74 lookupKey = c.getString(3); 75 photoUri = c.getString(4); 76 photoFileId = c.getInt(5); 77 } 78 } finally { 79 if (c != null) { 80 c.close(); 81 } 82 } 83 84 if (contactId != -1 && !TextUtils.isEmpty(lookupKey) && !TextUtils.isEmpty(photoUri)) { 85 if (photoFileId == 0) { 86 // Trigger a VIEW action on this photo, which will force the Contacts 87 // Sync adapter to sync the HiRes version of the contact photo. 88 syncContactHiResPhoto(context, rawContactId); 89 } 90 return photoUri; 91 } 92 return getDefaultPictureUri(context); 93 } 94 95 private static void syncContactHiResPhoto(Context context, long rawContactId) { 96 final String serviceName = "com.google.android.syncadapters.contacts." + 97 "SyncHighResPhotoIntentService"; 98 final String servicePackageName = "com.google.android.syncadapters.contacts"; 99 final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, 100 rawContactId); 101 final Intent intent = new Intent(); 102 intent.setClassName(servicePackageName, serviceName); 103 intent.setAction(Intent.ACTION_VIEW); 104 intent.setDataAndType(uri, RawContacts.CONTENT_ITEM_TYPE); 105 try { 106 context.startService(intent); 107 } catch (Exception e) { 108 109 } 110 } 111 112 /** 113 * Returns a default image to be used when an account has no picture associated with it. 114 */ 115 public static String getDefaultPictureUri(Context context) { 116 // TODO: get a better default image. 117 ShortcutIconResource iconResource = new ShortcutIconResource(); 118 iconResource.packageName = context.getPackageName(); 119 iconResource.resourceName = context.getResources().getResourceName( 120 R.drawable.default_contact_picture); 121 return UriUtils.getShortcutIconResourceUri(iconResource).toString(); 122 } 123 } 124