1 /* 2 * Copyright (C) 2020 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.messaging.ui.mediapicker; 18 19 import android.Manifest; 20 import android.app.Activity; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.provider.ContactsContract.Contacts; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import com.android.messaging.R; 31 import com.android.messaging.datamodel.data.PendingAttachmentData; 32 import com.android.messaging.ui.UIIntents; 33 import com.android.messaging.util.ContactUtil; 34 import com.android.messaging.util.ContentType; 35 import com.android.messaging.util.SafeAsyncTask; 36 37 /** 38 * Chooser which allows the user to select an existing contact from contacts apps on this device. 39 * Note that this chooser requires the Manifest.permission.READ_CONTACTS which is one of the miminum 40 * set of permissions for this app. Thus no case to request READ_CONTACTS permission on it actually. 41 */ 42 class ContactMediaChooser extends MediaChooser { 43 private View mEnabledView; 44 private View mMissingPermissionView; 45 ContactMediaChooser(final MediaPicker mediaPicker)46 ContactMediaChooser(final MediaPicker mediaPicker) { 47 super(mediaPicker); 48 } 49 50 @Override getSupportedMediaTypes()51 public int getSupportedMediaTypes() { 52 return MediaPicker.MEDIA_TYPE_VCARD; 53 } 54 55 @Override getIconResource()56 public int getIconResource() { 57 return R.drawable.ic_person_light; 58 } 59 60 @Override getIconDescriptionResource()61 public int getIconDescriptionResource() { 62 return R.string.mediapicker_contactChooserDescription; 63 } 64 65 @Override getActionBarTitleResId()66 int getActionBarTitleResId() { 67 return R.string.mediapicker_contact_title; 68 } 69 70 @Override createView(final ViewGroup container)71 protected View createView(final ViewGroup container) { 72 final LayoutInflater inflater = getLayoutInflater(); 73 final View view = 74 inflater.inflate( 75 R.layout.mediapicker_contact_chooser, 76 container /* root */, 77 false /* attachToRoot */); 78 mEnabledView = view.findViewById(R.id.mediapicker_enabled); 79 mMissingPermissionView = view.findViewById(R.id.missing_permission_view); 80 mEnabledView.setOnClickListener( 81 new View.OnClickListener() { 82 @Override 83 public void onClick(final View v) { 84 // Launch an external picker to pick a contact as attachment. 85 UIIntents.get().launchContactCardPicker(mMediaPicker); 86 } 87 }); 88 return view; 89 } 90 91 @Override setSelected(final boolean selected)92 protected void setSelected(final boolean selected) { 93 super.setSelected(selected); 94 if (selected && !ContactUtil.hasReadContactsPermission()) { 95 mMediaPicker.requestPermissions( 96 new String[] {Manifest.permission.READ_CONTACTS}, 97 MediaPicker.READ_CONTACT_PERMISSION_REQUEST_CODE); 98 } 99 } 100 101 @Override onRequestPermissionsResult( final int requestCode, final String permissions[], final int[] grantResults)102 protected void onRequestPermissionsResult( 103 final int requestCode, final String permissions[], final int[] grantResults) { 104 if (requestCode == MediaPicker.READ_CONTACT_PERMISSION_REQUEST_CODE) { 105 final boolean permissionGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED; 106 mEnabledView.setVisibility(permissionGranted ? View.VISIBLE : View.GONE); 107 mMissingPermissionView.setVisibility(permissionGranted ? View.GONE : View.VISIBLE); 108 } 109 } 110 111 @Override onActivityResult(int requestCode, int resultCode, Intent data)112 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 113 if (requestCode == UIIntents.REQUEST_PICK_CONTACT_CARD 114 && resultCode == Activity.RESULT_OK) { 115 Uri contactUri = data.getData(); 116 if (contactUri != null) { 117 String lookupKey = null; 118 try (final Cursor c = getContext().getContentResolver().query( 119 contactUri, 120 new String[] {Contacts.LOOKUP_KEY}, 121 null, 122 null, 123 null)) { 124 if (c != null) { 125 c.moveToFirst(); 126 lookupKey = c.getString(0); 127 } 128 } 129 final Uri vCardUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey); 130 if (vCardUri != null) { 131 SafeAsyncTask.executeOnThreadPool(new Runnable() { 132 @Override 133 public void run() { 134 final PendingAttachmentData pendingItem = 135 PendingAttachmentData.createPendingAttachmentData( 136 ContentType.TEXT_VCARD.toLowerCase(), vCardUri); 137 mMediaPicker.dispatchPendingItemAdded(pendingItem); 138 } 139 }); 140 } 141 } 142 } 143 } 144 } 145