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