1 | // Copyright (c) 2013 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.ui; |
6 | |
7 | import android.view.View; |
8 | |
9 | import org.chromium.base.JNINamespace; |
10 | import org.chromium.ui.ViewAndroidDelegate; |
11 | import org.chromium.ui.WindowAndroid; |
12 | |
13 | /** |
14 | * From the Chromium architecture point of view, ViewAndroid and its native counterpart |
15 | * serve purpose of representing Android view where Chrome expects to have a cross platform |
16 | * handle to the system view type. As Views are Java object on Android, this ViewAndroid |
17 | * and its native counterpart provide the expected abstractions on the C++ side and allow |
18 | * it to be flexibly glued to an actual Android Java View at runtime. |
19 | * |
20 | * It should only be used where access to Android Views is needed from the C++ code. |
21 | */ |
22 | @JNINamespace("ui") |
23 | public class ViewAndroid { |
24 | // Native pointer to the c++ ViewAndroid object. |
25 | private int mNativeViewAndroid = 0; |
26 | private final ViewAndroidDelegate mViewAndroidDelegate; |
27 | private final WindowAndroid mWindowAndroid; |
28 | private int mKeepScreenOnCount; |
29 | private View mKeepScreenOnView; |
30 | |
31 | /** |
32 | * Constructs a View object. |
33 | */ |
34 | public ViewAndroid(WindowAndroid nativeWindow, ViewAndroidDelegate viewAndroidDelegate) { |
35 | mWindowAndroid = nativeWindow; |
36 | mViewAndroidDelegate = viewAndroidDelegate; |
37 | mNativeViewAndroid = nativeInit(mWindowAndroid.getNativePointer()); |
38 | } |
39 | |
40 | public ViewAndroidDelegate getViewAndroidDelegate() { |
41 | return mViewAndroidDelegate; |
42 | } |
43 | |
44 | /** |
45 | * Destroys the c++ ViewAndroid object if one has been created. |
46 | */ |
47 | public void destroy() { |
48 | if (mNativeViewAndroid != 0) { |
49 | nativeDestroy(mNativeViewAndroid); |
50 | mNativeViewAndroid = 0; |
51 | } |
52 | } |
53 | |
54 | /** |
55 | * Returns a pointer to the c++ AndroidWindow object. |
56 | * @return A pointer to the c++ AndroidWindow. |
57 | */ |
58 | public int getNativePointer() { |
59 | return mNativeViewAndroid; |
60 | } |
61 | |
62 | /** |
63 | * Set KeepScreenOn flag. If the flag already set, increase mKeepScreenOnCount. |
64 | */ |
65 | public void incrementKeepScreenOnCount() { |
66 | mKeepScreenOnCount++; |
67 | if (mKeepScreenOnCount == 1) { |
68 | mKeepScreenOnView = mViewAndroidDelegate.acquireAnchorView(); |
69 | mKeepScreenOnView.setKeepScreenOn(true); |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * Decrease mKeepScreenOnCount, if it is decreased to 0, remove the flag. |
75 | */ |
76 | public void decrementKeepScreenOnCount() { |
77 | assert mKeepScreenOnCount > 0; |
78 | mKeepScreenOnCount--; |
79 | if (mKeepScreenOnCount == 0) { |
80 | mViewAndroidDelegate.releaseAnchorView(mKeepScreenOnView); |
81 | mKeepScreenOnView = null; |
82 | } |
83 | } |
84 | |
85 | private native int nativeInit(int windowPtr); |
86 | private native void nativeDestroy(int nativeViewAndroid); |
87 | } |