summaryrefslogtreecommitdiff
path: root/emulator/screen-sharing-agent/app/src/main/cpp/accessors/window_manager.cc
blob: 7cdb2b4430c21d823560e9ab0ecbfc5a3ece32a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "window_manager.h"

#include "accessors/display_manager.h"
#include "accessors/service_manager.h"
#include "jvm.h"
#include "log.h"

static screensharing::WindowManager* window_manager_instance_ = nullptr;

namespace screensharing {

using namespace std;

WindowManager::WindowManager(Jni jni)
    : window_manager_(ServiceManager::GetServiceAsInterface(jni, "window", "android/view/IWindowManager")),
      rotation_(),
      rotation_watchers_(new set<RotationWatcher*>()) {
  JClass window_manager_class(window_manager_.GetClass());
  int api_level = android_get_device_api_level();
  // The getDefaultDisplayRotation method was called getRotation before API 26.
  // See https://android.googlesource.com/platform/frameworks/base/+/5406e7ade87c33f70c83a283781dcc48fb67cdb9%5E%21/#F2.
  const char* method_name = api_level >= 26 ? "getDefaultDisplayRotation" : "getRotation";
  get_default_display_rotation_method_ = window_manager_class.GetMethodId(method_name, "()I");
  freeze_rotation_method_ = window_manager_class.GetMethodId("freezeRotation", "(I)V");
  thaw_rotation_method_ = window_manager_class.GetMethodId("thawRotation", "()V");
  is_rotation_frozen_method_ = window_manager_class.GetMethodId("isRotationFrozen", "()Z");
  // The second parameter was added in API 26.
  // See https://android.googlesource.com/platform/frameworks/base/+/35fa3c26adcb5f6577849fd0df5228b1f67cf2c6%5E%21/#F4.
  const char* signature = api_level >= 26 ? "(Landroid/view/IRotationWatcher;I)I" : "(Landroid/view/IRotationWatcher;)I";
  jmethodID watch_rotation_method_ = window_manager_class.GetMethodId("watchRotation", signature);
  JClass rotation_watcher_class = jni.GetClass("com/android/tools/screensharing/RotationWatcher");
  jmethodID rotation_watcher_constructor = rotation_watcher_class.GetConstructorId("()V");
  watcher_object_ = rotation_watcher_class.NewObject(rotation_watcher_constructor);
  rotation_ = api_level >= 26 ?
      window_manager_.CallIntMethod(watch_rotation_method_, watcher_object_.ref(), DEFAULT_DISPLAY) :
      window_manager_.CallIntMethod(watch_rotation_method_, watcher_object_.ref());
  window_manager_.MakeGlobal();
  watcher_object_.MakeGlobal();
}

WindowManager::~WindowManager() {
  Jni jni = Jvm::GetJni();
  JClass window_manager_class(window_manager_.GetClass(jni));
  jmethodID remove_rotation_watcher_method =
      window_manager_class.GetMethodId("removeRotationWatcher", "(Landroid/view/IRotationWatcher;)V");
  window_manager_.CallVoidMethod(jni, remove_rotation_watcher_method);
}

WindowManager& WindowManager::GetInstance(Jni jni) {
  if (window_manager_instance_ == nullptr) {
    window_manager_instance_ = new WindowManager(jni);
  }
  return *window_manager_instance_;
}

int WindowManager::GetDefaultDisplayRotation(Jni jni) {
  WindowManager& instance = GetInstance(jni);
  return instance.window_manager_.CallIntMethod(jni, instance.get_default_display_rotation_method_);
}

void WindowManager::FreezeRotation(Jni jni, int32_t rotation) {
  WindowManager& instance = GetInstance(jni);
  Log::D("WindowManager::FreezeRotation: setting display orientation to %d", rotation);
  instance.window_manager_.CallVoidMethod(jni, instance.freeze_rotation_method_, rotation);
}

void WindowManager::ThawRotation(Jni jni) {
  WindowManager& instance = GetInstance(jni);
  instance.window_manager_.CallVoidMethod(jni, instance.thaw_rotation_method_);
}

bool WindowManager::IsRotationFrozen(Jni jni) {
  WindowManager& instance = GetInstance(jni);
  return instance.window_manager_.CallBooleanMethod(jni, instance.is_rotation_frozen_method_);
}

int32_t WindowManager::WatchRotation(Jni jni, RotationWatcher* watcher) {
  WindowManager& instance = GetInstance(jni);
  for (;;) {
    auto old_watchers = instance.rotation_watchers_.load();
    auto new_watchers = new set<RotationWatcher*>(*old_watchers);
    if (new_watchers->insert(watcher).second && instance.rotation_watchers_.compare_exchange_strong(old_watchers, new_watchers)) {
      delete old_watchers;
      break;
    }
    delete new_watchers;
  }
  return instance.rotation_;
}

void WindowManager::RemoveRotationWatcher(Jni jni, RotationWatcher* watcher) {
  WindowManager& instance = GetInstance(jni);
  for (;;) {
    auto old_watchers = instance.rotation_watchers_.load();
    auto new_watchers = new set<RotationWatcher*>(*old_watchers);
    if (new_watchers->erase(watcher) != 0 && instance.rotation_watchers_.compare_exchange_strong(old_watchers, new_watchers)) {
      delete old_watchers;
      break;
    }
    delete new_watchers;
  }
}

void WindowManager::OnRotationChanged(int32_t rotation) {
  rotation_ = rotation;
  for (auto watcher : *rotation_watchers_.load()) {
    watcher->OnRotationChanged(rotation);
  }
}

}  // namespace screensharing

extern "C"
JNIEXPORT void JNICALL
Java_com_android_tools_screensharing_RotationWatcher_onRotationChanged(JNIEnv* jni_env, jobject thiz, jint rotation) {
  if (window_manager_instance_ != nullptr) {
    window_manager_instance_->OnRotationChanged(rotation);
  }
}