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