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.
5 #include "ui/android/window_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/jni_weak_ref.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "jni/WindowAndroid_jni.h"
13 #include "ui/android/window_android_compositor.h"
14 #include "ui/android/window_android_observer.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::ScopedJavaLocalRef
;
21 WindowAndroid::WindowAndroid(JNIEnv
* env
, jobject obj
) : compositor_(NULL
) {
22 java_window_
.Reset(env
, obj
);
25 void WindowAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
29 ScopedJavaLocalRef
<jobject
> WindowAndroid::GetJavaObject() {
30 return base::android::ScopedJavaLocalRef
<jobject
>(java_window_
);
33 bool WindowAndroid::RegisterWindowAndroid(JNIEnv
* env
) {
34 return RegisterNativesImpl(env
);
37 WindowAndroid::~WindowAndroid() {
41 void WindowAndroid::OnCompositingDidCommit() {
42 FOR_EACH_OBSERVER(WindowAndroidObserver
,
44 OnCompositingDidCommit());
47 void WindowAndroid::AddObserver(WindowAndroidObserver
* observer
) {
48 if (!observer_list_
.HasObserver(observer
))
49 observer_list_
.AddObserver(observer
);
52 void WindowAndroid::RemoveObserver(WindowAndroidObserver
* observer
) {
53 observer_list_
.RemoveObserver(observer
);
56 void WindowAndroid::AttachCompositor(WindowAndroidCompositor
* compositor
) {
57 if (compositor_
&& compositor
!= compositor_
)
60 compositor_
= compositor
;
61 FOR_EACH_OBSERVER(WindowAndroidObserver
,
63 OnAttachCompositor());
66 void WindowAndroid::DetachCompositor() {
68 FOR_EACH_OBSERVER(WindowAndroidObserver
,
70 OnDetachCompositor());
71 observer_list_
.Clear();
74 void WindowAndroid::RequestVSyncUpdate() {
75 JNIEnv
* env
= AttachCurrentThread();
76 Java_WindowAndroid_requestVSyncUpdate(env
, GetJavaObject().obj());
79 void WindowAndroid::SetNeedsAnimate() {
81 compositor_
->SetNeedsAnimate();
84 void WindowAndroid::Animate(base::TimeTicks begin_frame_time
) {
86 WindowAndroidObserver
, observer_list_
, OnAnimate(begin_frame_time
));
89 void WindowAndroid::OnVSync(JNIEnv
* env
,
92 jlong period_micros
) {
93 base::TimeTicks
frame_time(base::TimeTicks::FromInternalValue(time_micros
));
94 base::TimeDelta
vsync_period(
95 base::TimeDelta::FromMicroseconds(period_micros
));
97 WindowAndroidObserver
,
99 OnVSync(frame_time
, vsync_period
));
101 compositor_
->OnVSync(frame_time
, vsync_period
);
104 void WindowAndroid::OnVisibilityChanged(JNIEnv
* env
,
107 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
,
108 OnRootWindowVisibilityChanged(visible
));
111 void WindowAndroid::OnActivityStopped(JNIEnv
* env
, jobject obj
) {
112 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
, OnActivityStopped());
115 void WindowAndroid::OnActivityStarted(JNIEnv
* env
, jobject obj
) {
116 FOR_EACH_OBSERVER(WindowAndroidObserver
, observer_list_
, OnActivityStarted());
119 bool WindowAndroid::HasPermission(const std::string
& permission
) {
120 JNIEnv
* env
= AttachCurrentThread();
121 return Java_WindowAndroid_hasPermission(
123 GetJavaObject().obj(),
124 base::android::ConvertUTF8ToJavaString(env
, permission
).obj());
127 bool WindowAndroid::CanRequestPermission(const std::string
& permission
) {
128 JNIEnv
* env
= AttachCurrentThread();
129 return Java_WindowAndroid_canRequestPermission(
131 GetJavaObject().obj(),
132 base::android::ConvertUTF8ToJavaString(env
, permission
).obj());
135 // ----------------------------------------------------------------------------
136 // Native JNI methods
137 // ----------------------------------------------------------------------------
139 jlong
Init(JNIEnv
* env
, jobject obj
) {
140 WindowAndroid
* window
= new WindowAndroid(env
, obj
);
141 return reinterpret_cast<intptr_t>(window
);