Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7139437 authored by Martin Herndl's avatar Martin Herndl Committed by Danny Baumann
Browse files

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
parent ca7b74de
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -27,17 +27,20 @@
    </string-array>

    <string-array name="people_lookup_providers" translatable="false">
        <item>Auskunft</item>
        <item>WhitePages</item>
    </string-array>

    <string-array name="people_lookup_provider_names" translatable="false">
        <item>Auskunft (AT)</item>
        <item>WhitePages (US)</item>
    </string-array>

    <string-array name="reverse_lookup_providers" translatable="false">
        <item>OpenCnam</item>
        <item>Auskunft</item>
        <item>DasTelefonbuch</item>
        <item>Gebeld</item>
        <item>OpenCnam</item>
        <item>WhitePages</item>
        <item>WhitePages_CA</item>
        <item>YellowPages</item>
@@ -46,9 +49,10 @@
    </string-array>

    <string-array name="reverse_lookup_provider_names" translatable="false">
        <item>OpenCnam (US)</item>
        <item>Auskunft (AT)</item>
        <item>Das Telefonbuch (DE)</item>
        <item>Gebeld (NL)</item>
        <item>OpenCnam (US)</item>
        <item>WhitePages (US)</item>
        <item>WhitePages (CA)</item>
        <item>YellowPages (US)</item>
+2 −0
Original line number Diff line number Diff line
@@ -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() {
+6 −0
Original line number Diff line number Diff line
@@ -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;
        }
+6 −0
Original line number Diff line number Diff line
@@ -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;
        }
+125 −0
Original line number Diff line number Diff line
/**
 * 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;
    }
}
Loading