1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.launcher3.testcomponent; 17 18 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 20 import static android.content.pm.PackageManager.DONT_KILL_APP; 21 import static android.os.ParcelFileDescriptor.MODE_READ_WRITE; 22 23 import android.app.Activity; 24 import android.app.ActivityManager; 25 import android.app.Instrumentation; 26 import android.content.ComponentName; 27 import android.content.ContentProvider; 28 import android.content.ContentValues; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.Bundle; 32 import android.os.ParcelFileDescriptor; 33 import android.util.Base64; 34 35 import androidx.test.InstrumentationRegistry; 36 37 import com.android.launcher3.tapl.TestHelpers; 38 39 import java.io.File; 40 import java.io.FileNotFoundException; 41 import java.io.IOException; 42 43 /** 44 * Content provider to receive commands from tests 45 */ 46 public class TestCommandReceiver extends ContentProvider { 47 48 public static final String ENABLE_TEST_LAUNCHER = "enable-test-launcher"; 49 public static final String DISABLE_TEST_LAUNCHER = "disable-test-launcher"; 50 public static final String KILL_PROCESS = "kill-process"; 51 public static final String GET_SYSTEM_HEALTH_MESSAGE = "get-system-health-message"; 52 53 @Override onCreate()54 public boolean onCreate() { 55 return true; 56 } 57 58 @Override delete(Uri uri, String selection, String[] selectionArgs)59 public int delete(Uri uri, String selection, String[] selectionArgs) { 60 throw new UnsupportedOperationException("unimplemented mock method"); 61 } 62 63 @Override getType(Uri uri)64 public String getType(Uri uri) { 65 throw new UnsupportedOperationException("unimplemented mock method"); 66 } 67 68 @Override insert(Uri uri, ContentValues values)69 public Uri insert(Uri uri, ContentValues values) { 70 throw new UnsupportedOperationException("unimplemented mock method"); 71 } 72 73 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)74 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 75 String sortOrder) { 76 throw new UnsupportedOperationException("unimplemented mock method"); 77 } 78 79 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)80 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 81 throw new UnsupportedOperationException("unimplemented mock method"); 82 } 83 84 @Override call(String method, String arg, Bundle extras)85 public Bundle call(String method, String arg, Bundle extras) { 86 switch (method) { 87 case ENABLE_TEST_LAUNCHER: { 88 getContext().getPackageManager().setComponentEnabledSetting( 89 new ComponentName(getContext(), TestLauncherActivity.class), 90 COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); 91 return null; 92 } 93 case DISABLE_TEST_LAUNCHER: { 94 getContext().getPackageManager().setComponentEnabledSetting( 95 new ComponentName(getContext(), TestLauncherActivity.class), 96 COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); 97 return null; 98 } 99 case KILL_PROCESS: { 100 ((ActivityManager) getContext().getSystemService(Activity.ACTIVITY_SERVICE)). 101 killBackgroundProcesses(arg); 102 return null; 103 } 104 105 case GET_SYSTEM_HEALTH_MESSAGE: { 106 final Bundle response = new Bundle(); 107 response.putString("result", 108 TestHelpers.getSystemHealthMessage(getContext(), Long.parseLong(arg))); 109 return response; 110 } 111 } 112 return super.call(method, arg, extras); 113 } 114 callCommand(String command)115 public static Bundle callCommand(String command) { 116 return callCommand(command, null); 117 } 118 callCommand(String command, String arg)119 public static Bundle callCommand(String command, String arg) { 120 Instrumentation inst = InstrumentationRegistry.getInstrumentation(); 121 Uri uri = Uri.parse("content://" + inst.getContext().getPackageName() + ".commands"); 122 return inst.getTargetContext().getContentResolver().call(uri, command, arg, null); 123 } 124 125 @Override openFile(Uri uri, String mode)126 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 127 String path = Base64.encodeToString(uri.getPath().getBytes(), 128 Base64.NO_CLOSE | Base64.NO_PADDING | Base64.NO_WRAP); 129 File file = new File(getContext().getCacheDir(), path); 130 if (!file.exists()) { 131 // Create an empty file so that we can pass its descriptor 132 try { 133 file.createNewFile(); 134 } catch (IOException e) { 135 } 136 } 137 138 return ParcelFileDescriptor.open(file, MODE_READ_WRITE); 139 } 140 } 141