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 com.android.commands.vr;
18 
19 import android.app.Vr2dDisplayProperties;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 
24 import android.service.vr.IVrManager;
25 import com.android.internal.os.BaseCommand;
26 
27 import java.io.PrintStream;
28 
29 public final class Vr extends BaseCommand {
30 
31     /**
32      * Command-line entry point.
33      *
34      * @param args The command-line arguments
35      */
main(String[] args)36     public static void main(String[] args) {
37       (new Vr()).run(args);
38     }
39 
40     private static final String COMMAND_SET_PERSISTENT_VR_MODE_ENABLED =
41         "set-persistent-vr-mode-enabled";
42     private static final String COMMAND_SET_VR2D_DISPLAY_PROPERTIES =
43         "set-display-props";
44     private static final String COMMAND_ENABLE_VD = "enable-virtual-display";
45 
46     private IVrManager mVrService;
47 
48     @Override
onShowUsage(PrintStream out)49     public void onShowUsage(PrintStream out) {
50         out.println(
51                 "usage: vr [subcommand]\n" +
52                 "usage: vr set-persistent-vr-mode-enabled [true|false]\n" +
53                 "usage: vr set-display-props [width] [height] [dpi]\n" +
54                 "usage: vr enable-virtual-display [true|false]\n"
55                 );
56     }
57 
58     @Override
onRun()59     public void onRun() throws Exception {
60         mVrService = IVrManager.Stub.asInterface(ServiceManager.getService(Context.VR_SERVICE));
61         if (mVrService == null) {
62             showError("Error: Could not access the Vr Manager. Is the system running?");
63             return;
64         }
65 
66         String command = nextArgRequired();
67         switch (command) {
68             case COMMAND_SET_VR2D_DISPLAY_PROPERTIES:
69                 runSetVr2dDisplayProperties();
70                 break;
71             case COMMAND_SET_PERSISTENT_VR_MODE_ENABLED:
72                 runSetPersistentVrModeEnabled();
73                 break;
74             case COMMAND_ENABLE_VD:
75                 runEnableVd();
76                 break;
77             default:
78                 throw new IllegalArgumentException ("unknown command '" + command + "'");
79         }
80     }
81 
runSetVr2dDisplayProperties()82     private void runSetVr2dDisplayProperties() throws RemoteException {
83         String widthStr = nextArgRequired();
84         int width = Integer.parseInt(widthStr);
85 
86         String heightStr = nextArgRequired();
87         int height = Integer.parseInt(heightStr);
88 
89         String dpiStr = nextArgRequired();
90         int dpi = Integer.parseInt(dpiStr);
91 
92         Vr2dDisplayProperties vr2dDisplayProperties =
93                 new Vr2dDisplayProperties(width, height, dpi);
94 
95         try {
96             mVrService.setVr2dDisplayProperties(vr2dDisplayProperties);
97         } catch (RemoteException re) {
98             System.err.println("Error: Can't set persistent mode " + re);
99         }
100     }
101 
runEnableVd()102     private void runEnableVd() throws RemoteException {
103         Vr2dDisplayProperties.Builder builder = new Vr2dDisplayProperties.Builder();
104 
105         String value = nextArgRequired();
106         if ("true".equals(value)) {
107             builder.setEnabled(true);
108         } else if ("false".equals(value)) {
109             builder.setEnabled(false);
110         } // Don't do anything if not exactly true/false
111 
112         try {
113             mVrService.setVr2dDisplayProperties(builder.build());
114         } catch (RemoteException re) {
115             System.err.println("Error: Can't enable (" + value +") virtual display" + re);
116         }
117     }
118 
runSetPersistentVrModeEnabled()119     private void runSetPersistentVrModeEnabled() throws RemoteException {
120         String enableStr = nextArg();
121         boolean enabled = Boolean.parseBoolean(enableStr);
122         try {
123             mVrService.setPersistentVrModeEnabled(enabled);
124         } catch (RemoteException re) {
125             System.err.println("Error: Can't set persistent mode " + re);
126         }
127     }
128 }
129