1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package com.android.mms.util;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.database.sqlite.SqliteWrapper;
22import android.net.Uri;
23import android.provider.Telephony.Mms;
24import android.provider.Telephony.Mms.Addr;
25import android.text.TextUtils;
26
27import com.android.i18n.phonenumbers.PhoneNumberUtil;
28import com.android.mms.MmsApp;
29import com.android.mms.R;
30import com.google.android.mms.pdu.EncodedStringValue;
31import com.google.android.mms.pdu.PduHeaders;
32import com.google.android.mms.pdu.PduPersister;
33
34public class AddressUtils {
35    private static final String TAG = "AddressUtils";
36    private static PhoneNumberUtil mPhoneNumberUtil;
37
38    private AddressUtils() {
39        // Forbidden being instantiated.
40    }
41
42    public static String getFrom(Context context, Uri uri) {
43        String msgId = uri.getLastPathSegment();
44        Uri.Builder builder = Mms.CONTENT_URI.buildUpon();
45
46        builder.appendPath(msgId).appendPath("addr");
47
48        Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
49                            builder.build(), new String[] {Addr.ADDRESS, Addr.CHARSET},
50                            Addr.TYPE + "=" + PduHeaders.FROM, null, null);
51
52        if (cursor != null) {
53            try {
54                if (cursor.moveToFirst()) {
55                    String from = cursor.getString(0);
56
57                    if (!TextUtils.isEmpty(from)) {
58                        byte[] bytes = PduPersister.getBytes(from);
59                        int charset = cursor.getInt(1);
60                        return new EncodedStringValue(charset, bytes)
61                                .getString();
62                    }
63                }
64            } finally {
65                cursor.close();
66            }
67        }
68        return context.getString(R.string.hidden_sender_address);
69    }
70
71    /**
72     * isPossiblePhoneNumberCanDoFileAccess does a more accurate test if the input is a
73     * phone number, but it can do file access to load country prefixes and other info, so
74     * it's not safe to call from the UI thread.
75     * @param query the phone number to test
76     * @return true if query looks like a valid phone number
77     */
78    public static boolean isPossiblePhoneNumberCanDoFileAccess(String query) {
79        String currentCountry = MmsApp.getApplication().getCurrentCountryIso().toUpperCase();
80        if (mPhoneNumberUtil == null) {
81            mPhoneNumberUtil = PhoneNumberUtil.getInstance();
82        }
83        return mPhoneNumberUtil.isPossibleNumber(query, currentCountry);
84    }
85}
86