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 java.io.File; 36 import java.io.FileNotFoundException; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 40 import androidx.test.InstrumentationRegistry; 41 42 /** 43 * Content provider to receive commands from tests 44 */ 45 public class TestCommandReceiver extends ContentProvider { 46 47 public static final String ENABLE_TEST_LAUNCHER = "enable-test-launcher"; 48 public static final String DISABLE_TEST_LAUNCHER = "disable-test-launcher"; 49 public static final String KILL_PROCESS = "kill-process"; 50 51 @Override onCreate()52 public boolean onCreate() { 53 return true; 54 } 55 56 @Override delete(Uri uri, String selection, String[] selectionArgs)57 public int delete(Uri uri, String selection, String[] selectionArgs) { 58 throw new UnsupportedOperationException("unimplemented mock method"); 59 } 60 61 @Override getType(Uri uri)62 public String getType(Uri uri) { 63 throw new UnsupportedOperationException("unimplemented mock method"); 64 } 65 66 @Override insert(Uri uri, ContentValues values)67 public Uri insert(Uri uri, ContentValues values) { 68 throw new UnsupportedOperationException("unimplemented mock method"); 69 } 70 71 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)72 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 73 String sortOrder) { 74 throw new UnsupportedOperationException("unimplemented mock method"); 75 } 76 77 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)78 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 79 throw new UnsupportedOperationException("unimplemented mock method"); 80 } 81 82 @Override call(String method, String arg, Bundle extras)83 public Bundle call(String method, String arg, Bundle extras) { 84 switch (method) { 85 case ENABLE_TEST_LAUNCHER: { 86 getContext().getPackageManager().setComponentEnabledSetting( 87 new ComponentName(getContext(), TestLauncherActivity.class), 88 COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); 89 return null; 90 } 91 case DISABLE_TEST_LAUNCHER: { 92 getContext().getPackageManager().setComponentEnabledSetting( 93 new ComponentName(getContext(), TestLauncherActivity.class), 94 COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); 95 return null; 96 } 97 case KILL_PROCESS: { 98 ((ActivityManager) getContext().getSystemService(Activity.ACTIVITY_SERVICE)). 99 killBackgroundProcesses(arg); 100 return null; 101 } 102 } 103 return super.call(method, arg, extras); 104 } 105 callCommand(String command)106 public static Bundle callCommand(String command) { 107 return callCommand(command, null); 108 } 109 callCommand(String command, String arg)110 public static Bundle callCommand(String command, String arg) { 111 Instrumentation inst = InstrumentationRegistry.getInstrumentation(); 112 Uri uri = Uri.parse("content://" + inst.getContext().getPackageName() + ".commands"); 113 return inst.getTargetContext().getContentResolver().call(uri, command, arg, null); 114 } 115 116 @Override openFile(Uri uri, String mode)117 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 118 String path = Base64.encodeToString(uri.getPath().getBytes(), 119 Base64.NO_CLOSE | Base64.NO_PADDING | Base64.NO_WRAP); 120 File file = new File(getContext().getCacheDir(), path); 121 if (!file.exists()) { 122 // Create an empty file so that we can pass its descriptor 123 try { 124 file.createNewFile(); 125 } catch (IOException e) { } 126 } 127 128 return ParcelFileDescriptor.open(file, MODE_READ_WRITE); 129 } 130 } 131