jni: fixed build, these do not exist here
[vuplus_xbmc] / xbmc / android / jni / Context.cpp
1 /*
2  *      Copyright (C) 2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "Context.h"
22 #include "PackageManager.h"
23 #include <android/log.h>
24 #include "Intent.h"
25 #include "IntentFilter.h"
26 #include "ClassLoader.h"
27 #include "jutils/jutils-details.hpp"
28 #include "BroadcastReceiver.h"
29 #include "JNIThreading.h"
30 #include "ApplicationInfo.h"
31 #include "File.h"
32 #include "ContentResolver.h"
33 #include "BaseColumns.h"
34 #include "MediaStore.h"
35 #include "PowerManager.h"
36 #include "Cursor.h"
37 #include "ConnectivityManager.h"
38 #include "AudioManager.h"
39
40 #include <android/native_activity.h>
41
42 using namespace jni;
43
44 jhobject CJNIContext::m_context(0);
45 CJNIContext* CJNIContext::m_appInstance(NULL);
46
47 CJNIContext::CJNIContext(const ANativeActivity *nativeActivity)
48 {
49   m_context.reset(nativeActivity->clazz);
50   xbmc_jni_on_load(nativeActivity->vm, nativeActivity->env);
51   PopulateStaticFields();
52   m_appInstance = this;
53 }
54
55 CJNIContext::~CJNIContext()
56 {
57   m_appInstance = NULL;
58   m_context.release();
59   xbmc_jni_on_unload();
60 }
61
62 void CJNIContext::PopulateStaticFields()
63 {
64   CJNIBaseColumns::PopulateStaticFields();
65   CJNIMediaStoreMediaColumns::PopulateStaticFields();
66   CJNIPowerManager::PopulateStaticFields();
67   CJNIPackageManager::PopulateStaticFields();
68   CJNIMediaStoreMediaColumns::PopulateStaticFields();
69   CJNICursor::PopulateStaticFields();
70   CJNIContentResolver::PopulateStaticFields();
71   CJNIConnectivityManager::PopulateStaticFields();
72   CJNIAudioManager::PopulateStaticFields();
73 }
74
75 CJNIPackageManager CJNIContext::GetPackageManager()
76 {
77   return call_method<jhobject>(m_context,
78     "getPackageManager", "()Landroid/content/pm/PackageManager;");
79 }
80
81 void CJNIContext::startActivity(const CJNIIntent &intent)
82 {
83   call_method<void>(jhobject(m_context),
84     "startActivity", "(Landroid/content/Intent;)V",
85     intent.get_raw());
86 }
87
88 int CJNIContext::checkCallingOrSelfPermission(const std::string &permission)
89 {
90   return call_method<int>(m_context,
91     "checkCallingOrSelfPermission", "(Ljava/lang/String;)I",
92     jcast<jhstring>(permission));
93 }
94
95 jhobject CJNIContext::getSystemService(const std::string &service)
96 {
97   return call_method<jhobject>(m_context,
98     "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;",
99     jcast<jhstring>(service));
100 }
101
102 CJNIIntent CJNIContext::registerReceiver(const CJNIBroadcastReceiver &receiver, const CJNIIntentFilter &filter)
103 {
104   return call_method<jhobject>(m_context,
105     "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;",
106     receiver.get_raw(), filter.get_raw());
107 }
108
109 CJNIIntent CJNIContext::registerReceiver(const CJNIIntentFilter &filter)
110 {
111   return call_method<jhobject>(m_context,
112     "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;",
113     (jobject)NULL, filter.get_raw());
114 }
115
116 void CJNIContext::unregisterReceiver(const CJNIBroadcastReceiver &receiver)
117 {
118   call_method<void>(m_context,
119     "unregisterReceiver", "(Landroid/content/BroadcastReceiver;)V",
120     receiver.get_raw());
121 }
122
123 CJNIIntent CJNIContext::sendBroadcast(const CJNIIntent &intent)
124 {
125   return call_method<jhobject>(m_context,
126     "sendBroadcast", "(Landroid/content/Intent;)V",
127     intent.get_raw());
128 }
129
130 CJNIIntent CJNIContext::getIntent()
131 {
132   return call_method<jhobject>(m_context,
133     "getIntent", "()Landroid/content/Intent;");
134 }
135
136 CJNIClassLoader CJNIContext::getClassLoader()
137 {
138   return call_method<jhobject>(m_context,
139     "getClassLoader", "()Ljava/lang/ClassLoader;");
140 }
141
142 CJNIApplicationInfo CJNIContext::getApplicationInfo()
143 {
144   return call_method<jhobject>(m_context,
145     "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
146 }
147
148 std::string CJNIContext::getPackageResourcePath()
149 {
150   return jcast<std::string>(call_method<jhstring>(m_context,
151     "getPackageResourcePath", "()Ljava/lang/String;"));
152 }
153
154 CJNIFile CJNIContext::getCacheDir()
155 {
156   return call_method<jhobject>(m_context,
157     "getCacheDir", "()Ljava/io/File;");
158 }
159
160 CJNIFile CJNIContext::getDir(const std::string &path, int mode)
161 {
162   return call_method<jhobject>(m_context,
163     "getDir", "(Ljava/lang/String;I)Ljava/io/File;",
164     jcast<jhstring>(path), mode);
165 }
166
167 CJNIFile CJNIContext::getExternalFilesDir(const std::string &path)
168 {
169   return call_method<jhobject>(m_context,
170     "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;",
171     jcast<jhstring>(path));
172 }
173
174 CJNIContentResolver CJNIContext::getContentResolver()
175 {
176   return call_method<jhobject>(m_context,
177     "getContentResolver", "()Landroid/content/ContentResolver;");
178 }
179
180 void CJNIContext::_onNewIntent(JNIEnv *env, jobject context, jobject intent)
181 {
182   (void)env;
183   (void)context;
184   if(m_appInstance)
185     m_appInstance->onNewIntent(CJNIIntent(jhobject(intent)));
186 }