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 17 package android.server.displayservice; 18 19 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; 20 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION; 21 22 import android.app.Notification; 23 import android.app.NotificationChannel; 24 import android.app.NotificationManager; 25 import android.app.Service; 26 import android.content.Intent; 27 import android.graphics.PixelFormat; 28 import android.hardware.display.DisplayManager; 29 import android.hardware.display.VirtualDisplay; 30 import android.media.ImageReader; 31 import android.os.Bundle; 32 import android.os.IBinder; 33 import android.util.Log; 34 import android.view.Surface; 35 36 public class VirtualDisplayService extends Service { 37 private static final String NOTIFICATION_CHANNEL_ID = "cts/VirtualDisplayService"; 38 private static final String TAG = "VirtualDisplayService"; 39 40 private static final int FOREGROUND_ID = 1; 41 42 private static final int DENSITY = 160; 43 private static final int HEIGHT = 480; 44 private static final int WIDTH = 800; 45 46 private ImageReader mReader; 47 private VirtualDisplay mVirtualDisplay; 48 49 @Override 50 public void onCreate() { 51 super.onCreate(); 52 53 NotificationManager notificationManager = getSystemService(NotificationManager.class); 54 notificationManager.createNotificationChannel(new NotificationChannel( 55 NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, 56 NotificationManager.IMPORTANCE_DEFAULT)); 57 Notification notif = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID) 58 .setSmallIcon(android.R.drawable.ic_dialog_alert) 59 .build(); 60 startForeground(FOREGROUND_ID, notif); 61 } 62 63 @Override 64 public int onStartCommand(Intent intent, int flags, int startId) { 65 String command = intent.getStringExtra("command"); 66 Log.d(TAG, "Got command: " + command); 67 68 if ("create".equals(command)) { 69 createVirtualDisplay(intent); 70 } if ("off".equals(command)) { 71 mVirtualDisplay.setSurface(null); 72 } else if ("on".equals(command)) { 73 mVirtualDisplay.setSurface(mReader.getSurface()); 74 } else if ("destroy".equals(command)) { 75 destroyVirtualDisplay(); 76 stopSelf(); 77 } 78 79 return START_NOT_STICKY; 80 } 81 82 @Override 83 public IBinder onBind(Intent intent) { 84 return null; 85 } 86 87 private void createVirtualDisplay(Intent intent) { 88 mReader = ImageReader.newInstance(WIDTH, HEIGHT, PixelFormat.RGBA_8888, 2); 89 90 final DisplayManager displayManager = getSystemService(DisplayManager.class); 91 final String name = "CtsVirtualDisplay"; 92 93 int flags = VIRTUAL_DISPLAY_FLAG_PRESENTATION | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; 94 if (intent.getBooleanExtra("show_content_when_locked", false /* defaultValue */)) { 95 flags |= 1 << 5; // VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD 96 } 97 mVirtualDisplay = displayManager.createVirtualDisplay( 98 name, WIDTH, HEIGHT, DENSITY, mReader.getSurface(), flags); 99 } 100 101 private void destroyVirtualDisplay() { 102 mVirtualDisplay.release(); 103 mReader.close(); 104 } 105 } 106