Repository navigation
Expand file tree
/
Copy pathNDKHelper.java
More file actions
239 lines (207 loc) · 7.46 KB
/
Copy pathNDKHelper.java
File metadata and controls
239 lines (207 loc) · 7.46 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright 2014 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.
*/
package com.sample.helper;
import java.io.File;
import java.io.FileInputStream;
import javax.microedition.khronos.opengles.GL10;
import android.annotation.TargetApi;
import android.app.NativeActivity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.opengl.GLUtils;
import android.os.Build;
import android.util.Log;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class NDKHelper {
public NDKHelper(NativeActivity act) {
activity = act;
}
public void loadLibrary(String soname) {
if (soname.isEmpty() == false) {
System.loadLibrary(soname);
loadedSO = true;
}
}
public static Boolean checkSOLoaded() {
if (loadedSO == false) {
Log.e("NDKHelper",
"--------------------------------------------\n"
+ ".so has not been loaded. To use JUI helper, please initialize with \n"
+ "NDKHelper::Init( ANativeActivity* activity, const char* helper_class_name, const char* native_soname);\n"
+ "--------------------------------------------\n");
return false;
} else
return true;
}
private static boolean loadedSO = false;
NativeActivity activity;
//
// Load Bitmap
// Java helper is useful decoding PNG, TIFF etc rather than linking libPng
// etc separately
//
private int nextPOT(int i) {
int pot = 1;
while (pot < i)
pot <<= 1;
return pot;
}
private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth,
float newHeight) {
if (bitmapToScale == null)
return null;
// get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);
// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0,
bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix,
true);
}
public boolean loadTexture(String path) {
Bitmap bitmap = null;
try {
String str = path;
if (!path.startsWith("/")) {
str = "/" + path;
}
File file = new File(activity.getExternalFilesDir(null), str);
if (file.canRead()) {
bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
} else {
bitmap = BitmapFactory.decodeStream(activity.getResources()
.getAssets().open(path));
}
// Matrix matrix = new Matrix();
// // resize the bit map
// matrix.postScale(-1F, 1F);
//
// // recreate the new Bitmap and set it back
// bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
// bitmap.getHeight(), matrix, true);
} catch (Exception e) {
Log.w("NDKHelper", "Coundn't load a file:" + path);
return false;
}
if (bitmap != null) {
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
return true;
}
public Bitmap openBitmap(String path, boolean iScalePOT) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(activity.getResources()
.getAssets().open(path));
if (iScalePOT) {
int originalWidth = getBitmapWidth(bitmap);
int originalHeight = getBitmapHeight(bitmap);
int width = nextPOT(originalWidth);
int height = nextPOT(originalHeight);
if (originalWidth != width || originalHeight != height) {
// Scale it
bitmap = scaleBitmap(bitmap, width, height);
}
}
} catch (Exception e) {
Log.w("NDKHelper", "Coundn't load a file:" + path);
}
return bitmap;
}
public int getBitmapWidth(Bitmap bmp) {
return bmp.getWidth();
}
public int getBitmapHeight(Bitmap bmp) {
return bmp.getHeight();
}
public void getBitmapPixels(Bitmap bmp, int[] pixels) {
int w = bmp.getWidth();
int h = bmp.getHeight();
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
}
public void closeBitmap(Bitmap bmp) {
bmp.recycle();
}
public String getNativeLibraryDirectory(Context appContext) {
ApplicationInfo ai = activity.getApplicationInfo();
Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir);
if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
|| (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return ai.nativeLibraryDir;
}
return "/system/lib/";
}
public String getApplicationName() {
final PackageManager pm = activity.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(activity.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
String applicationName = (String) (ai != null ? pm
.getApplicationLabel(ai) : "(unknown)");
return applicationName;
}
//
// Audio related helpers
//
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public int getNativeAudioBufferSize() {
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT >= 17) {
AudioManager am = (AudioManager) activity
.getSystemService(Context.AUDIO_SERVICE);
String framesPerBuffer = am
.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
return Integer.parseInt(framesPerBuffer);
} else {
return 0;
}
}
public int getNativeAudioSampleRate() {
return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
}
/*
* Helper to execute function in UIThread
*/
public void runOnUIThread(final long p) {
if (checkSOLoaded()) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
RunOnUiThreadHandler(p);
}
});
}
return;
}
/*
* Native code helper for RunOnUiThread
*/
native public void RunOnUiThreadHandler(long pointer);
}