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 static android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION; 20 21 import android.app.admin.DevicePolicyManager; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.pm.UserInfo; 25 import android.provider.DeviceConfig; 26 import android.telecom.DefaultDialerManager; 27 import android.text.TextUtils; 28 import android.util.ArraySet; 29 30 import com.android.car.settings.profiles.ProfileHelper; 31 import com.android.internal.telephony.SmsApplication; 32 33 import java.util.List; 34 import java.util.Set; 35 36 /** Utility functions for use in applications settings. */ 37 public class ApplicationsUtils { 38 39 /** Whether or not app hibernation is enabled on the device **/ 40 public static final String PROPERTY_APP_HIBERNATION_ENABLED = "app_hibernation_enabled"; 41 ApplicationsUtils()42 private ApplicationsUtils() { 43 } 44 45 /** 46 * Returns {@code true} if the package should always remain enabled. 47 */ 48 // TODO: investigate making this behavior configurable via a feature factory with the current 49 // contents as the default. isKeepEnabledPackage(Context context, String packageName)50 public static boolean isKeepEnabledPackage(Context context, String packageName) { 51 // Find current default phone/sms app. We should keep them enabled. 52 Set<String> keepEnabledPackages = new ArraySet<>(); 53 String defaultDialer = DefaultDialerManager.getDefaultDialerApplication(context); 54 if (!TextUtils.isEmpty(defaultDialer)) { 55 keepEnabledPackages.add(defaultDialer); 56 } 57 ComponentName defaultSms = SmsApplication.getDefaultSmsApplication( 58 context, /* updateIfNeeded= */ true); 59 if (defaultSms != null) { 60 keepEnabledPackages.add(defaultSms.getPackageName()); 61 } 62 return keepEnabledPackages.contains(packageName); 63 } 64 65 /** 66 * Returns {@code true} if the given {@code packageName} is device owner or profile owner of at 67 * least one user. 68 */ isProfileOrDeviceOwner(String packageName, DevicePolicyManager dpm, ProfileHelper profileHelper)69 public static boolean isProfileOrDeviceOwner(String packageName, DevicePolicyManager dpm, 70 ProfileHelper profileHelper) { 71 if (dpm.isDeviceOwnerAppOnAnyUser(packageName)) { 72 return true; 73 } 74 List<UserInfo> userInfos = profileHelper.getAllProfiles(); 75 for (int i = 0; i < userInfos.size(); i++) { 76 ComponentName cn = dpm.getProfileOwnerAsUser(userInfos.get(i).id); 77 if (cn != null && cn.getPackageName().equals(packageName)) { 78 return true; 79 } 80 } 81 return false; 82 } 83 84 /** 85 * Returns {@code true} if the hibernation feature is enabled, as configured through {@link 86 * DeviceConfig}, which can be overridden remotely with a flag or through adb. 87 */ isHibernationEnabled()88 public static boolean isHibernationEnabled() { 89 return DeviceConfig.getBoolean( 90 NAMESPACE_APP_HIBERNATION, PROPERTY_APP_HIBERNATION_ENABLED, true); 91 } 92 } 93