summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/glidephotomanager/impl/DefaultLookupUriGenerator.java
blob: 6b90e803c663a5a2d6da0ae0d39640312dd8bd7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.glidephotomanager.impl;

import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.DisplayNameSources;
import com.android.dialer.glidephotomanager.PhotoInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Generate a lookup URI that will populate the quick contact with the number. Used when the lookup
 * URI is not available (non-contact and no other sources). The info is encoded into a JSON string
 * that is not governed by any public interface. URI format:
 * content://com.android.contacts/contacts/lookup/encoded/[JSON]
 *
 * <p>The JSON is a object containing "display_name", "display_name_source" ({@link
 * DisplayNameSources}), and several {@link ContactsContract.Data} rows keyed by the {@link
 * ContactsContract.Data#MIMETYPE}. In this case only {@link
 * ContactsContract.CommonDataKinds.Phone#CONTENT_ITEM_TYPE} is available.
 *
 * <p>Example JSON:<br>
 * {"display_name":"+1 650-253-0000","display_name_source":30,"vnd.android.cursor.item\/contact":{
 * "vnd.android.cursor.item\/phone_v2":[{"data1":"+1 650-253-0000","data2":12}]}}
 */
final class DefaultLookupUriGenerator {

  static Uri generateUri(PhotoInfo photoInfo) {
    JSONObject lookupJson = new JSONObject();
    try {
      lookupJson.put(Contacts.DISPLAY_NAME, photoInfo.getFormattedNumber());
      // DISPLAY_NAME_SOURCE required by contacts, otherwise the URI will not be recognized.
      lookupJson.put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME);
      JSONObject contactRows = new JSONObject();
      JSONObject phone = new JSONObject();
      phone.put(CommonDataKinds.Phone.NUMBER, photoInfo.getFormattedNumber());
      contactRows.put(CommonDataKinds.Phone.CONTENT_ITEM_TYPE, new JSONArray().put(phone));

      lookupJson.put(Contacts.CONTENT_ITEM_TYPE, contactRows);
    } catch (JSONException e) {
      throw new AssertionError(e);
    }
    return Contacts.CONTENT_LOOKUP_URI
        .buildUpon()
        .appendPath("encoded")
        .encodedFragment(lookupJson.toString())
        // Directory is required in the URI but it does not exist, use MAX_VALUE to avoid clashing
        // with other directory
        .appendQueryParameter(
            ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Integer.MAX_VALUE))
        .build();
  }
}