Home | History | Annotate | Download | only in telecom
      1 /*
      2  * Copyright (C) 2015 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.server.telecom;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.content.pm.UserInfo;
     22 import android.media.AudioManager;
     23 import android.media.RingtoneManager;
     24 import android.media.Ringtone;
     25 import android.net.Uri;
     26 import android.os.UserHandle;
     27 import android.os.UserManager;
     28 import android.provider.Settings;
     29 
     30 import android.telecom.Log;
     31 import android.telecom.PhoneAccount;
     32 import android.text.TextUtils;
     33 
     34 import com.android.internal.annotations.VisibleForTesting;
     35 import com.android.internal.telephony.CallerInfo;
     36 
     37 import java.util.List;
     38 
     39 /**
     40  * Uses the incoming {@link Call}'s ringtone URI (obtained by the Contact Lookup) to obtain a
     41  * {@link Ringtone} from the {@link RingtoneManager} that can be played by the system during an
     42  * incoming call. If the ringtone URI is null, use the default Ringtone for the active user.
     43  */
     44 @VisibleForTesting
     45 public class RingtoneFactory {
     46 
     47     private final Context mContext;
     48     private final CallsManager mCallsManager;
     49 
     50     public RingtoneFactory(CallsManager callsManager, Context context) {
     51         mContext = context;
     52         mCallsManager = callsManager;
     53     }
     54 
     55     public Ringtone getRingtone(Call incomingCall) {
     56         // Use the default ringtone of the work profile if the contact is a work profile contact.
     57         Context userContext = isWorkContact(incomingCall) ?
     58                 getWorkProfileContextForUser(mCallsManager.getCurrentUserHandle()) :
     59                 getContextForUserHandle(mCallsManager.getCurrentUserHandle());
     60         Uri ringtoneUri = incomingCall.getRingtone();
     61         Ringtone ringtone = null;
     62 
     63         if(ringtoneUri != null && userContext != null) {
     64             // Ringtone URI is explicitly specified. First, try to create a Ringtone with that.
     65             ringtone = RingtoneManager.getRingtone(userContext, ringtoneUri);
     66         }
     67         if(ringtone == null) {
     68             // Contact didn't specify ringtone or custom Ringtone creation failed. Get default
     69             // ringtone for user or profile.
     70             Context contextToUse = hasDefaultRingtoneForUser(userContext) ? userContext : mContext;
     71             Uri defaultRingtoneUri;
     72             if (UserManager.get(contextToUse).isUserUnlocked(contextToUse.getUserId())) {
     73                 defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(contextToUse,
     74                         RingtoneManager.TYPE_RINGTONE);
     75             } else {
     76                 defaultRingtoneUri = Settings.System.DEFAULT_RINGTONE_URI;
     77             }
     78             if (defaultRingtoneUri == null) {
     79                 return null;
     80             }
     81             ringtone = RingtoneManager.getRingtone(contextToUse, defaultRingtoneUri);
     82         }
     83         if (ringtone != null) {
     84             ringtone.setStreamType(AudioManager.STREAM_RING);
     85         }
     86         return ringtone;
     87     }
     88 
     89     private Context getWorkProfileContextForUser(UserHandle userHandle) {
     90         // UserManager.getEnabledProfiles returns the enabled profiles along with the user's handle
     91         // itself (so we must filter out the user).
     92         List<UserInfo> profiles = UserManager.get(mContext).getEnabledProfiles(
     93                 userHandle.getIdentifier());
     94         UserInfo workprofile = null;
     95         int managedProfileCount = 0;
     96         for (UserInfo profile : profiles) {
     97             UserHandle profileUserHandle = profile.getUserHandle();
     98             if (profileUserHandle != userHandle && profile.isManagedProfile()) {
     99                 managedProfileCount++;
    100                 workprofile = profile;
    101             }
    102         }
    103         // There may be many different types of profiles, so only count Managed (Work) Profiles.
    104         if(managedProfileCount == 1) {
    105             return getContextForUserHandle(workprofile.getUserHandle());
    106         }
    107         // There are multiple managed profiles for the associated user and we do not have enough
    108         // info to determine which profile is the work profile. Just use the default.
    109         return null;
    110     }
    111 
    112     private Context getContextForUserHandle(UserHandle userHandle) {
    113         if(userHandle == null) {
    114             return null;
    115         }
    116         try {
    117             return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, userHandle);
    118         } catch (PackageManager.NameNotFoundException e) {
    119             Log.w("RingtoneFactory", "Package name not found: " + e.getMessage());
    120         }
    121         return null;
    122     }
    123 
    124     private boolean hasDefaultRingtoneForUser(Context userContext) {
    125         if(userContext == null) {
    126             return false;
    127         }
    128         return !TextUtils.isEmpty(Settings.System.getStringForUser(userContext.getContentResolver(),
    129                 Settings.System.RINGTONE, userContext.getUserId()));
    130     }
    131 
    132     private boolean isWorkContact(Call incomingCall) {
    133         CallerInfo contactCallerInfo = incomingCall.getCallerInfo();
    134         return (contactCallerInfo != null) &&
    135                 (contactCallerInfo.userType == CallerInfo.USER_TYPE_WORK);
    136     }
    137 }
    138