diff options
author | Xiao-Long Chen <chenxiaolong@cxl.epac.to> | 2016-09-12 09:34:02 +0200 |
---|---|---|
committer | Michael Bestas <mkbestas@lineageos.org> | 2020-12-12 01:23:35 +0200 |
commit | f7d515057b9754c9d5e75f781bb88078867b8425 (patch) | |
tree | f4b1f84ad6b65a43e7285531c3e7cb0e0578b0c3 /java/com/android/dialer/lookup/auskunft | |
parent | b51f11cde8d1455f6dc1a75a147306fcf86c730f (diff) |
Re-add dialer lookup.
Author: Xiao-Long Chen <chenxiaolong@cxl.epac.to>
Date: Mon Sep 12 09:34:02 2016 +0200
Re-add dialer lookup.
BUGBASH-612: do not send phone numbers to non-ssl sites for reverse/forward/people lookups
Change-Id: I677460ad5767b8698ee24d6d43ff159aee55387a
Author: Joey <joey@lineageos.org>
Date: Wed Mar 28 21:11:16 2018 +0200
Dialer: comply with EU's GDPR
Disable lookup by default and add a disclaimer for the feature
Change-Id: If7a181952304dbaee736762bdfd5819eddc5f89b
Signed-off-by: Joey <joey@lineageos.org>
Change-Id: I4ff90a678618fa8c7b5970dff3dd246b0c87135c
Diffstat (limited to 'java/com/android/dialer/lookup/auskunft')
3 files changed, 225 insertions, 0 deletions
diff --git a/java/com/android/dialer/lookup/auskunft/AuskunftApi.java b/java/com/android/dialer/lookup/auskunft/AuskunftApi.java new file mode 100644 index 000000000..5b6b2512c --- /dev/null +++ b/java/com/android/dialer/lookup/auskunft/AuskunftApi.java @@ -0,0 +1,114 @@ +/** + * 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.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) 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; + } + + ContactInfo info = new ContactInfo(); + info.name = cleanupResult(name); + info.number = cleanupResult(number); + info.address = cleanupResult(address); + info.url = uri.toString(); + + infos.add(info); + } + 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; + } + + static class ContactInfo { + String name; + String number; + String url; + String address; + }; +} diff --git a/java/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java b/java/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java new file mode 100644 index 000000000..6feb1a58f --- /dev/null +++ b/java/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java @@ -0,0 +1,57 @@ +/** + * 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.phonenumbercache.ContactInfo; +import com.android.dialer.lookup.ContactBuilder; +import com.android.dialer.lookup.PeopleLookup; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class AuskunftPeopleLookup extends PeopleLookup { + private static final String TAG = AuskunftPeopleLookup.class.getSimpleName(); + + public AuskunftPeopleLookup(Context context) { + } + + @Override + public List<ContactInfo> lookup(Context context, String filter) { + try { + List<AuskunftApi.ContactInfo> infos = AuskunftApi.query(filter); + if (infos != null) { + List<ContactInfo> result = new ArrayList<>(); + for (AuskunftApi.ContactInfo info : infos) { + result.add(ContactBuilder.forPeopleLookup(info.number) + .setName(ContactBuilder.Name.createDisplayName(info.name)) + .addPhoneNumber(ContactBuilder.PhoneNumber.createMainNumber(info.number)) + .addWebsite(ContactBuilder.WebsiteUrl.createProfile(info.url)) + .addAddress(ContactBuilder.Address.createFormattedHome(info.address)) + .build()); + } + return result; + } + } catch (IOException e) { + Log.e(TAG, "People lookup failed", e); + } + return null; + } +} diff --git a/java/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java b/java/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java new file mode 100644 index 000000000..6b6f41543 --- /dev/null +++ b/java/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java @@ -0,0 +1,54 @@ +/** + * 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.phonenumbercache.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 { + 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<AuskunftApi.ContactInfo> infos = AuskunftApi.query(normalizedNumber); + AuskunftApi.ContactInfo info = infos != null && !infos.isEmpty() ? infos.get(0) : null; + if (info == null) { + return null; + } + + return ContactBuilder.forReverseLookup(normalizedNumber, formattedNumber) + .setName(ContactBuilder.Name.createDisplayName(info.name)) + .addPhoneNumber(ContactBuilder.PhoneNumber.createMainNumber(info.number)) + .addWebsite(ContactBuilder.WebsiteUrl.createProfile(info.url)) + .addAddress(ContactBuilder.Address.createFormattedHome(info.address)) + .build(); + } +} |