1 /* 2 * Copyright (C) 2018 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.app.role.RoleManager; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager; 23 import android.os.UserHandle; 24 import android.telecom.Log; 25 26 import com.android.internal.util.IndentingPrintWriter; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.concurrent.Executor; 31 import java.util.function.IntConsumer; 32 import java.util.stream.Collectors; 33 34 public class RoleManagerAdapterImpl implements RoleManagerAdapter { 35 private static final String ROLE_CALL_REDIRECTION_APP = RoleManager.ROLE_CALL_REDIRECTION; 36 private static final String ROLE_CALL_SCREENING = RoleManager.ROLE_CALL_SCREENING; 37 private static final String ROLE_DIALER = RoleManager.ROLE_DIALER; 38 39 private String mOverrideDefaultCallRedirectionApp = null; 40 private String mOverrideDefaultCallScreeningApp = null; 41 private String mOverrideDefaultDialerApp = null; 42 private List<String> mOverrideCallCompanionApps = new ArrayList<>(); 43 private Context mContext; 44 private RoleManager mRoleManager; 45 private UserHandle mCurrentUserHandle; 46 RoleManagerAdapterImpl(Context context, RoleManager roleManager)47 public RoleManagerAdapterImpl(Context context, RoleManager roleManager) { 48 mContext = context; 49 mRoleManager = roleManager; 50 } 51 52 @Override getDefaultCallRedirectionApp()53 public String getDefaultCallRedirectionApp() { 54 if (mOverrideDefaultCallRedirectionApp != null) { 55 return mOverrideDefaultCallRedirectionApp; 56 } 57 return getRoleManagerCallRedirectionApp(); 58 } 59 60 @Override setTestDefaultCallRedirectionApp(String packageName)61 public void setTestDefaultCallRedirectionApp(String packageName) { 62 mOverrideDefaultCallRedirectionApp = packageName; 63 } 64 65 @Override getDefaultCallScreeningApp()66 public String getDefaultCallScreeningApp() { 67 if (mOverrideDefaultCallScreeningApp != null) { 68 return mOverrideDefaultCallScreeningApp; 69 } 70 return getRoleManagerCallScreeningApp(); 71 } 72 73 @Override setTestDefaultCallScreeningApp(String packageName)74 public void setTestDefaultCallScreeningApp(String packageName) { 75 mOverrideDefaultCallScreeningApp = packageName; 76 } 77 78 @Override getDefaultDialerApp(int user)79 public String getDefaultDialerApp(int user) { 80 if (mOverrideDefaultDialerApp != null) { 81 return mOverrideDefaultDialerApp; 82 } 83 return getRoleManagerDefaultDialerApp(user); 84 } 85 86 @Override observeDefaultDialerApp(Executor executor, IntConsumer observer)87 public void observeDefaultDialerApp(Executor executor, IntConsumer observer) { 88 mRoleManager.addOnRoleHoldersChangedListenerAsUser(executor, (roleName, user) -> 89 observer.accept(user.getIdentifier()), UserHandle.ALL); 90 } 91 92 @Override setTestDefaultDialer(String packageName)93 public void setTestDefaultDialer(String packageName) { 94 mOverrideDefaultDialerApp = packageName; 95 } 96 97 @Override getCallCompanionApps()98 public List<String> getCallCompanionApps() { 99 List<String> callCompanionApps = new ArrayList<>(); 100 callCompanionApps.addAll(mOverrideCallCompanionApps); 101 return callCompanionApps; 102 } 103 104 @Override addOrRemoveTestCallCompanionApp(String packageName, boolean isAdded)105 public void addOrRemoveTestCallCompanionApp(String packageName, boolean isAdded) { 106 if (isAdded) { 107 mOverrideCallCompanionApps.add(packageName); 108 } else { 109 mOverrideCallCompanionApps.remove(packageName); 110 } 111 } 112 113 @Override setCurrentUserHandle(UserHandle currentUserHandle)114 public void setCurrentUserHandle(UserHandle currentUserHandle) { 115 mCurrentUserHandle = currentUserHandle; 116 } 117 getRoleManagerCallScreeningApp()118 private String getRoleManagerCallScreeningApp() { 119 List<String> roleHolders = mRoleManager.getRoleHoldersAsUser(ROLE_CALL_SCREENING, 120 mCurrentUserHandle); 121 if (roleHolders == null || roleHolders.isEmpty()) { 122 return null; 123 } 124 return roleHolders.get(0); 125 } 126 getRoleManagerDefaultDialerApp(int user)127 private String getRoleManagerDefaultDialerApp(int user) { 128 List<String> roleHolders = mRoleManager.getRoleHoldersAsUser(ROLE_DIALER, 129 new UserHandle(user)); 130 if (roleHolders == null || roleHolders.isEmpty()) { 131 return null; 132 } 133 return roleHolders.get(0); 134 } 135 136 // TODO in R: Use companion app manager getRoleManagerCallCompanionApps()137 private List<String> getRoleManagerCallCompanionApps() { 138 return new ArrayList<>(); 139 } 140 getRoleManagerCallRedirectionApp()141 private String getRoleManagerCallRedirectionApp() { 142 List<String> roleHolders = mRoleManager.getRoleHoldersAsUser(ROLE_CALL_REDIRECTION_APP, 143 mCurrentUserHandle); 144 if (roleHolders == null || roleHolders.isEmpty()) { 145 return null; 146 } 147 return roleHolders.get(0); 148 } 149 150 /** 151 * Returns the application label that corresponds to the given package name 152 * 153 * @param packageName A valid package name. 154 * 155 * @return Application label for the given package name, or null if not found. 156 */ 157 @Override getApplicationLabelForPackageName(String packageName)158 public String getApplicationLabelForPackageName(String packageName) { 159 PackageManager pm = mContext.getPackageManager(); 160 ApplicationInfo info = null; 161 try { 162 info = pm.getApplicationInfo(packageName, 0); 163 } catch (PackageManager.NameNotFoundException e) { 164 Log.d(this, "Application info not found for packageName " + packageName); 165 } 166 if (info == null) { 167 return packageName; 168 } else { 169 return info.loadLabel(pm).toString(); 170 } 171 } 172 173 /** 174 * Dumps the state of the {@link InCallController}. 175 * 176 * @param pw The {@code IndentingPrintWriter} to write the state to. 177 */ dump(IndentingPrintWriter pw)178 public void dump(IndentingPrintWriter pw) { 179 pw.print("DefaultCallRedirectionApp: "); 180 if (mOverrideDefaultCallRedirectionApp != null) { 181 pw.print("(override "); 182 pw.print(mOverrideDefaultCallRedirectionApp); 183 pw.print(") "); 184 pw.print(getRoleManagerCallRedirectionApp()); 185 } 186 pw.println(); 187 188 pw.print("DefaultCallScreeningApp: "); 189 if (mOverrideDefaultCallScreeningApp != null) { 190 pw.print("(override "); 191 pw.print(mOverrideDefaultCallScreeningApp); 192 pw.print(") "); 193 pw.print(getRoleManagerCallScreeningApp()); 194 } 195 pw.println(); 196 197 pw.print("DefaultCallCompanionApps: "); 198 if (mOverrideCallCompanionApps != null) { 199 pw.print("(override "); 200 pw.print(mOverrideCallCompanionApps.stream().collect(Collectors.joining(", "))); 201 pw.print(") "); 202 List<String> appsInRole = getRoleManagerCallCompanionApps(); 203 if (appsInRole != null) { 204 pw.print(appsInRole.stream().collect(Collectors.joining(", "))); 205 } 206 } 207 pw.println(); 208 } 209 } 210