Ver código fonte

修改介绍页

zhaoyadi 9 meses atrás
pai
commit
9084d8e66a

+ 0 - 4
xiaodou/build.gradle

@@ -33,10 +33,6 @@ android {
                     'libs',
                     'src/main/cpp/libs'
             ]
-
-            res{
-                srcDir("src/main/res-xiaodou")
-            }
         }
     }
     buildFeatures {

+ 4 - 3
xiaodou/src/main/java/com/luojigou/product/ProductSelectActivity.kt

@@ -4,15 +4,16 @@ import android.content.Intent
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import androidx.recyclerview.widget.GridLayoutManager
+import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
+import androidx.recyclerview.widget.RecyclerView.LayoutManager
 import com.luojigou.product.base.BaseActivity
 import com.luojigou.product.xiaodou.XDIntroduceActivity
 import com.google.android.material.R.drawable as MaterialDrawable
 
 class ProductSelectActivity : BaseActivity() {
     private lateinit var productAdapter: ProductAdapter
-    private lateinit var productLayoutManager: GridLayoutManager
+    private lateinit var productLayoutManager: LayoutManager
     private lateinit var productRecyclerView: RecyclerView
 
     private val productList = listOf(
@@ -34,7 +35,7 @@ class ProductSelectActivity : BaseActivity() {
                 }
             }
         }
-        productLayoutManager = GridLayoutManager(this, 2)
+        productLayoutManager = LinearLayoutManager(this)
         productRecyclerView.layoutManager = productLayoutManager
         productRecyclerView.adapter = productAdapter
     }

+ 13 - 39
xiaodou/src/main/java/com/luojigou/product/ble/XDBLEUtils.kt

@@ -3,12 +3,8 @@ package com.luojigou.product.ble
 import android.util.Log
 
 object XDBLEUtils {
-    const val CONNECT_SUCCESS = 0
-    const val CONNECTING = 1
-    const val CONNECT_FAIL = 2
-    const val CONNECT_NOT_FOUND = 3
-    const val CONNECT_PWD_ERROR = 4
-
+    const val START_FIRST: Byte = 0x7E.toByte()
+    const val START_SECOND: Byte = 0x01.toByte()
     const val END_BYTE: Byte = 255.toByte()
     fun parseConnectResponse(data: ByteArray): Int {
         if (data.size < 17) return -99
@@ -17,39 +13,17 @@ object XDBLEUtils {
     }
 
     fun convertToConnectRequest(ssid: String, password: String): ByteArray {
-        val stringBuffer = StringBuffer()
-        stringBuffer.append(Char(0x7E))
-        stringBuffer.append(Char(0x01))
-        stringBuffer.append(Char(ssid.length))
-        ssid.toCharArray().forEach {
-            stringBuffer.append(it)
-        }
-        stringBuffer.append(Char(password.length))
-        password.toCharArray().forEach {
-            stringBuffer.append(it)
-        }
-        val byteArray = stringBuffer.toString().toByteArray()
-        return byteArrayOf(*byteArray, END_BYTE)
-    }
-
-    fun convert(): ByteArray {
-        val ssid = "abcd"
-        val password = "12345678"
-
-        val stringBuffer = StringBuffer()
-
-        stringBuffer.append(Char(0x7E))
-        stringBuffer.append(Char(0x01))
-        stringBuffer.append(Char(ssid.length))
-        ssid.toCharArray().forEach {
-            stringBuffer.append(it)
-        }
-        stringBuffer.append(Char(password.length))
-        password.toCharArray().forEach {
-            stringBuffer.append(it)
-        }
-        stringBuffer.append(0xFF)
-        val byteArray = stringBuffer.toString().toByteArray()
+        val ssidArray = ssid.toByteArray(Charsets.UTF_8)
+        val passwordArray = password.toByteArray(Charsets.UTF_8)
+        val byteArray =  byteArrayOf(
+            START_FIRST,
+            START_SECOND,
+            ssidArray.size.toByte(),
+            *ssidArray,
+            passwordArray.size.toByte(),
+            *passwordArray,
+            END_BYTE
+        )
         return byteArray
     }
 }

+ 16 - 8
xiaodou/src/main/java/com/luojigou/product/ble/XDScanBLENormalFragment.kt

@@ -5,6 +5,7 @@ import android.content.BroadcastReceiver
 import android.content.Context
 import android.content.Intent
 import android.content.IntentFilter
+import android.os.Build
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
@@ -18,12 +19,12 @@ import androidx.fragment.app.Fragment
 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.luojigou.product.R
+import com.luojigou.product.wifi.XDScanWifiActivity
 import com.luojigou.product.xiaodou.XDConnectService
 import com.luojigou.product.xiaodou.XDConnectService.Companion.ACTION_SCAN_RESULT
 import com.luojigou.product.xiaodou.XDConnectService.Companion.ACTION_SCAN_RESULT_DATA
 import com.luojigou.product.xiaodou.XDConnectService.Companion.ACTION_START_SCAN
 import com.luojigou.product.xiaodou.XDConnectService.Companion.ACTION_STOP_SCAN
-import com.luojigou.product.wifi.XDScanWifiActivity
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.MainScope
 
@@ -89,21 +90,28 @@ class XDScanBLENormalFragment(private val host: XDScanBLEStatus.Host) : Fragment
                     }
 
                     ACTION_SCAN_RESULT -> {
-                        val list = intent.getParcelableArrayListExtra(
-                            ACTION_SCAN_RESULT_DATA, XDScanBLEDevice::class.java
-                        )
+                        val list = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                            intent.getParcelableArrayListExtra(
+                                ACTION_SCAN_RESULT_DATA, XDScanBLEDevice::class.java
+                            )
+                        } else {
+                            intent.getParcelableArrayListExtra(ACTION_SCAN_RESULT_DATA)
+                        }
 
                         if (list != null) {
                             setAdapterValue(list)
 
                             if (list.size == 1) {
-                                val intent = Intent(XDConnectService.ACTION_SELECT_BLE_DEVICE)
-                                intent.putExtra(
+                                val selectIntent = Intent(XDConnectService.ACTION_SELECT_BLE_DEVICE)
+                                selectIntent.putExtra(
                                     XDConnectService.ACTION_SELECT_BLE_DEVICE_DATA,
                                     list[0]
                                 )
-                                requireActivity().sendBroadcast(intent)
-                                XDScanWifiActivity.start(requireContext())
+
+                                activity?.run {
+                                    sendBroadcast(selectIntent)
+                                    XDScanWifiActivity.start(this)
+                                }
                             }
                         }
                     }

+ 45 - 16
xiaodou/src/main/java/com/luojigou/product/intro/AbsIntroduceActivity.kt

@@ -1,32 +1,43 @@
 package com.luojigou.product.intro
 
+import android.content.Intent
+import android.net.Uri
+import android.provider.Settings
 import android.util.Log
-import android.view.View
 import androidx.activity.result.ActivityResultLauncher
 import androidx.activity.result.contract.ActivityResultContracts
 import com.luojigou.product.R
 import com.luojigou.product.base.BaseActivity
 import com.luojigou.product.utils.PermissionUtils
 
-abstract class AbsIntroduceActivity : BaseActivity(), View.OnClickListener,
-    IntroducePermissionFragment.Host {
+
+abstract class AbsIntroduceActivity : BaseActivity(), IntroducePermissionFragment.Host,
+    IntroducePermissionManualFragment.Host {
 
     private lateinit var permissionUtils: PermissionUtils
 
     private lateinit var requestPermission: ActivityResultLauncher<Array<String>>
 
+
+    private var requesting = false
     override fun onCreate(savedInstanceState: android.os.Bundle?) {
         super.onCreate(savedInstanceState)
         setTitle("连接引导")
-        requestPermission =
-            registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
-                if (it.all { permission -> permission.value }) {
-                    setFragment(IntroduceNormalFragment())
-                } else {
-                    Log.e("XDScanWifiActivity", "requestPermission: 权限被拒绝")
-                }
+        requestPermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
+            if (it.all { permission -> permission.value }) {
+                setFragment(IntroduceNormalFragment())
+            } else {
+                setFragment(IntroducePermissionManualFragment(this))
+                Log.e("XDScanWifiActivity", "requestPermission: 权限被拒绝")
             }
+
+            requesting = false
+        }
         permissionUtils = PermissionUtils(this)
+    }
+
+    override fun onResume() {
+        super.onResume()
         checkAndSetFragment()
     }
 
@@ -34,21 +45,39 @@ abstract class AbsIntroduceActivity : BaseActivity(), View.OnClickListener,
         if (permissionUtils.checkAllPermissions()) {
             setFragment(IntroduceNormalFragment())
         } else {
-            setFragment(IntroducePermissionFragment(this))
+            if (permissionUtils.shouldShowRequestPermissionRationale()) {
+                setFragment(IntroducePermissionManualFragment(this))
+            } else {
+                setFragment(IntroducePermissionFragment(this))
+            }
         }
     }
 
     override fun getLayoutId(): Int = R.layout.activity_introduce
 
-    override fun onClick(v: View?) {
-        when (v?.id) {
-            R.id.btn_intro_start -> startActivity()
+    override fun requestPermission() {
+        if (requesting) {
+            return;
         }
-    }
 
-    override fun requestPermission() {
+        requesting = true
         requestPermission.launch(permissionUtils.requestAllPermissions())
     }
 
+    override fun manualRequestPermission() {
+        try {
+            val settingsIntent = Intent()
+            settingsIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
+            settingsIntent.addCategory(Intent.CATEGORY_DEFAULT)
+            settingsIntent.setData(Uri.parse("package:" + this.packageName))
+            settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+            settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
+            settingsIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
+            this.startActivity(settingsIntent)
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+    }
+
     abstract fun startActivity()
 }

+ 5 - 2
xiaodou/src/main/java/com/luojigou/product/intro/IntroduceNormalFragment.kt

@@ -18,7 +18,7 @@ class IntroduceNormalFragment : BaseFragment(), View.OnClickListener {
             R.layout.fragment_introduce_normal,
             container,
             false,
-            )
+        )
     }
 
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -30,7 +30,10 @@ class IntroduceNormalFragment : BaseFragment(), View.OnClickListener {
 
     override fun onClick(v: View?) {
         when (v?.id) {
-            R.id.btn_intro_start -> requireActivity()
+            R.id.btn_intro_start -> {
+                val activity = requireActivity() as AbsIntroduceActivity
+                activity.startActivity()
+            }
         }
     }
 }

+ 30 - 0
xiaodou/src/main/java/com/luojigou/product/intro/IntroducePermissionManualFragment.kt

@@ -0,0 +1,30 @@
+package com.luojigou.product.intro
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.Button
+import com.luojigou.product.R
+import com.luojigou.product.base.BaseFragment
+
+class IntroducePermissionManualFragment (private val host: Host) : BaseFragment() {
+    override fun onCreateView(
+        inflater: LayoutInflater,
+        container: ViewGroup?,
+        savedInstanceState: Bundle?
+    ): View? {
+        return inflater.inflate(R.layout.fragment_introduce_permission_manual, container, false)
+    }
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+
+        val button = view.findViewById<Button>(R.id.btn_intro_request)
+        button.setOnClickListener { host.manualRequestPermission() }
+    }
+
+    interface Host {
+        fun manualRequestPermission()
+    }
+}

+ 16 - 1
xiaodou/src/main/java/com/luojigou/product/utils/PermissionUtils.kt

@@ -1,6 +1,7 @@
 package com.luojigou.product.utils
 
 import android.Manifest
+import android.app.Activity
 import android.content.Context
 import android.content.pm.PackageManager
 import android.os.Build
@@ -34,8 +35,16 @@ internal class PermissionUtils(private val context: Context) {
             }
         }
 
+        private val notificationPermissions: Array<String> by lazy {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                arrayOf(Manifest.permission.POST_NOTIFICATIONS)
+            } else {
+                arrayOf()
+            }
+        }
+
         private val permissions: Array<String> by lazy {
-            blePermissions + wifiPermissions
+            blePermissions + wifiPermissions + notificationPermissions
         }
     }
 
@@ -45,6 +54,12 @@ internal class PermissionUtils(private val context: Context) {
         } == null
     }
 
+    fun shouldShowRequestPermissionRationale(): Boolean {
+        return permissions.find {
+            !ActivityCompat.shouldShowRequestPermissionRationale(context as Activity, it)
+        } != null
+    }
+
     fun requestAllPermissions(): Array<String> {
         return permissions.filter {
             ActivityCompat.checkSelfPermission(context, it) != PackageManager.PERMISSION_GRANTED

+ 17 - 11
xiaodou/src/main/res/layout/fragment_introduce_permission.xml

@@ -13,43 +13,49 @@
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
+            android:layout_marginVertical="8dp"
             android:gravity="center"
-            android:text="权限申请说明" />
+            android:text="权限申请说明"
+            android:textFontWeight="600"
+            android:textSize="14pt" />
 
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity=""
+            android:lineHeight="18dp"
             android:paddingHorizontal="20dp"
-            android:text="通过手机给设备配网过程中会用到蓝牙、WiFi等功能,这些功能需要用户授权相关权限后才可正常使用。" />
+            android:text="        通过手机/平板给设备配网的过程中会用到蓝牙、WiFi等功能,这些功能需要用户授权相关权限后才可正常使用,本软件保证绝对不滥用和记录您的蓝牙、WIFI信息。"
+            android:textSize="14dp" />
 
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity=""
+            android:layout_marginVertical="12dp"
             android:paddingHorizontal="20dp"
-            android:text="蓝牙权限说明" />
+            android:text="蓝牙权限说明"
+            android:textFontWeight="500"
+            android:textSize="16dp" />
 
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity=""
             android:paddingHorizontal="20dp"
-            android:text="" />
+            android:text="        本功能在配网过程中需要使用蓝牙来扫描和发现设备,以及连接设备和传输数据,应用需要通过蓝牙与设备互相传递数据。" />
 
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity=""
+            android:layout_marginVertical="12dp"
             android:paddingHorizontal="20dp"
-            android:text="WIFI权限说明" />
+            android:text="WIFI权限说明"
+            android:textFontWeight="500"
+            android:textSize="16dp" />
 
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity=""
             android:paddingHorizontal="20dp"
-            android:text="" />
+            android:text="        本功能在配网过程中需要使用WIFI来扫描和发现附近的可用网络,应用需要选择可用网络来给设备配置可用的WIFI。" />
     </LinearLayout>
 
     <Button

+ 74 - 0
xiaodou/src/main/res/layout/fragment_introduce_permission_manual.xml

@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1"
+        android:orientation="vertical">
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginVertical="8dp"
+            android:gravity="center"
+            android:text="权限申请说明"
+            android:textColor="#FF666666"
+            android:textFontWeight="600"
+            android:textSize="22dp" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:lineHeight="18dp"
+            android:paddingHorizontal="20dp"
+
+            android:text="        通过手机/平板给设备配网的过程中会用到蓝牙、WiFi等功能,这些功能需要用户授权相关权限后才可正常使用,本软件保证绝对不滥用和记录您的蓝牙、WIFI信息。"
+            android:textColor="#FF999999"
+            android:textSize="14dp" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginVertical="12dp"
+            android:paddingHorizontal="20dp"
+            android:text="蓝牙权限说明"
+            android:textColor="#FF333333"
+            android:textFontWeight="500"
+            android:textSize="16dp" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingHorizontal="20dp"
+            android:text="        本功能在配网过程中需要使用蓝牙来扫描和发现设备,以及连接设备和传输数据,应用需要通过蓝牙与设备互相传递数据。"
+            android:textColor="#FF999999" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginVertical="12dp"
+            android:paddingHorizontal="20dp"
+            android:text="WIFI权限说明"
+            android:textColor="#FF333333"
+            android:textFontWeight="500"
+            android:textSize="16dp" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingHorizontal="20dp"
+            android:text="        本功能在配网过程中需要使用WIFI来扫描和发现附近的可用网络,应用通过选择可用网络来给设备配置WIFI网络。"
+            android:textColor="#FF999999" />
+    </LinearLayout>
+
+    <Button
+        android:id="@+id/btn_intro_request"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_margin="16dp"
+        android:text="手动去设置授予" />
+</LinearLayout>

+ 17 - 12
xiaodou/src/main/res/layout/item_product.xml

@@ -1,20 +1,25 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:orientation="vertical">
-
-    <ImageView
-        android:id="@+id/iv_product_img"
-        android:layout_width="match_parent"
-        android:layout_height="160dp"
-        android:layout_margin="16dp" />
+    android:layout_height="64dp"
+    android:layout_marginVertical="4dp"
+    android:orientation="horizontal">
 
     <TextView
         android:id="@+id/tv_product_name"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_horizontal"
-        android:text="" />
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_marginStart="16dp"
+        android:layout_weight="1"
+        android:gravity="center_vertical"
+        android:text=""
+        android:textSize="14pt" />
+
+    <ImageView
+        android:id="@+id/iv_product_img"
+        android:layout_width="64dp"
+        android:layout_height="64dp"
+        android:scaleType="fitCenter"
+        android:layout_marginHorizontal="16dp" />
 
 </LinearLayout>