summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/lookup/openstreetmap/OpenStreetMapForwardLookup.java
blob: 4482e6043de6f066b9a8fc6b87b089d01a5ae3b1 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2014 The OmniROM Project
 * Copyright (C) 2014 Xiao-Long Chen <chillermillerlong@hotmail.com>
 *
 * 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.
 */

// Partially based on OmniROM's implementation

package com.android.dialer.lookup.openstreetmap;

import android.content.Context;
import android.location.Location;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.util.Log;

import com.android.dialer.phonenumbercache.ContactInfo;
import com.android.dialer.lookup.ContactBuilder;
import com.android.dialer.lookup.ForwardLookup;
import com.android.dialer.lookup.LookupUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class OpenStreetMapForwardLookup extends ForwardLookup {
  private static final String TAG = OpenStreetMapForwardLookup.class.getSimpleName();

  /** Search within radius (meters) */
  private static final int RADIUS = 30000;

  /** Query URL */
  private static final String LOOKUP_URL = "https://overpass-api.de/api/interpreter";
  private static final String LOOKUP_QUERY =
      "[out:json];node[name~\"%s\"][phone](around:%d,%f,%f);out body;";

  private static final String RESULT_ELEMENTS = "elements";
  private static final String RESULT_TAGS = "tags";
  private static final String TAG_NAME = "name";
  private static final String TAG_PHONE = "phone";
  private static final String TAG_HOUSENUMBER = "addr:housenumber";
  private static final String TAG_STREET = "addr:street";
  private static final String TAG_CITY = "addr:city";
  private static final String TAG_POSTCODE = "addr:postcode";
  private static final String TAG_WEBSITE = "website";

  public OpenStreetMapForwardLookup(Context context) {
  }

  @Override
  public List<ContactInfo> lookup(Context context, String filter, Location lastLocation) {
    // The OSM API doesn't support case-insentive searches, but does
    // support regular expressions.
    String regex = "";
    for (int i = 0; i < filter.length(); i++) {
      char c = filter.charAt(i);
      regex += "[" + Character.toUpperCase(c) + Character.toLowerCase(c) + "]";
    }

    String request = String.format(Locale.ENGLISH, LOOKUP_QUERY, regex,
        RADIUS, lastLocation.getLatitude(), lastLocation.getLongitude());

    try {
      return getEntries(new JSONObject(LookupUtils.httpPost(LOOKUP_URL, null, request)));
    } catch (IOException e) {
      Log.e(TAG, "Failed to execute query", e);
    } catch (JSONException e) {
      Log.e(TAG, "JSON error", e);
    }

    return null;
  }

  private List<ContactInfo> getEntries(JSONObject results) throws JSONException {
    ArrayList<ContactInfo> details = new ArrayList<>();
    JSONArray elements = results.getJSONArray(RESULT_ELEMENTS);

    for (int i = 0; i < elements.length(); i++) {
      try {
        JSONObject element = elements.getJSONObject(i);
        JSONObject tags = element.getJSONObject(RESULT_TAGS);

        String displayName = tags.getString(TAG_NAME);
        String phoneNumber = tags.getString(TAG_PHONE);

        // Take the first number if there are multiple
        if (phoneNumber.contains(";")) {
          phoneNumber = phoneNumber.split(";")[0];
          phoneNumber = phoneNumber.trim();
        }

        // The address is split
        String addressHouseNumber = tags.optString(TAG_HOUSENUMBER, null);
        String addressStreet = tags.optString(TAG_STREET, null);
        String addressCity = tags.optString(TAG_CITY, null);
        String addressPostCode = tags.optString(TAG_POSTCODE, null);

        String address = String.format("%s %s, %s %s",
            addressHouseNumber != null ? addressHouseNumber : "",
            addressStreet != null ? addressStreet : "",
            addressCity != null ? addressCity : "",
            addressPostCode != null ? addressPostCode : "");

        address = address.trim().replaceAll("\\s+", " ");
        if (address.isEmpty()) {
            address = null;
        }

        ContactBuilder builder = ContactBuilder.forForwardLookup(phoneNumber)
            .setName(ContactBuilder.Name.createDisplayName(displayName))
            .addPhoneNumber(ContactBuilder.PhoneNumber.createMainNumber(phoneNumber))
            .setPhotoUri(ContactBuilder.PHOTO_URI_BUSINESS);

        if (address != null) {
            ContactBuilder.Address a = new ContactBuilder.Address();
            a.formattedAddress = address;
            a.city = addressCity;
            a.street = addressStreet;
            a.postCode = addressPostCode;
            a.type = StructuredPostal.TYPE_WORK;
            builder.addAddress(a);
        }

        String website = tags.optString(TAG_WEBSITE, null);
        if (website != null) {
            ContactBuilder.WebsiteUrl w = new ContactBuilder.WebsiteUrl();
            w.url = website;
            w.type = Website.TYPE_HOMEPAGE;
            builder.addWebsite(w);
        }

        details.add(builder.build());
      } catch (JSONException e) {
        Log.e(TAG, "Skipping the suggestions at index " + i, e);
      }
    }

    return details;
  }
}