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