Flutter Engine
The Flutter Engine
native_activity.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#include "flutter/testing/android/native_activity/native_activity.h"
6
7#include "flutter/fml/message_loop.h"
8
9namespace flutter {
10
11NativeActivity::NativeActivity(ANativeActivity* activity)
12 : activity_(activity) {
14
15 activity->instance = this;
16
17 activity->callbacks->onStart = [](ANativeActivity* activity) {
18 reinterpret_cast<NativeActivity*>(activity->instance)->OnStart();
19 };
20 activity->callbacks->onStop = [](ANativeActivity* activity) {
21 reinterpret_cast<NativeActivity*>(activity->instance)->OnStop();
22 };
23 activity->callbacks->onPause = [](ANativeActivity* activity) {
24 reinterpret_cast<NativeActivity*>(activity->instance)->OnPause();
25 };
26 activity->callbacks->onResume = [](ANativeActivity* activity) {
27 reinterpret_cast<NativeActivity*>(activity->instance)->OnResume();
28 };
29 activity->callbacks->onDestroy = [](ANativeActivity* activity) {
30 delete reinterpret_cast<NativeActivity*>(activity->instance);
31 };
32 activity->callbacks->onSaveInstanceState = [](ANativeActivity* activity,
33 size_t* out_size) -> void* {
34 auto mapping = reinterpret_cast<NativeActivity*>(activity->instance)
36 if (mapping == nullptr || mapping->GetMapping() == nullptr) {
37 *out_size = 0;
38 return nullptr;
39 }
40
41 // This will be `free`d by the framework.
42 auto copied = malloc(mapping->GetSize());
43 FML_CHECK(copied != nullptr)
44 << "Allocation failure while saving instance state.";
45 memcpy(copied, mapping->GetMapping(), mapping->GetSize());
46 *out_size = mapping->GetSize();
47 return copied;
48 };
49 activity->callbacks->onWindowFocusChanged = [](ANativeActivity* activity,
50 int has_focus) {
51 reinterpret_cast<NativeActivity*>(activity->instance)
52 ->OnWindowFocusChanged(has_focus);
53 };
54 activity->callbacks->onNativeWindowCreated = [](ANativeActivity* activity,
56 reinterpret_cast<NativeActivity*>(activity->instance)
58 };
59 activity->callbacks->onNativeWindowResized = [](ANativeActivity* activity,
61 reinterpret_cast<NativeActivity*>(activity->instance)
63 };
64 activity->callbacks->onNativeWindowRedrawNeeded =
65 [](ANativeActivity* activity, ANativeWindow* window) {
66 reinterpret_cast<NativeActivity*>(activity->instance)
68 };
69 activity->callbacks->onNativeWindowDestroyed = [](ANativeActivity* activity,
71 reinterpret_cast<NativeActivity*>(activity->instance)
73 };
74 activity->callbacks->onInputQueueCreated = [](ANativeActivity* activity,
75 AInputQueue* queue) {
76 reinterpret_cast<NativeActivity*>(activity->instance)
78 };
79 activity->callbacks->onInputQueueDestroyed = [](ANativeActivity* activity,
80 AInputQueue* queue) {
81 reinterpret_cast<NativeActivity*>(activity->instance)
83 };
84 activity->callbacks->onConfigurationChanged = [](ANativeActivity* activity) {
85 reinterpret_cast<NativeActivity*>(activity->instance)
87 };
88 activity->callbacks->onLowMemory = [](ANativeActivity* activity) {
89 reinterpret_cast<NativeActivity*>(activity->instance)->OnLowMemory();
90 };
91}
92
94
96
98
100
102
103std::shared_ptr<fml::Mapping> NativeActivity::OnSaveInstanceState() {
104 return nullptr;
105}
106
108
110
112
114
116
118
120
122
124
126 ANativeActivity_finish(activity_);
127}
128
129} // namespace flutter
130
131extern "C" __attribute__((visibility("default"))) void ANativeActivity_onCreate(
132 ANativeActivity* activity,
135 std::unique_ptr<fml::Mapping> saved_state_mapping;
137 saved_state_mapping = std::make_unique<fml::MallocMapping>(
139 }
140 flutter::NativeActivityMain(activity, std::move(saved_state_mapping))
141 .release(); // Will be freed when the frame calls the onDestroy. See the
142 // delete in that callback.
143}
An instance of a native activity. Users of the native_activity_apk are meant to subclass this and ret...
virtual void OnNativeWindowCreated(ANativeWindow *window)
virtual void OnNativeWindowResized(ANativeWindow *window)
void Terminate()
Perform graceful termination of the activity. Will eventually lead to the other activity lifecycle ca...
virtual void OnInputQueueDestroyed(AInputQueue *queue)
virtual void OnNativeWindowRedrawNeeded(ANativeWindow *window)
virtual void OnNativeWindowDestroyed(ANativeWindow *window)
NativeActivity(ANativeActivity *activity)
virtual void OnInputQueueCreated(AInputQueue *queue)
virtual void OnConfigurationChanged()
virtual std::shared_ptr< fml::Mapping > OnSaveInstanceState()
virtual void OnWindowFocusChanged(bool has_focus)
static MallocMapping Copy(const T *begin, const T *end)
Definition: mapping.h:162
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
GLFWwindow * window
Definition: main.cc:45
VkQueue queue
Definition: main.cc:55
#define FML_CHECK(condition)
Definition: logging.h:85
void * malloc(size_t size)
Definition: allocation.cc:19
std::unique_ptr< NativeActivity > NativeActivityMain(ANativeActivity *activity, std::unique_ptr< fml::Mapping > saved_state)
__attribute__((visibility("default"))) void ANativeActivity_onCreate(ANativeActivity *activity
void size_t saved_state_size
void * saved_state
struct ANativeWindow ANativeWindow