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