diff options
author | Andrea Fagiani <andfagiani@gmail.com> | 2014-09-06 19:09:57 +0200 |
---|---|---|
committer | Ethan Chen <intervigil@gmail.com> | 2014-10-02 05:04:53 +0000 |
commit | 2491d19f178f2b78a00cce00bce5dc8ec0d60231 (patch) | |
tree | 5cae7e96bae8cd90630f57ae2711f839911c9011 | |
parent | c10bafdd9bab3af4e9dca4fae749fd0ab2202acd (diff) | |
download | android_packages_apps_Dialer-2491d19f178f2b78a00cce00bce5dc8ec0d60231.tar.gz android_packages_apps_Dialer-2491d19f178f2b78a00cce00bce5dc8ec0d60231.tar.bz2 android_packages_apps_Dialer-2491d19f178f2b78a00cce00bce5dc8ec0d60231.zip |
Add reverse lookup provider "PagineBianche" (IT).
Change-Id: I4db5cbc6bebe847572e872dff93500a0dd451611
4 files changed, 245 insertions, 0 deletions
diff --git a/src/com/android/dialer/lookup/LookupSettings.java b/src/com/android/dialer/lookup/LookupSettings.java index 3d969a32d..b52bff7ef 100644 --- a/src/com/android/dialer/lookup/LookupSettings.java +++ b/src/com/android/dialer/lookup/LookupSettings.java @@ -45,6 +45,7 @@ public final class LookupSettings { public static final String RLP_ZABASEARCH = "ZabaSearch"; public static final String RLP_CYNGN_CHINESE = "CyngnChinese"; public static final String RLP_DASTELEFONBUCH = "DasTelefonbuch"; + public static final String RLP_PAGINEBIANCHE = "PagineBianche"; public static final String RLP_DEFAULT = RLP_OPENCNAM; private LookupSettings() { diff --git a/src/com/android/dialer/lookup/ReverseLookup.java b/src/com/android/dialer/lookup/ReverseLookup.java index 7009f93c7..e0fe1e98c 100644 --- a/src/com/android/dialer/lookup/ReverseLookup.java +++ b/src/com/android/dialer/lookup/ReverseLookup.java @@ -19,6 +19,7 @@ package com.android.dialer.lookup; import com.android.dialer.calllog.ContactInfo; import com.android.dialer.lookup.cyngn.CyngnChineseReverseLookup; import com.android.dialer.lookup.dastelefonbuch.TelefonbuchReverseLookup; +import com.android.dialer.lookup.paginebianche.PagineBiancheReverseLookup; import com.android.dialer.lookup.opencnam.OpenCnamReverseLookup; import com.android.dialer.lookup.whitepages.WhitePagesReverseLookup; import com.android.dialer.lookup.yellowpages.YellowPagesReverseLookup; @@ -55,6 +56,8 @@ public abstract class ReverseLookup { INSTANCE = new CyngnChineseReverseLookup(context); } else if (provider.equals(LookupSettings.RLP_DASTELEFONBUCH)) { INSTANCE = new TelefonbuchReverseLookup(context); + } else if (provider.equals(LookupSettings.RLP_PAGINEBIANCHE)) { + INSTANCE = new PagineBiancheReverseLookup(context); } } @@ -82,6 +85,9 @@ public abstract class ReverseLookup { } else if (provider.equals(LookupSettings.RLP_DASTELEFONBUCH) && INSTANCE instanceof TelefonbuchReverseLookup) { return true; + } else if (provider.equals(LookupSettings.RLP_PAGINEBIANCHE) + && INSTANCE instanceof PagineBiancheReverseLookup) { + return true; } else { return false; } diff --git a/src/com/android/dialer/lookup/paginebianche/PagineBiancheApi.java b/src/com/android/dialer/lookup/paginebianche/PagineBiancheApi.java new file mode 100644 index 000000000..4bc93a2d8 --- /dev/null +++ b/src/com/android/dialer/lookup/paginebianche/PagineBiancheApi.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2014 The CyanogenMod 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.lookup.paginebianche; + +import android.content.Context; +import android.net.Uri; +import android.text.Html; + +import com.android.dialer.lookup.LookupSettings; + +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class PagineBiancheApi { + private static final String TAG = PagineBiancheApi.class.getSimpleName(); + + private static final String REVERSE_LOOKUP_URL = + "http://www.paginebianche.it/ricerca-da-numero?"; + + private PagineBiancheApi() { + } + + public static ContactInfo reverseLookup(Context context, String number) + throws IOException { + Uri uri = Uri.parse(REVERSE_LOOKUP_URL) + .buildUpon() + .appendQueryParameter("qs", number) + .build(); + + String output = httpGet(uri.toString()); + if (output == null) { + return null; + } + + // Cut out everything we're not interested in (scripts etc.) to + // speed up the subsequent matching. + Pattern regex = Pattern.compile("class=\"org fn\"(.*?)class=\"link_social", Pattern.DOTALL); + Matcher matcher = regex.matcher(output); + if (!matcher.find()) { + return null; + } + + output = matcher.group(1); + + String name = parseName(output); + if (name == null) { + return null; + } + + String phoneNumber = parseNumber(output); + String address = parseAddress(output); + + ContactInfo info = new ContactInfo(); + info.name = name; + info.address = address; + info.formattedNumber = phoneNumber != null ? phoneNumber : number; + info.website = uri.toString(); + + return info; + } + + private static String httpGet(String url) throws IOException { + HttpClient client = new DefaultHttpClient(); + HttpGet get = new HttpGet(url); + + HttpResponse response = client.execute(get); + int status = response.getStatusLine().getStatusCode(); + + // Android's org.apache.http doesn't have the RedirectStrategy class + if (status == HttpStatus.SC_MOVED_PERMANENTLY + || status == HttpStatus.SC_MOVED_TEMPORARILY) { + Header[] headers = response.getHeaders("Location"); + + if (headers != null && headers.length != 0) { + String newUrl = headers[headers.length - 1].getValue(); + return httpGet(newUrl); + } else { + return null; + } + } + + if (status != HttpStatus.SC_OK) { + return null; + } + + return EntityUtils.toString(response.getEntity()); + } + + private static String parseName(String output) { + Pattern regex = Pattern.compile("\\stitle=\"(.*?)\""); + Matcher m = regex.matcher(output); + + if (m.find()) { + return m.group(1); + } + + return null; + } + + private static String parseNumber(String output) { + Pattern regex = Pattern.compile("class=\"tel\".*?class=\"value\">(.*?)<"); + Matcher m = regex.matcher(output); + if (m.find()) { + return m.group(1); + } + + return null; + } + + private static String parseAddress(String output) { + Pattern regex = Pattern.compile("class=\"street-address\">(.*?)</div>"); + Matcher m = regex.matcher(output); + if (m.find()) { + return m.group(1).replaceAll("</?span.*?>", ""); + } + + return null; + } + + public static class ContactInfo { + String name; + String address; + String formattedNumber; + String website; + } +} diff --git a/src/com/android/dialer/lookup/paginebianche/PagineBiancheReverseLookup.java b/src/com/android/dialer/lookup/paginebianche/PagineBiancheReverseLookup.java new file mode 100644 index 000000000..aa8191a3a --- /dev/null +++ b/src/com/android/dialer/lookup/paginebianche/PagineBiancheReverseLookup.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2014 The CyanogenMod 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.lookup.paginebianche; + +import android.content.Context; +import android.net.Uri; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; +import android.provider.ContactsContract.CommonDataKinds.Website; +import android.util.Pair; + +import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.ContactBuilder; +import com.android.dialer.lookup.ReverseLookup; + +import java.io.IOException; + +public class PagineBiancheReverseLookup extends ReverseLookup { + private static final String TAG = PagineBiancheReverseLookup.class.getSimpleName(); + + public PagineBiancheReverseLookup(Context context) { + } + + /** + * Perform phone number lookup. + * + * @param context The application context + * @param normalizedNumber The normalized phone number + * @param formattedNumber The formatted phone number + * @return The phone number info object + */ + public Pair<ContactInfo, Object> lookupNumber(Context context, + String normalizedNumber, String formattedNumber) { + PagineBiancheApi.ContactInfo info = null; + + if (normalizedNumber.startsWith("+") && !normalizedNumber.startsWith("+39")) { + // PagineBianche only supports Italian numbers + return null; + } + + try { + info = PagineBiancheApi.reverseLookup(context, normalizedNumber.replace("+39","")); + } catch (IOException e) { + return null; + } + + if (info == null) { + return null; + } + + ContactBuilder builder = new ContactBuilder( + ContactBuilder.REVERSE_LOOKUP, + normalizedNumber, formattedNumber); + + ContactBuilder.Name n = new ContactBuilder.Name(); + n.displayName = info.name; + builder.setName(n); + + ContactBuilder.PhoneNumber pn = new ContactBuilder.PhoneNumber(); + pn.number = info.formattedNumber; + pn.type = Phone.TYPE_MAIN; + builder.addPhoneNumber(pn); + + if (info.address != null) { + ContactBuilder.Address a = new ContactBuilder.Address(); + a.formattedAddress = info.address; + a.type = StructuredPostal.TYPE_HOME; + builder.addAddress(a); + } + + // No website information because PagineBianche does not provide any + + return Pair.create(builder.build(), null); + } +} |