Browse Source

@fun: cad dwg

bai 2 years ago
parent
commit
28a926b5ab

+ 18 - 6
app/src/main/AndroidManifest.xml

@@ -1,7 +1,9 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-          package="com.ys.bdtp.adm">
+          xmlns:tools="http://schemas.android.com/tools" package="com.ys.bdtp.adm">
 
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.INTERNET"/>
     <application
@@ -21,14 +23,14 @@
                 android:label="@string/app_name"
                 android:networkSecurityConfig="@xml/network_security_config"
                 android:theme="@style/Theme.Adm.NoActionBar">
-                        <intent-filter>
-                            <action android:name="android.intent.action.MAIN"/>
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
 
-                            <category android:name="android.intent.category.LAUNCHER"/>
-                        </intent-filter>
+                <category android:name="android.intent.category.LAUNCHER"/>
+            </intent-filter>
         </activity>
 
-<!--        <activity android:name=".mvvm.ui.examine.ModelExamineActivity" android:exported="true"/>-->
+        <!--        <activity android:name=".mvvm.ui.examine.ModelExamineActivity" android:exported="true"/>-->
         <activity android:name=".mvvm.ui.examine.Space3DActivity" android:exported="true">
             <!--<intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
@@ -36,6 +38,16 @@
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>-->
         </activity>
+
+        <provider
+                android:name="androidx.core.content.FileProvider"
+                android:authorities="com.ys.bdtp.adm.fileprovider"
+                android:exported="false"
+                android:grantUriPermissions="true">
+            <meta-data
+                    android:name="android.support.FILE_PROVIDER_PATHS"
+                    android:resource="@xml/file_paths"/>
+        </provider>
     </application>
 
 </manifest>

+ 1 - 0
app/src/main/java/com/ys/bdtp/adm/app/tools/Md5.kt

@@ -1,5 +1,6 @@
 package com.ys.bdtp.adm.app.tools
 
+import com.ys.bdtp.adm.app.tools.file.toBytes
 import java.io.File
 import java.io.InputStream
 import java.security.MessageDigest

+ 9 - 3
app/src/main/java/com/ys/bdtp/adm/app/tools/file/Files.kt

@@ -1,8 +1,10 @@
-package com.ys.bdtp.adm.app.tools
+package com.ys.bdtp.adm.app.tools.file
 
+import android.content.Context
+import android.net.Uri
 import android.util.Log
-import com.ys.bdtp.adm.app.tools.file.FileId
-import com.ys.bdtp.adm.app.tools.file.FileInfo
+import androidx.core.content.FileProvider
+import com.ys.bdtp.adm.app.tools.md5
 import com.ys.bdtp.adm.mvvm.model.nt.Api
 import com.ys.bdtp.adm.mvvm.model.nt.FailureResponse
 import com.ys.bdtp.adm.mvvm.model.nt.SuccessResponse
@@ -119,3 +121,7 @@ suspend fun fileDownload(api: Api, param: Map<String, String>, file: File, fileI
         }
     }
 }
+
+fun File.getUri(context: Context, authority: String = "com.ys.bdtp.adm.fileprovider"): Uri {
+    return FileProvider.getUriForFile(context, authority, this)
+}

+ 7 - 3
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/cad/CadDrawingItem.kt

@@ -1,6 +1,7 @@
 package com.ys.bdtp.adm.mvvm.ui.screen.cad
 
 import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
 import androidx.compose.foundation.layout.Arrangement
 import androidx.compose.foundation.layout.Row
 import androidx.compose.foundation.layout.fillMaxWidth
@@ -23,7 +24,7 @@ import com.ys.bdtp.adm.app.theme.trBgColor
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.CadEntity
 
 @Composable
-fun CadDrawingItem(cad: CadEntity) {
+fun CadDrawingItem(cad: CadEntity, block: () -> Unit) {
     Row(
         modifier = Modifier
             .fillMaxWidth()
@@ -57,13 +58,16 @@ fun CadDrawingItem(cad: CadEntity) {
             maxLines = 1,
             modifier = Modifier.weight(2f)
         )
+
         Text(
             text = "打开图纸核查",
             fontSize = 16.sp,
             color = textBlueColor,
             textAlign = TextAlign.Center,
             maxLines = 1,
-            modifier = Modifier.weight(3f)
+            modifier = Modifier.weight(3f).clickable {
+                block()
+            }
         )
 
         Switch(
@@ -93,5 +97,5 @@ fun CadDrawingItem(cad: CadEntity) {
 @Preview(widthDp = 900)
 @Composable
 fun PreviewCadDrawingItem() {
-   // CadDrawingItem()
+    // CadDrawingItem()
 }

+ 19 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/cad/CadScreen.kt

@@ -1,5 +1,6 @@
 package com.ys.bdtp.adm.mvvm.ui.screen.cad
 
+import android.content.Intent
 import androidx.compose.foundation.background
 import androidx.compose.foundation.layout.*
 import androidx.compose.foundation.lazy.LazyColumn
@@ -9,6 +10,7 @@ import androidx.compose.runtime.*
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
@@ -16,6 +18,7 @@ import com.ys.bdtp.adm.app.theme.textBlackColor
 import com.ys.bdtp.adm.app.theme.trDivColor
 import com.ys.bdtp.adm.app.theme.underColor
 import com.ys.bdtp.adm.app.theme.whiteColor
+import com.ys.bdtp.adm.app.tools.file.getUri
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.CadEntity
 import com.ys.bdtp.adm.mvvm.model.db.relation.FloorWithCads
 import com.ys.bdtp.adm.mvvm.model.db.relation.ProjectWithBuildingAndFloors
@@ -25,10 +28,12 @@ import com.ys.bdtp.adm.mvvm.ui.screen.main.cadToLogin
 import com.ys.bdtp.adm.mvvm.vm.cad.CadViewModel
 import kotlinx.coroutines.launch
 import org.kodein.di.compose.rememberViewModel
+import java.io.File
 
 @Composable
 fun CadScreen(projectId: String) {
     val nav = LocalNavHostController.current!!
+    val context = LocalContext.current
     val vm by rememberViewModel<CadViewModel>()
     val scope = rememberCoroutineScope()
 
@@ -127,7 +132,20 @@ fun CadScreen(projectId: String) {
                             floor?.let {
                                 items(it.cads) { cad: CadEntity ->
                                     Box {
-                                        CadDrawingItem(cad)
+                                        CadDrawingItem(cad) {
+                                            val path = context.getExternalFilesDir("dwg")
+                                            val file = File(path, cad.fileName)
+                                            if (file.exists()) {
+                                                val uri = file.getUri(context)
+                                                val intent = Intent(Intent.ACTION_VIEW)
+                                                intent.setDataAndType(uri, "application/dwg")
+                                                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
+                                                context.startActivity(intent)
+                                            } else {
+                                                println("not found")
+                                            }
+
+                                        }
                                         Spacer(
                                             modifier = Modifier
                                                 .align(Alignment.BottomCenter)

+ 15 - 0
app/src/main/res/xml/file_paths.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<paths>
+    <!-- 物理路径为Context.getFilesDir() + /files/* -->
+    <files-path path="dwg" name="dwg" />
+    <!-- 物理路径为Context.getCacheDir() + /files/* -->
+    <cache-path path="files/" name="cache" />
+    <!-- 物理路径为Environment.getExternalStorageDirectory() + /files/* -->
+    <external-path path="files/" name="external" />
+    <!-- 物理路径为Context.getExternalFilesDir(String) + /files/* -->
+    <external-files-path path="dwg" name="dwg"/>
+    <!-- 物理路径为Context.getExternalCacheDir() + /files/* -->
+    <external-cache-path  path="files/" name="externalcache"/>
+    <!-- 物理路径为`Context.getExternalMediaDirs() + /files/*, 要求API21+ -->
+    <external-media-path name="externalmedia" path="files/" />
+</paths>