Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 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 package com.android.dialer.util;
     17 
     18 import android.app.Activity;
     19 import android.content.ActivityNotFoundException;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.ResolveInfo;
     25 import android.content.res.Resources;
     26 import android.graphics.Point;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.provider.Telephony;
     30 import android.telecom.TelecomManager;
     31 import android.text.TextUtils;
     32 import android.view.View;
     33 import android.view.inputmethod.InputMethodManager;
     34 import android.widget.ImageView;
     35 import android.widget.TextView;
     36 import android.widget.Toast;
     37 
     38 import com.android.contacts.common.ContactsUtils;
     39 import com.android.contacts.common.interactions.TouchPointManager;
     40 import com.android.dialer.R;
     41 import com.android.incallui.CallCardFragment;
     42 import com.android.incallui.Log;
     43 
     44 import java.util.List;
     45 import java.util.Locale;
     46 
     47 /**
     48  * General purpose utility methods for the Dialer.
     49  */
     50 public class DialerUtils {
     51 
     52     /**
     53      * Attempts to start an activity and displays a toast with the default error message if the
     54      * activity is not found, instead of throwing an exception.
     55      *
     56      * @param context to start the activity with.
     57      * @param intent to start the activity with.
     58      */
     59     public static void startActivityWithErrorToast(Context context, Intent intent) {
     60         startActivityWithErrorToast(context, intent, R.string.activity_not_available);
     61     }
     62 
     63     /**
     64      * Attempts to start an activity and displays a toast with a provided error message if the
     65      * activity is not found, instead of throwing an exception.
     66      *
     67      * @param context to start the activity with.
     68      * @param intent to start the activity with.
     69      * @param msgId Resource ID of the string to display in an error message if the activity is
     70      *              not found.
     71      */
     72     public static void startActivityWithErrorToast(Context context, Intent intent, int msgId) {
     73         try {
     74             if (Intent.ACTION_CALL.equals(intent.getAction())) {
     75                 // All dialer-initiated calls should pass the touch point to the InCallUI
     76                 Point touchPoint = TouchPointManager.getInstance().getPoint();
     77                 if (touchPoint.x != 0 || touchPoint.y != 0) {
     78                     Bundle extras = new Bundle();
     79                     extras.putParcelable(TouchPointManager.TOUCH_POINT, touchPoint);
     80                     intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
     81                 }
     82 
     83                 ((Activity) context).startActivityForResult(intent, 0);
     84             } else {
     85                 context.startActivity(intent);
     86             }
     87         } catch (ActivityNotFoundException e) {
     88             Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
     89         }
     90     }
     91 
     92     /**
     93      * Returns the component name to use in order to send an SMS using the default SMS application,
     94      * or null if none exists.
     95      */
     96     public static ComponentName getSmsComponent(Context context) {
     97         String smsPackage = Telephony.Sms.getDefaultSmsPackage(context);
     98         if (smsPackage != null) {
     99             final PackageManager packageManager = context.getPackageManager();
    100             final Intent intent = new Intent(Intent.ACTION_SENDTO,
    101                     Uri.fromParts(ContactsUtils.SCHEME_SMSTO, "", null));
    102             final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
    103             for (ResolveInfo resolveInfo : resolveInfos) {
    104                 if (smsPackage.equals(resolveInfo.activityInfo.packageName)) {
    105                     return new ComponentName(smsPackage, resolveInfo.activityInfo.name);
    106                 }
    107             }
    108         }
    109         return null;
    110     }
    111 
    112     /**
    113      * Sets the image asset and text for an empty list view (see empty_list_view.xml).
    114      *
    115      * @param emptyListView The empty list view.
    116      * @param imageResId The resource id for the drawable to set as the image.
    117      * @param strResId The resource id for the string to set as the message.
    118      * @param res The resources to obtain the image and string from.
    119      */
    120     public static void configureEmptyListView(
    121             View emptyListView, int imageResId, int strResId, Resources res) {
    122         ImageView emptyListViewImage =
    123                 (ImageView) emptyListView.findViewById(R.id.emptyListViewImage);
    124 
    125         emptyListViewImage.setImageDrawable(res.getDrawable(imageResId));
    126         emptyListViewImage.setContentDescription(res.getString(strResId));
    127 
    128         TextView emptyListViewMessage =
    129                 (TextView) emptyListView.findViewById(R.id.emptyListViewMessage);
    130         emptyListViewMessage.setText(res.getString(strResId));
    131     }
    132 
    133     /**
    134      * Closes an {@link AutoCloseable}, silently ignoring any checked exceptions. Does nothing if
    135      * null.
    136      *
    137      * @param closeable to close.
    138      */
    139     public static void closeQuietly(AutoCloseable closeable) {
    140         if (closeable != null) {
    141             try {
    142                 closeable.close();
    143             } catch (RuntimeException rethrown) {
    144                 throw rethrown;
    145             } catch (Exception ignored) {
    146             }
    147         }
    148     }
    149 
    150     /**
    151      * Joins a list of {@link CharSequence} into a single {@link CharSequence} seperated by a
    152      * localized delimiter such as ", ".
    153      *
    154      * @param resources Resources used to get list delimiter.
    155      * @param list List of char sequences to join.
    156      * @return Joined char sequences.
    157      */
    158     public static CharSequence join(Resources resources, Iterable<CharSequence> list) {
    159         final CharSequence separator = resources.getString(R.string.list_delimeter);
    160         return TextUtils.join(separator, list);
    161     }
    162 
    163     /**
    164      * @return True if the application is currently in RTL mode.
    165      */
    166     public static boolean isRtl() {
    167         return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ==
    168             View.LAYOUT_DIRECTION_RTL;
    169     }
    170 
    171     public static void showInputMethod(View view) {
    172         final InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(
    173                 Context.INPUT_METHOD_SERVICE);
    174         if (imm != null) {
    175             imm.showSoftInput(view, 0);
    176         }
    177     }
    178 
    179     public static void hideInputMethod(View view) {
    180         final InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(
    181                 Context.INPUT_METHOD_SERVICE);
    182         if (imm != null) {
    183             imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    184         }
    185     }
    186 }
    187