From c4329b1e96e3f5dfe9b3bb9e2c2a64d6e238216f Mon Sep 17 00:00:00 2001 From: Chih-Wei Huang Date: Fri, 14 Feb 2020 18:07:33 +0800 Subject: [PATCH 1/1] Use DownloadManager to get native bridge libraries --- AndroidManifest.xml | 6 + .../android/settings/DownloadCompleteReceiver.java | 36 ++++++ .../system/AndroidX86DashboardFragment.java | 122 ++++++++++++++++++++- 3 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 src/com/android/settings/DownloadCompleteReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fc950d54e6..9d13023b44 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3228,6 +3228,12 @@ android:value="true" /> + + + + + + 0; + } + + private void checkNativeBridgeStatus() { + boolean nb = SystemProperties.getBoolean(PROPERTY_NATIVEBRIDGE, false); + if (!isNativeBridgeAvailable()) { + queryDownloading(mDownloadManager); + if (sDownloadStatus == DownloadManager.STATUS_RUNNING) { + nb = true; + } else if (nb) { + downloadNativeBridge(); + } + } + mNativeBridgePreference.setChecked(nb); + } + + private void setNativeBridge(boolean enabled) { + Log.d(TAG, "setNativeBridge: " + enabled); + if (isNativeBridgeAvailable()) { + setNativeBridgeProperty(enabled); + } else if (enabled) { + downloadNativeBridge(); + } else if (sDownloadId != -1) { + mDownloadManager.remove(sDownloadId); + sDownloadId = -1; + } + } + + private void downloadNativeBridge() { + String url, file; + if (VMRuntime.getRuntime().is64Bit()) { + url = "https://t.cn/EJrmYMH"; + file = "houdini9_y.sfs"; + } else { + url = "https://t.cn/EJrmzZv"; + file = "houdini9_x.sfs"; + } + + File path = Environment.getExternalStoragePublicDirectory("arm"); + path.mkdirs(); + File nb = new File(path, file); + if (nb.exists()) { + if (sDownloadId != -1) { + switch (sDownloadStatus) { + case DownloadManager.STATUS_SUCCESSFUL: + setNativeBridgeProperty(true); // fall through + case DownloadManager.STATUS_RUNNING: + return; + default: + break; + } + } + nb.delete(); + } + mDownloadManager.remove(sDownloadId); + + Request request = new Request(Uri.parse(url)). + setDestinationUri(Uri.fromFile(nb)). + setTitle(NB_LIBRARIES + file.substring(7, 10)); + + sDownloadId = mDownloadManager.enqueue(request); + Log.i(TAG, "downloading " + url); + } + + private static void queryDownloading(DownloadManager dm) { + Query query = new Query().setFilterByString(NB_LIBRARIES); + Cursor c = null; + try { + c = dm.query(query); + if (c.moveToFirst()) { + sDownloadId = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_ID)); + sDownloadStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); + Log.i(TAG, "id: " + sDownloadId + " status: " + sDownloadStatus); + } + } catch (Exception e) { + Log.w(TAG, "Exception: " + e); + } finally { + if (c != null) { + c.close(); + } + } + } + + private static void setNativeBridgeProperty(boolean enabled) { + SystemProperties.set(PROPERTY_NATIVEBRIDGE, enabled ? "1" : "0"); + } + + public static void onDownloadComplete(DownloadManager dm) { + queryDownloading(dm); + boolean success = sDownloadStatus == DownloadManager.STATUS_SUCCESSFUL; + if (success) { + Log.i(TAG, "download success, native bridge enabled"); + } else { + Log.w(TAG, "download failed: " + sDownloadStatus); + } + setNativeBridgeProperty(success); + } } -- 2.11.0