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

Commit 6605b629 authored by Adnan's avatar Adnan
Browse files

Chinese Location Lookup [1/2] Dialer: Chinese Reverse Lookup

    PS6: Add static integers for reference

Change-Id: I22ede59cfa8785ac04ad1b1f19d1c69e24b9fb89
parent 1b93ca5c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ public final class LookupSettings {
    public static final String RLP_YELLOWPAGES = "YellowPages";
    public static final String RLP_YELLOWPAGES_CA = "YellowPages_CA";
    public static final String RLP_ZABASEARCH = "ZabaSearch";
    public static final String RLP_CYNGN_CHINESE = "CyngnChinese";
    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.cyngn.CyngnChineseReverseLookup;
import com.android.dialer.lookup.opencnam.OpenCnamReverseLookup;
import com.android.dialer.lookup.whitepages.WhitePagesReverseLookup;
import com.android.dialer.lookup.yellowpages.YellowPagesReverseLookup;
@@ -49,6 +50,8 @@ public abstract class ReverseLookup {
                INSTANCE = new YellowPagesReverseLookup(context);
            } else if (provider.equals(LookupSettings.RLP_ZABASEARCH)) {
                INSTANCE = new ZabaSearchReverseLookup(context);
            } else if (provider.equals(LookupSettings.RLP_CYNGN_CHINESE)) {
                INSTANCE = new CyngnChineseReverseLookup(context);
            }
        }

@@ -70,6 +73,9 @@ public abstract class ReverseLookup {
        } else if (provider.equals(LookupSettings.RLP_ZABASEARCH)
                && INSTANCE instanceof ZabaSearchReverseLookup) {
            return true;
        } else if (provider.equals(LookupSettings.RLP_CYNGN_CHINESE)
                && INSTANCE instanceof CyngnChineseReverseLookup) {
            return true;
        } else {
            return false;
        }
+110 −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.cyngn;

import android.content.ContentResolver;
import android.content.Context;
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;

public class CyngnChineseReverseLookup extends ReverseLookup {

    private static final String TAG = CyngnChineseReverseLookup.class.getSimpleName();

    private static final int COMMON_CHINESE_PHONE_NUMBER_LENGTH = 11;
    private static final int COMMON_CHINESE_PHONE_NUMBER_AREANO_START = 2;
    private static final int COMMON_CHINESE_PHONE_NUMBER_AREANO_END = 5;

    private static final boolean DEBUG = false;

    public CyngnChineseReverseLookup(Context context) {
    }

    /**
     * Perform phone number lookup.
     *
     * @param context          The application context
     * @param normalizedNumber The normalized phone number
     * @param formattedNumber  The formatted phone number
     * @return The phone number info object
     */
    public Pair<ContactInfo, Object> lookupNumber(Context context,
                                                  String normalizedNumber, String formattedNumber) {
        String displayName;

        displayName = queryProvider(context, normalizedNumber);

        if (displayName == null) {
            return null;
        }

        if (DEBUG) Log.d(TAG, "Reverse lookup returned name: " + displayName);

        String number = formattedNumber != null
                ? formattedNumber : normalizedNumber;

        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.setPhotoUri(ContactBuilder.PHOTO_URI_BUSINESS);

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

    private String queryProvider(Context context, String normalizedNumber) {
        if (normalizedNumber.length() < COMMON_CHINESE_PHONE_NUMBER_LENGTH) {
            return null;
        }

        //trim carrier code, and get area prefix
        String areaPrefix = normalizedNumber.substring(COMMON_CHINESE_PHONE_NUMBER_AREANO_START,
                COMMON_CHINESE_PHONE_NUMBER_AREANO_END);

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

        String city = null;

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