blip  0.1
NativeActivity.hpp
Go to the documentation of this file.
1 #ifndef __blip_NativeActivity_hpp
2 #define __blip_NativeActivity_hpp
3 
4 #include <blip/Blip.hpp>
5 
6 #include <commonc++/Buffer.h++>
7 #include <commonc++/ConditionVar.h++>
8 #include <commonc++/JavaException.h++>
9 #include <commonc++/Mutex.h++>
10 #include <commonc++/Pipe.h++>
11 #include <commonc++/ScopedPtr.h++>
12 #include <commonc++/String.h++>
13 #include <commonc++/Thread.h++>
14 
15 CCXX_FWD_DECL(JavaVirtualMachine);
16 
17 #include <android/native_activity.h>
18 
19 struct AInputQueue;
20 struct ANativeActivity;
21 struct ANativeWindow;
22 
23 namespace blip {
24 
25 class AssetManager;
26 class DeviceConfiguration;
27 class Event;
28 class InputEventQueue;
29 class Intent;
30 class JavaGlue;
31 class KeyEvent;
32 class Looper;
33 class MotionEvent;
34 class Rectangle;
35 class RenderingContext;
36 class SensorEvent;
37 class SensorManager;
38 class StorageManager;
39 class TimerEvent;
40 class TimerManager;
41 class Window;
42 
44 typedef void* NativeContext;
45 
71 class NativeActivity : private ccxx::Thread
72 {
74  friend void blip_send_request(ANativeActivity*, NativeActivityRequest*);
75  friend void blip_main(ANativeActivity*, void*, size_t, NativeActivity*);
76  friend void blip_exit(NativeActivity*);
79  public:
80 
82  virtual ~NativeActivity();
83 
84  protected:
85 
91  NativeActivity(NativeContext context);
92 
94  virtual void run();
97  void sleep(timespan_ms_t msec);
98 
106  virtual void onCreate(ccxx::ByteBuffer* buffer);
107 
112  virtual void onStart();
113 
118  virtual void onResume();
119 
124  virtual void onPause();
125 
130  virtual void onStop();
131 
136  virtual void onDestroy();
137 
145  virtual void onSaveInstanceState(ccxx::ByteBuffer* buffer);
146 
152  virtual void onConfigurationChanged();
153 
157  void finish();
158 
166  virtual bool onKeyEvent(const KeyEvent& event);
167 
175  virtual bool onMotionEvent(const MotionEvent& event);
176 
185  virtual void onSensorEvent(const SensorEvent& event);
186 
194  virtual void onTimerEvent(const TimerEvent& event);
195 
201  virtual void onIdle() = 0;
202 
211  virtual void onWindowFocusChanged(bool hasFocus);
212 
220  virtual void onWindowCreated();
221 
228  virtual void onWindowDestroyed();
229 
236  virtual void onWindowResized();
237 
243  virtual void onWindowRedrawNeeded();
244 
252  virtual void onContentRectChanged(const Rectangle &rect);
253 
258  virtual void onLowMemory();
259 
264 
269  { return(_config.get()); }
270 
274  inline ccxx::JavaVirtualMachine* getJavaVirtualMachine()
275  { return(_jvm.get()); }
276 
281  { return(_renderingContext.get()); }
282 
288 
294 
300 
304  inline Window* getWindow()
305  { return(_window.get()); }
306 
310  inline const Intent& getIntent()
311  { return(*(_intent.get())); }
312 
320  Intent* newIntent();
321 
325  void startActivity(Intent* intent);
326 
330  ccxx::String getInternalDataPath() const;
331 
335  ccxx::String getExternalDataPath() const;
336 
342  void showSoftInput(bool forced = false);
343 
346  void hideSoftInput();
347 
348  private:
349 
350  void init();
351  void setInputEventQueue(AInputQueue* queue);
352  void deleteInputEventQueue();
353  void updateConfiguration();
354  void setWindow(ANativeWindow* window);
355  void deleteWindow();
356 
357  void postRequest(NativeActivityRequest* request);
358  bool processRequest();
359  void processTimers();
360 
361  ANativeActivity* _activity;
362  ccxx::ScopedPtr<InputEventQueue> _inputEventQueue;
363  ccxx::ScopedPtr<SensorManager> _sensorManager;
364  ccxx::ScopedPtr<DeviceConfiguration> _config;
365  ccxx::ScopedPtr<AssetManager> _assetManager;
366  ccxx::ScopedPtr<StorageManager> _storageManager;
367  ccxx::ScopedPtr<TimerManager> _timerManager;
368  ccxx::ScopedPtr<Window> _window;
369  ccxx::ScopedPtr<RenderingContext> _renderingContext;
370  ccxx::ScopedPtr<ccxx::JavaVirtualMachine> _jvm;
371  ccxx::ScopedPtr<JavaGlue> _java;
372  ccxx::ScopedPtr<Intent> _intent;
373  ccxx::ScopedPtr<Looper> _looper;
374  ccxx::Mutex _mutex;
375  ccxx::ConditionVar _cond;
376  ccxx::Pipe _eventPipe;
377  ccxx::Pipe _timerPipe;
378  NativeActivityRequest* _request;
379  byte_t* _savedState;
380  uint_t _savedStateSize;
381  bool _running;
382  bool _focused;
383 
384  CCXX_COPY_DECLS(NativeActivity);
385 };
386 
387 extern void blip_main(ANativeActivity* activity, void* savedState,
388  size_t savedStateSize, NativeActivity* object);
389 
390 } // namespace blip
391 
392 #define EXPORT_NATIVE_ACTIVITY(CLASS, FUNCTION) \
393  extern "C" { \
394  void FUNCTION(ANativeActivity* activity, \
395  void* savedState, \
396  size_t savedStateSize) \
397  { \
398  blip::blip_main(activity, savedState, savedStateSize, \
399  new CLASS((void*)activity)); \
400  } \
401  };
402 
403 #endif // __blip_NativeActivity_hpp
Definition: Intent.hpp:19
An asset manager, which provides access to the application&#39;s raw asset files.
Definition: AssetManager.hpp:34
virtual void onIdle()=0
Idle callback.
virtual void onWindowFocusChanged(bool hasFocus)
This method is called when the activity&#39;s window gains or loses input focus.
Definition: NativeActivity.cpp:307
A native window.
Definition: Window.hpp:19
virtual bool onKeyEvent(const KeyEvent &event)
This method is called when a KeyEvent is received.
Definition: NativeActivity.cpp:452
virtual void onWindowResized()
This method is called when the activity&#39;s window has changed size.
Definition: NativeActivity.cpp:323
A class that manages Timers and schedules TimerEvents for delivery when Timers fire.
Definition: TimerManager.hpp:27
ccxx::String getInternalDataPath() const
Get the filesystem path to application-private data in the device&#39;s internal storage (e...
Definition: NativeActivity.cpp:486
Definition: StorageManager.hpp:18
virtual void onContentRectChanged(const Rectangle &rect)
This method is called when the rectangle in the activity&#39;s window in which content should be placed h...
Definition: NativeActivity.cpp:347
virtual void onWindowRedrawNeeded()
This method is called when the activity&#39;s window needs to be redrawn.
Definition: NativeActivity.cpp:331
ccxx::String getExternalDataPath() const
Get the filesystem path to shared data in the device&#39;s external storage (e.g., removable flash media)...
Definition: NativeActivity.cpp:494
virtual void onDestroy()
This method is called when the activity is about to be destroyed.
Definition: NativeActivity.cpp:127
Window * getWindow()
Get the native Window for this activity.
Definition: NativeActivity.hpp:304
const Intent & getIntent()
Get the Intent that this activity was launched with.
Definition: NativeActivity.hpp:310
virtual void onPause()
This method is called when the activity is being paused.
Definition: NativeActivity.cpp:111
virtual void onResume()
This method is called when the activity is being resumed.
Definition: NativeActivity.cpp:103
virtual void onConfigurationChanged()
This method is called when the device configuration has changed.
Definition: NativeActivity.cpp:444
StorageManager * getStorageManager()
Get the StorageManager for this activity.
Definition: NativeActivity.cpp:386
void blip_main(ANativeActivity *activity, void *savedState, size_t savedStateSize, NativeActivity *object)
virtual bool onMotionEvent(const MotionEvent &event)
This method is called when a MotionEvent is received.
Definition: NativeActivity.cpp:461
The native rendering context.
Definition: RenderingContext.hpp:24
void finish()
Finish this activity.
Definition: NativeActivity.cpp:135
virtual void onSaveInstanceState(ccxx::ByteBuffer *buffer)
Called when the activity should save its instance state.
Definition: NativeActivity.cpp:299
An abstract base class for sensor events.
Definition: SensorEvent.hpp:20
virtual void onStart()
This method is called when the activity is being started.
Definition: NativeActivity.cpp:95
A container for device configuration information.
Definition: DeviceConfiguration.hpp:116
Intent * newIntent()
Construct a new Intent object.
Definition: NativeActivity.cpp:424
WAV file format details at: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/.
Definition: AccelerometerSensorEvent.cpp:3
SensorManager * getSensorManager()
Get the SensorManager for this activity.
Definition: NativeActivity.cpp:374
ccxx::JavaVirtualMachine * getJavaVirtualMachine()
Get the Java Virtual Machine for this activity.
Definition: NativeActivity.hpp:274
void startActivity(Intent *intent)
Start a new activity with the given intent.
Definition: NativeActivity.cpp:433
CCXX_FWD_DECL(JavaVirtualMachine)
virtual void onCreate(ccxx::ByteBuffer *buffer)
This method is called when the activity is being created.
Definition: NativeActivity.cpp:87
void * NativeContext
Definition: NativeActivity.hpp:43
An abstract base class for all "native" (C++) Android activities.
Definition: NativeActivity.hpp:71
A rectangle in cartesian space.
Definition: Rectangle.hpp:12
virtual void onWindowCreated()
This method is called when the activity&#39;s window has been created.
Definition: NativeActivity.cpp:315
virtual void onSensorEvent(const SensorEvent &event)
This method is called when a SensorEvent is received.
Definition: NativeActivity.cpp:470
RenderingContext * getRenderingContext()
Get the rendering context for this activity.
Definition: NativeActivity.hpp:280
virtual ~NativeActivity()
Destructor.
Definition: NativeActivity.cpp:79
A class that provides access to the hardware sensors.
Definition: SensorManager.hpp:23
void showSoftInput(bool forced=false)
Display the soft input method (e.g., a virtual on-screen keyboard).
Definition: NativeActivity.cpp:502
void hideSoftInput()
Hide the soft input method (e.g., a virtual on-screen keyboard).
Definition: NativeActivity.cpp:514
A keyboard input event.
Definition: KeyEvent.hpp:17
void sleep(timespan_ms_t msec)
Definition: NativeActivity.cpp:144
virtual void onLowMemory()
This method is called when the system is running low on memory.
Definition: NativeActivity.cpp:355
NativeActivity(NativeContext context)
Construct a new NativeActivity.
Definition: NativeActivity.cpp:58
A motion input event.
Definition: MotionEvent.hpp:22
TimerManager * getTimerManager()
Get the timer manager for this activity.
Definition: NativeActivity.cpp:397
AssetManager * getAssetManager()
Get the asset manager for this activity.
Definition: NativeActivity.cpp:363
A timer event.
Definition: TimerEvent.hpp:17
virtual void onWindowDestroyed()
This method is called when the activity&#39;s window is about to be destroyed.
Definition: NativeActivity.cpp:339
virtual void onTimerEvent(const TimerEvent &event)
This method is called when a TimerEvent has been received.
Definition: NativeActivity.cpp:478
virtual void onStop()
This method is called when the activity is being stopped.
Definition: NativeActivity.cpp:119
Definition: NativeActivityRequest.hpp:35
const DeviceConfiguration * getDeviceConfiguration()
Get the current device configuration.
Definition: NativeActivity.hpp:268