|
| 1 | +package io.virtualapp.settings; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.ProgressDialog; |
| 5 | +import android.os.SystemClock; |
| 6 | +import android.support.v7.app.AlertDialog; |
| 7 | +import android.text.TextUtils; |
| 8 | +import android.util.Log; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import com.lody.virtual.client.core.InstallStrategy; |
| 12 | +import com.lody.virtual.client.core.VirtualCore; |
| 13 | +import com.lody.virtual.os.VEnvironment; |
| 14 | +import com.lody.virtual.remote.InstallResult; |
| 15 | + |
| 16 | +import org.json.JSONException; |
| 17 | +import org.json.JSONObject; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileWriter; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import io.virtualapp.R; |
| 26 | +import io.virtualapp.gms.FakeGms; |
| 27 | +import io.virtualapp.home.LoadingActivity; |
| 28 | +import okhttp3.OkHttpClient; |
| 29 | +import okhttp3.Request; |
| 30 | +import okhttp3.Response; |
| 31 | +import okhttp3.ResponseBody; |
| 32 | + |
| 33 | +import static io.virtualapp.utils.DialogUtil.showDialog; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author weishu |
| 37 | + * @date 2018/7/5. |
| 38 | + */ |
| 39 | +public class OnlinePlugin { |
| 40 | + |
| 41 | + private static final String TAG = "OnlinePlugin"; |
| 42 | + |
| 43 | + public static final String FILE_MANAGE_PACKAGE = "com.amaze.filemanager"; |
| 44 | + public static final String FILE_MANAGE_URL = "http://vaexposed.weishu.me/amaze.json"; |
| 45 | + |
| 46 | + public static final String PERMISSION_MANAGE_PACKAGE = "eu.faircode.xlua"; |
| 47 | + public static final String PERMISSION_MANAGE_URL = "http://vaexposed.weishu.me/xlua.json"; |
| 48 | + |
| 49 | + public static void openOrDownload(Activity context, String packageName, String url, String tips) { |
| 50 | + if (context == null || packageName == null) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (VirtualCore.get().isAppInstalled(packageName)) { |
| 55 | + LoadingActivity.launch(context, packageName, 0); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + AlertDialog failDialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_DayNight_Dialog_Alert) |
| 60 | + .setTitle(android.R.string.dialog_alert_title) |
| 61 | + .setMessage(tips) |
| 62 | + .setPositiveButton(android.R.string.ok, ((dialog1, which1) -> { |
| 63 | + ProgressDialog progressDialog = new ProgressDialog(context); |
| 64 | + progressDialog.setCancelable(false); |
| 65 | + progressDialog.show(); |
| 66 | + |
| 67 | + Executors.newSingleThreadExecutor().submit(() -> { |
| 68 | + String error = downloadAndInstall(context, progressDialog, url, packageName); |
| 69 | + try { |
| 70 | + progressDialog.dismiss(); |
| 71 | + } catch (Throwable e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + |
| 75 | + if (error == null) { |
| 76 | + context.runOnUiThread(() -> { |
| 77 | + LoadingActivity.launch(context, packageName, 0); |
| 78 | + }); |
| 79 | + } else { |
| 80 | + context.runOnUiThread(() -> Toast.makeText(context, error, Toast.LENGTH_SHORT).show()); |
| 81 | + } |
| 82 | + |
| 83 | + }); |
| 84 | + |
| 85 | + })) |
| 86 | + .setNegativeButton(android.R.string.cancel, null) |
| 87 | + .create(); |
| 88 | + |
| 89 | + showDialog(failDialog); |
| 90 | + } |
| 91 | + |
| 92 | + private static String downloadAndInstall(Activity activity, ProgressDialog dialog, String url, String packageName) { |
| 93 | + OkHttpClient client = new OkHttpClient.Builder() |
| 94 | + .connectTimeout(30, TimeUnit.SECONDS) |
| 95 | + .readTimeout(30, TimeUnit.SECONDS) |
| 96 | + .writeTimeout(30, TimeUnit.SECONDS) |
| 97 | + .build(); |
| 98 | + |
| 99 | + Request request = new Request.Builder() |
| 100 | + .url(url) |
| 101 | + .build(); |
| 102 | + |
| 103 | + updateMessage(activity, dialog, "Prepare download..."); |
| 104 | + Response response; |
| 105 | + try { |
| 106 | + response = client.newCall(request).execute(); |
| 107 | + } catch (IOException e) { |
| 108 | + return "Download failed, please check your network, error: 0"; |
| 109 | + } |
| 110 | + |
| 111 | + if (!response.isSuccessful()) { |
| 112 | + return "Download failed, please check your network, error: 1"; |
| 113 | + } |
| 114 | + |
| 115 | + Log.i(TAG, "response success: " + response.code()); |
| 116 | + if (200 != response.code()) { |
| 117 | + return "Download failed, please check your network, error: 2"; |
| 118 | + } |
| 119 | + |
| 120 | + updateMessage(activity, dialog, "Parsing config..."); |
| 121 | + ResponseBody body = response.body(); |
| 122 | + if (body == null) { |
| 123 | + return "Download failed, please check your network, error: 3"; |
| 124 | + } |
| 125 | + |
| 126 | + String string; |
| 127 | + try { |
| 128 | + string = body.string(); |
| 129 | + } catch (IOException e) { |
| 130 | + return "Download failed, please check your network, error: 4"; |
| 131 | + } |
| 132 | + |
| 133 | + JSONObject jsonObject; |
| 134 | + try { |
| 135 | + jsonObject = new JSONObject(string); |
| 136 | + } catch (JSONException e) { |
| 137 | + return "Download failed, please check your network, error: 5"; |
| 138 | + } |
| 139 | + String downloadLink; |
| 140 | + boolean isXposed; |
| 141 | + try { |
| 142 | + downloadLink = jsonObject.getString("url"); |
| 143 | + isXposed = jsonObject.optBoolean("xposed", false); |
| 144 | + } catch (JSONException e) { |
| 145 | + return "Download failed, please check your network, error: 6"; |
| 146 | + } |
| 147 | + |
| 148 | + File outFile = new File(activity.getCacheDir(), packageName + ".apk"); |
| 149 | + FakeGms.downloadFile(downloadLink, outFile, progress -> updateMessage(activity, dialog, "download " + packageName + "..." + progress + "%")); |
| 150 | + |
| 151 | + updateMessage(activity, dialog, "installing " + packageName); |
| 152 | + |
| 153 | + InstallResult installResult = VirtualCore.get().installPackage(outFile.getAbsolutePath(), InstallStrategy.UPDATE_IF_EXIST); |
| 154 | + if (!installResult.isSuccess) { |
| 155 | + return "install " + packageName + " failed: " + installResult.error; |
| 156 | + } |
| 157 | + |
| 158 | + if (isXposed) { |
| 159 | + // Enable the Xposed module. |
| 160 | + updateMessage(activity, dialog, "enable " + packageName + " in Xposed Installer"); |
| 161 | + |
| 162 | + File dataDir = VEnvironment.getDataUserPackageDirectory(0, "de.robv.android.xposed.installer"); |
| 163 | + File modulePath = VEnvironment.getPackageResourcePath(packageName); |
| 164 | + File configDir = new File(dataDir, "exposed_conf" + File.separator + "modules.list"); |
| 165 | + FileWriter writer = null; |
| 166 | + try { |
| 167 | + writer = new FileWriter(configDir, true); |
| 168 | + writer.append(modulePath.getAbsolutePath()); |
| 169 | + writer.flush(); |
| 170 | + |
| 171 | + } catch (IOException e) { |
| 172 | + e.printStackTrace(); |
| 173 | + } finally { |
| 174 | + if (writer != null) { |
| 175 | + try { |
| 176 | + writer.close(); |
| 177 | + } catch (IOException e) { |
| 178 | + e.printStackTrace(); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + updateMessage(activity, dialog, " install success!!"); |
| 185 | + SystemClock.sleep(300); |
| 186 | + |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + private static void updateMessage(Activity activity, ProgressDialog dialog, String msg) { |
| 191 | + if (activity == null || dialog == null || TextUtils.isEmpty(msg)) { |
| 192 | + return; |
| 193 | + } |
| 194 | + Log.i(TAG, "update dialog message: " + msg); |
| 195 | + activity.runOnUiThread(() -> { |
| 196 | + dialog.setMessage(msg); |
| 197 | + }); |
| 198 | + } |
| 199 | +} |
0 commit comments