1 /* 2 * Copyright (C) 2016 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 package android.car.cts; 17 18 import android.car.Car; 19 import android.car.CarNotConnectedException; 20 import android.car.content.pm.CarPackageManager; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.os.Bundle; 25 import android.platform.test.annotations.RequiresDevice; 26 import android.test.suitebuilder.annotation.SmallTest; 27 28 import java.util.List; 29 30 @SmallTest 31 @RequiresDevice 32 public class CarPackageManagerTest extends CarApiTestBase { 33 34 private CarPackageManager mCarPm; 35 private static String TAG = CarPackageManagerTest.class.getSimpleName(); 36 37 /** Name of the meta-data attribute for the automotive application XML resource */ 38 private static final String METADATA_ATTRIBUTE = "android.car.application"; 39 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 mCarPm = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE); 45 } 46 47 public void testActivityDistractionOptimized() throws Exception { 48 assertFalse(mCarPm.isActivityDistractionOptimized("com.basic.package", "DummyActivity")); 49 // Real system activity is not allowed as well. 50 assertFalse(mCarPm.isActivityDistractionOptimized("com.android.phone", "CallActivity")); 51 52 try { 53 mCarPm.isActivityDistractionOptimized("com.android.settings", null); 54 fail(); 55 } catch (IllegalArgumentException expected) { 56 // Expected. 57 } 58 try { 59 mCarPm.isActivityDistractionOptimized(null, "Any"); 60 fail(); 61 } catch (IllegalArgumentException expected) { 62 // Expected. 63 } 64 try { 65 mCarPm.isActivityDistractionOptimized(null, null); 66 fail(); 67 } catch (IllegalArgumentException expected) { 68 // Expected. 69 } 70 } 71 72 public void testSystemActivitiesAllowed() throws CarNotConnectedException { 73 List<PackageInfo> packages = getContext().getPackageManager().getInstalledPackages( 74 PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); 75 76 for (PackageInfo info : packages) { 77 if (info.applicationInfo == null) { 78 continue; 79 } 80 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 || 81 ((info.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)) { 82 83 Bundle metaData = info.applicationInfo.metaData; 84 if (metaData == null || metaData.getInt(METADATA_ATTRIBUTE, 0) == 0) { 85 continue; // No car metadata, ignoring this app. 86 } 87 88 if (info.activities != null && info.activities.length > 0) { 89 String activity = info.activities[0].name; 90 String packageName = info.packageName; 91 assertTrue("Failed for package: " + packageName + ", activity: " + activity, 92 mCarPm.isActivityDistractionOptimized(packageName, activity)); 93 } 94 } 95 } 96 } 97 98 public void testServiceDistractionOptimized() throws Exception { 99 assertFalse(mCarPm.isServiceDistractionOptimized("com.basic.package", "")); 100 assertTrue(mCarPm.isServiceDistractionOptimized("com.android.settings", "Any")); 101 assertTrue(mCarPm.isServiceDistractionOptimized("com.android.settings", "")); 102 assertTrue(mCarPm.isServiceDistractionOptimized("com.android.settings", null)); 103 104 try { 105 mCarPm.isServiceDistractionOptimized(null, "Any"); 106 fail(); 107 } catch (IllegalArgumentException expected) { 108 // Expected. 109 } 110 } 111 112 } 113