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.ContentProviderClient; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.Intent.ShortcutIconResource; 25 import android.database.Cursor; 26 import android.net.Uri; 27 import android.provider.ContactsContract; 28 import android.provider.ContactsContract.Data; 29 import android.provider.ContactsContract.RawContacts; 30 import android.provider.ContactsContract.CommonDataKinds.Photo; 31 import android.text.TextUtils; 32 import android.util.Log; 33 34 import com.android.tv.settings.R; 35 36 /** 37 * Utility functions for retrieving account pictures. 38 */ 39 public final class AccountImageHelper { 40 41 static final String[] CONTACT_PROJECTION_DATA = new String[] { 42 ContactsContract.Data._ID, 43 ContactsContract.Data.CONTACT_ID, 44 ContactsContract.Data.RAW_CONTACT_ID, 45 ContactsContract.Data.LOOKUP_KEY, 46 ContactsContract.Data.PHOTO_URI, 47 ContactsContract.Data.PHOTO_FILE_ID 48 }; 49 static final String CONTACT_SELECTION = 50 ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE ?"; 51 52 /** 53 * Non instantiable. 54 */ 55 private AccountImageHelper() { 56 } 57 58 /** 59 * Tries to retrieve the Picture for the provided account, from the Contacts database. 60 */ 61 public static String getAccountPictureUri(Context context, Account account) { 62 // Look up this account in the contacts database. 63 64 String[] selectionArgs = new String[] { 65 account.name }; 66 Cursor c = null; 67 long contactId = -1; 68 String lookupKey = null; 69 String photoUri = null; 70 int photoFileId = 0; 71 long rawContactId = 0; 72 try { 73 c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 74 CONTACT_PROJECTION_DATA, CONTACT_SELECTION, selectionArgs, null); 75 if (c.moveToNext()) { 76 contactId = c.getLong(1); 77 rawContactId = c.getLong(2); 78 lookupKey = c.getString(3); 79 photoUri = c.getString(4); 80 photoFileId = c.getInt(5); 81 } 82 } finally { 83 if (c != null) { 84 c.close(); 85 } 86 } 87 88 if (contactId != -1 && !TextUtils.isEmpty(lookupKey) && !TextUtils.isEmpty(photoUri)) { 89 if (photoFileId == 0) { 90 // Trigger a VIEW action on this photo, which will force the Contacts 91 // Sync adapter to sync the HiRes version of the contact photo. 92 syncContactHiResPhoto(context, rawContactId); 93 } 94 return photoUri; 95 } 96 return getDefaultPictureUri(context); 97 } 98 99 private static void syncContactHiResPhoto(Context context, long rawContactId) { 100 final String serviceName = "com.google.android.syncadapters.contacts." + 101 "SyncHighResPhotoIntentService"; 102 final String servicePackageName = "com.google.android.syncadapters.contacts"; 103 final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, 104 rawContactId); 105 final Intent intent = new Intent(); 106 intent.setClassName(servicePackageName, serviceName); 107 intent.setAction(Intent.ACTION_VIEW); 108 intent.setDataAndType(uri, RawContacts.CONTENT_ITEM_TYPE); 109 try { 110 context.startService(intent); 111 } catch (Exception e) { 112 113 } 114 } 115 116 /** 117 * Returns a default image to be used when an account has no picture associated with it. 118 */ 119 public static String getDefaultPictureUri(Context context) { 120 // TODO: get a better default image. 121 ShortcutIconResource iconResource = new ShortcutIconResource(); 122 iconResource.packageName = context.getPackageName(); 123 iconResource.resourceName = context.getResources().getResourceName( 124 R.drawable.default_contact_picture); 125 return UriUtils.getShortcutIconResourceUri(iconResource).toString(); 126 } 127 } 128