summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
diff options
context:
space:
mode:
authorDanny Baumann <dannybaumann@web.de>2014-10-02 12:55:23 +0200
committerDanny Baumann <dannybaumann@web.de>2014-10-02 13:51:19 +0200
commite64d747e3ae87cc6ddf83b96882e7bcad6ebcd31 (patch)
tree89cf361a07a7156808d185dfc603e021e6f62b37 /src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
parent2491d19f178f2b78a00cce00bce5dc8ec0d60231 (diff)
downloadandroid_packages_apps_Dialer-e64d747e3ae87cc6ddf83b96882e7bcad6ebcd31.tar.gz
android_packages_apps_Dialer-e64d747e3ae87cc6ddf83b96882e7bcad6ebcd31.tar.bz2
android_packages_apps_Dialer-e64d747e3ae87cc6ddf83b96882e7bcad6ebcd31.zip
Code cleanup.
Avoid repetetive boilerplate code by using helper methods. Change-Id: Id3ce8d07d35d59c08cbc67fa35bbf6daa67c9608
Diffstat (limited to 'src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java')
-rw-r--r--src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java197
1 files changed, 38 insertions, 159 deletions
diff --git a/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java b/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
index 75078b92f..b52a67b1f 100644
--- a/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
+++ b/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
@@ -16,25 +16,18 @@
package com.android.dialer.lookup.yellowpages;
+import android.content.Context;
+import android.text.TextUtils;
+
import com.android.dialer.lookup.LookupSettings;
+import com.android.dialer.lookup.LookupUtils;
-import android.content.Context;
-import android.text.Html;
+import org.apache.http.client.methods.HttpGet;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-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;
-
public class YellowPagesApi {
private static final String TAG = YellowPagesApi.class.getSimpleName();
@@ -61,81 +54,33 @@ public class YellowPagesApi {
}
private void fetchPage() throws IOException {
- mOutput = httpGet(mLookupUrl + mNumber);
- }
-
- private 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;
- }
- }
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- response.getEntity().writeTo(out);
-
- return new String(out.toByteArray());
+ mOutput = LookupUtils.httpGet(new HttpGet(mLookupUrl + mNumber));
}
private String getPhotoUrl(String website) throws IOException {
- String output = httpGet(website);
-
- Matcher m;
-
- Pattern regexGallery = Pattern.compile(
- "href=\"([^\"]+gallery\\?lid=[^\"]+)\"", Pattern.DOTALL);
-
- String galleryUrl = null;
-
- m = regexGallery.matcher(output);
- if (m.find()) {
- galleryUrl = "http://www.yellowpages.com" + m.group(1).trim();
- }
-
- if (galleryUrl == null) {
+ String output = LookupUtils.httpGet(new HttpGet(website));
+ String galleryRef = LookupUtils.firstRegexResult(output,
+ "href=\"([^\"]+gallery\\?lid=[^\"]+)\"", true);
+ if (galleryRef == null) {
return null;
}
// Get first image
+ HttpGet get = new HttpGet("http://www.yellowpages.com" + galleryRef);
+ output = LookupUtils.httpGet(get);
- output = httpGet(galleryUrl);
-
- Pattern regexPhoto = Pattern.compile(
- "\"type\":\"image\",\"src\":\"([^\"]+)\"", Pattern.DOTALL);
-
- String photoUrl = null;
-
- m = regexPhoto.matcher(output);
- if (m.find()) {
- photoUrl = m.group(1).trim();
- }
-
- return photoUrl;
+ return LookupUtils.firstRegexResult(output,
+ "\"type\":\"image\",\"src\":\"([^\"]+)\"", true);
}
private String[] parseNameWebsiteUnitedStates() {
- Matcher m;
-
Pattern regexNameAndWebsite = Pattern.compile(
"<a href=\"([^>]+?)\"[^>]+?class=\"url[^>]+?>([^<]+)</a>",
Pattern.DOTALL);
String name = null;
String website = null;
- m = regexNameAndWebsite.matcher(mOutput);
+ Matcher m = regexNameAndWebsite.matcher(mOutput);
if (m.find()) {
website = m.group(1).trim();
name = m.group(2).trim();
@@ -145,8 +90,6 @@ public class YellowPagesApi {
}
private String[] parseNameWebsiteCanada() {
- Matcher m;
-
Pattern regexNameAndWebsite = Pattern.compile(
"class=\"ypgListingTitleLink utagLink\".*?href=\"(.*?)\">"
+ "(<span\\s+class=\"listingTitle\">.*?</span>)",
@@ -154,14 +97,10 @@ public class YellowPagesApi {
String name = null;
String website = null;
- m = regexNameAndWebsite.matcher(mOutput);
+ Matcher m = regexNameAndWebsite.matcher(mOutput);
if (m.find()) {
website = m.group(1).trim();
- name = m.group(2).trim();
- }
-
- if (name != null) {
- name = Html.fromHtml(name).toString().trim();
+ name = LookupUtils.fromHtml(m.group(2).trim());
}
if (website != null) {
@@ -172,90 +111,43 @@ public class YellowPagesApi {
}
private String parseNumberUnitedStates() {
- Matcher m;
-
- Pattern regexPhoneNumber = Pattern.compile(
- "business-phone.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
- String phoneNumber = null;
-
- m = regexPhoneNumber.matcher(mOutput);
- if (m.find()) {
- phoneNumber = m.group(1).trim();
- }
-
- return phoneNumber;
+ return LookupUtils.firstRegexResult(mOutput,
+ "business-phone.*?>\n*([^\n<]+)\n*<", true);
}
private String parseNumberCanada() {
- Matcher m;
-
- Pattern regexPhoneNumber = Pattern.compile(
- "<div\\s+class=\"phoneNumber\">(.*?)</div>", Pattern.DOTALL);
- String phoneNumber = null;
-
- m = regexPhoneNumber.matcher(mOutput);
- if (m.find()) {
- phoneNumber = m.group(1).trim();
- }
-
- return phoneNumber;
+ return LookupUtils.firstRegexResult(mOutput,
+ "<div\\s+class=\"phoneNumber\">(.*?)</div>", true);
}
private String parseAddressUnitedStates() {
- Matcher m;
-
- Pattern regexAddressStreet = Pattern.compile(
- "street-address.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
- Pattern regexAddressCity = Pattern.compile(
- "locality.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
- Pattern regexAddressState = Pattern.compile(
- "region.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
- Pattern regexAddressZip = Pattern.compile(
- "postal-code.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
-
- String addressStreet = null;
- String addressCity = null;
- String addressState = null;
- String addressZip = null;
-
- m = regexAddressStreet.matcher(mOutput);
- if (m.find()) {
- addressStreet = m.group(1).trim();
- if (addressStreet.endsWith(",")) {
- addressStreet = addressStreet.substring(0,
- addressStreet.length() - 1);
- }
+ String addressStreet = LookupUtils.firstRegexResult(mOutput,
+ "street-address.*?>\n*([^\n<]+)\n*<", true);
+ if (addressStreet != null && addressStreet.endsWith(",")) {
+ addressStreet = addressStreet.substring(0, addressStreet.length() - 1);
}
- m = regexAddressCity.matcher(mOutput);
- if (m.find()) {
- addressCity = m.group(1).trim();
- }
-
- m = regexAddressState.matcher(mOutput);
- if (m.find()) {
- addressState = m.group(1).trim();
- }
-
- m = regexAddressZip.matcher(mOutput);
- if (m.find()) {
- addressZip = m.group(1).trim();
- }
+ String addressCity = LookupUtils.firstRegexResult(mOutput,
+ "locality.*?>\n*([^\n<]+)\n*<", true);
+ String addressState = LookupUtils.firstRegexResult(mOutput,
+ "region.*?>\n*([^\n<]+)\n*<", true);
+ String addressZip = LookupUtils.firstRegexResult(mOutput,
+ "postal-code.*?>\n*([^\n<]+)\n*<", true);
StringBuilder sb = new StringBuilder();
- if (addressStreet != null && addressStreet.length() != 0) {
+ if (!TextUtils.isEmpty(addressStreet)) {
sb.append(addressStreet);
}
- if (addressCity != null && addressCity.length() != 0) {
+ if (!TextUtils.isEmpty(addressCity)) {
sb.append(", ");
sb.append(addressCity);
}
- if (addressState != null && addressState.length() != 0) {
+ if (!TextUtils.isEmpty(addressState)) {
sb.append(", ");
sb.append(addressState);
}
- if (addressZip != null && addressZip.length() != 0) {
+ if (!TextUtils.isEmpty(addressZip)) {
sb.append(", ");
sb.append(addressZip);
}
@@ -269,22 +161,9 @@ public class YellowPagesApi {
}
private String parseAddressCanada() {
- Matcher m;
-
- Pattern regexAddress = Pattern.compile(
- "<div\\s+class=\"address\">(.*?)</div>", Pattern.DOTALL);
- String address = null;
-
- m = regexAddress.matcher(mOutput);
- if (m.find()) {
- address = m.group(1).trim();
- }
-
- if (address != null) {
- address = Html.fromHtml(address).toString().trim();
- }
-
- return address;
+ String address = LookupUtils.firstRegexResult(mOutput,
+ "<div\\s+class=\"address\">(.*?)</div>", true);
+ return LookupUtils.fromHtml(address);
}
private void buildContactInfo() throws IOException {