1 /* 2 * Copyright 2019 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.car.settings.applications; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.car.userlib.CarUserManagerHelper; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.pm.UserInfo; 24 import android.telecom.DefaultDialerManager; 25 import android.text.TextUtils; 26 import android.util.ArraySet; 27 28 import com.android.internal.telephony.SmsApplication; 29 30 import java.util.List; 31 import java.util.Set; 32 33 /** Utility functions for use in applications settings. */ 34 public class ApplicationsUtils { 35 ApplicationsUtils()36 private ApplicationsUtils() { 37 } 38 39 /** 40 * Returns {@code true} if the package should always remain enabled. 41 */ 42 // TODO: investigate making this behavior configurable via a feature factory with the current 43 // contents as the default. isKeepEnabledPackage(Context context, String packageName)44 public static boolean isKeepEnabledPackage(Context context, String packageName) { 45 // Find current default phone/sms app. We should keep them enabled. 46 Set<String> keepEnabledPackages = new ArraySet<>(); 47 String defaultDialer = DefaultDialerManager.getDefaultDialerApplication(context); 48 if (!TextUtils.isEmpty(defaultDialer)) { 49 keepEnabledPackages.add(defaultDialer); 50 } 51 ComponentName defaultSms = SmsApplication.getDefaultSmsApplication( 52 context, /* updateIfNeeded= */ true); 53 if (defaultSms != null) { 54 keepEnabledPackages.add(defaultSms.getPackageName()); 55 } 56 return keepEnabledPackages.contains(packageName); 57 } 58 59 /** 60 * Returns {@code true} if the given {@code packageName} is device owner or profile owner of at 61 * least one user. 62 */ isProfileOrDeviceOwner(String packageName, DevicePolicyManager dpm, CarUserManagerHelper um)63 public static boolean isProfileOrDeviceOwner(String packageName, DevicePolicyManager dpm, 64 CarUserManagerHelper um) { 65 if (dpm.isDeviceOwnerAppOnAnyUser(packageName)) { 66 return true; 67 } 68 List<UserInfo> userInfos = um.getAllUsers(); 69 for (int i = 0; i < userInfos.size(); i++) { 70 ComponentName cn = dpm.getProfileOwnerAsUser(userInfos.get(i).id); 71 if (cn != null && cn.getPackageName().equals(packageName)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 } 78