فهرست منبع

支持应用安装

ketchup 3 سال پیش
والد
کامیت
54ef96d5de
3فایلهای تغییر یافته به همراه69 افزوده شده و 1 حذف شده
  1. 10 0
      net/src/main/AndroidManifest.xml
  2. 41 1
      net/src/main/java/com/drake/net/utils/FileUtils.kt
  3. 18 0
      net/src/main/res/xml/file_paths.xml

+ 10 - 0
net/src/main/AndroidManifest.xml

@@ -8,6 +8,7 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
 
     <application
         android:networkSecurityConfig="@xml/network_security_config"
@@ -18,6 +19,15 @@
             android:enabled="true"
             android:exported="false"
             android:multiprocess="true" />
+        <provider
+            android:name="androidx.core.content.FileProvider"
+            android:authorities="${applicationId}.fileProvider"
+            android:exported="false"
+            android:grantUriPermissions="true">
+            <meta-data
+                android:name="android.support.FILE_PROVIDER_PATHS"
+                android:resource="@xml/file_paths" />
+        </provider>
     </application>
 
 </manifest>

+ 41 - 1
net/src/main/java/com/drake/net/utils/FileUtils.kt

@@ -1,10 +1,14 @@
 package com.drake.net.utils
 
+import android.content.Intent
+import android.net.Uri
+import android.os.Build
 import android.webkit.MimeTypeMap
+import androidx.core.content.FileProvider
+import com.drake.net.NetConfig
 import okhttp3.MediaType
 import okhttp3.MediaType.Companion.toMediaTypeOrNull
 import okhttp3.RequestBody
-import okhttp3.RequestBody.Companion.asRequestBody
 import okio.BufferedSink
 import okio.source
 import java.io.File
@@ -57,4 +61,40 @@ fun File.toRequestBody(contentType: MediaType? = null): RequestBody {
             source().use { source -> sink.writeAll(source) }
         }
     }
+}
+
+/**
+ * 安装APK
+ */
+fun File.install() {
+    val context = NetConfig.app
+
+    if (!this.exists()) {
+        throw IllegalArgumentException("The file does not exist ($absolutePath)")
+    }
+    if (name.substringAfterLast(".").lowercase() != "apk") {
+        throw IllegalArgumentException("The file is not an apk file ($absolutePath)")
+    }
+
+    val intent = Intent(Intent.ACTION_VIEW)
+    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+    }
+    val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+        FileProvider.getUriForFile(
+            context,
+            "${context.packageName}.fileProvider",
+            this
+        )
+    } else {
+        Uri.fromFile(this)
+    }
+    intent.setDataAndType(uri, "application/vnd.android.package-archive")
+
+    if (context.packageManager.resolveActivity(intent, 0) != null) {
+        context.startActivity(intent)
+    } else {
+        throw UnsupportedOperationException("Unable to find installation activity")
+    }
 }

+ 18 - 0
net/src/main/res/xml/file_paths.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<paths>
+    <files-path
+        name="files"
+        path="." />
+    <cache-path
+        name="cache"
+        path="." />
+    <external-path
+        name="external"
+        path="." />
+    <external-files-path
+        name="external_file_path"
+        path="." />
+    <external-cache-path
+        name="external_cache_path"
+        path="." />
+</paths>