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 com.android.mms.R;
20import com.google.android.mms.pdu.EncodedStringValue;
21import com.google.android.mms.pdu.PduHeaders;
22import com.google.android.mms.pdu.PduPersister;
23import android.database.sqlite.SqliteWrapper;
24
25import android.content.Context;
26import android.database.Cursor;
27import android.net.Uri;
28import android.provider.Telephony.Mms;
29import android.provider.Telephony.Mms.Addr;
30import android.text.TextUtils;
31import android.telephony.PhoneNumberUtils;
32
33public class AddressUtils {
34    private static final String TAG = "AddressUtils";
35
36    private AddressUtils() {
37        // Forbidden being instantiated.
38    }
39
40    public static String getFrom(Context context, Uri uri) {
41        String msgId = uri.getLastPathSegment();
42        Uri.Builder builder = Mms.CONTENT_URI.buildUpon();
43
44        builder.appendPath(msgId).appendPath("addr");
45
46        Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
47                            builder.build(), new String[] {Addr.ADDRESS, Addr.CHARSET},
48                            Addr.TYPE + "=" + PduHeaders.FROM, null, null);
49
50        if (cursor != null) {
51            try {
52                if (cursor.moveToFirst()) {
53                    String from = cursor.getString(0);
54
55                    if (!TextUtils.isEmpty(from)) {
56                        byte[] bytes = PduPersister.getBytes(from);
57                        int charset = cursor.getInt(1);
58                        return new EncodedStringValue(charset, bytes)
59                                .getString();
60                    }
61                }
62            } finally {
63                cursor.close();
64            }
65        }
66        return context.getString(R.string.hidden_sender_address);
67    }
68}
69