• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
17 package com.android.mms.util;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.sqlite.SqliteWrapper;
22 import android.net.Uri;
23 import android.provider.Telephony.Mms;
24 import android.provider.Telephony.Mms.Addr;
25 import android.text.TextUtils;
26 
27 import com.android.i18n.phonenumbers.PhoneNumberUtil;
28 import com.android.mms.MmsApp;
29 import com.android.mms.R;
30 import com.google.android.mms.pdu.EncodedStringValue;
31 import com.google.android.mms.pdu.PduHeaders;
32 import com.google.android.mms.pdu.PduPersister;
33 
34 public class AddressUtils {
35     private static PhoneNumberUtil mPhoneNumberUtil;
36 
AddressUtils()37     private AddressUtils() {
38         // Forbidden being instantiated.
39     }
40 
getFrom(Context context, Uri uri)41     public static String getFrom(Context context, Uri uri) {
42         String msgId = uri.getLastPathSegment();
43         Uri.Builder builder = Mms.CONTENT_URI.buildUpon();
44 
45         builder.appendPath(msgId).appendPath("addr");
46 
47         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
48                             builder.build(), new String[] {Addr.ADDRESS, Addr.CHARSET},
49                             Addr.TYPE + "=" + PduHeaders.FROM, null, null);
50 
51         if (cursor != null) {
52             try {
53                 if (cursor.moveToFirst()) {
54                     String from = cursor.getString(0);
55 
56                     if (!TextUtils.isEmpty(from)) {
57                         byte[] bytes = PduPersister.getBytes(from);
58                         int charset = cursor.getInt(1);
59                         return new EncodedStringValue(charset, bytes)
60                                 .getString();
61                     }
62                 }
63             } finally {
64                 cursor.close();
65             }
66         }
67         return context.getString(R.string.hidden_sender_address);
68     }
69 
70     /**
71      * isPossiblePhoneNumberCanDoFileAccess does a more accurate test if the input is a
72      * phone number, but it can do file access to load country prefixes and other info, so
73      * it's not safe to call from the UI thread.
74      * @param query the phone number to test
75      * @return true if query looks like a valid phone number
76      */
isPossiblePhoneNumberCanDoFileAccess(String query)77     public static boolean isPossiblePhoneNumberCanDoFileAccess(String query) {
78         String currentCountry = MmsApp.getApplication().getCurrentCountryIso().toUpperCase();
79         if (mPhoneNumberUtil == null) {
80             mPhoneNumberUtil = PhoneNumberUtil.getInstance();
81         }
82         return mPhoneNumberUtil.isPossibleNumber(query, currentCountry);
83     }
84 }
85