diff options
| author | Martin Herndl <martin.herndl@gmail.com> | 2015-07-15 16:17:29 +0200 |
|---|---|---|
| committer | Danny Baumann <dannybaumann@web.de> | 2015-12-07 10:07:15 -0800 |
| commit | d7139437781c5ccb7d2ae299fbd3a5221db53961 (patch) | |
| tree | c4828ee8b983f96ab3ccc61d0febc791ed52bd3e /src/com | |
| parent | ca7b74de9880823d33548ed1328fd2192c9ae395 (diff) | |
| download | packages_apps_Dialer-d7139437781c5ccb7d2ae299fbd3a5221db53961.tar.gz packages_apps_Dialer-d7139437781c5ccb7d2ae299fbd3a5221db53961.tar.bz2 packages_apps_Dialer-d7139437781c5ccb7d2ae299fbd3a5221db53961.zip | |
Add people and reverse lookup provider "Auskunft" (AT).
Change-Id: Ib57a4aca35bcd80f320c5a2a44faac4c9c9d3026
Lookup: improve "Auskunft" (AT) API.
This is a follow-up on Ib57a4aca35bcd80f320c5a2a44faac4c9c9d3026
which improves the parsing process by working the search response of
section by section to not miss any incomplete data sets.
The old process was kind of error prone because it searched for all
names, numbers and addresses and tried to match those which of
course failed if there was e.g. an address missing.
Change-Id: Ib685477916e06e63e9b63f2fff7f2bf47a9d9ba3
Diffstat (limited to 'src/com')
6 files changed, 232 insertions, 0 deletions
diff --git a/src/com/android/dialer/lookup/LookupSettings.java b/src/com/android/dialer/lookup/LookupSettings.java index 8e0e18cae..4518c1d67 100644 --- a/src/com/android/dialer/lookup/LookupSettings.java +++ b/src/com/android/dialer/lookup/LookupSettings.java @@ -36,6 +36,7 @@ public final class LookupSettings { /** People lookup providers */ public static final String PLP_WHITEPAGES = "WhitePages"; + public static final String PLP_AUSKUNFT = "Auskunft"; public static final String PLP_DEFAULT = PLP_WHITEPAGES; /** Reverse lookup providers */ @@ -48,6 +49,7 @@ public final class LookupSettings { public static final String RLP_CYNGN_CHINESE = "CyngnChinese"; public static final String RLP_DASTELEFONBUCH = "DasTelefonbuch"; public static final String RLP_GEBELD = "Gebeld"; + public static final String RLP_AUSKUNFT = "Auskunft"; public static final String RLP_DEFAULT = RLP_OPENCNAM; private LookupSettings() { diff --git a/src/com/android/dialer/lookup/PeopleLookup.java b/src/com/android/dialer/lookup/PeopleLookup.java index 08c3d7dc5..4a247c037 100644 --- a/src/com/android/dialer/lookup/PeopleLookup.java +++ b/src/com/android/dialer/lookup/PeopleLookup.java @@ -17,6 +17,7 @@ package com.android.dialer.lookup; import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.auskunft.AuskunftPeopleLookup; import com.android.dialer.lookup.whitepages.WhitePagesPeopleLookup; import android.content.Context; @@ -35,6 +36,8 @@ public abstract class PeopleLookup { if (provider.equals(LookupSettings.PLP_WHITEPAGES)) { INSTANCE = new WhitePagesPeopleLookup(context); + } else if (provider.equals(LookupSettings.PLP_AUSKUNFT)) { + INSTANCE = new AuskunftPeopleLookup(context); } } @@ -45,6 +48,9 @@ public abstract class PeopleLookup { if (provider.equals(LookupSettings.PLP_WHITEPAGES) && INSTANCE instanceof WhitePagesPeopleLookup) { return true; + } else if (provider.equals(LookupSettings.PLP_AUSKUNFT) + && INSTANCE instanceof AuskunftPeopleLookup) { + return true; } else { return false; } diff --git a/src/com/android/dialer/lookup/ReverseLookup.java b/src/com/android/dialer/lookup/ReverseLookup.java index 32da8ae02..e2eb7a588 100644 --- a/src/com/android/dialer/lookup/ReverseLookup.java +++ b/src/com/android/dialer/lookup/ReverseLookup.java @@ -17,6 +17,7 @@ package com.android.dialer.lookup; import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.auskunft.AuskunftReverseLookup; import com.android.dialer.lookup.cyngn.CyngnChineseReverseLookup; import com.android.dialer.lookup.dastelefonbuch.TelefonbuchReverseLookup; import com.android.dialer.lookup.gebeld.GebeldReverseLookup; @@ -59,6 +60,8 @@ public abstract class ReverseLookup { INSTANCE = new TelefonbuchReverseLookup(context); } else if (provider.equals(LookupSettings.RLP_GEBELD)) { INSTANCE = new GebeldReverseLookup(context); + } else if (provider.equals(LookupSettings.RLP_AUSKUNFT)) { + INSTANCE = new AuskunftReverseLookup(context); } } @@ -89,6 +92,9 @@ public abstract class ReverseLookup { } else if (provider.equals(LookupSettings.RLP_GEBELD) && INSTANCE instanceof GebeldReverseLookup) { return true; + } else if (provider.equals(LookupSettings.RLP_AUSKUNFT) + && INSTANCE instanceof AuskunftReverseLookup) { + return true; } else { return false; } diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftApi.java b/src/com/android/dialer/lookup/auskunft/AuskunftApi.java new file mode 100644 index 000000000..7b7c98cce --- /dev/null +++ b/src/com/android/dialer/lookup/auskunft/AuskunftApi.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2015, 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.auskunft; + +import android.content.Context; +import android.net.Uri; +import android.util.Log; + +import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.ContactBuilder; +import com.android.dialer.lookup.LookupUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public final class AuskunftApi { + private static final String TAG = AuskunftApi.class.getSimpleName(); + + private static final String PEOPLE_LOOKUP_URL = + "https://auskunft.at/suche"; + + private static final String SEARCH_RESULTS_REGEX = + "(?i)<section[\\s]+class=[\"']?search-entry(.*?)?</section"; + private static final String NAME_REGEX = + "(?i)<h1[\\s]+itemprop=[\"']?name[\"']?>(.*?)</h1"; + private static final String NUMBER_REGEX = + "(?i)phone[\"'][\\s]+?href=[\"']{1}tel:(.*?)[\"']{1}"; + private static final String ADDRESS_REGEX = + "(?i)<span[\\s]+itemprop=[\"']?streetAddress[\"']?>(.*?)</a"; + + private static final String BUSINESS_IDENTIFIER = "(Firma)"; + + private AuskunftApi() { + } + + public static List<ContactInfo> query(String filter, int lookupType, String normalizedNumber, + String formattedNumber) throws IOException { + // build URI + Uri uri = Uri.parse(PEOPLE_LOOKUP_URL) + .buildUpon() + .appendQueryParameter("query", filter) + .build(); + + // get all search entry sections + List<String> entries = LookupUtils.allRegexResults(LookupUtils.httpGet(uri.toString(), + null), SEARCH_RESULTS_REGEX, true); + + // abort lookup if nothing found + if (entries == null || entries.isEmpty()) { + Log.w(TAG, "nothing found"); + return null; + } + + // build response by iterating through the search entries and parsing their HTML data + List<ContactInfo> infos = new ArrayList<ContactInfo>(); + for (String entry : entries) { + // parse wanted data and replace null values + String name = replaceNullResult(LookupUtils.firstRegexResult(entry, NAME_REGEX, true)); + String address = replaceNullResult(LookupUtils.firstRegexResult( + entry, ADDRESS_REGEX, true)); + String number = replaceNullResult(LookupUtils.firstRegexResult( + entry, NUMBER_REGEX, true)); + // ignore entry if name or number is empty (should not occur) + // missing addresses won't be a problem (but do occur) + if (name.isEmpty() || number.isEmpty()) { + continue; + } + // figure out if we have a business contact + boolean isBusiness = name.contains(BUSINESS_IDENTIFIER); + // cleanup results + name = cleanupResult(name); + number = cleanupResult(number); + address = cleanupResult(address); + // set normalized and formatted number if we're not doing a reverse lookup + if (lookupType != ContactBuilder.REVERSE_LOOKUP) { + normalizedNumber = formattedNumber = number; + } + // build contact and add to list + ContactBuilder builder = new ContactBuilder(lookupType, normalizedNumber, + formattedNumber); + builder.setName(ContactBuilder.Name.createDisplayName(name)); + builder.addPhoneNumber( + ContactBuilder.PhoneNumber.createMainNumber(number)); + builder.addWebsite(ContactBuilder.WebsiteUrl.createProfile(uri.toString())); + builder.addAddress(ContactBuilder.Address.createFormattedHome(address)); + builder.setIsBusiness(isBusiness); + infos.add(builder.build()); + } + return infos; + } + + private static String cleanupResult(String result) { + // get displayable text + result = LookupUtils.fromHtml(result); + // replace newlines with spaces + result = result.replaceAll("\\r|\\n", " "); + // replace multiple spaces with one + result = result.replaceAll("\\s+", " "); + // remove business identifier that is originally not part of the name + result = result.replace(BUSINESS_IDENTIFIER, ""); + // final trimming + result = result.trim(); + + return result; + } + + private static String replaceNullResult(String result) { + return (result == null) ? "" : result; + } +} diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java b/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java new file mode 100644 index 000000000..970f2d0ef --- /dev/null +++ b/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2015, 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.auskunft; + +import android.content.Context; +import android.util.Log; + +import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.ContactBuilder; +import com.android.dialer.lookup.PeopleLookup; + +import java.io.IOException; +import java.util.List; + +public class AuskunftPeopleLookup extends PeopleLookup { + private static final String TAG = AuskunftPeopleLookup.class.getSimpleName(); + + public AuskunftPeopleLookup(Context context) { + } + + @Override + public ContactInfo[] lookup(Context context, String filter) { + List<ContactInfo> infos = null; + try { + infos = AuskunftApi.query(filter, ContactBuilder.PEOPLE_LOOKUP, null, null); + } catch (IOException e) { + Log.e(TAG, "People lookup failed", e); + } + return (infos != null && !infos.isEmpty()) + ? infos.toArray(new ContactInfo[infos.size()]) : null; + } +} diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java b/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java new file mode 100644 index 000000000..383b4d53d --- /dev/null +++ b/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2015, 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.auskunft; + +import android.content.Context; + +import com.android.dialer.calllog.ContactInfo; +import com.android.dialer.lookup.ContactBuilder; +import com.android.dialer.lookup.ReverseLookup; + +import java.io.IOException; +import java.util.List; + +public class AuskunftReverseLookup extends ReverseLookup { + private static final String TAG = AuskunftReverseLookup.class.getSimpleName(); + + public AuskunftReverseLookup(Context context) { + } + + @Override + public ContactInfo lookupNumber(Context context, String normalizedNumber, + String formattedNumber) throws IOException { + // only Austrian numbers are supported + if (normalizedNumber.startsWith("+") && !normalizedNumber.startsWith("+43")) { + return null; + } + + // query the API and return null if nothing found or general error + List<ContactInfo> infos = AuskunftApi.query(normalizedNumber, ContactBuilder.REVERSE_LOOKUP, + normalizedNumber, formattedNumber); + return (infos != null && !infos.isEmpty()) ? infos.get(0) : null; + } +} |
