Merge pull request #5095 from koying/fixdroidappcrash
[vuplus_xbmc] / xbmc / android / activity / XBMCApp.h
1 #pragma once
2 /*
3  *      Copyright (C) 2012-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <math.h>
23 #include <pthread.h>
24 #include <string>
25 #include <vector>
26
27 #include <android/native_activity.h>
28
29 #include "IActivityHandler.h"
30 #include "IInputHandler.h"
31
32 #include "xbmc.h"
33 #include "android/jni/Context.h"
34 #include "android/jni/BroadcastReceiver.h"
35 #include "threads/Event.h"
36
37 // forward delares
38 class CJNIWakeLock;
39 class CAESinkAUDIOTRACK;
40 typedef struct _JNIEnv JNIEnv;
41
42 struct androidIcon
43 {
44   unsigned int width;
45   unsigned int height;
46   void *pixels;
47 };  
48
49 struct androidPackage
50 {
51   std::string packageName;
52   std::string packageLabel;
53 };
54
55
56 class CXBMCApp : public IActivityHandler, public CJNIContext, public CJNIBroadcastReceiver
57 {
58 public:
59   CXBMCApp(ANativeActivity *nativeActivity);
60   virtual ~CXBMCApp();
61   virtual void onReceive(CJNIIntent intent);
62   virtual void onNewIntent(CJNIIntent intent);
63
64   bool isValid() { return m_activity != NULL; }
65
66   void onStart();
67   void onResume();
68   void onPause();
69   void onStop();
70   void onDestroy();
71
72   void onSaveState(void **data, size_t *size);
73   void onConfigurationChanged();
74   void onLowMemory();
75
76   void onCreateWindow(ANativeWindow* window);
77   void onResizeWindow();
78   void onDestroyWindow();
79   void onGainFocus();
80   void onLostFocus();
81
82
83   static const ANativeWindow** GetNativeWindow(int timeout);
84   static int SetBuffersGeometry(int width, int height, int format);
85   static int android_printf(const char *format, ...);
86   
87   static int GetBatteryLevel();
88   static bool StartActivity(const std::string &package, const std::string &intent = std::string(), const std::string &dataType = std::string(), const std::string &dataURI = std::string());
89   static std::vector <androidPackage> GetApplications();
90   static bool GetIconSize(const std::string &packageName, int *width, int *height);
91   static bool GetIcon(const std::string &packageName, void* buffer, unsigned int bufSize); 
92
93   /*!
94    * \brief If external storage is available, it returns the path for the external storage (for the specified type)
95    * \param path will contain the path of the external storage (for the specified type)
96    * \param type optional type. Possible values are "", "files", "music", "videos", "pictures", "photos, "downloads"
97    * \return true if external storage is available and a valid path has been stored in the path parameter
98    */
99   static bool GetExternalStorage(std::string &path, const std::string &type = "");
100   static bool GetStorageUsage(const std::string &path, std::string &usage);
101   static int GetMaxSystemVolume();
102   static int GetSystemVolume();
103   static void SetSystemVolume(int val);
104
105   static int GetDPI();
106 protected:
107   // limit who can access Volume
108   friend class CAESinkAUDIOTRACK;
109
110   static int GetMaxSystemVolume(JNIEnv *env);
111   static void SetSystemVolume(JNIEnv *env, float percent);
112
113 private:
114   static bool HasLaunchIntent(const std::string &package);
115   bool getWakeLock();
116   std::string GetFilenameFromIntent(const CJNIIntent &intent);
117   void run();
118   void stop();
119   void SetupEnv();
120   static ANativeActivity *m_activity;
121   CJNIWakeLock *m_wakeLock;
122   static int m_batteryLevel;  
123   static int m_initialVolume;  
124   bool m_firstrun;
125   bool m_exiting;
126   pthread_t m_thread;
127   static CCriticalSection m_applicationsMutex;
128   static std::vector<androidPackage> m_applications;
129
130   static ANativeWindow* m_window;
131   static CEvent m_windowCreated;
132
133   void XBMC_Pause(bool pause);
134   void XBMC_Stop();
135   bool XBMC_DestroyDisplay();
136   bool XBMC_SetupDisplay();
137 };