Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 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; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 20 | import android.content.pm.PackageManager; |
| 21 | import android.content.pm.UserInfo; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 22 | import android.media.AudioManager; |
| 23 | import android.media.RingtoneManager; |
| 24 | import android.media.Ringtone; |
| 25 | import android.net.Uri; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 26 | import android.os.UserHandle; |
| 27 | import android.os.UserManager; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 28 | import android.provider.Settings; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 29 | |
| 30 | import android.telecom.Log; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 31 | import android.telecom.PhoneAccount; |
| 32 | import android.text.TextUtils; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.annotations.VisibleForTesting; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 35 | import com.android.internal.telephony.CallerInfo; |
| 36 | |
| 37 | import java.util.List; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 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. |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 43 | */ |
| 44 | @VisibleForTesting |
| 45 | public class RingtoneFactory { |
| 46 | |
| 47 | private final Context mContext; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 48 | private final CallsManager mCallsManager; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 49 | |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 50 | public RingtoneFactory(CallsManager callsManager, Context context) { |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 51 | mContext = context; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 52 | mCallsManager = callsManager; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 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; |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 62 | |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 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. |
Hall Liu | a4ae750 | 2017-01-12 17:28:48 -0800 | [diff] [blame] | 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); |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 82 | } |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 83 | if (ringtone != null) { |
| 84 | ringtone.setStreamType(AudioManager.STREAM_RING); |
| 85 | } |
| 86 | return ringtone; |
| 87 | } |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 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 | } |
Robin Lee | 46fdd98 | 2016-09-06 20:24:38 +0100 | [diff] [blame] | 128 | return !TextUtils.isEmpty(Settings.System.getStringForUser(userContext.getContentResolver(), |
| 129 | Settings.System.RINGTONE, userContext.getUserId())); |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 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 | } |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 137 | } |