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.annotation.Nullable; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.content.pm.UserInfo; 23 import android.media.AudioAttributes; 24 import android.media.RingtoneManager; 25 import android.media.Ringtone; 26 import android.media.VolumeShaper; 27 import android.net.Uri; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.provider.Settings; 31 32 import android.telecom.Log; 33 import android.text.TextUtils; 34 35 import com.android.internal.annotations.VisibleForTesting; 36 import android.telecom.CallerInfo; 37 38 import java.util.List; 39 40 /** 41 * Uses the incoming {@link Call}'s ringtone URI (obtained by the Contact Lookup) to obtain a 42 * {@link Ringtone} from the {@link RingtoneManager} that can be played by the system during an 43 * incoming call. If the ringtone URI is null, use the default Ringtone for the active user. 44 */ 45 @VisibleForTesting 46 public class RingtoneFactory { 47 48 private final Context mContext; 49 private final CallsManager mCallsManager; 50 RingtoneFactory(CallsManager callsManager, Context context)51 public RingtoneFactory(CallsManager callsManager, Context context) { 52 mContext = context; 53 mCallsManager = callsManager; 54 } 55 56 /** 57 * Determines if a ringtone has haptic channels. 58 * @param ringtone The ringtone URI. 59 * @return {@code true} if there is a haptic channel, {@code false} otherwise. 60 */ hasHapticChannels(Ringtone ringtone)61 public boolean hasHapticChannels(Ringtone ringtone) { 62 boolean hasHapticChannels = RingtoneManager.hasHapticChannels(ringtone.getUri()); 63 Log.i(this, "hasHapticChannels %s -> %b", ringtone.getUri(), hasHapticChannels); 64 return hasHapticChannels; 65 } 66 getRingtone(Call incomingCall, @Nullable VolumeShaper.Configuration volumeShaperConfig, boolean hapticChannelsMuted)67 public Ringtone getRingtone(Call incomingCall, 68 @Nullable VolumeShaper.Configuration volumeShaperConfig, boolean hapticChannelsMuted) { 69 // Initializing ringtones on the main thread can deadlock 70 ThreadUtil.checkNotOnMainThread(); 71 72 AudioAttributes audioAttrs = getDefaultRingtoneAudioAttributes(hapticChannelsMuted); 73 74 // Use the default ringtone of the work profile if the contact is a work profile contact. 75 // or the default ringtone of the receiving user. 76 Context userContext = isWorkContact(incomingCall) ? 77 getWorkProfileContextForUser(mCallsManager.getCurrentUserHandle()) : 78 getContextForUserHandle(incomingCall.getAssociatedUser()); 79 Uri ringtoneUri = incomingCall.getRingtone(); 80 Ringtone ringtone = null; 81 82 if (ringtoneUri != null && userContext != null) { 83 // Ringtone URI is explicitly specified. First, try to create a Ringtone with that. 84 try { 85 ringtone = RingtoneManager.getRingtone( 86 userContext, ringtoneUri, volumeShaperConfig, audioAttrs); 87 } catch (Exception e) { 88 Log.e(this, e, "getRingtone: exception while getting ringtone."); 89 } 90 } 91 if (ringtone == null) { 92 // Contact didn't specify ringtone or custom Ringtone creation failed. Get default 93 // ringtone for user or profile. 94 Context contextToUse = hasDefaultRingtoneForUser(userContext) ? userContext : mContext; 95 Uri defaultRingtoneUri; 96 if (UserManager.get(contextToUse).isUserUnlocked(contextToUse.getUserId())) { 97 defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(contextToUse, 98 RingtoneManager.TYPE_RINGTONE); 99 if (defaultRingtoneUri == null) { 100 Log.i(this, "getRingtone: defaultRingtoneUri for user is null."); 101 } 102 } else { 103 defaultRingtoneUri = Settings.System.DEFAULT_RINGTONE_URI; 104 if (defaultRingtoneUri == null) { 105 Log.i(this, "getRingtone: Settings.System.DEFAULT_RINGTONE_URI is null."); 106 } 107 } 108 109 if (defaultRingtoneUri == null) { 110 return null; 111 } 112 113 try { 114 ringtone = RingtoneManager.getRingtone( 115 contextToUse, defaultRingtoneUri, volumeShaperConfig, audioAttrs); 116 } catch (Exception e) { 117 Log.e(this, e, "getRingtone: exception while getting ringtone."); 118 } 119 } 120 return ringtone; 121 } 122 getDefaultRingtoneAudioAttributes(boolean hapticChannelsMuted)123 private AudioAttributes getDefaultRingtoneAudioAttributes(boolean hapticChannelsMuted) { 124 return new AudioAttributes.Builder() 125 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) 126 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 127 .setHapticChannelsMuted(hapticChannelsMuted) 128 .build(); 129 } 130 131 /** Returns a ringtone to be used when ringer is not audible for the incoming call. */ 132 @Nullable getHapticOnlyRingtone()133 public Ringtone getHapticOnlyRingtone() { 134 // Initializing ringtones on the main thread can deadlock 135 ThreadUtil.checkNotOnMainThread(); 136 Uri ringtoneUri = Uri.parse("file://" + mContext.getString( 137 com.android.internal.R.string.config_defaultRingtoneVibrationSound)); 138 AudioAttributes audioAttrs = getDefaultRingtoneAudioAttributes( 139 /* hapticChannelsMuted */ false); 140 Ringtone ringtone = RingtoneManager.getRingtone( 141 mContext, ringtoneUri, /* volumeShaperConfig */ null, audioAttrs); 142 if (ringtone != null) { 143 // Make sure the sound is muted. 144 ringtone.setVolume(0); 145 } 146 return ringtone; 147 } 148 getWorkProfileContextForUser(UserHandle userHandle)149 private Context getWorkProfileContextForUser(UserHandle userHandle) { 150 // UserManager.getEnabledProfiles returns the enabled profiles along with the user's handle 151 // itself (so we must filter out the user). 152 List<UserInfo> profiles = UserManager.get(mContext).getEnabledProfiles( 153 userHandle.getIdentifier()); 154 UserInfo workprofile = null; 155 int managedProfileCount = 0; 156 for (UserInfo profile : profiles) { 157 UserHandle profileUserHandle = profile.getUserHandle(); 158 if (profileUserHandle != userHandle && profile.isManagedProfile()) { 159 managedProfileCount++; 160 workprofile = profile; 161 } 162 } 163 // There may be many different types of profiles, so only count Managed (Work) Profiles. 164 if(managedProfileCount == 1) { 165 return getContextForUserHandle(workprofile.getUserHandle()); 166 } 167 // There are multiple managed profiles for the associated user and we do not have enough 168 // info to determine which profile is the work profile. Just use the default. 169 return null; 170 } 171 getContextForUserHandle(UserHandle userHandle)172 private Context getContextForUserHandle(UserHandle userHandle) { 173 if(userHandle == null) { 174 return null; 175 } 176 try { 177 return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, userHandle); 178 } catch (PackageManager.NameNotFoundException e) { 179 Log.w("RingtoneFactory", "Package name not found: " + e.getMessage()); 180 } 181 return null; 182 } 183 hasDefaultRingtoneForUser(Context userContext)184 private boolean hasDefaultRingtoneForUser(Context userContext) { 185 if(userContext == null) { 186 return false; 187 } 188 return !TextUtils.isEmpty(Settings.System.getStringForUser(userContext.getContentResolver(), 189 Settings.System.RINGTONE, userContext.getUserId())); 190 } 191 isWorkContact(Call incomingCall)192 private boolean isWorkContact(Call incomingCall) { 193 CallerInfo contactCallerInfo = incomingCall.getCallerInfo(); 194 return (contactCallerInfo != null) && 195 (contactCallerInfo.userType == CallerInfo.USER_TYPE_WORK); 196 } 197 } 198