packages/apps/Settings
Revisión | c4329b1e96e3f5dfe9b3bb9e2c2a64d6e238216f (tree) |
---|---|
Tiempo | 2020-02-21 13:36:01 |
Autor | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
Use DownloadManager to get native bridge libraries
@@ -3228,6 +3228,12 @@ | ||
3228 | 3228 | android:value="true" /> |
3229 | 3229 | </activity> |
3230 | 3230 | |
3231 | + <receiver android:name=".DownloadCompleteReceiver"> | |
3232 | + <intent-filter> | |
3233 | + <action android:name="android.intent.action.DOWNLOAD_COMPLETE" /> | |
3234 | + </intent-filter> | |
3235 | + </receiver> | |
3236 | + | |
3231 | 3237 | <activity |
3232 | 3238 | android:name=".Settings$AccountDashboardActivity" |
3233 | 3239 | android:label="@string/account_dashboard_title" |
@@ -0,0 +1,36 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2020 The Android-x86 Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +package com.android.settings; | |
18 | + | |
19 | +import android.app.DownloadManager; | |
20 | +import android.content.BroadcastReceiver; | |
21 | +import android.content.Context; | |
22 | +import android.content.Intent; | |
23 | + | |
24 | +import static com.android.settings.system.AndroidX86DashboardFragment.onDownloadComplete; | |
25 | + | |
26 | +public final class DownloadCompleteReceiver extends BroadcastReceiver { | |
27 | + private static final String TAG = "Dl-Receiver"; | |
28 | + | |
29 | + @Override | |
30 | + public void onReceive(Context context, Intent intent) { | |
31 | + if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { | |
32 | + DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); | |
33 | + onDownloadComplete(dm); | |
34 | + } | |
35 | + } | |
36 | +} |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - * Copyright (C) 2018 The Android-x86 Open Source Project | |
2 | + * Copyright (C) 2018-2020 The Android-x86 Open Source Project | |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -16,34 +16,50 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.system; |
18 | 18 | |
19 | +import android.app.DownloadManager; | |
20 | +import android.app.DownloadManager.Query; | |
21 | +import android.app.DownloadManager.Request; | |
22 | +import android.content.Context; | |
23 | +import android.database.Cursor; | |
24 | +import android.net.Uri; | |
19 | 25 | import android.os.Bundle; |
26 | +import android.os.Environment; | |
20 | 27 | import android.os.SystemProperties; |
21 | 28 | import android.support.v7.preference.Preference; |
22 | 29 | import android.support.v14.preference.SwitchPreference; |
30 | +import android.util.Log; | |
23 | 31 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
24 | 32 | import com.android.settings.R; |
25 | 33 | import com.android.settings.SettingsPreferenceFragment; |
34 | +import dalvik.system.VMRuntime; | |
35 | +import java.io.File; | |
26 | 36 | |
27 | 37 | public class AndroidX86DashboardFragment extends SettingsPreferenceFragment { |
28 | 38 | |
39 | + private DownloadManager mDownloadManager; | |
29 | 40 | private SwitchPreference mNativeBridgePreference; |
30 | 41 | private SwitchPreference mHwInfoPreference; |
31 | 42 | private SwitchPreference mAppsUsagePreference; |
32 | 43 | |
44 | + private static final String TAG = "Dl-NB"; | |
33 | 45 | private static final String KEY_TOGGLE_NB = "toggle_nb"; |
34 | 46 | private static final String PROPERTY_NATIVEBRIDGE = "persist.sys.nativebridge"; |
35 | 47 | private static final String KEY_TOGGLE_HW_INFO = "toggle_hw_info"; |
36 | 48 | private static final String PROPERTY_HW_INFO = "persist.sys.hw_statistics"; |
37 | 49 | private static final String KEY_TOGGLE_APPS_USAGE = "toggle_apps_usage"; |
38 | 50 | private static final String PROPERTY_APPS_USAGE = "persist.sys.apps_statistics"; |
51 | + private static final String NB_LIBRARIES = "Native bridge libraries "; | |
52 | + private static long sDownloadId = -1; | |
53 | + private static int sDownloadStatus = -1; | |
39 | 54 | |
40 | 55 | @Override |
41 | 56 | public void onCreate(Bundle icicle) { |
42 | 57 | super.onCreate(icicle); |
43 | 58 | |
59 | + mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); | |
44 | 60 | addPreferencesFromResource(R.xml.android_x86_options); |
45 | 61 | mNativeBridgePreference = (SwitchPreference) findPreference(KEY_TOGGLE_NB); |
46 | - mNativeBridgePreference.setChecked(SystemProperties.getBoolean(PROPERTY_NATIVEBRIDGE, false)); | |
62 | + checkNativeBridgeStatus(); | |
47 | 63 | mHwInfoPreference = (SwitchPreference) findPreference(KEY_TOGGLE_HW_INFO); |
48 | 64 | mHwInfoPreference.setChecked(SystemProperties.getBoolean(PROPERTY_HW_INFO, true)); |
49 | 65 | mAppsUsagePreference = (SwitchPreference) findPreference(KEY_TOGGLE_APPS_USAGE); |
@@ -53,7 +69,7 @@ public class AndroidX86DashboardFragment extends SettingsPreferenceFragment { | ||
53 | 69 | @Override |
54 | 70 | public boolean onPreferenceTreeClick(Preference preference) { |
55 | 71 | if (preference == mNativeBridgePreference) { |
56 | - SystemProperties.set(PROPERTY_NATIVEBRIDGE, mNativeBridgePreference.isChecked() ? "1" : "0"); | |
72 | + setNativeBridge(mNativeBridgePreference.isChecked()); | |
57 | 73 | } else if (preference == mHwInfoPreference) { |
58 | 74 | SystemProperties.set(PROPERTY_HW_INFO, Boolean.toString(mHwInfoPreference.isChecked())); |
59 | 75 | } else if (preference == mAppsUsagePreference) { |
@@ -66,4 +82,104 @@ public class AndroidX86DashboardFragment extends SettingsPreferenceFragment { | ||
66 | 82 | public int getMetricsCategory() { |
67 | 83 | return MetricsEvent.APPLICATION; |
68 | 84 | } |
85 | + | |
86 | + private boolean isNativeBridgeAvailable() { | |
87 | + File file = new File("/system/lib/libhoudini.so"); | |
88 | + return file.length() > 0; | |
89 | + } | |
90 | + | |
91 | + private void checkNativeBridgeStatus() { | |
92 | + boolean nb = SystemProperties.getBoolean(PROPERTY_NATIVEBRIDGE, false); | |
93 | + if (!isNativeBridgeAvailable()) { | |
94 | + queryDownloading(mDownloadManager); | |
95 | + if (sDownloadStatus == DownloadManager.STATUS_RUNNING) { | |
96 | + nb = true; | |
97 | + } else if (nb) { | |
98 | + downloadNativeBridge(); | |
99 | + } | |
100 | + } | |
101 | + mNativeBridgePreference.setChecked(nb); | |
102 | + } | |
103 | + | |
104 | + private void setNativeBridge(boolean enabled) { | |
105 | + Log.d(TAG, "setNativeBridge: " + enabled); | |
106 | + if (isNativeBridgeAvailable()) { | |
107 | + setNativeBridgeProperty(enabled); | |
108 | + } else if (enabled) { | |
109 | + downloadNativeBridge(); | |
110 | + } else if (sDownloadId != -1) { | |
111 | + mDownloadManager.remove(sDownloadId); | |
112 | + sDownloadId = -1; | |
113 | + } | |
114 | + } | |
115 | + | |
116 | + private void downloadNativeBridge() { | |
117 | + String url, file; | |
118 | + if (VMRuntime.getRuntime().is64Bit()) { | |
119 | + url = "https://t.cn/EJrmYMH"; | |
120 | + file = "houdini9_y.sfs"; | |
121 | + } else { | |
122 | + url = "https://t.cn/EJrmzZv"; | |
123 | + file = "houdini9_x.sfs"; | |
124 | + } | |
125 | + | |
126 | + File path = Environment.getExternalStoragePublicDirectory("arm"); | |
127 | + path.mkdirs(); | |
128 | + File nb = new File(path, file); | |
129 | + if (nb.exists()) { | |
130 | + if (sDownloadId != -1) { | |
131 | + switch (sDownloadStatus) { | |
132 | + case DownloadManager.STATUS_SUCCESSFUL: | |
133 | + setNativeBridgeProperty(true); // fall through | |
134 | + case DownloadManager.STATUS_RUNNING: | |
135 | + return; | |
136 | + default: | |
137 | + break; | |
138 | + } | |
139 | + } | |
140 | + nb.delete(); | |
141 | + } | |
142 | + mDownloadManager.remove(sDownloadId); | |
143 | + | |
144 | + Request request = new Request(Uri.parse(url)). | |
145 | + setDestinationUri(Uri.fromFile(nb)). | |
146 | + setTitle(NB_LIBRARIES + file.substring(7, 10)); | |
147 | + | |
148 | + sDownloadId = mDownloadManager.enqueue(request); | |
149 | + Log.i(TAG, "downloading " + url); | |
150 | + } | |
151 | + | |
152 | + private static void queryDownloading(DownloadManager dm) { | |
153 | + Query query = new Query().setFilterByString(NB_LIBRARIES); | |
154 | + Cursor c = null; | |
155 | + try { | |
156 | + c = dm.query(query); | |
157 | + if (c.moveToFirst()) { | |
158 | + sDownloadId = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_ID)); | |
159 | + sDownloadStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); | |
160 | + Log.i(TAG, "id: " + sDownloadId + " status: " + sDownloadStatus); | |
161 | + } | |
162 | + } catch (Exception e) { | |
163 | + Log.w(TAG, "Exception: " + e); | |
164 | + } finally { | |
165 | + if (c != null) { | |
166 | + c.close(); | |
167 | + } | |
168 | + } | |
169 | + } | |
170 | + | |
171 | + private static void setNativeBridgeProperty(boolean enabled) { | |
172 | + SystemProperties.set(PROPERTY_NATIVEBRIDGE, enabled ? "1" : "0"); | |
173 | + } | |
174 | + | |
175 | + public static void onDownloadComplete(DownloadManager dm) { | |
176 | + queryDownloading(dm); | |
177 | + boolean success = sDownloadStatus == DownloadManager.STATUS_SUCCESSFUL; | |
178 | + if (success) { | |
179 | + Log.i(TAG, "download success, native bridge enabled"); | |
180 | + } else { | |
181 | + Log.w(TAG, "download failed: " + sDownloadStatus); | |
182 | + } | |
183 | + setNativeBridgeProperty(success); | |
184 | + } | |
69 | 185 | } |