| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package android.telecom; |
| 16 |
|
| 17 |
import android.app.ActivityManager; |
| 18 |
import android.app.role.RoleManager; |
| 19 |
import android.content.Context; |
| 20 |
import android.content.Intent; |
| 21 |
import android.content.pm.ActivityInfo; |
| 22 |
import android.content.pm.PackageManager; |
| 23 |
import android.content.pm.ResolveInfo; |
| 24 |
import android.net.Uri; |
| 25 |
import android.os.AsyncTask; |
| 26 |
import android.os.Binder; |
| 27 |
import android.os.Process; |
| 28 |
import android.os.UserHandle; |
| 29 |
import android.text.TextUtils; |
| 30 |
import android.util.Slog; |
| 31 |
|
| 32 |
import com.android.internal.util.CollectionUtils; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
import java.util.concurrent.CompletableFuture; |
| 37 |
import java.util.concurrent.ExecutionException; |
| 38 |
import java.util.concurrent.TimeUnit; |
| 39 |
import java.util.concurrent.TimeoutException; |
| 40 |
import java.util.function.Consumer; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class DefaultDialerManager { |
| 49 |
private static final String TAG = "DefaultDialerManager"; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static boolean setDefaultDialerApplication(Context context, String packageName) { |
| 62 |
return setDefaultDialerApplication(context, packageName, ActivityManager.getCurrentUser()); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static boolean setDefaultDialerApplication(Context context, String packageName, |
| 76 |
int user) { |
| 77 |
final long identity = Binder.clearCallingIdentity(); |
| 78 |
try { |
| 79 |
CompletableFuture<Void> future = new CompletableFuture<>(); |
| 80 |
Consumer<Boolean> callback = successful -> { |
| 81 |
if (successful) { |
| 82 |
future.complete(null); |
| 83 |
} else { |
| 84 |
future.completeExceptionally(new RuntimeException()); |
| 85 |
} |
| 86 |
}; |
| 87 |
context.getSystemService(RoleManager.class).addRoleHolderAsUser( |
| 88 |
RoleManager.ROLE_DIALER, packageName, 0, UserHandle.of(user), |
| 89 |
AsyncTask.THREAD_POOL_EXECUTOR, callback); |
| 90 |
future.get(5, TimeUnit.SECONDS); |
| 91 |
return true; |
| 92 |
} catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 93 |
Slog.e(TAG, "Failed to set default dialer to " + packageName + " for user " + user, e); |
| 94 |
return false; |
| 95 |
} finally { |
| 96 |
Binder.restoreCallingIdentity(identity); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static String getDefaultDialerApplication(Context context) { |
| 114 |
return getDefaultDialerApplication(context, context.getUserId()); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static String getDefaultDialerApplication(Context context, int user) { |
| 131 |
final long identity = Binder.clearCallingIdentity(); |
| 132 |
try { |
| 133 |
return CollectionUtils.firstOrNull(context.getSystemService(RoleManager.class) |
| 134 |
.getRoleHoldersAsUser(RoleManager.ROLE_DIALER, UserHandle.of(user))); |
| 135 |
} finally { |
| 136 |
Binder.restoreCallingIdentity(identity); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static List<String> getInstalledDialerApplications(Context context, int userId) { |
| 152 |
PackageManager packageManager = context.getPackageManager(); |
| 153 |
|
| 154 |
|
| 155 |
Intent intent = new Intent(Intent.ACTION_DIAL); |
| 156 |
List<ResolveInfo> resolveInfoList = |
| 157 |
packageManager.queryIntentActivitiesAsUser(intent, 0, userId); |
| 158 |
|
| 159 |
List<String> packageNames = new ArrayList<>(); |
| 160 |
|
| 161 |
for (ResolveInfo resolveInfo : resolveInfoList) { |
| 162 |
final ActivityInfo activityInfo = resolveInfo.activityInfo; |
| 163 |
if (activityInfo != null |
| 164 |
&& !packageNames.contains(activityInfo.packageName) |
| 165 |
|
| 166 |
&& resolveInfo.targetUserId == UserHandle.USER_CURRENT) { |
| 167 |
packageNames.add(activityInfo.packageName); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
final Intent dialIntentWithTelScheme = new Intent(Intent.ACTION_DIAL); |
| 172 |
dialIntentWithTelScheme.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, "", null)); |
| 173 |
return filterByIntent(context, packageNames, dialIntentWithTelScheme, userId); |
| 174 |
} |
| 175 |
|
| 176 |
public static List<String> getInstalledDialerApplications(Context context) { |
| 177 |
return getInstalledDialerApplications(context, Process.myUserHandle().getIdentifier()); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public static boolean isDefaultOrSystemDialer(Context context, String packageName) { |
| 193 |
if (TextUtils.isEmpty(packageName)) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
final TelecomManager tm = getTelecomManager(context); |
| 197 |
return packageName.equals(tm.getDefaultDialerPackage()) |
| 198 |
|| packageName.equals(tm.getSystemDialerPackage()); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private static List<String> filterByIntent(Context context, List<String> packageNames, |
| 211 |
Intent intent, int userId) { |
| 212 |
if (packageNames == null || packageNames.isEmpty()) { |
| 213 |
return new ArrayList<>(); |
| 214 |
} |
| 215 |
|
| 216 |
final List<String> result = new ArrayList<>(); |
| 217 |
final List<ResolveInfo> resolveInfoList = context.getPackageManager() |
| 218 |
.queryIntentActivitiesAsUser(intent, 0, userId); |
| 219 |
final int length = resolveInfoList.size(); |
| 220 |
for (int i = 0; i < length; i++) { |
| 221 |
final ActivityInfo info = resolveInfoList.get(i).activityInfo; |
| 222 |
if (info != null && packageNames.contains(info.packageName) |
| 223 |
&& !result.contains(info.packageName)) { |
| 224 |
result.add(info.packageName); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return result; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
private static TelecomManager getTelecomManager(Context context) { |
| 233 |
return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); |
| 234 |
} |
| 235 |
} |