blob: b4523380806e29356382fde98e86cfd113f9c41b [file] [log] [blame]
Brad Ebingerd931a012015-10-21 12:54:08 -07001/*
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
17package com.android.server.telecom;
18
19import android.content.Context;
Brad Ebingerc9286f42016-03-10 16:02:54 -080020import android.content.pm.PackageManager;
21import android.content.pm.UserInfo;
Brad Ebingerd931a012015-10-21 12:54:08 -070022import android.media.AudioManager;
23import android.media.RingtoneManager;
24import android.media.Ringtone;
25import android.net.Uri;
Brad Ebingerc9286f42016-03-10 16:02:54 -080026import android.os.UserHandle;
27import android.os.UserManager;
Brad Ebingerd931a012015-10-21 12:54:08 -070028import android.provider.Settings;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070029
30import android.telecom.Log;
Brad Ebingerc9286f42016-03-10 16:02:54 -080031import android.telecom.PhoneAccount;
32import android.text.TextUtils;
Brad Ebingerd931a012015-10-21 12:54:08 -070033
34import com.android.internal.annotations.VisibleForTesting;
Brad Ebingerc9286f42016-03-10 16:02:54 -080035import com.android.internal.telephony.CallerInfo;
36
37import java.util.List;
Brad Ebingerd931a012015-10-21 12:54:08 -070038
39/**
Brad Ebingerc9286f42016-03-10 16:02:54 -080040 * 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 Ebingerd931a012015-10-21 12:54:08 -070043 */
44@VisibleForTesting
45public class RingtoneFactory {
46
47 private final Context mContext;
Brad Ebingerc9286f42016-03-10 16:02:54 -080048 private final CallsManager mCallsManager;
Brad Ebingerd931a012015-10-21 12:54:08 -070049
Brad Ebingerc9286f42016-03-10 16:02:54 -080050 public RingtoneFactory(CallsManager callsManager, Context context) {
Brad Ebingerd931a012015-10-21 12:54:08 -070051 mContext = context;
Brad Ebingerc9286f42016-03-10 16:02:54 -080052 mCallsManager = callsManager;
Brad Ebingerd931a012015-10-21 12:54:08 -070053 }
54
Brad Ebingerc9286f42016-03-10 16:02:54 -080055 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 Ebingerd931a012015-10-21 12:54:08 -070062
Brad Ebingerc9286f42016-03-10 16:02:54 -080063 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 Liua4ae7502017-01-12 17:28:48 -080070 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 Ebingerc9286f42016-03-10 16:02:54 -080082 }
Brad Ebingerd931a012015-10-21 12:54:08 -070083 if (ringtone != null) {
84 ringtone.setStreamType(AudioManager.STREAM_RING);
85 }
86 return ringtone;
87 }
Brad Ebingerc9286f42016-03-10 16:02:54 -080088
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 Lee46fdd982016-09-06 20:24:38 +0100128 return !TextUtils.isEmpty(Settings.System.getStringForUser(userContext.getContentResolver(),
129 Settings.System.RINGTONE, userContext.getUserId()));
Brad Ebingerc9286f42016-03-10 16:02:54 -0800130 }
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 Ebingerd931a012015-10-21 12:54:08 -0700137}