1 /* 2 * Copyright (C) 2018 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.dialer.contacts.hiresphoto; 18 19 import android.content.ComponentName; 20 import android.content.ContentUris; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.provider.ContactsContract.Contacts; 26 import android.provider.ContactsContract.RawContacts; 27 import android.support.annotation.VisibleForTesting; 28 import com.android.dialer.common.LogUtil; 29 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor; 30 import com.android.dialer.common.database.Selection; 31 import com.android.dialer.inject.ApplicationContext; 32 import com.google.common.util.concurrent.ListenableFuture; 33 import com.google.common.util.concurrent.ListeningExecutorService; 34 import java.util.ArrayList; 35 import java.util.List; 36 import javax.inject.Inject; 37 38 /** Use the contacts sync adapter to load high resolution photos for a Google account. */ 39 public class HighResolutionPhotoRequesterImpl implements HighResolutionPhotoRequester { 40 41 private static class RequestFailedException extends Exception { RequestFailedException(String message)42 RequestFailedException(String message) { 43 super(message); 44 } 45 RequestFailedException(String message, Throwable cause)46 RequestFailedException(String message, Throwable cause) { 47 super(message, cause); 48 } 49 } 50 51 @VisibleForTesting 52 static final ComponentName SYNC_HIGH_RESOLUTION_PHOTO_SERVICE = 53 new ComponentName( 54 "com.google.android.syncadapters.contacts", 55 "com.google.android.syncadapters.contacts.SyncHighResPhotoIntentService"); 56 57 private final Context appContext; 58 private final ListeningExecutorService backgroundExecutor; 59 60 @Inject HighResolutionPhotoRequesterImpl( @pplicationContext Context appContext, @BackgroundExecutor ListeningExecutorService backgroundExecutor)61 HighResolutionPhotoRequesterImpl( 62 @ApplicationContext Context appContext, 63 @BackgroundExecutor ListeningExecutorService backgroundExecutor) { 64 this.appContext = appContext; 65 this.backgroundExecutor = backgroundExecutor; 66 } 67 68 @Override request(Uri contactUri)69 public ListenableFuture<Void> request(Uri contactUri) { 70 return backgroundExecutor.submit( 71 () -> { 72 try { 73 requestInternal(contactUri); 74 } catch (RequestFailedException e) { 75 LogUtil.e("HighResolutionPhotoRequesterImpl.request", "request failed", e); 76 } 77 return null; 78 }); 79 } 80 81 private void requestInternal(Uri contactUri) throws RequestFailedException { 82 for (Long rawContactId : getGoogleRawContactIds(getContactId(contactUri))) { 83 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId); 84 Intent intent = new Intent(Intent.ACTION_VIEW); 85 intent.setComponent(SYNC_HIGH_RESOLUTION_PHOTO_SERVICE); 86 intent.setDataAndType(rawContactUri, RawContacts.CONTENT_ITEM_TYPE); 87 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 88 try { 89 LogUtil.i( 90 "HighResolutionPhotoRequesterImpl.requestInternal", 91 "requesting photo for " + rawContactUri); 92 appContext.startService(intent); 93 } catch (IllegalStateException | SecurityException e) { 94 throw new RequestFailedException("unable to start sync adapter", e); 95 } 96 } 97 } 98 99 private long getContactId(Uri contactUri) throws RequestFailedException { 100 try (Cursor cursor = 101 appContext 102 .getContentResolver() 103 .query(contactUri, new String[] {Contacts._ID}, null, null, null)) { 104 if (cursor == null || !cursor.moveToFirst()) { 105 throw new RequestFailedException("cannot get contact ID"); 106 } 107 return cursor.getLong(0); 108 } 109 } 110 111 private List<Long> getGoogleRawContactIds(long contactId) throws RequestFailedException { 112 List<Long> result = new ArrayList<>(); 113 Selection selection = 114 Selection.column(RawContacts.CONTACT_ID) 115 .is("=", contactId) 116 .buildUpon() 117 .and(Selection.column(RawContacts.ACCOUNT_TYPE).is("=", "com.google")) 118 .build(); 119 try (Cursor cursor = 120 appContext 121 .getContentResolver() 122 .query( 123 RawContacts.CONTENT_URI, 124 new String[] {RawContacts._ID, RawContacts.ACCOUNT_TYPE}, 125 selection.getSelection(), 126 selection.getSelectionArgs(), 127 null)) { 128 if (cursor == null) { 129 throw new RequestFailedException("null cursor from raw contact IDs"); 130 } 131 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { 132 result.add(cursor.getLong(0)); 133 } 134 } 135 return result; 136 } 137 } 138