|
| 1 | +/* |
| 2 | + * Copyright(c) 2020 Bob Shen <[email protected]> |
| 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.ayst.romupgrade.util; |
| 18 | + |
| 19 | +import android.app.AlertDialog; |
| 20 | +import android.app.Dialog; |
| 21 | +import android.content.Context; |
| 22 | +import android.content.DialogInterface; |
| 23 | +import android.content.Intent; |
| 24 | +import android.net.Uri; |
| 25 | +import android.os.Build; |
| 26 | +import android.provider.Settings; |
| 27 | +import android.util.Log; |
| 28 | +import android.view.WindowManager; |
| 29 | + |
| 30 | +import androidx.annotation.RequiresApi; |
| 31 | +import androidx.core.content.FileProvider; |
| 32 | + |
| 33 | +import com.ayst.romupgrade.R; |
| 34 | + |
| 35 | +import java.io.BufferedReader; |
| 36 | +import java.io.DataOutputStream; |
| 37 | +import java.io.File; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStreamReader; |
| 40 | +import java.nio.charset.Charset; |
| 41 | + |
| 42 | +public class InstallUtil { |
| 43 | + private static final String TAG = "InstallUtil"; |
| 44 | + private static final String AUTHORITY = "com.ayst.romupgrade.fileProvider"; |
| 45 | + |
| 46 | + public static void install(Context context, String path) { |
| 47 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 48 | + installO(context, path); |
| 49 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 50 | + installN(context, path); |
| 51 | + } else { |
| 52 | + installOther(context, path); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * android1.x-6.x |
| 58 | + * |
| 59 | + * @param context Context |
| 60 | + * @param path Package |
| 61 | + */ |
| 62 | + private static void installOther(Context context, String path) { |
| 63 | + Intent install = new Intent(Intent.ACTION_VIEW); |
| 64 | + install.setDataAndType(Uri.parse("file://" + path), |
| 65 | + "application/vnd.android.package-archive"); |
| 66 | + install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 67 | + context.startActivity(install); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * android7.x |
| 72 | + * |
| 73 | + * @param context Context |
| 74 | + * @param path Package |
| 75 | + */ |
| 76 | + private static void installN(Context context, String path) { |
| 77 | + Uri apkUri = FileProvider.getUriForFile(context, AUTHORITY, new File(path)); |
| 78 | + Intent install = new Intent(Intent.ACTION_VIEW); |
| 79 | + install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 80 | + install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 81 | + install.setDataAndType(apkUri, "application/vnd.android.package-archive"); |
| 82 | + context.startActivity(install); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * android8.x |
| 87 | + * |
| 88 | + * @param context Context |
| 89 | + * @param path Package |
| 90 | + */ |
| 91 | + @RequiresApi(api = Build.VERSION_CODES.O) |
| 92 | + private static void installO(Context context, String path) { |
| 93 | + boolean isGranted = context.getPackageManager().canRequestPackageInstalls(); |
| 94 | + if (isGranted) { |
| 95 | + installN(context, path); |
| 96 | + } else { |
| 97 | + Dialog dialog = new AlertDialog.Builder(context.getApplicationContext()) |
| 98 | + .setTitle(R.string.upgrade_unknown_sources_package) |
| 99 | + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 100 | + public void onClick(DialogInterface d, int w) { |
| 101 | + Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); |
| 102 | + context.startActivity(intent); |
| 103 | + } |
| 104 | + }).create(); |
| 105 | + dialog.setCancelable(false); |
| 106 | + dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); |
| 107 | + dialog.show(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Silent install |
| 113 | + * |
| 114 | + * @param path Package |
| 115 | + * @return true: success false: failed |
| 116 | + */ |
| 117 | + public static boolean installSilent(String path) { |
| 118 | + boolean result = false; |
| 119 | + BufferedReader es = null; |
| 120 | + DataOutputStream os = null; |
| 121 | + |
| 122 | + try { |
| 123 | + Process process = Runtime.getRuntime().exec("su"); |
| 124 | + os = new DataOutputStream(process.getOutputStream()); |
| 125 | + |
| 126 | + String command = "pm install -r " + path + "\n"; |
| 127 | + os.write(command.getBytes(Charset.forName("utf-8"))); |
| 128 | + os.flush(); |
| 129 | + os.writeBytes("exit\n"); |
| 130 | + os.flush(); |
| 131 | + |
| 132 | + process.waitFor(); |
| 133 | + es = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 134 | + |
| 135 | + String line; |
| 136 | + StringBuilder builder = new StringBuilder(); |
| 137 | + while ((line = es.readLine()) != null) { |
| 138 | + builder.append(line); |
| 139 | + } |
| 140 | + Log.d(TAG, "install msg is " + builder.toString()); |
| 141 | + |
| 142 | + /* Installation is considered a Failure if the result contains |
| 143 | + the Failure character, or a success if it is not. |
| 144 | + */ |
| 145 | + if (!builder.toString().contains("Failure")) { |
| 146 | + result = true; |
| 147 | + } |
| 148 | + } catch (Exception e) { |
| 149 | + Log.e(TAG, e.getMessage(), e); |
| 150 | + } finally { |
| 151 | + try { |
| 152 | + if (os != null) { |
| 153 | + os.close(); |
| 154 | + } |
| 155 | + if (es != null) { |
| 156 | + es.close(); |
| 157 | + } |
| 158 | + } catch (IOException e) { |
| 159 | + Log.e(TAG, e.getMessage(), e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return result; |
| 164 | + } |
| 165 | +} |
0 commit comments