Browse Source

1.添加锁定布局逻辑以及素材

master
wangwanlei 11 months ago
parent
commit
cc8ec96ab3

+ 31
- 100
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt View File

33
 import com.xhly.corelib.utils.toFormat
33
 import com.xhly.corelib.utils.toFormat
34
 import com.xhly.manageapp.AppAdapter
34
 import com.xhly.manageapp.AppAdapter
35
 import com.xhly.manageapp.R
35
 import com.xhly.manageapp.R
36
+import com.xhly.manageapp.bean.strategy.StrategyBean
36
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
37
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
37
 import com.xhly.manageapp.databinding.ActivityMainBinding
38
 import com.xhly.manageapp.databinding.ActivityMainBinding
38
 import com.xhly.manageapp.service.ManageAccessibilityService
39
 import com.xhly.manageapp.service.ManageAccessibilityService
97
         mBinding.loginoutIv.setOnClickListener {
98
         mBinding.loginoutIv.setOnClickListener {
98
             showEditDialog()
99
             showEditDialog()
99
         }
100
         }
100
-        mBinding.stoptv.setOnClickListener {
101
+        mBinding.codeenterCv.setOnClickListener {
102
+            //如果密码一致,则解锁
101
             spUtils.setParam(Const.DISABLEPAD, false)
103
             spUtils.setParam(Const.DISABLEPAD, false)
104
+            useStrategy()
105
+            if (mBinding.mainLlayout.visibility == View.GONE){
106
+                Toast("当前不在可用时间。")
107
+            }
102
         }
108
         }
103
         onBackPressedDispatcher.addCallback {
109
         onBackPressedDispatcher.addCallback {
104
             //屏蔽返回
110
             //屏蔽返回
105
-            getAppInfo()
111
+
106
         }
112
         }
107
     }
113
     }
108
 
114
 
110
     override fun onResume() {
116
     override fun onResume() {
111
         super.onResume()
117
         super.onResume()
112
         AppSocketService.lastPkgName = ""/*getAppInfo()*/
118
         AppSocketService.lastPkgName = ""/*getAppInfo()*/
119
+        viewModel.updateStrategy()
113
         useStrategy()
120
         useStrategy()
114
         try {
121
         try {
115
             var s =
122
             var s =
123
     private fun useStrategy() {
130
     private fun useStrategy() {
124
         //获得sputils中的数据设置状态,以及策略
131
         //获得sputils中的数据设置状态,以及策略
125
         try {
132
         try {
126
-            var param = spUtils.getParam(Const.DISABLEPAD, false)
127
-            if (param as Boolean) {
128
-                mBinding.stoptv.visibility = View.VISIBLE
133
+            //如果存储的是锁定则锁定
134
+            var block = false
135
+            val strategyBean =
136
+                spUtils.getFromJson(Const.STRATEGYBEANKEY, StrategyBean::class.java) as StrategyBean
137
+            strategyBean?.let { bean ->
138
+                //获得是否在使用时间范围
139
+                block = StrategyUtils.getAppTimeStrategy(bean)
140
+            }
141
+            val param = spUtils.getParam(Const.DISABLEPAD, false) as Boolean
142
+            if (param || block) {
143
+                mBinding.mainLlayout.visibility = View.GONE
144
+                mBinding.lockLlayout.visibility = View.VISIBLE
129
             } else {
145
             } else {
130
-                mBinding.stoptv.visibility = View.GONE
146
+                mBinding.mainLlayout.visibility = View.VISIBLE
147
+                mBinding.lockLlayout.visibility = View.GONE
131
             }
148
             }
132
             changeWhiteList()
149
             changeWhiteList()
133
-            viewModel.updateStrategy()
134
         } catch (e: Exception) {
150
         } catch (e: Exception) {
135
-
151
+            val param = spUtils.getParam(Const.DISABLEPAD, false) as Boolean
152
+            if (param) {
153
+                mBinding.mainLlayout.visibility = View.GONE
154
+                mBinding.lockLlayout.visibility = View.VISIBLE
155
+            } else {
156
+                mBinding.mainLlayout.visibility = View.VISIBLE
157
+                mBinding.lockLlayout.visibility = View.GONE
158
+            }
136
         }
159
         }
137
     }
160
     }
138
 
161
 
186
         }
209
         }
187
     }
210
     }
188
 
211
 
189
-    private fun getAppInfo() {
190
-        //统计使用时长
191
-        var instance = Calendar.getInstance()/*  instance.set(Calendar.HOUR_OF_DAY, 0)
192
-          instance.set(Calendar.MINUTE, 0)
193
-          instance.set(Calendar.SECOND, 1)*/
194
-        var timeInMillis = instance.timeInMillis - 1000 * 60 * 10
195
-        val usageManager = getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager
196
-        if (usageManager != null) {
197
-            val intervalType = UsageStatsManager.INTERVAL_DAILY
198
-            val endTime = System.currentTimeMillis()
199
-            val startTime = timeInMillis
200
-            //queryUsageStats查询使用时长,时间范围设置后,返回的数据时间范围不一定和设置的时间范围一致。
201
-            /*   val applicationList = usageManager.queryUsageStats(intervalType, startTime, endTime)
202
-               for (usageStats in applicationList) {
203
-                   dataList.filter { it.packageName.equals(usageStats.packageName) }.forEach {
204
-                       LogShow("应用时长统计"+it.name+"使用时间"+millisecondsToHMS(usageStats.totalTimeInForeground))
205
-                   }
206
-                   if (usageStats.packageName.equals("com.xhly.manageapp")){
207
-                       LogShow("应用时长统计XhlyManageApp"+"使用时间"+millisecondsToHMS(usageStats.totalTimeInForeground))
208
-                   }
209
-               }*/
210
-
211
-            //queryEvents查询使用时长,时间范围设置后,返回的数据时间范围和设置的一致,但需要自己判断时长
212
-            var queryEvents = usageManager.queryEvents(startTime, endTime)
213
-            var event = UsageEvents.Event()
214
-
215
-            var totalTime = 0L
216
-            var totalStartTime = 0L
217
-            while (queryEvents.hasNextEvent()) {
218
-                queryEvents.getNextEvent(event)
219
-                when (event.eventType) {
220
-                    UsageEvents.Event.ACTIVITY_RESUMED -> {
221
-                        var first =
222
-                            dataList.firstOrNull { event.packageName.equals(it.packageName) }
223
-                        first?.let {
224
-                            it.startTime = event.timeStamp
225
-                        }
226
-                        if (event.packageName.equals("com.xhly.manageapp")) {
227
-                            totalStartTime = event.timeStamp
228
-                        }
229
-
230
-                        if (event.packageName.equals("mark.via")) {
231
-                            LogShow("打开时间" + event.timeStamp.toFormat())
232
-
233
-                        }
234
-                    }
235
-
236
-                    UsageEvents.Event.ACTIVITY_PAUSED -> {
237
-                        var first =
238
-                            dataList.firstOrNull { event.packageName.equals(it.packageName) }
239
-                        first?.let {
240
-                            if (it.startTime == 0L) {
241
-                                it.startTime = instance.timeInMillis
242
-                            }
243
-                            it.foregroundTime += (event.timeStamp - it.startTime)
244
-                        }
245
-                        if (event.packageName.equals("mark.via")) {
246
-                            LogShow("关闭时间" + event.timeStamp.toFormat())
247
-                        }
248
-                        if (event.packageName.equals("com.xhly.manageapp")) {
249
-                            totalTime += (event.timeStamp - totalStartTime)
250
-                        }
251
-                    }
252
-                }
253
-            }
254
-
255
-            dataList.filter { it.foregroundTime > 0L }.forEach {
256
-                // LogShow("events应用时长统计" + it.name + "使用时间" + millisecondsToHMS(it.foregroundTime))
257
-            }
258
-            if (totalTime > 0L) {
259
-                // LogShow("events应用时长统计com.xhly.manageapp使用时间" + millisecondsToHMS(totalTime))
260
-            } else {
261
-                // LogShow("events应用时长统计com.xhly.manageapp使用时间无法获取")
262
-            }
263
-        }
264
-    }
265
-
266
     fun checkAccessibility(context: Context): Boolean {
212
     fun checkAccessibility(context: Context): Boolean {
267
         // 判断辅助功能是否开启
213
         // 判断辅助功能是否开启
268
         if (!isServiceON(this, ManageAccessibilityService::class.java.name)) {
214
         if (!isServiceON(this, ManageAccessibilityService::class.java.name)) {
384
         })
330
         })
385
     }
331
     }
386
 
332
 
387
-    private fun test() {
388
-        /* val appInfo = filter[0]
389
-         serviceScope.launch {
390
-             val logStartBean = LogAppStartBean()
391
-             ManageApplication.getDeviceInfo()?.let {
392
-                 logStartBean.sn = it
393
-             }
394
-             logStartBean.appName = appInfo.name
395
-             logStartBean.appPackage = appInfo.packageName
396
-             logStartBean.versionName = appInfo.versionName
397
-             logStartBean.versionNum = appInfo.versionNum
398
-             LogShow("当前应用" + logStartBean.appName + "|||" + logStartBean.versionName + "|||" + logStartBean.versionNum)
399
-             logService.postLogAppStartAdd(logStartBean)
400
-         }*/
401
-    }
402
 
333
 
403
     private fun startWorkManager() {
334
     private fun startWorkManager() {
404
         WorkManager.getInstance(this).cancelAllWork()
335
         WorkManager.getInstance(this).cancelAllWork()

+ 46
- 8
app/src/main/java/com/xhly/manageapp/utils/StrategyUtils.kt View File

1
 package com.xhly.manageapp.utils
1
 package com.xhly.manageapp.utils
2
 
2
 
3
 import com.xhly.corelib.Const
3
 import com.xhly.corelib.Const
4
+import com.xhly.corelib.utils.hourToCurrentDateLong
4
 import com.xhly.manageapp.ManageApplication
5
 import com.xhly.manageapp.ManageApplication
5
 import com.xhly.manageapp.bean.strategy.StrategyBean
6
 import com.xhly.manageapp.bean.strategy.StrategyBean
7
+import java.util.Calendar
6
 
8
 
7
 object StrategyUtils {
9
 object StrategyUtils {
8
     public fun setStrategy(strategyBean: StrategyBean) {
10
     public fun setStrategy(strategyBean: StrategyBean) {
36
             val installPackageWhiteList = ManageApplication.getInstallPackageWhiteList()
38
             val installPackageWhiteList = ManageApplication.getInstallPackageWhiteList()
37
             ManageApplication.removeInstallPackageWhiteList(installPackageWhiteList)
39
             ManageApplication.removeInstallPackageWhiteList(installPackageWhiteList)
38
         }
40
         }
39
-        //是否开启时间策略
40
-        if (strategyBean.hastime == 1) {
41
-            var currentTimeMillis = System.currentTimeMillis()
42
 
41
 
43
-        } else {
42
+        //是否开启时间策略,这个策略在生命周期中调用getAppTimeStrategy(strategyBean)
44
 
43
 
45
-        }
46
 
44
 
47
         //是否设备策略
45
         //是否设备策略
48
         if (strategyBean.hasdevice == 1) {
46
         if (strategyBean.hasdevice == 1) {
54
         }
52
         }
55
 
53
 
56
         //违规处理,直接在事件触发时判断,不再这里处理
54
         //违规处理,直接在事件触发时判断,不再这里处理
57
-        if (strategyBean.hasviolate==1){
55
+        if (strategyBean.hasviolate == 1) {
58
 
56
 
59
         }
57
         }
60
-
61
-
62
     }
58
     }
63
 
59
 
64
     private fun deviceStrategy(strategyBean: StrategyBean) {
60
     private fun deviceStrategy(strategyBean: StrategyBean) {
108
         }
104
         }
109
     }
105
     }
110
 
106
 
107
+    /**
108
+     * 时间策略处理,返回为真则锁定
109
+     */
110
+    public fun getAppTimeStrategy(strategyBean: StrategyBean): Boolean {
111
+        //是否开启时间策略,返回为真则锁定
112
+        var lockFlag = false
113
+        val dayList = arrayListOf(2, 3, 4, 5, 6, 7, 1)
114
+        val currentTimeMillis = System.currentTimeMillis()
115
+        if (strategyBean.hastime == 1) {
116
+            val calendar = Calendar.getInstance()
117
+            //返回值 1 周日 2周一 3周二 4周三 5周四 6周五 7周六
118
+            val dayWeek = calendar.get(Calendar.DAY_OF_WEEK)
119
+            val padTimes = strategyBean.padTimes
120
+            if (padTimes == null || padTimes.size == 0) {
121
+                //开启策略,但不返回时间则表示锁定
122
+                return true
123
+            }
124
+            padTimes?.forEach { appTimeManageBean ->
125
+                val day = dayList[appTimeManageBean.week - 1]
126
+                if (day == dayWeek) {
127
+                    val starttime = appTimeManageBean.starttime
128
+                    val stoptime = appTimeManageBean.stoptime
129
+                    starttime?.let {
130
+                        val start = starttime.hourToCurrentDateLong()
131
+                        stoptime?.let {
132
+                            val end = stoptime.hourToCurrentDateLong()
133
+                            //只有有一个时段符合即为可用
134
+                            if (currentTimeMillis > start && currentTimeMillis < end) {
135
+                                //在使用范围不锁定
136
+                                return false
137
+                            } else {
138
+                                lockFlag = true
139
+                            }
140
+                        }
141
+                    }
142
+                }
143
+            }
144
+            return lockFlag
145
+        }
146
+        return false
147
+    }
148
+
111
     public fun getAppTimeStrategy() {
149
     public fun getAppTimeStrategy() {
112
 
150
 
113
     }
151
     }

BIN
app/src/main/res/drawable-xxhdpi/icon_lock.png View File


+ 9
- 0
app/src/main/res/drawable/icon_etenter.xml View File

1
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
2
+    android:width="64dp"
3
+    android:height="64dp"
4
+    android:viewportWidth="1024"
5
+    android:viewportHeight="1024">
6
+  <path
7
+      android:pathData="M330.5,17.9l450,450.6c24.1,24.1 24.1,63 0,86.5l-450.2,450.8a61.4,61.4 0,0 1,-87 0,61.4 61.4,0 0,1 0,-87.1l406.8,-406.9L243.3,105A61.4,61.4 0,0 1,286.7 0a63.1,63.1 0,0 1,43.8 17.9z"
8
+      android:fillColor="#339DFF"/>
9
+</vector>

+ 56
- 17
app/src/main/res/layout/activity_main.xml View File

1
 <?xml version="1.0" encoding="utf-8"?>
1
 <?xml version="1.0" encoding="utf-8"?>
2
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    xmlns:app="http://schemas.android.com/apk/res-auto"
3
     android:layout_width="match_parent"
4
     android:layout_width="match_parent"
4
     android:layout_height="match_parent"
5
     android:layout_height="match_parent"
5
     android:background="@drawable/main_bg"
6
     android:background="@drawable/main_bg"
10
         android:layout_height="match_parent">
11
         android:layout_height="match_parent">
11
 
12
 
12
         <LinearLayout
13
         <LinearLayout
14
+            android:id="@+id/main_llayout"
13
             android:layout_width="match_parent"
15
             android:layout_width="match_parent"
14
             android:layout_height="match_parent"
16
             android:layout_height="match_parent"
15
             android:orientation="vertical"
17
             android:orientation="vertical"
69
                 android:overScrollMode="never" />
71
                 android:overScrollMode="never" />
70
         </LinearLayout>
72
         </LinearLayout>
71
 
73
 
72
-        <TextView
73
-            android:id="@+id/timestoptv"
74
-            android:layout_width="match_parent"
75
-            android:layout_height="match_parent"
76
-            android:background="@color/white"
77
-            android:gravity="center"
78
-            android:text="@string/timestoptips"
79
-            android:textColor="@color/black"
80
-            android:textSize="20sp" />
81
-
82
-        <TextView
83
-            android:id="@+id/stoptv"
74
+        <LinearLayout
75
+            android:id="@+id/lock_llayout"
84
             android:layout_width="match_parent"
76
             android:layout_width="match_parent"
85
             android:layout_height="match_parent"
77
             android:layout_height="match_parent"
86
-            android:background="@color/white"
87
-            android:gravity="center"
88
-            android:text="@string/stoptips"
89
-            android:textColor="@color/black"
90
-            android:textSize="20sp" />
78
+            android:gravity="center_horizontal"
79
+            android:background="@drawable/main_bg"
80
+            android:orientation="vertical">
81
+
82
+            <ImageView
83
+                android:layout_width="157dp"
84
+                android:layout_height="151dp"
85
+                android:layout_marginTop="104dp"
86
+                android:src="@drawable/icon_lock" />
87
+
88
+            <FrameLayout
89
+                android:layout_width="wrap_content"
90
+                android:layout_height="53dp"
91
+                android:layout_marginTop="26dp">
92
+
93
+                <EditText
94
+                    android:id="@+id/lock_et"
95
+                    android:layout_width="357dp"
96
+                    android:layout_height="53dp"
97
+                    android:background="@drawable/shape_shadow_bg"
98
+                    android:hint="@string/passcode"
99
+                    android:paddingLeft="28dp"
100
+                    android:paddingRight="28dp"
101
+                    android:singleLine="true"
102
+                    android:textColor="@color/codetipscolor"
103
+                    android:textColorHint="@color/codehintcolor"
104
+                    android:textSize="16sp" />
105
+
106
+                <androidx.cardview.widget.CardView
107
+                    android:id="@+id/codeenter_cv"
108
+                    android:layout_width="24dp"
109
+                    android:layout_height="24dp"
110
+                    android:layout_gravity="center_vertical|right"
111
+                    android:layout_marginRight="18dp"
112
+                    app:cardBackgroundColor="@color/white">
113
+
114
+                    <ImageView
115
+                        android:layout_width="wrap_content"
116
+                        android:layout_height="11dp"
117
+                        android:layout_gravity="center"
118
+                        android:src="@drawable/icon_etenter" />
119
+                </androidx.cardview.widget.CardView>
120
+            </FrameLayout>
121
+
122
+            <TextView
123
+                android:layout_width="wrap_content"
124
+                android:layout_height="wrap_content"
125
+                android:layout_marginTop="18dp"
126
+                android:text="@string/locktips"
127
+                android:textColor="@color/codetipscolor"
128
+                android:textSize="17sp" />
129
+        </LinearLayout>
91
     </FrameLayout>
130
     </FrameLayout>
92
 </LinearLayout>
131
 </LinearLayout>

+ 2
- 0
app/src/main/res/values/colors.xml View File

7
     <color name="teal_700">#FF018786</color>
7
     <color name="teal_700">#FF018786</color>
8
     <color name="black">#FF000000</color>
8
     <color name="black">#FF000000</color>
9
     <color name="white">#FFFFFFFF</color>
9
     <color name="white">#FFFFFFFF</color>
10
+    <color name="codetipscolor">#253A70</color>
11
+    <color name="codehintcolor">#B8C2D9</color>
10
 </resources>
12
 </resources>

+ 2
- 0
app/src/main/res/values/strings.xml View File

13
     <string name="loginnow">立即登录</string>
13
     <string name="loginnow">立即登录</string>
14
     <string name="accountnoempty">账号不能为空</string>
14
     <string name="accountnoempty">账号不能为空</string>
15
     <string name="codenoempty">密码不能为空</string>
15
     <string name="codenoempty">密码不能为空</string>
16
+    <string name="passcode">密码</string>
16
     <string name="exit">退出</string>
17
     <string name="exit">退出</string>
17
     <string name="close">关闭</string>
18
     <string name="close">关闭</string>
18
     <string name="clear">清除缓存</string>
19
     <string name="clear">清除缓存</string>
19
     <string name="speed">一键加速</string>
20
     <string name="speed">一键加速</string>
21
+    <string name="locktips">您的设备已违规或者被管理员限制使用,请联系管理员</string>
20
 </resources>
22
 </resources>

Loading…
Cancel
Save