1 /* 2 * Copyright (C) 2017 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 com.android.cts.deviceadminservice; 17 18 import android.content.ComponentName; 19 import android.content.pm.PackageManager; 20 import android.test.AndroidTestCase; 21 import android.util.Log; 22 23 public class ComponentController extends AndroidTestCase { 24 private static final String TAG = "ComponentController"; 25 26 private void enableComponent(ComponentName cn, boolean enabled) { 27 getContext().getPackageManager().setComponentEnabledSetting(cn, 28 (enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 29 PackageManager.COMPONENT_ENABLED_STATE_DISABLED) 30 , PackageManager.DONT_KILL_APP); 31 Log.i(TAG, "setComponentEnabledSetting: " + cn + " enabled=" + enabled); 32 } 33 34 public void testEnableService1() { 35 enableComponent(new ComponentName(getContext(), MyService.class), true); 36 } 37 38 public void testDisableService1() { 39 enableComponent(new ComponentName(getContext(), MyService.class), false); 40 } 41 42 public void testEnableService2() { 43 enableComponent(new ComponentName(getContext(), MyService2.class), true); 44 } 45 46 public void testDisableService2() { 47 enableComponent(new ComponentName(getContext(), MyService2.class), false); 48 } 49 } 50