Commit 8235460e authored by mengcuiguang's avatar mengcuiguang

首页添加网络、网速,自启动优化

parent 622a8d13
......@@ -169,6 +169,7 @@ dependencies {
implementation Deps.koin_androidx_viewmodel
implementation Deps.grandcentrix_sharedPreference
implementation Deps.rxpermissions
// BASE64Decoder
implementation files('libs/sun.misc.BASE64Decoder.jar')
......
......@@ -2,10 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mints.cleaner">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<application
android:name=".App"
......@@ -22,12 +25,17 @@
<activity android:name=".ui.albumClean.AlbumCleanActivity" />
<activity
android:name=".ui.multiClean.MultiCleanActivity"
android:name=".ui.NavigationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="portrait"></activity>
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 屏幕适配 -->
<meta-data
......@@ -42,17 +50,6 @@
android:launchMode="singleTop"
android:theme="@style/CustomerTransparentTheme" />
<activity
android:name=".ui.NavigationActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
......
package com.mints.cleaner.adapter.home
import android.text.TextUtils
import android.widget.GridView
import android.widget.ImageView
import android.widget.SimpleAdapter
......@@ -8,6 +9,8 @@ import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mints.cleaner.R
import com.mints.cleaner.util.LogUtil
import com.mints.cleaner.util.NetSpeed
import com.mints.cleaner.util.NetWorkUtils
class HomeAdapter : BaseMultiItemQuickAdapter<HomeMultiItem, BaseViewHolder> {
......@@ -21,6 +24,8 @@ class HomeAdapter : BaseMultiItemQuickAdapter<HomeMultiItem, BaseViewHolder> {
when (helper.itemViewType) {
HomeMultiItem.ITEM_TOP -> {
helper.addOnClickListener(R.id.btn_clean)
setCleanTopItemStyle(helper)
}
HomeMultiItem.ITEM_MID -> {
val gridView = helper.getView<GridView>(R.id.gv_clean)
......@@ -48,6 +53,31 @@ class HomeAdapter : BaseMultiItemQuickAdapter<HomeMultiItem, BaseViewHolder> {
}
}
/**
* top样式
*/
private fun setCleanTopItemStyle(helper: BaseViewHolder) {
// 是否连网
val isNetwork = NetWorkUtils.isNetworkAvailable(mContext)
if (isNetwork) {
// 是否连接wifi
val wifiName = NetWorkUtils.getConnectWifiSsid(mContext)
if (!TextUtils.isEmpty(wifiName) && wifiName!!.contains(mContext.getString(R.string.no_wifi))) {
helper.setText(R.id.tv_wifi, mContext.getString(R.string.no_authorize))
helper.setText(R.id.tv_wifi_status, mContext.getString(R.string.no_connect_wifi))
} else {
helper.setText(R.id.tv_wifi, wifiName)
helper.setText(R.id.tv_wifi_status, mContext.getString(R.string.connect_wifi))
}
helper.setText(R.id.tv_wifi_speed, NetSpeed.getNetSpeed(mContext))
} else {
helper.setText(R.id.tv_wifi, mContext.getString(R.string.no_network))
helper.setText(R.id.tv_wifi_status, mContext.getString(R.string.no_connect_wifi))
}
}
private var onGridViewItemClickListener: OnGridViewItemClickListener? = null
fun setOnGridViewItemClickListener(listener: OnGridViewItemClickListener?) {
......
package com.mints.cleaner.service.keep
import android.app.Activity
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
/**
* 一像素的Activity
*/
class PixelActivity : AppCompatActivity() {
class PixelActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......
......@@ -54,7 +54,7 @@ class MainFragment : BaseFragment() {
private fun initViewPager() {
mainViewpager.isUserInputEnabled = false
mainViewpager.offscreenPageLimit = 2
mainViewpager.offscreenPageLimit = 1
mainViewpager.adapter = object : FragmentStateAdapter(this) {
override fun createFragment(position: Int) = fragmentList[position]
......
package com.mints.cleaner.ui
import android.content.Intent
import android.view.KeyEvent
import com.mints.cleaner.App
import com.mints.cleaner.R
import com.mints.cleaner.service.ScreenService
......@@ -17,4 +18,11 @@ class NavigationActivity : BaseActivity() {
override fun initData() {
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
// 直接返回后台
KeyEvent.KEYCODE_BACK -> moveTaskToBack(true)
}
return true
}
}
\ No newline at end of file
package com.mints.cleaner.ui.home
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.DefaultItemAnimator
import com.mints.cleaner.R
import com.mints.cleaner.adapter.home.HomeAdapter
import com.mints.cleaner.databinding.FragmentCleanBinding
......@@ -11,18 +13,28 @@ import com.mints.cleaner.ui.PreviewActivity
import com.mints.cleaner.ui.albumClean.AlbumCleanActivity
import com.mints.cleaner.ui.main.MainViewModel
import com.mints.cleaner.ui.multiClean.MultiCleanActivity
import com.mints.cleaner.util.LogUtil
import com.mints.cleaner.util.NetSpeed
import com.mints.cleaner.util.WeakHandler
import com.mints.core.base.BaseVMFragment
import com.mints.ktx.ext.startKtxActivity
import com.mints.ktx.ext.toast
import kotlinx.android.synthetic.main.fragment_clean.*
import org.koin.androidx.viewmodel.ext.android.viewModel
/**
* 主页面
*/
class HomeFragment : BaseVMFragment<FragmentCleanBinding>(R.layout.fragment_clean) {
class HomeFragment : BaseVMFragment<FragmentCleanBinding>(R.layout.fragment_clean),
WeakHandler.IHandler {
private val mainViewModel by viewModel<MainViewModel>()
private val cleanAdapter by lazy { HomeAdapter(mainViewModel.repository.getHomeData()) }
private val speedHandler by lazy { WeakHandler(this) }
// 是否暂停
private var speedIsPause: Boolean = false
override fun initView() {
binding.run {
......@@ -40,9 +52,32 @@ class HomeFragment : BaseVMFragment<FragmentCleanBinding>(R.layout.fragment_clea
}
initRecyclerView()
}
override fun onResume() {
super.onResume()
LogUtil.e("onResume" + speedIsPause)
if (!speedIsPause) {
speedIsPause = true
speedHandler.sendEmptyMessageDelayed(NetSpeed.SPEED_MSG, NetSpeed.SPEED_INTERVAL)
}
}
override fun onPause() {
super.onPause()
LogUtil.e("onPause" + speedIsPause)
speedIsPause = false
}
private fun initRecyclerView() {
// 解决动画产生的item闪屏
(rv_clean.getItemAnimator() as DefaultItemAnimator).supportsChangeAnimations = false
cleanAdapter.run {
addFooterView(LayoutInflater.from(activity).inflate(R.layout.home_footer, null))
......@@ -114,4 +149,15 @@ class HomeFragment : BaseVMFragment<FragmentCleanBinding>(R.layout.fragment_clea
}
override fun handleMsg(msg: Message?) {
if (speedIsPause) {
msg?.let {
if (it.what == NetSpeed.SPEED_MSG) {
cleanAdapter.notifyItemChanged(0)
speedHandler.sendEmptyMessageDelayed(NetSpeed.SPEED_MSG, NetSpeed.SPEED_INTERVAL)
}
}
}
}
}
\ No newline at end of file
package com.mints.cleaner.ui.login
import android.Manifest
import android.os.Bundle
import android.text.TextUtils
import com.mints.cleaner.R
import com.mints.cleaner.databinding.ActivityLoginBinding
import com.mints.core.base.BaseVMActivity
......@@ -7,6 +10,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.koin.androidx.viewmodel.ext.android.viewModel
import androidx.lifecycle.Observer
import com.mints.cleaner.util.LogUtil
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.android.synthetic.main.activity_login.*
......@@ -19,6 +23,19 @@ class LoginActivity : BaseVMActivity() {
binding.run {
viewModel = loginViewModel
}
// RxPermissions(this@LoginActivity)
// .request(
// Manifest.permission.ACCESS_COARSE_LOCATION,
// Manifest.permission.ACCESS_FINE_LOCATION
// )
// .subscribe { granted: Boolean ->
// if (granted) {
// // 已授权
// } else {
// //未授权
// }
// }
}
override fun initData() {
......
package com.mints.cleaner.util
import android.content.Context
import android.net.TrafficStats
/**
* 获取网速
*/
object NetSpeed {
val SPEED_INTERVAL: Long = 1000 //获取网速速度 1秒
val SPEED_MSG = 101
private var lastTotalRxBytes: Long = 0
private var lastTimeStamp: Long = 0
/**
* 获取网速
*/
fun getNetSpeed(context: Context): String {
val nowTotalRxBytes = getTotalRxBytes(context.applicationInfo.uid)
val nowTimeStamp = System.currentTimeMillis()
val speed =
(nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp) //毫秒转换
lastTimeStamp = nowTimeStamp
lastTotalRxBytes = nowTotalRxBytes
return "$speed kb/s"
}
private fun getTotalRxBytes(uid: Int): Long {
return if (TrafficStats.getUidRxBytes(uid) == TrafficStats.UNSUPPORTED.toLong()) 0 else TrafficStats.getTotalRxBytes() / 1024 //转为KB
}
}
\ No newline at end of file
package com.mints.cleaner.util
import android.content.Context
import android.content.Context.WIFI_SERVICE
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
class NetWorkUtils {
......@@ -13,5 +16,12 @@ class NetWorkUtils {
val info = manager.activeNetworkInfo
return !(null == info || !info.isAvailable)
}
fun getConnectWifiSsid(context: Context): String? {
val wifiManager =
context.getSystemService(WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
return wifiInfo.ssid
}
}
}
\ No newline at end of file
package com.mints.cleaner.util
import android.os.Handler
import android.os.Looper
import android.os.Message
import java.lang.ref.WeakReference
class WeakHandler : Handler {
interface IHandler {
fun handleMsg(msg: Message?)
}
private val mRef: WeakReference<IHandler>
constructor(handler: IHandler) {
mRef = WeakReference(handler)
}
constructor(looper: Looper?, handler: IHandler) : super(looper) {
mRef = WeakReference(handler)
}
override fun handleMessage(msg: Message) {
val handler = mRef.get()
if (handler != null && msg != null) handler.handleMsg(msg)
}
}
\ No newline at end of file
......@@ -27,6 +27,7 @@
app:title="@{title}" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_clean"
adapter="@{adapter}"
android:layout_width="match_parent"
android:layout_height="match_parent"
......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<FrameLayout
android:id="@+id/fl_wifi"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true">
<com.mints.cleaner.widget.CircleProgress
android:id="@+id/cp_wifi"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
app:antiAlias="true"
app:arcWidth="14dp"
app:bgArcColor="#E8E8E8"
app:bgArcColor="#fff"
app:bgArcWidth="14dp"
app:maxValue="16"
app:startAngle="135"
app:sweepAngle="270"
app:value="0" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_wifi"></ImageView>
<TextView
android:id="@+id/tv_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:textColor="@color/white"
android:textSize="20sp"
tools:text="test"></TextView>
<TextView
android:id="@+id/tv_wifi_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Wifi 已连接"
android:textColor="@color/white"
android:textSize="12sp"></TextView>
</LinearLayout>
</FrameLayout>
<Button
android:id="@+id/btn_clean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cp_wifi"
android:layout_below="@id/fl_wifi"
android:layout_centerHorizontal="true"
android:background="@drawable/btn_circle"
android:paddingStart="30dp"
......
......@@ -44,5 +44,10 @@
<string name="selector_all_image">全部图片</string>
<string name="empty_text">正在扫描...</string>
<string name="no_wifi">unknown ssid</string>
<string name="no_authorize">未授权</string>
<string name="no_connect_wifi">Wifi 未连接</string>
<string name="connect_wifi">Wifi 已连接</string>
<string name="no_network">无网络</string>
</resources>
\ No newline at end of file
......@@ -45,6 +45,7 @@ object Versions {
const val grandcentrix_sharedPreference = "0.12.0"
const val retrofit_adapter_rxjava = "2.1.0"
const val orhanobut_logger = "2.2.0"
const val rxpermissions = "0.9.3@aar"
}
object Deps {
......@@ -94,5 +95,6 @@ object Deps {
const val koin_androidx_viewmodel = "org.koin:koin-androidx-viewmodel:${Versions.koin}"
// 类似sharedPreference 可多进程间访问
const val grandcentrix_sharedPreference = "net.grandcentrix.tray:tray:${Versions.grandcentrix_sharedPreference}"
const val rxpermissions = "com.tbruyelle.rxpermissions:rxpermissions:${Versions.rxpermissions}"
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment