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.
5 package org.chromium.ui;
7 import android.view.View;
9 import org.chromium.base.JNINamespace;
10 import org.chromium.ui.ViewAndroidDelegate;
11 import org.chromium.ui.WindowAndroid;
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.
20 * It should only be used where access to Android Views is needed from the C++ code.
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;
32 * Constructs a View object.
34 public ViewAndroid(WindowAndroid nativeWindow, ViewAndroidDelegate viewAndroidDelegate) {
35 mWindowAndroid = nativeWindow;
36 mViewAndroidDelegate = viewAndroidDelegate;
37 mNativeViewAndroid = nativeInit(mWindowAndroid.getNativePointer());
40 public ViewAndroidDelegate getViewAndroidDelegate() {
41 return mViewAndroidDelegate;
45 * Destroys the c++ ViewAndroid object if one has been created.
47 public void destroy() {
48 if (mNativeViewAndroid != 0) {
49 nativeDestroy(mNativeViewAndroid);
50 mNativeViewAndroid = 0;
55 * Returns a pointer to the c++ AndroidWindow object.
56 * @return A pointer to the c++ AndroidWindow.
58 public int getNativePointer() {
59 return mNativeViewAndroid;
63 * Set KeepScreenOn flag. If the flag already set, increase mKeepScreenOnCount.
65 public void incrementKeepScreenOnCount() {
67 if (mKeepScreenOnCount == 1) {
68 mKeepScreenOnView = mViewAndroidDelegate.acquireAnchorView();
69 mKeepScreenOnView.setKeepScreenOn(true);
74 * Decrease mKeepScreenOnCount, if it is decreased to 0, remove the flag.
76 public void decrementKeepScreenOnCount() {
77 assert mKeepScreenOnCount > 0;
79 if (mKeepScreenOnCount == 0) {
80 mViewAndroidDelegate.releaseAnchorView(mKeepScreenOnView);
81 mKeepScreenOnView = null;
85 private native int nativeInit(int windowPtr);
86 private native void nativeDestroy(int nativeViewAndroid);