|
16 | 16 |
|
17 | 17 | package com.runrev.android.libraries; |
18 | 18 |
|
19 | | -import com.runrev.android.Engine; |
| 19 | +import com.runrev.android.*; |
20 | 20 | import com.runrev.android.nativecontrol.NativeControlModule; |
21 | 21 |
|
22 | 22 | import android.app.AlertDialog; |
|
29 | 29 | import android.view.*; |
30 | 30 | import android.webkit.*; |
31 | 31 | import android.widget.*; |
| 32 | +import android.net.*; |
| 33 | +import android.os.*;; |
| 34 | +import android.media.*; |
| 35 | +import android.provider.*; |
32 | 36 |
|
33 | 37 | import java.lang.reflect.*; |
34 | 38 | import java.net.*; |
35 | 39 | import java.security.*; |
36 | 40 | import java.util.*; |
37 | 41 | import java.util.regex.*; |
| 42 | +import java.io.*; |
38 | 43 |
|
39 | 44 | import org.json.JSONArray; |
40 | 45 | import org.json.JSONException; |
@@ -236,8 +241,129 @@ public boolean onTouch(View v, MotionEvent event) |
236 | 241 | return !m_allow_user_interaction; |
237 | 242 | } |
238 | 243 | }); |
239 | | - |
240 | | - m_chrome_client = new WebChromeClient() { |
| 244 | + |
| 245 | + class MCWebChromeClient extends WebChromeClient implements LiveCodeActivity.OnActivityResultListener |
| 246 | + { |
| 247 | + private static final int WEBVIEW_INPUT_FILE_RESULT = 9999; |
| 248 | + private ValueCallback<Uri[]> m_browser_file_path_callback = null; |
| 249 | + private File m_temp_image_file = null; |
| 250 | + |
| 251 | + public void onActivityResult (int p_request_code, int p_result_code, Intent p_data) |
| 252 | + { |
| 253 | + LiveCodeActivity t_activity = (LiveCodeActivity)getContext(); |
| 254 | + t_activity.removeOnActivityResultListener(p_request_code); |
| 255 | + |
| 256 | + switch (p_request_code) |
| 257 | + { |
| 258 | + case WEBVIEW_INPUT_FILE_RESULT: |
| 259 | + Uri[] t_uris = null; |
| 260 | + if (p_result_code == Activity.RESULT_OK) |
| 261 | + { |
| 262 | + if (p_data.getData() != null) |
| 263 | + { |
| 264 | + t_uris = WebChromeClient.FileChooserParams.parseResult(p_result_code, p_data); |
| 265 | + } |
| 266 | + else if (m_temp_image_file != null) |
| 267 | + { |
| 268 | + t_uris = new Uri[] {Uri.fromFile(m_temp_image_file)}; |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + m_browser_file_path_callback.onReceiveValue(t_uris); |
| 273 | + m_browser_file_path_callback = null; |
| 274 | + |
| 275 | + if (m_temp_image_file != null) |
| 276 | + { |
| 277 | + FileProvider.getProvider(getContext()).removePath(m_temp_image_file.getPath()); |
| 278 | + m_temp_image_file = null; |
| 279 | + } |
| 280 | + default: |
| 281 | + break; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public boolean onShowFileChooser(WebView p_webview, ValueCallback<Uri[]> p_file_path_callback, WebChromeClient.FileChooserParams p_file_chooser_params) |
| 287 | + { |
| 288 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) |
| 289 | + { |
| 290 | + try { |
| 291 | + LiveCodeActivity t_activity = (LiveCodeActivity)getContext(); |
| 292 | + |
| 293 | + Intent t_intent = p_file_chooser_params.createIntent(); |
| 294 | + |
| 295 | + List<Intent> t_extra_intents = new ArrayList<Intent>(); |
| 296 | + |
| 297 | + Intent t_gallery_intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); |
| 298 | + t_gallery_intent.setType("image/*"); |
| 299 | + t_extra_intents.add(t_gallery_intent); |
| 300 | + |
| 301 | + boolean t_have_temp_file = false; |
| 302 | + try |
| 303 | + { |
| 304 | + // try external cache first |
| 305 | + m_temp_image_file = File.createTempFile("img", ".jpg", t_activity.getExternalCacheDir()); |
| 306 | + m_temp_image_file.setWritable(true, false); |
| 307 | + t_have_temp_file = true; |
| 308 | + } |
| 309 | + catch (IOException e) |
| 310 | + { |
| 311 | + m_temp_image_file = null; |
| 312 | + t_have_temp_file = false; |
| 313 | + } |
| 314 | + |
| 315 | + if (!t_have_temp_file) |
| 316 | + { |
| 317 | + try |
| 318 | + { |
| 319 | + // now try internal cache - should succeed but may not have enough space on the device for large image files |
| 320 | + m_temp_image_file = File.createTempFile("img", ".jpg", t_activity.getCacheDir()); |
| 321 | + m_temp_image_file.setWritable(true, false); |
| 322 | + t_have_temp_file = true; |
| 323 | + } |
| 324 | + catch (IOException e) |
| 325 | + { |
| 326 | + m_temp_image_file = null; |
| 327 | + t_have_temp_file = false; |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + if (t_have_temp_file) |
| 332 | + { |
| 333 | + String t_path = m_temp_image_file.getPath(); |
| 334 | + |
| 335 | + Uri t_uri; |
| 336 | + t_uri = FileProvider.getProvider(getContext()).addPath(t_path, t_path, "image/jpeg", false, ParcelFileDescriptor.MODE_READ_WRITE); |
| 337 | + |
| 338 | + Intent t_image_capture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 339 | + |
| 340 | + t_image_capture.putExtra(MediaStore.EXTRA_OUTPUT, t_uri); |
| 341 | + t_image_capture.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 342 | + t_extra_intents.add(t_image_capture); |
| 343 | + } |
| 344 | + |
| 345 | + Intent t_chooser_intent = Intent.createChooser(t_intent, "Select a file");; |
| 346 | + t_chooser_intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, t_extra_intents.toArray(new Parcelable[] {})); |
| 347 | + |
| 348 | + if (t_chooser_intent.resolveActivity(t_activity.getPackageManager()) == null) |
| 349 | + { |
| 350 | + return false; |
| 351 | + } |
| 352 | + |
| 353 | + m_browser_file_path_callback = p_file_path_callback; |
| 354 | + t_activity.setOnActivityResultListener(this, WEBVIEW_INPUT_FILE_RESULT); |
| 355 | + t_activity.startActivityForResult(t_chooser_intent, WEBVIEW_INPUT_FILE_RESULT); |
| 356 | + |
| 357 | + return true; |
| 358 | + } |
| 359 | + catch (Exception e) |
| 360 | + { |
| 361 | + |
| 362 | + } |
| 363 | + } |
| 364 | + return false; |
| 365 | + } |
| 366 | + |
241 | 367 | @Override |
242 | 368 | public void onShowCustomView(View view, CustomViewCallback callback) |
243 | 369 | { |
@@ -362,9 +488,10 @@ public void onProgressChanged(WebView p_view, int p_progress) |
362 | 488 | doProgressChanged(p_view.getUrl(), p_progress); |
363 | 489 | wakeEngineThread(); |
364 | 490 | } |
365 | | - |
366 | | - }; |
367 | | - |
| 491 | + } |
| 492 | + |
| 493 | + m_chrome_client = new MCWebChromeClient(); |
| 494 | + |
368 | 495 | setWebChromeClient(m_chrome_client); |
369 | 496 | getSettings().setJavaScriptEnabled(true); |
370 | 497 | getSettings().setAllowFileAccessFromFileURLs(true); |
|
0 commit comments