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

Commit e64d747e authored by Danny Baumann's avatar Danny Baumann
Browse files

Code cleanup.

Avoid repetetive boilerplate code by using helper methods.

Change-Id: Id3ce8d07d35d59c08cbc67fa35bbf6daa67c9608
parent 2491d19f
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -259,6 +259,13 @@ public class ContactBuilder {
        public String postCode;
        public String country;

        public static Address createFormattedHome(String address) {
            Address a = new Address();
            a.formattedAddress = address;
            a.type = StructuredPostal.TYPE_HOME;
            return a;
        }

        public JSONObject getJsonObject() throws JSONException {
            JSONObject json = new JSONObject();
            json.putOpt(StructuredPostal.FORMATTED_ADDRESS,
@@ -301,6 +308,12 @@ public class ContactBuilder {
        public String phoneticMiddleName;
        public String phoneticFamilyName;

        public static Name createDisplayName(String displayName) {
            Name name = new Name();
            name.displayName = displayName;
            return name;
        }

        public JSONObject getJsonObject() throws JSONException {
            JSONObject json = new JSONObject();
            json.putOpt(StructuredName.DISPLAY_NAME, displayName);
@@ -337,6 +350,13 @@ public class ContactBuilder {
        public int type;
        public String label;

        public static PhoneNumber createMainNumber(String number) {
            PhoneNumber n = new PhoneNumber();
            n.number = number;
            n.type = Phone.TYPE_MAIN;
            return n;
        }

        public JSONObject getJsonObject() throws JSONException {
            JSONObject json = new JSONObject();
            json.put(Phone.NUMBER, number);
@@ -358,6 +378,13 @@ public class ContactBuilder {
        public int type;
        public String label;

        public static WebsiteUrl createProfile(String url) {
            WebsiteUrl u = new WebsiteUrl();
            u.url = url;
            u.type = Website.TYPE_PROFILE;
            return u;
        }

        public JSONObject getJsonObject() throws JSONException {
            JSONObject json = new JSONObject();
            json.put(Website.URL, url);
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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;

import android.text.Html;

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 LookupUtils {
    private static final String USER_AGENT =
            "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0";

    public static String httpGet(HttpGet request) throws IOException {
        HttpClient client = new DefaultHttpClient();

        request.setHeader("User-Agent", USER_AGENT);

        HttpResponse response = client.execute(request);
        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) {
                HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue());
                for (Header header : request.getAllHeaders()) {
                    newGet.addHeader(header);
                }
                return httpGet(newGet);
            } else {
                throw new IOException("Empty redirection header");
            }
        }

        if (status != HttpStatus.SC_OK) {
            throw new IOException("HTTP failure (status " + status + ")");
        }

        return EntityUtils.toString(response.getEntity());
    }

    public static String firstRegexResult(String input, String regex, boolean dotall) {
        if (input == null) {
            return null;
        }
        Pattern pattern = Pattern.compile(regex, dotall ? Pattern.DOTALL : 0);
        Matcher m = pattern.matcher(input);
        return m.find() ? m.group(1).trim() : null;
    }

    public static String fromHtml(String input) {
        if (input == null) {
            return null;
        }
        return Html.fromHtml(input).toString().trim();
    }
}
+5 −5
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.util.Pair;

import java.io.IOException;

public abstract class ReverseLookup {
    private static final String TAG = ReverseLookup.class.getSimpleName();
@@ -98,9 +99,8 @@ public abstract class ReverseLookup {
     *
     * @param context The application context
     * @param uri The image URI
     * @param data Extra data (a authentication token, perhaps)
     */
    public Bitmap lookupImage(Context context, Uri uri, Object data) {
    public Bitmap lookupImage(Context context, Uri uri) {
        return null;
    }

@@ -112,6 +112,6 @@ public abstract class ReverseLookup {
     * @param formattedNumber The formatted phone number
     * @return The phone number info object
     */
    public abstract Pair<ContactInfo, Object> lookupNumber(Context context,
            String normalizedNumber, String formattedNumber);
    public abstract ContactInfo lookupNumber(Context context,
            String normalizedNumber, String formattedNumber) throws IOException;
}
+13 −16
Original line number Diff line number Diff line
@@ -27,8 +27,8 @@ import android.os.Looper;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

@@ -91,28 +91,25 @@ public class ReverseLookupThread extends Thread {
        }

        // Lookup contact if it's not cached
        Object data = null;
        if (info == null) {
            Pair<ContactInfo, Object> results =
                    ReverseLookup.getInstance(mContext).lookupNumber(
                            mContext, mNormalizedNumber, mFormattedNumber);
            try {
                info = ReverseLookup.getInstance(mContext).lookupNumber(mContext,
                        mNormalizedNumber, mFormattedNumber);
            } catch (IOException e) {
                // ignored, we'll return below
            }

            if (results == null) {
            if (info == null) {
                return;
            }

            info = results.first;
            data = results.second;

            // Put in cache only if the contact is valid
            if (info != null) {
            if (info.equals(ContactInfo.EMPTY)) {
                return;
            } else if (info.name != null) {
                LookupCache.cacheContact(mContext, info);
            }
        }
        }

        final ContactInfo infoFinal = info;

@@ -126,7 +123,7 @@ public class ReverseLookupThread extends Thread {
        if (info.photoUri != null) {
            if (!LookupCache.hasCachedImage(mContext, mNormalizedNumber)) {
                Bitmap bmp = ReverseLookup.getInstance(mContext).lookupImage(
                        mContext, info.photoUri, data);
                        mContext, info.photoUri);

                if (bmp != null) {
                    LookupCache.cacheImage(mContext, mNormalizedNumber, bmp);
+14 −25
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import android.util.Pair;
import com.android.dialer.calllog.ContactInfo;
import com.android.dialer.lookup.ContactBuilder;
import com.android.dialer.lookup.ReverseLookup;
@@ -36,6 +35,8 @@ public class CyngnChineseReverseLookup extends ReverseLookup {
    private static final int COMMON_CHINESE_PHONE_NUMBER_AREANO_END = 5;

    private static final boolean DEBUG = false;
    private static final Uri PROVIDER_URI =
            Uri.parse("content://com.cyngn.chineselocationlookup.provider");

    public CyngnChineseReverseLookup(Context context) {
    }
@@ -48,12 +49,9 @@ public class CyngnChineseReverseLookup extends ReverseLookup {
     * @param formattedNumber  The formatted phone number
     * @return The phone number info object
     */
    public Pair<ContactInfo, Object> lookupNumber(Context context,
    public ContactInfo lookupNumber(Context context,
            String normalizedNumber, String formattedNumber) {
        String displayName;

        displayName = queryProvider(context, normalizedNumber);

        String displayName = queryProvider(context, normalizedNumber);
        if (displayName == null) {
            return null;
        }
@@ -66,19 +64,11 @@ public class CyngnChineseReverseLookup extends ReverseLookup {
        ContactBuilder builder = new ContactBuilder(
                ContactBuilder.REVERSE_LOOKUP,
                normalizedNumber, formattedNumber);

        ContactBuilder.Name n = new ContactBuilder.Name();
        n.displayName = displayName;
        builder.setName(n);

        ContactBuilder.PhoneNumber pn = new ContactBuilder.PhoneNumber();
        pn.number = number;
        pn.type = ContactsContract.CommonDataKinds.Phone.TYPE_MAIN;
        builder.addPhoneNumber(pn);

        builder.setName(ContactBuilder.Name.createDisplayName(displayName));
        builder.addPhoneNumber(ContactBuilder.PhoneNumber.createMainNumber(number));
        builder.setPhotoUri(ContactBuilder.PHOTO_URI_BUSINESS);

        return Pair.create(builder.build(), null);
        return builder.build();
    }

    private String queryProvider(Context context, String normalizedNumber) {
@@ -91,20 +81,19 @@ public class CyngnChineseReverseLookup extends ReverseLookup {
                COMMON_CHINESE_PHONE_NUMBER_AREANO_END);

        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(Uri.parse("content://com.cyngn.chineselocationlookup.provider"),
        Cursor cursor = context.getContentResolver().query(PROVIDER_URI,
                null, null, new String[] { areaPrefix }, null);

        String city = null;
        if (cursor == null) {
            return null;
        }

        try {
            if (cursor.moveToFirst()) {
                city = cursor.getString(2);
                return cursor.getString(2);
            }
        } catch (NullPointerException e) {
            return null;
        } finally {
            cursor.close();
            return city;
        }
        return null;
    }
}
Loading