Commit 426fecc0 authored by wanglei's avatar wanglei

...

parent 5f7318cb
...@@ -3,6 +3,7 @@ package com.base.browserwhite.fcm ...@@ -3,6 +3,7 @@ package com.base.browserwhite.fcm
import android.content.Context import android.content.Context
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
import android.util.Log
import com.base.browserwhite.MyApplication import com.base.browserwhite.MyApplication
import com.base.browserwhite.bean.ConstObject import com.base.browserwhite.bean.ConstObject
import com.base.browserwhite.bean.ConstObject.ID_APP_PROCESS import com.base.browserwhite.bean.ConstObject.ID_APP_PROCESS
...@@ -12,6 +13,7 @@ import com.base.browserwhite.bean.ConstObject.ID_NEWS ...@@ -12,6 +13,7 @@ import com.base.browserwhite.bean.ConstObject.ID_NEWS
import com.base.browserwhite.bean.ConstObject.ID_WEATHER import com.base.browserwhite.bean.ConstObject.ID_WEATHER
import com.base.browserwhite.help.EventUtils import com.base.browserwhite.help.EventUtils
import com.base.browserwhite.utils.AppPreferences import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.TimeUtils.formatTimeH import com.base.browserwhite.utils.TimeUtils.formatTimeH
import com.base.browserwhite.utils.TimeUtils.isTimeBetweenEightAndTenPM import com.base.browserwhite.utils.TimeUtils.isTimeBetweenEightAndTenPM
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
...@@ -20,6 +22,7 @@ import java.util.Locale ...@@ -20,6 +22,7 @@ import java.util.Locale
object NotificationPushUtil { object NotificationPushUtil {
private val TAG = "NotificationPushUtil"
private var handlerThread: HandlerThread? = null private var handlerThread: HandlerThread? = null
private var handler: Handler? = null private var handler: Handler? = null
...@@ -51,11 +54,8 @@ object NotificationPushUtil { ...@@ -51,11 +54,8 @@ object NotificationPushUtil {
return return
} }
if (!canPushNotification(actionId, where)) { NotificationUiUtil.sendNotification(this, actionId, where)
return
}
NotificationUiUtil.sendNotification(this, actionId)
EventUtils.event("showNotification", where, null, false) EventUtils.event("showNotification", where, null, false)
// AppPreferences.getInstance().putInt("notificationCount_${getCurrentDate()}", todayShowCount + 1) // AppPreferences.getInstance().putInt("notificationCount_${getCurrentDate()}", todayShowCount + 1)
...@@ -66,7 +66,7 @@ object NotificationPushUtil { ...@@ -66,7 +66,7 @@ object NotificationPushUtil {
/** /**
* 是否可以推送判断 * 是否可以推送判断
*/ */
private fun canPushNotification(actionId: Int, where: String): Boolean { fun canPushNotification(actionId: Int, where: String): Boolean {
//默认可以推送 //默认可以推送
var canPush: Boolean = true var canPush: Boolean = true
...@@ -95,14 +95,17 @@ object NotificationPushUtil { ...@@ -95,14 +95,17 @@ object NotificationPushUtil {
ID_NEWS -> { ID_NEWS -> {
canPush = canPushNews(where) canPush = canPushNews(where)
LogEx.logDebug(TAG, "ID_NEWS canPush=$canPush")
} }
ID_WEATHER -> { ID_WEATHER -> {
canPush = canPushWeather(where) canPush = canPushWeather(where)
LogEx.logDebug(TAG, "ID_WEATHER canPush=$canPush")
} }
ID_APP_PROCESS -> { ID_APP_PROCESS -> {
canPush = canPushAppProcess(where) canPush = canPushAppProcess(where)
LogEx.logDebug(TAG, "ID_APP_PROCESS canPush=$canPush")
} }
} }
...@@ -156,12 +159,14 @@ object NotificationPushUtil { ...@@ -156,12 +159,14 @@ object NotificationPushUtil {
newsRunnable = Runnable { newsRunnable = Runnable {
LogEx.logDebug(TAG, "beginNewsPushTimber")
context.sendNotificationWhere(ID_NEWS, PUSH_WHERE_TIMBER) context.sendNotificationWhere(ID_NEWS, PUSH_WHERE_TIMBER)
newsRunnable?.let { newsHandler?.postDelayed(it, 30 * 60 * 1000) } newsRunnable?.let { newsHandler?.postDelayed(it, 30 * 60 * 1000) }
} }
newsRunnable?.let { newsHandler?.postDelayed(it, 5 * 60 * 1000) } // newsRunnable?.let { newsHandler?.postDelayed(it, 5 * 60 * 1000) }
newsRunnable?.let { newsHandler?.post(it) }
} }
fun stopNotificationHandlerNews() { fun stopNotificationHandlerNews() {
...@@ -207,6 +212,7 @@ object NotificationPushUtil { ...@@ -207,6 +212,7 @@ object NotificationPushUtil {
//新闻 //新闻
private fun canPushNews(where: String): Boolean { private fun canPushNews(where: String): Boolean {
return true
if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) { if (where == PUSH_WHERE_TIMBER || where == PUSH_WHERE_UNLOCK) {
val time = System.currentTimeMillis().formatTimeH() val time = System.currentTimeMillis().formatTimeH()
val lashPushTime = AppPreferences.getInstance().getLong("last_news_notification_time", 0).formatTimeH() val lashPushTime = AppPreferences.getInstance().getLong("last_news_notification_time", 0).formatTimeH()
......
...@@ -25,7 +25,7 @@ import com.base.browserwhite.bean.ConstObject.ID_UNINSTALL_APP ...@@ -25,7 +25,7 @@ import com.base.browserwhite.bean.ConstObject.ID_UNINSTALL_APP
import com.base.browserwhite.bean.ConstObject.ID_WEATHER import com.base.browserwhite.bean.ConstObject.ID_WEATHER
import com.base.browserwhite.bean.NewsBean import com.base.browserwhite.bean.NewsBean
import com.base.browserwhite.bean.WeatherBean import com.base.browserwhite.bean.WeatherBean
import com.base.browserwhite.help.NewsUtils.getNews import com.base.browserwhite.help.NewsUtils.getNewSp
import com.base.browserwhite.utils.TimeUtils.formatATime import com.base.browserwhite.utils.TimeUtils.formatATime
import com.base.browserwhite.utils.TimeUtils.isDayOrNight import com.base.browserwhite.utils.TimeUtils.isDayOrNight
import com.base.browserwhite.help.WeatherUtils import com.base.browserwhite.help.WeatherUtils
...@@ -34,8 +34,8 @@ import com.base.browserwhite.ui.activity.splash.Splash2Activity ...@@ -34,8 +34,8 @@ import com.base.browserwhite.ui.activity.splash.Splash2Activity
import com.base.browserwhite.utils.AppPreferences import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.ImageUtils.getBitmapFromURL import com.base.browserwhite.utils.ImageUtils.getBitmapFromURL
import com.base.browserwhite.utils.KotlinExt.toFormatSize import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.RamUtils.ramPair import com.base.browserwhite.utils.RamUtils.ramPair
import com.base.browserwhite.utils.TimeUtils.formatTimeH
import kotlin.random.Random import kotlin.random.Random
...@@ -45,11 +45,18 @@ import kotlin.random.Random ...@@ -45,11 +45,18 @@ import kotlin.random.Random
*/ */
object NotificationUiUtil { object NotificationUiUtil {
private val TAG = "NotificationUiUtil"
private const val CHANNEL_ID = "browser_notification_id" // 通知渠道ID private const val CHANNEL_ID = "browser_notification_id" // 通知渠道ID
private const val CHANNEL_NAME = "browser_fcm_channel" // 通知渠道名称 private const val CHANNEL_NAME = "browser_fcm_channel" // 通知渠道名称
@SuppressLint("RemoteViewLayout") @SuppressLint("RemoteViewLayout")
fun sendNotification(context: Context, actionId: Int) { fun sendNotification(context: Context, actionId: Int, where: String) {
if (!NotificationPushUtil.canPushNotification(actionId, where)) {
return
}
when (actionId) { when (actionId) {
ID_JUNK_CLEANER, ID_INSTALL_APP, ID_UNINSTALL_APP -> {//清理 ID_JUNK_CLEANER, ID_INSTALL_APP, ID_UNINSTALL_APP -> {//清理
var size = fastGetJunkSize(context) var size = fastGetJunkSize(context)
...@@ -82,11 +89,20 @@ object NotificationUiUtil { ...@@ -82,11 +89,20 @@ object NotificationUiUtil {
val mainHandler = Handler(Looper.getMainLooper()) val mainHandler = Handler(Looper.getMainLooper())
Thread { Thread {
val news: NewsBean = getNews()?.random() ?: return@Thread val news: NewsBean? = getNewSp()?.random()
val bitmap = getBitmapFromURL(news.orgImgPath) ?: return@Thread if (news == null) {
LogEx.logDebug(TAG, "NewsBean==null")
return@Thread
}
val bitmap = getBitmapFromURL(news.orgImgPath)
if (bitmap == null) {
LogEx.logDebug(TAG, "bitmap==null")
return@Thread
}
mainHandler.post { mainHandler.post {
val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_big_small) val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_news_big)
bigRemoteViews.setTextViewText(R.id.tv_head_line, news.headline) bigRemoteViews.setTextViewText(R.id.tv_head_line, news.headline)
bigRemoteViews.setImageViewBitmap(R.id.iv_media, bitmap) bigRemoteViews.setImageViewBitmap(R.id.iv_media, bitmap)
......
...@@ -41,6 +41,16 @@ object NewsUtils { ...@@ -41,6 +41,16 @@ object NewsUtils {
errorCallBack: (() -> Unit)? = null, errorCallBack: (() -> Unit)? = null,
beanCallBack: (beanList: List<NewsBean>) -> Unit beanCallBack: (beanList: List<NewsBean>) -> Unit
) { ) {
//第一次请求走sp
if (pageNumber == null) {
val list = getNewSp()
if (list.isNotEmpty()) {
beanCallBack.invoke(list)
return
}
}
val pkg = ConfigHelper.packageName val pkg = ConfigHelper.packageName
val s = JSONObject() val s = JSONObject()
...@@ -94,6 +104,7 @@ object NewsUtils { ...@@ -94,6 +104,7 @@ object NewsUtils {
LogEx.logDebug(TAG, "NewsUtils it=$it") LogEx.logDebug(TAG, "NewsUtils it=$it")
val dataJson = getDataJson(it) val dataJson = getDataJson(it)
LogEx.logDebug(TAG, "dataJson dataJson=$dataJson") LogEx.logDebug(TAG, "dataJson dataJson=$dataJson")
saveNewsSp(dataJson)
val list = parseNewsBean(dataJson) val list = parseNewsBean(dataJson)
LogEx.logDebug(TAG, "${list.size}") LogEx.logDebug(TAG, "${list.size}")
beanCallBack.invoke(list) beanCallBack.invoke(list)
...@@ -104,59 +115,59 @@ object NewsUtils { ...@@ -104,59 +115,59 @@ object NewsUtils {
} }
fun getNews(): List<NewsBean>? { // fun getNews(): List<NewsBean>? {
val pkg = ConfigHelper.packageName // val pkg = ConfigHelper.packageName
val s = JSONObject() // val s = JSONObject()
//
s.put("categoryId", 0) // s.put("categoryId", 0)
val s2 = JSONObject() // val s2 = JSONObject()
.put("${pkg}_3", AppPreferences.getInstance().getString("Equipment", "")) // .put("${pkg}_3", AppPreferences.getInstance().getString("Equipment", ""))
.put("${pkg}_4", AppPreferences.getInstance().getString("Manufacturer", "")) // .put("${pkg}_4", AppPreferences.getInstance().getString("Manufacturer", ""))
.put("${pkg}_5", AppPreferences.getInstance().getString("svn", "")) // .put("${pkg}_5", AppPreferences.getInstance().getString("svn", ""))
.put("${pkg}_8", BuildConfig.VERSION_NAME) // .put("${pkg}_8", BuildConfig.VERSION_NAME)
.put("${pkg}_9", AppPreferences.getInstance().getString("uuid", "")) // .put("${pkg}_9", AppPreferences.getInstance().getString("uuid", ""))
.put("${pkg}_10", AppPreferences.getInstance().getString("gid", "")) // .put("${pkg}_10", AppPreferences.getInstance().getString("gid", ""))
.put("${pkg}_13", "android") // .put("${pkg}_13", "android")
.put("${pkg}_14", BuildConfig.VERSION_CODE) // .put("${pkg}_14", BuildConfig.VERSION_CODE)
.put("${pkg}_15", "google") // .put("${pkg}_15", "google")
.put("${pkg}_24", BuildConfig.BUILD_TYPE) // .put("${pkg}_24", BuildConfig.BUILD_TYPE)
//
val data = JSONObject() // val data = JSONObject()
.put("data", s) // .put("data", s)
.put("bp", s2) // .put("bp", s2)
.toString() // .toString()
//
val body = AESHelper.encrypt(data) // val body = AESHelper.encrypt(data)
.toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull()) // .toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
//
val client = OkHttpClient.Builder() // val client = OkHttpClient.Builder()
.apply { // .apply {
if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) {
addInterceptor(HttpLoggingInterceptor().apply { // addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY // level = HttpLoggingInterceptor.Level.BODY
}) // })
} // }
connectTimeout(10, TimeUnit.SECONDS) // connectTimeout(10, TimeUnit.SECONDS)
readTimeout(20, TimeUnit.SECONDS) // readTimeout(20, TimeUnit.SECONDS)
writeTimeout(15, TimeUnit.SECONDS) // writeTimeout(15, TimeUnit.SECONDS)
}.build() // }.build()
//
val request = Request.Builder() // val request = Request.Builder()
.url(url) // .url(url)
.post(body) // .post(body)
.build() // .build()
//
val response = client.newCall(request).execute() // val response = client.newCall(request).execute()
response.body?.string()?.let { // response.body?.string()?.let {
LogEx.logDebug(TAG, "NewsUtils it=$it") // LogEx.logDebug(TAG, "NewsUtils it=$it")
val dataJson = getDataJson(it) // val dataJson = getDataJson(it)
LogEx.logDebug(TAG, "dataJson dataJson=$dataJson") // LogEx.logDebug(TAG, "dataJson dataJson=$dataJson")
val list = parseNewsBean(dataJson) // val list = parseNewsBean(dataJson)
LogEx.logDebug(TAG, "${list.size}") // LogEx.logDebug(TAG, "${list.size}")
return list // return list
} // }
return null // return null
} // }
private fun getDataJson(jsonString: String): String { private fun getDataJson(jsonString: String): String {
val jsonRootObject = JsonParser.parseString(jsonString).getAsJsonObject() val jsonRootObject = JsonParser.parseString(jsonString).getAsJsonObject()
...@@ -165,7 +176,21 @@ object NewsUtils { ...@@ -165,7 +176,21 @@ object NewsUtils {
private fun parseNewsBean(jsonString: String): List<NewsBean> { private fun parseNewsBean(jsonString: String): List<NewsBean> {
val newsBeanListType: Type = object : TypeToken<List<NewsBean>>() {}.type val newsBeanListType: Type = object : TypeToken<List<NewsBean>>() {}.type
return Gson().fromJson(jsonString, newsBeanListType) return Gson().fromJson(jsonString, newsBeanListType) ?: listOf()
}
fun saveNewsSp(jsonString: String) {
val lastUpData = AppPreferences.getInstance().getLong("last_update_new_time", 0)
if (lastUpData == 0L || System.currentTimeMillis() - lastUpData > 1 * 60 * 60 * 1000) {
AppPreferences.getInstance().put("newsp", jsonString)
AppPreferences.getInstance().put("last_update_new_time", System.currentTimeMillis())
}
}
fun getNewSp(): List<NewsBean> {
val json = AppPreferences.getInstance().getString("newsp", "")
return parseNewsBean(json)
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="175dp" android:layout_height="175dp"
android:layout_margin="9.5dp" android:layout_margin="9.5dp"
android:scaleType="fitXY"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<TextView <TextView
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
android:layout_width="91dp" android:layout_width="91dp"
android:layout_height="63dp" android:layout_height="63dp"
android:layout_marginStart="9.5dp" android:layout_marginStart="9.5dp"
android:scaleType="fitXY"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<TextView <TextView
......
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