1 /* 2 * Copyright (C) 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 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 static com.android.launcher3.testcomponent.TestCommandReceiver.DISABLE_TEST_LAUNCHER; 24 import static com.android.launcher3.testcomponent.TestCommandReceiver.ENABLE_TEST_LAUNCHER; 25 import static com.android.launcher3.testcomponent.TestCommandReceiver.EXTRA_VALUE; 26 import static com.android.launcher3.testcomponent.TestCommandReceiver.GET_SYSTEM_HEALTH_MESSAGE; 27 import static com.android.launcher3.testcomponent.TestCommandReceiver.KILL_PROCESS; 28 import static com.android.launcher3.testcomponent.TestCommandReceiver.SET_LIST_VIEW_SERVICE_BINDER; 29 30 import android.app.Activity; 31 import android.app.ActivityManager; 32 import android.content.ComponentName; 33 import android.content.ContentProvider; 34 import android.content.ContentValues; 35 import android.database.Cursor; 36 import android.net.Uri; 37 import android.os.Bundle; 38 import android.os.ParcelFileDescriptor; 39 import android.util.Base64; 40 41 import com.android.launcher3.tapl.TestHelpers; 42 43 import java.io.File; 44 import java.io.FileNotFoundException; 45 import java.io.IOException; 46 47 public class TestCommandProvider extends ContentProvider { 48 49 @Override onCreate()50 public boolean onCreate() { 51 return true; 52 } 53 54 @Override delete(Uri uri, String selection, String[] selectionArgs)55 public int delete(Uri uri, String selection, String[] selectionArgs) { 56 throw new UnsupportedOperationException("unimplemented mock method"); 57 } 58 59 @Override getType(Uri uri)60 public String getType(Uri uri) { 61 throw new UnsupportedOperationException("unimplemented mock method"); 62 } 63 64 @Override insert(Uri uri, ContentValues values)65 public Uri insert(Uri uri, ContentValues values) { 66 throw new UnsupportedOperationException("unimplemented mock method"); 67 } 68 69 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)70 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 71 String sortOrder) { 72 throw new UnsupportedOperationException("unimplemented mock method"); 73 } 74 75 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)76 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 77 throw new UnsupportedOperationException("unimplemented mock method"); 78 } 79 80 @Override call(String method, String arg, Bundle extras)81 public Bundle call(String method, String arg, Bundle extras) { 82 switch (method) { 83 case ENABLE_TEST_LAUNCHER: { 84 getContext().getPackageManager().setComponentEnabledSetting( 85 new ComponentName(getContext(), TestLauncherActivity.class), 86 COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); 87 return null; 88 } 89 case DISABLE_TEST_LAUNCHER: { 90 getContext().getPackageManager().setComponentEnabledSetting( 91 new ComponentName(getContext(), TestLauncherActivity.class), 92 COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); 93 return null; 94 } 95 case KILL_PROCESS: { 96 ((ActivityManager) getContext().getSystemService(Activity.ACTIVITY_SERVICE)) 97 .killBackgroundProcesses(arg); 98 return null; 99 } 100 101 case GET_SYSTEM_HEALTH_MESSAGE: { 102 final Bundle response = new Bundle(); 103 response.putString("result", 104 TestHelpers.getSystemHealthMessage(getContext(), Long.parseLong(arg))); 105 return response; 106 } 107 108 case SET_LIST_VIEW_SERVICE_BINDER: { 109 ListViewService.sBinderForTest = extras.getBinder(EXTRA_VALUE); 110 return null; 111 } 112 } 113 return super.call(method, arg, extras); 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 129 return ParcelFileDescriptor.open(file, MODE_READ_WRITE); 130 } 131 } 132