Commit 426fecc0 authored by wanglei's avatar wanglei

...

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