diff options
author | Danny Baumann <dannybaumann@web.de> | 2014-09-01 07:29:05 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2014-09-01 07:29:05 +0000 |
commit | 1a130e5da140faab5645a053cec8788aeff15c23 (patch) | |
tree | 49a044eb7f4eaec3d1bb80ea9b8fb9e3062d8802 | |
parent | 70f613ae56ceb40211beb2da2f2bc7f65b2a4667 (diff) | |
parent | cff06b01e0a4c4487d69be7f21de1ddb7f663f38 (diff) | |
download | android_packages_apps_Dialer-1a130e5da140faab5645a053cec8788aeff15c23.tar.gz android_packages_apps_Dialer-1a130e5da140faab5645a053cec8788aeff15c23.tar.bz2 android_packages_apps_Dialer-1a130e5da140faab5645a053cec8788aeff15c23.zip |
Merge "Add reverse lookup provider "Das Telefonbuch" (DE)." into cm-11.0
4 files changed, 264 insertions, 0 deletions
diff --git a/src/com/android/dialer/lookup/LookupSettings.java b/src/com/android/dialer/lookup/LookupSettings.java index aa8332810..3d969a32d 100644 --- a/src/com/android/dialer/lookup/LookupSettings.java +++ b/src/com/android/dialer/lookup/LookupSettings.java @@ -44,6 +44,7 @@ public final class LookupSettings { public static final String RLP_YELLOWPAGES_CA = "YellowPages_CA"; 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_DEFAULT = RLP_OPENCNAM; private LookupSettings() { diff --git a/src/com/android/dialer/lookup/ReverseLookup.java b/src/com/android/dialer/lookup/ReverseLookup.java index c35e07593..7009f93c7 100644 --- a/src/com/android/dialer/lookup/ReverseLookup.java +++ b/src/com/android/dialer/lookup/ReverseLookup.java @@ -18,6 +18,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.opencnam.OpenCnamReverseLookup; import com.android.dialer.lookup.whitepages.WhitePagesReverseLookup; import com.android.dialer.lookup.yellowpages.YellowPagesReverseLookup; @@ -52,6 +53,8 @@ public abstract class ReverseLookup { INSTANCE = new ZabaSearchReverseLookup(context); } else if (provider.equals(LookupSettings.RLP_CYNGN_CHINESE)) { INSTANCE = new CyngnChineseReverseLookup(context); + } else if (provider.equals(LookupSettings.RLP_DASTELEFONBUCH)) { + INSTANCE = new TelefonbuchReverseLookup(context); } } @@ -76,6 +79,9 @@ public abstract class ReverseLookup { } else if (provider.equals(LookupSettings.RLP_CYNGN_CHINESE) && INSTANCE instanceof CyngnChineseReverseLookup) { return true; + } else if (provider.equals(LookupSettings.RLP_DASTELEFONBUCH) + && INSTANCE instanceof TelefonbuchReverseLookup) { + return true; } else { return false; } diff --git a/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchApi.java b/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchApi.java new file mode 100644 index 000000000..4a6a6c41f --- /dev/null +++ b/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchApi.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2014 Danny Baumann <dannybaumann@web.de> + * + * 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.dastelefonbuch; + +import android.content.Context; +import android.net.Uri; + +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 TelefonbuchApi { + private static final String TAG = TelefonbuchApi.class.getSimpleName(); + + private static final String REVERSE_LOOKUP_URL = + "http://www.dastelefonbuch.de/?s=a20000" + + "&cmd=search&sort_ok=0&sp=55&vert_ok=0&aktion=23"; + + private static final String USER_AGENT = + "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0"; + + private TelefonbuchApi() { + } + + public static ContactInfo reverseLookup(Context context, String number) + throws IOException { + Uri uri = Uri.parse(REVERSE_LOOKUP_URL) + .buildUpon() + .appendQueryParameter("kw", number) + .build(); + String output = httpGet(uri.toString()); + + 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); + + get.setHeader("User-Agent", USER_AGENT); + + 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("<a id=\"name0.*?>\\s*\n?(.*?)\n?\\s*</a>", + Pattern.DOTALL); + Matcher m = regex.matcher(output); + + if (m.find()) { + return m.group(1).trim().replaceAll("&", "&"); + } + + return null; + } + + private static String parseNumber(String output) { + Pattern regex = Pattern.compile("<span\\s+class=\"ico fon.*?>.*<span>(.*?)</span>", + Pattern.DOTALL); + Matcher m = regex.matcher(output); + if (m.find()) { + return m.group(1).trim(); + } + + return null; + } + + private static String parseAddress(String output) { + String regexBase = "<span\\s+itemprop=\"%s\"\\s?>(.*?)</span>"; + + Pattern regexAddress = Pattern.compile( + String.format(regexBase, "street-address"), Pattern.DOTALL); + Matcher addressMatcher = regexAddress.matcher(output); + if (!addressMatcher.find()) { + return null; + } + + Pattern regexPostal = Pattern.compile( + String.format(regexBase, "postal-code"), Pattern.DOTALL); + Matcher postalMatcher = regexPostal.matcher(output); + if (!postalMatcher.find()) { + return null; + } + + Pattern regexLocation = Pattern.compile( + String.format(regexBase, "locality"), Pattern.DOTALL); + Matcher locationMatcher = regexLocation.matcher(output); + if (!locationMatcher.find()) { + return null; + } + + StringBuilder sb = new StringBuilder(); + sb.append(addressMatcher.group(1).trim()).append(", "); + sb.append(postalMatcher.group(1).trim()).append(" "); + sb.append(locationMatcher.group(1).trim()); + + return sb.toString(); + } + + public static class ContactInfo { + String name; + String city; + String address; + String formattedNumber; + String website; + } +} diff --git a/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchReverseLookup.java b/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchReverseLookup.java new file mode 100644 index 000000000..18c24e163 --- /dev/null +++ b/src/com/android/dialer/lookup/dastelefonbuch/TelefonbuchReverseLookup.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2014 Danny Baumann <dannybaumann@web.de> + * + * 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.dastelefonbuch; + +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 TelefonbuchReverseLookup extends ReverseLookup { + private static final String TAG = TelefonbuchReverseLookup.class.getSimpleName(); + + public TelefonbuchReverseLookup(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) { + TelefonbuchApi.ContactInfo info = null; + + if (normalizedNumber.startsWith("+") && !normalizedNumber.startsWith("+49")) { + // Das Telefonbuch only supports German numbers + return null; + } + + try { + info = TelefonbuchApi.reverseLookup(context, normalizedNumber); + } 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); + } + + ContactBuilder.WebsiteUrl w = new ContactBuilder.WebsiteUrl(); + w.url = info.website; + w.type = Website.TYPE_PROFILE; + builder.addWebsite(w); + + return Pair.create(builder.build(), null); + } +} |