Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.common;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.database.Cursor;
     22 import android.net.Uri;
     23 import android.provider.ContactsContract.CommonDataKinds.Im;
     24 import android.provider.ContactsContract.DisplayPhoto;
     25 import android.telephony.PhoneNumberUtils;
     26 import android.text.TextUtils;
     27 import android.util.Pair;
     28 
     29 import com.android.contacts.common.model.account.AccountWithDataSet;
     30 import com.android.contacts.common.model.dataitem.ImDataItem;
     31 import com.android.contacts.common.testing.NeededForTesting;
     32 import com.android.contacts.common.model.AccountTypeManager;
     33 
     34 import java.util.List;
     35 
     36 public class ContactsUtils {
     37     private static final String TAG = "ContactsUtils";
     38 
     39     // Telecomm related schemes are in CallUtil
     40     public static final String SCHEME_IMTO = "imto";
     41     public static final String SCHEME_MAILTO = "mailto";
     42     public static final String SCHEME_SMSTO = "smsto";
     43 
     44     private static int sThumbnailSize = -1;
     45 
     46     // TODO find a proper place for the canonical version of these
     47     public interface ProviderNames {
     48         String YAHOO = "Yahoo";
     49         String GTALK = "GTalk";
     50         String MSN = "MSN";
     51         String ICQ = "ICQ";
     52         String AIM = "AIM";
     53         String XMPP = "XMPP";
     54         String JABBER = "JABBER";
     55         String SKYPE = "SKYPE";
     56         String QQ = "QQ";
     57     }
     58 
     59     /**
     60      * This looks up the provider name defined in
     61      * ProviderNames from the predefined IM protocol id.
     62      * This is used for interacting with the IM application.
     63      *
     64      * @param protocol the protocol ID
     65      * @return the provider name the IM app uses for the given protocol, or null if no
     66      * provider is defined for the given protocol
     67      * @hide
     68      */
     69     public static String lookupProviderNameFromId(int protocol) {
     70         switch (protocol) {
     71             case Im.PROTOCOL_GOOGLE_TALK:
     72                 return ProviderNames.GTALK;
     73             case Im.PROTOCOL_AIM:
     74                 return ProviderNames.AIM;
     75             case Im.PROTOCOL_MSN:
     76                 return ProviderNames.MSN;
     77             case Im.PROTOCOL_YAHOO:
     78                 return ProviderNames.YAHOO;
     79             case Im.PROTOCOL_ICQ:
     80                 return ProviderNames.ICQ;
     81             case Im.PROTOCOL_JABBER:
     82                 return ProviderNames.JABBER;
     83             case Im.PROTOCOL_SKYPE:
     84                 return ProviderNames.SKYPE;
     85             case Im.PROTOCOL_QQ:
     86                 return ProviderNames.QQ;
     87         }
     88         return null;
     89     }
     90 
     91     /**
     92      * Test if the given {@link CharSequence} contains any graphic characters,
     93      * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
     94      */
     95     public static boolean isGraphic(CharSequence str) {
     96         return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
     97     }
     98 
     99     /**
    100      * Returns true if two objects are considered equal.  Two null references are equal here.
    101      */
    102     @NeededForTesting
    103     public static boolean areObjectsEqual(Object a, Object b) {
    104         return a == b || (a != null && a.equals(b));
    105     }
    106 
    107     /**
    108      * Returns true if two {@link Intent}s are both null, or have the same action.
    109      */
    110     public static final boolean areIntentActionEqual(Intent a, Intent b) {
    111         if (a == b) {
    112             return true;
    113         }
    114         if (a == null || b == null) {
    115             return false;
    116         }
    117         return TextUtils.equals(a.getAction(), b.getAction());
    118     }
    119 
    120     public static boolean areContactWritableAccountsAvailable(Context context) {
    121         final List<AccountWithDataSet> accounts =
    122                 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
    123         return !accounts.isEmpty();
    124     }
    125 
    126     public static boolean areGroupWritableAccountsAvailable(Context context) {
    127         final List<AccountWithDataSet> accounts =
    128                 AccountTypeManager.getInstance(context).getGroupWritableAccounts();
    129         return !accounts.isEmpty();
    130     }
    131 
    132     /**
    133      * Returns the size (width and height) of thumbnail pictures as configured in the provider. This
    134      * can safely be called from the UI thread, as the provider can serve this without performing
    135      * a database access
    136      */
    137     public static int getThumbnailSize(Context context) {
    138         if (sThumbnailSize == -1) {
    139             final Cursor c = context.getContentResolver().query(
    140                     DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
    141                     new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null);
    142             try {
    143                 c.moveToFirst();
    144                 sThumbnailSize = c.getInt(0);
    145             } finally {
    146                 c.close();
    147             }
    148         }
    149         return sThumbnailSize;
    150     }
    151 
    152     private static Intent getCustomImIntent(ImDataItem im, int protocol) {
    153         String host = im.getCustomProtocol();
    154         final String data = im.getData();
    155         if (TextUtils.isEmpty(data)) {
    156             return null;
    157         }
    158         if (protocol != Im.PROTOCOL_CUSTOM) {
    159             // Try bringing in a well-known host for specific protocols
    160             host = ContactsUtils.lookupProviderNameFromId(protocol);
    161         }
    162         if (TextUtils.isEmpty(host)) {
    163             return null;
    164         }
    165         final String authority = host.toLowerCase();
    166         final Uri imUri = new Uri.Builder().scheme(SCHEME_IMTO).authority(
    167                 authority).appendPath(data).build();
    168         final Intent intent = new Intent(Intent.ACTION_SENDTO, imUri);
    169         return intent;
    170     }
    171 
    172     /**
    173      * Returns the proper Intent for an ImDatItem. If available, a secondary intent is stored
    174      * in the second Pair slot
    175      */
    176     public static Pair<Intent, Intent> buildImIntent(Context context, ImDataItem im) {
    177         Intent intent = null;
    178         Intent secondaryIntent = null;
    179         final boolean isEmail = im.isCreatedFromEmail();
    180 
    181         if (!isEmail && !im.isProtocolValid()) {
    182             return new Pair<>(null, null);
    183         }
    184 
    185         final String data = im.getData();
    186         if (TextUtils.isEmpty(data)) {
    187             return new Pair<>(null, null);
    188         }
    189 
    190         final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
    191 
    192         if (protocol == Im.PROTOCOL_GOOGLE_TALK) {
    193             final int chatCapability = im.getChatCapability();
    194             if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) {
    195                 intent = new Intent(Intent.ACTION_SENDTO,
    196                                 Uri.parse("xmpp:" + data + "?message"));
    197                 secondaryIntent = new Intent(Intent.ACTION_SENDTO,
    198                         Uri.parse("xmpp:" + data + "?call"));
    199             } else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) {
    200                 // Allow Talking and Texting
    201                 intent =
    202                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message"));
    203                 secondaryIntent =
    204                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call"));
    205             } else {
    206                 intent =
    207                     new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message"));
    208             }
    209         } else {
    210             // Build an IM Intent
    211             intent = getCustomImIntent(im, protocol);
    212         }
    213         return new Pair<>(intent, secondaryIntent);
    214     }
    215 }
    216