1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.vr;
6 
7 import android.app.Activity;
8 import android.app.PendingIntent;
9 import android.content.Intent;
10 
11 import com.google.vr.ndk.base.DaydreamApi;
12 import com.google.vr.ndk.base.GvrApi;
13 
14 import org.chromium.base.ContextUtils;
15 import org.chromium.base.StrictModeContext;
16 
17 /**
18  * A wrapper for DaydreamApi.
19  */
20 public class VrDaydreamApi {
21     private DaydreamApi mDaydreamApi;
22 
getDaydreamApi()23     private DaydreamApi getDaydreamApi() {
24         if (mDaydreamApi == null) {
25             mDaydreamApi = DaydreamApi.create(ContextUtils.getApplicationContext());
26         }
27         return mDaydreamApi;
28     }
29 
registerDaydreamIntent(final PendingIntent pendingIntent)30     public boolean registerDaydreamIntent(final PendingIntent pendingIntent) {
31         DaydreamApi daydreamApi = getDaydreamApi();
32         if (daydreamApi == null) return false;
33         daydreamApi.registerDaydreamIntent(pendingIntent);
34         return true;
35     }
36 
unregisterDaydreamIntent()37     public boolean unregisterDaydreamIntent() {
38         DaydreamApi daydreamApi = getDaydreamApi();
39         if (daydreamApi == null) return false;
40         daydreamApi.unregisterDaydreamIntent();
41         return true;
42     }
43 
launchInVr(final PendingIntent pendingIntent)44     public boolean launchInVr(final PendingIntent pendingIntent) {
45         DaydreamApi daydreamApi = getDaydreamApi();
46         if (daydreamApi == null) return false;
47         daydreamApi.launchInVr(pendingIntent);
48         return true;
49     }
50 
launchInVr(final Intent intent)51     public boolean launchInVr(final Intent intent) {
52         DaydreamApi daydreamApi = getDaydreamApi();
53         if (daydreamApi == null) return false;
54         daydreamApi.launchInVr(intent);
55         return true;
56     }
57 
exitFromVr(Activity activity, int requestCode, final Intent intent)58     public boolean exitFromVr(Activity activity, int requestCode, final Intent intent) {
59         DaydreamApi daydreamApi = getDaydreamApi();
60         if (daydreamApi == null) return false;
61         daydreamApi.exitFromVr(activity, requestCode, intent);
62         return true;
63     }
64 
isDaydreamCurrentViewer()65     public boolean isDaydreamCurrentViewer() {
66         DaydreamApi daydreamApi = getDaydreamApi();
67         if (daydreamApi == null) return false;
68         int type = GvrApi.ViewerType.CARDBOARD;
69         // If this is the first time any app reads the daydream config file, daydream may create its
70         // config directory... crbug.com/686104
71         try (StrictModeContext smc = StrictModeContext.allowDiskWrites()) {
72             type = daydreamApi.getCurrentViewerType();
73         } catch (RuntimeException ex) {
74             // TODO(mthiesse, b/110092501): Remove this exception handling once Daydream handles
75             // this exception.
76             // We occasionally get the hidden CursorWindowAllocationException here, presumably from
77             // being OOM when trying to check the current viewer type.
78             return false;
79         }
80         return type == GvrApi.ViewerType.DAYDREAM;
81     }
82 
launchVrHomescreen()83     public boolean launchVrHomescreen() {
84         DaydreamApi daydreamApi = getDaydreamApi();
85         if (daydreamApi == null) return false;
86         daydreamApi.launchVrHomescreen();
87         return true;
88     }
89 
close()90     public void close() {
91         if (mDaydreamApi == null) return;
92         mDaydreamApi.close();
93         mDaydreamApi = null;
94     }
95 }
96