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.glidephotomanager.impl; 18 19 import android.net.Uri; 20 import android.provider.ContactsContract; 21 import android.provider.ContactsContract.CommonDataKinds; 22 import android.provider.ContactsContract.Contacts; 23 import android.provider.ContactsContract.DisplayNameSources; 24 import com.android.dialer.glidephotomanager.PhotoInfo; 25 import org.json.JSONArray; 26 import org.json.JSONException; 27 import org.json.JSONObject; 28 29 /** 30 * Generate a lookup URI that will populate the quick contact with the number. Used when the lookup 31 * URI is not available (non-contact and no other sources). The info is encoded into a JSON string 32 * that is not governed by any public interface. URI format: 33 * content://com.android.contacts/contacts/lookup/encoded/[JSON] 34 * 35 * <p>The JSON is a object containing "display_name", "display_name_source" ({@link 36 * DisplayNameSources}), and several {@link ContactsContract.Data} rows keyed by the {@link 37 * ContactsContract.Data#MIMETYPE}. In this case only {@link 38 * ContactsContract.CommonDataKinds.Phone#CONTENT_ITEM_TYPE} is available. 39 * 40 * <p>Example JSON:<br> 41 * {"display_name":"+1 650-253-0000","display_name_source":30,"vnd.android.cursor.item\/contact":{ 42 * "vnd.android.cursor.item\/phone_v2":[{"data1":"+1 650-253-0000","data2":12}]}} 43 */ 44 final class DefaultLookupUriGenerator { 45 generateUri(PhotoInfo photoInfo)46 static Uri generateUri(PhotoInfo photoInfo) { 47 JSONObject lookupJson = new JSONObject(); 48 try { 49 lookupJson.put(Contacts.DISPLAY_NAME, photoInfo.getFormattedNumber()); 50 // DISPLAY_NAME_SOURCE required by contacts, otherwise the URI will not be recognized. 51 lookupJson.put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.PHONE); 52 JSONObject contactRows = new JSONObject(); 53 JSONObject phone = new JSONObject(); 54 phone.put(CommonDataKinds.Phone.NUMBER, photoInfo.getFormattedNumber()); 55 contactRows.put(CommonDataKinds.Phone.CONTENT_ITEM_TYPE, new JSONArray().put(phone)); 56 57 lookupJson.put(Contacts.CONTENT_ITEM_TYPE, contactRows); 58 } catch (JSONException e) { 59 throw new AssertionError(e); 60 } 61 return Contacts.CONTENT_LOOKUP_URI 62 .buildUpon() 63 .appendPath("encoded") 64 .encodedFragment(lookupJson.toString()) 65 // Directory is required in the URI but it does not exist, use MAX_VALUE to avoid clashing 66 // with other directory 67 .appendQueryParameter( 68 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Integer.MAX_VALUE)) 69 .build(); 70 } 71 } 72