Skip to content

Commit

Permalink
调整代码样式
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenhe committed Sep 22, 2021
1 parent 3af1432 commit faf64bb
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 151 deletions.
19 changes: 11 additions & 8 deletions app/src/main/java/cc/chenhe/qqnotifyevo/MyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MyApplication : Application() {
Thread.setDefaultUncaughtExceptionHandler(CrashHandler)
Timber.tag(TAG).i("\n\n")
Timber.tag(TAG).i("==================================================")
Timber.tag(TAG).i("= App Create")
Timber.tag(TAG).i("= App Create ver: ${getVersion(this)}")
Timber.tag(TAG).i("==================================================\n")
registerNotificationChannel()

Expand Down Expand Up @@ -76,16 +76,19 @@ class MyApplication : Application() {

private fun registerNotificationChannel() {
Timber.tag(TAG).d("Register system notification channels")
val group = NotificationChannelGroup(NOTIFY_QQ_GROUP_ID, getString(R.string.notify_group_base))
val group =
NotificationChannelGroup(NOTIFY_QQ_GROUP_ID, getString(R.string.notify_group_base))


val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
val tipChannel = NotificationChannel(NOTIFY_SELF_TIPS_CHANNEL_ID,
getString(R.string.notify_self_tips_channel_name),
NotificationManager.IMPORTANCE_DEFAULT).apply {
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
val tipChannel = NotificationChannel(
NOTIFY_SELF_TIPS_CHANNEL_ID,
getString(R.string.notify_self_tips_channel_name),
NotificationManager.IMPORTANCE_DEFAULT
).apply {
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), att)
}

Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/cc/chenhe/qqnotifyevo/core/AvatarManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import kotlin.math.min
* @param period 缓存有效期(毫秒)。
*/
class AvatarManager private constructor(
private val cacheDir: File,
var period: Long
private val cacheDir: File,
var period: Long
) {

companion object {
Expand All @@ -41,8 +41,9 @@ class AvatarManager private constructor(
}
}

private class AvatarLruCache : LruCache<Int, Bitmap>(min((Runtime.getRuntime().freeMemory() / 4),
MAX_MEMORY_CACHE_SIZE).toInt()) {
private class AvatarLruCache : LruCache<Int, Bitmap>(
min((Runtime.getRuntime().freeMemory() / 4), MAX_MEMORY_CACHE_SIZE).toInt()
) {
override fun sizeOf(key: Int, value: Bitmap): Int {
return value.allocationByteCount
}
Expand Down Expand Up @@ -81,7 +82,8 @@ class AvatarManager private constructor(
lru.put(conversionId, it)
}
} catch (e: Exception) {
Timber.tag(TAG).e("Decode avatar file error, delete the cache. conversionId=$conversionId")
Timber.tag(TAG)
.e("Decode avatar file error, delete the cache. conversionId=$conversionId")
e.printStackTrace()
file.delete()
lru.remove(conversionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import java.util.*
* 将会遍历所有历史会话并分别发送通知。
*/
class InnerNotificationProcessor(
private val commander: Commander,
context: Context
private val commander: Commander,
context: Context,
) : NotificationProcessor(context) {

companion object {
Expand Down Expand Up @@ -57,14 +57,15 @@ class InnerNotificationProcessor(
}
}

private fun sendNotification(context: Context, tag: Tag, id: Int,
notification: Notification) {
private fun sendNotification(context: Context, tag: Tag, id: Int, notification: Notification) {
NotificationManagerCompat.from(context).notify(id, notification)
addNotifyId(tag, id)
}

override fun renewQzoneNotification(context: Context, tag: Tag, conversation: Conversation,
sbn: StatusBarNotification, original: Notification): Notification {
override fun renewQzoneNotification(
context: Context, tag: Tag, conversation: Conversation,
sbn: StatusBarNotification, original: Notification
): Notification {

val notification = createQZoneNotification(context, tag, conversation, original).apply {
contentIntent = original.contentIntent
Expand All @@ -76,14 +77,20 @@ class InnerNotificationProcessor(
return notification
}

override fun renewConversionNotification(context: Context, tag: Tag, channel: NotifyChannel,
conversation: Conversation, sbn: StatusBarNotification,
original: Notification): Notification {
override fun renewConversionNotification(
context: Context,
tag: Tag,
channel: NotifyChannel,
conversation: Conversation,
sbn: StatusBarNotification,
original: Notification
): Notification {
val history = getHistoryMessage(tag)
var notification: Notification? = null
for (c in history) {
if (c.name != conversation.name || c.isGroup && channel != NotifyChannel.GROUP ||
!c.isGroup && channel == NotifyChannel.GROUP) {
!c.isGroup && channel == NotifyChannel.GROUP
) {
// 确保只刷新新增的通知
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ class NevoNotificationProcessor(context: Context) : NotificationProcessor(contex
private const val REQ_MULTI_MSG_DONT_SHOW = 2
}

override fun renewQzoneNotification(context: Context, tag: Tag, conversation: Conversation,
sbn: StatusBarNotification, original: Notification): Notification {
override fun renewQzoneNotification(
context: Context,
tag: Tag,
conversation: Conversation,
sbn: StatusBarNotification,
original: Notification
): Notification {
return createQZoneNotification(context, tag, conversation, original)
}

override fun renewConversionNotification(context: Context, tag: Tag, channel: NotifyChannel,
conversation: Conversation, sbn: StatusBarNotification,
original: Notification): Notification {
override fun renewConversionNotification(
context: Context,
tag: Tag,
channel: NotifyChannel,
conversation: Conversation,
sbn: StatusBarNotification,
original: Notification
): Notification {
return createConversationNotification(context, tag, channel, conversation, original)
}

Expand All @@ -40,30 +50,42 @@ class NevoNotificationProcessor(context: Context) : NotificationProcessor(contex
return
}
if (nevoMultiMsgTip(ctx)) {
val dontShow = PendingIntent.getBroadcast(ctx, REQ_MULTI_MSG_DONT_SHOW,
Intent(ctx, StaticReceiver::class.java).also {
it.action = ACTION_MULTI_MSG_DONT_SHOW
}, PendingIntent.FLAG_UPDATE_CURRENT)
val dontShow = PendingIntent.getBroadcast(
ctx, REQ_MULTI_MSG_DONT_SHOW,
Intent(ctx, StaticReceiver::class.java).also {
it.action = ACTION_MULTI_MSG_DONT_SHOW
}, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val learnMore = PendingIntent.getActivity(ctx, REQ_MULTI_MSG_LEARN_MORE,
Intent(ctx, PreferenceAty::class.java).also {
it.putExtra(PreferenceAty.EXTRA_NEVO_MULTI_MSG, true)
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}, PendingIntent.FLAG_UPDATE_CURRENT)
val learnMore = PendingIntent.getActivity(
ctx, REQ_MULTI_MSG_LEARN_MORE,
Intent(ctx, PreferenceAty::class.java).also {
it.putExtra(PreferenceAty.EXTRA_NEVO_MULTI_MSG, true)
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val style = NotificationCompat.BigTextStyle()
.setBigContentTitle(ctx.getString(R.string.notify_multi_msg_title))
.bigText(ctx.getString(R.string.notify_multi_msg_content))
.setBigContentTitle(ctx.getString(R.string.notify_multi_msg_title))
.bigText(ctx.getString(R.string.notify_multi_msg_content))
val n = NotificationCompat.Builder(ctx, NOTIFY_SELF_TIPS_CHANNEL_ID)
.setStyle(style)
.setAutoCancel(true)
.setContentTitle(ctx.getString(R.string.notify_multi_msg_title))
.setContentText(ctx.getString(R.string.notify_multi_msg_content))
.setSmallIcon(R.drawable.ic_notify_warning)
.setContentIntent(learnMore)
.addAction(R.drawable.ic_notify_action_dnot_show, ctx.getString(R.string.dont_show), dontShow)
.addAction(R.drawable.ic_notify_action_learn_more, ctx.getString(R.string.learn_more), learnMore)
.build()
.setStyle(style)
.setAutoCancel(true)
.setContentTitle(ctx.getString(R.string.notify_multi_msg_title))
.setContentText(ctx.getString(R.string.notify_multi_msg_content))
.setSmallIcon(R.drawable.ic_notify_warning)
.setContentIntent(learnMore)
.addAction(
R.drawable.ic_notify_action_dnot_show,
ctx.getString(R.string.dont_show),
dontShow
)
.addAction(
R.drawable.ic_notify_action_learn_more,
ctx.getString(R.string.learn_more),
learnMore
)
.build()

NotificationManagerCompat.from(ctx).notify(NOTIFY_ID_MULTI_MSG, n)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/cc/chenhe/qqnotifyevo/log/LogWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import java.util.*
import kotlin.math.abs

class LogWriter(
private val logDir: File,
time: Long = System.currentTimeMillis()
private val logDir: File,
time: Long = System.currentTimeMillis()
) : AutoCloseable {

companion object {
Expand Down
54 changes: 33 additions & 21 deletions app/src/main/java/cc/chenhe/qqnotifyevo/preference/PermissionFr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,31 @@ class PermissionFr : PreferenceFragmentCompat() {
}
"auto_start" -> {
AlertDialog.Builder(context)
.setTitle(R.string.pref_auto_start)
.setMessage(R.string.pref_auto_start_message)
.setPositiveButton(R.string.confirm, null)
.show()
.setTitle(R.string.pref_auto_start)
.setMessage(R.string.pref_auto_start_message)
.setPositiveButton(R.string.confirm, null)
.show()
return true
}
}
return super.onPreferenceTreeClick(preference)
}

private fun refreshSummary() {
notification.summary = getString(if (isNotificationListenerEnabled(ctx))
R.string.pref_enable_permit else R.string.pref_disable_permit)

accessibility.summary = getString(if (isAccessibilitySettingsOn(ctx))
R.string.pref_enable_permit else R.string.pref_disable_permit)

battery.summary = getString(if (isIgnoreBatteryOptimization(ctx))
R.string.pref_battery_optimize_disable else R.string.pref_battery_optimize_enable)
notification.summary = getString(
if (isNotificationListenerEnabled(ctx))
R.string.pref_enable_permit else R.string.pref_disable_permit
)

accessibility.summary = getString(
if (isAccessibilitySettingsOn(ctx))
R.string.pref_enable_permit else R.string.pref_disable_permit
)

battery.summary = getString(
if (isIgnoreBatteryOptimization(ctx))
R.string.pref_battery_optimize_disable else R.string.pref_battery_optimize_enable
)
}

private fun isNotificationListenerEnabled(context: Context): Boolean {
Expand All @@ -94,19 +100,24 @@ class PermissionFr : PreferenceFragmentCompat() {

private fun isAccessibilitySettingsOn(context: Context): Boolean {
var accessibilityEnabled = 0
val service = context.packageName + "/" + AccessibilityMonitorService::class.java.canonicalName
val service =
context.packageName + "/" + AccessibilityMonitorService::class.java.canonicalName
try {
accessibilityEnabled = Settings.Secure.getInt(context.applicationContext.contentResolver,
Settings.Secure.ACCESSIBILITY_ENABLED)
accessibilityEnabled = Settings.Secure.getInt(
context.applicationContext.contentResolver,
Settings.Secure.ACCESSIBILITY_ENABLED
)
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}

val mStringColonSplitter = TextUtils.SimpleStringSplitter(':')

if (accessibilityEnabled == 1) {
val settingValue = Settings.Secure.getString(context.applicationContext.contentResolver,
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
val settingValue = Settings.Secure.getString(
context.applicationContext.contentResolver,
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
)
if (settingValue != null) {
mStringColonSplitter.setString(settingValue)
while (mStringColonSplitter.hasNext()) {
Expand Down Expand Up @@ -140,11 +151,12 @@ class PermissionFr : PreferenceFragmentCompat() {
@SuppressLint("BatteryLife")
private fun ignoreBatteryOptimization(activity: Activity) {
val powerManager = activity.getSystemService(POWER_SERVICE) as PowerManager
val hasIgnored: Boolean
hasIgnored = powerManager.isIgnoringBatteryOptimizations(activity.packageName)
val hasIgnored: Boolean = powerManager.isIgnoringBatteryOptimizations(activity.packageName)
if (!hasIgnored) {
Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + activity.packageName)).let {
Intent(
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + activity.packageName)
).let {
activity.startActivity(it)
}
}
Expand Down
41 changes: 22 additions & 19 deletions app/src/main/java/cc/chenhe/qqnotifyevo/preference/PreferenceAty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@ class PreferenceAty : AppCompatActivity() {
initPreferenceFragment()
} else {
supportFragmentManager.beginTransaction()
.replace(R.id.frameLayout, UpgradingFr())
.commit()
.replace(R.id.frameLayout, UpgradingFr())
.commit()
upgradeReceiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context, i: Intent?) {
if (i?.action == ACTION_APPLICATION_UPGRADE_COMPLETE) {
LocalBroadcastManager.getInstance(this@PreferenceAty).unregisterReceiver(this)
LocalBroadcastManager.getInstance(this@PreferenceAty)
.unregisterReceiver(this)
upgradeReceiver = null
initPreferenceFragment()
}
}
}
LocalBroadcastManager.getInstance(this).registerReceiver(upgradeReceiver!!,
IntentFilter(ACTION_APPLICATION_UPGRADE_COMPLETE))
LocalBroadcastManager.getInstance(this).registerReceiver(
upgradeReceiver!!,
IntentFilter(ACTION_APPLICATION_UPGRADE_COMPLETE)
)
if (!UpgradeService.isRunningOrPrepared()) {
// 避免极端情况下在注册监听器之前更新完成
initPreferenceFragment()
Expand All @@ -72,8 +75,8 @@ class PreferenceAty : AppCompatActivity() {
}
mainPreferenceFr = MainPreferenceFr().also { fr ->
supportFragmentManager.beginTransaction()
.replace(R.id.frameLayout, fr)
.commit()
.replace(R.id.frameLayout, fr)
.commit()
}
}

Expand All @@ -87,18 +90,18 @@ class PreferenceAty : AppCompatActivity() {
if (intent?.extras?.getBoolean(EXTRA_NEVO_MULTI_MSG, false) == true) {
NotificationManagerCompat.from(this).cancel(NOTIFY_ID_MULTI_MSG)
AlertDialog.Builder(this)
.setTitle(R.string.tip)
.setMessage(R.string.multi_msg_dialog)
.setNeutralButton(R.string.multi_msg_dialog_neutral, null)
.setPositiveButton(R.string.multi_msg_dialog_positive) { _, _ ->
mainPreferenceFr?.setMode(MODE_LEGACY)
}
.setNegativeButton(R.string.dont_show) { _, _ ->
sendBroadcast(Intent(this, StaticReceiver::class.java).also {
it.action = ACTION_MULTI_MSG_DONT_SHOW
})
}
.show()
.setTitle(R.string.tip)
.setMessage(R.string.multi_msg_dialog)
.setNeutralButton(R.string.multi_msg_dialog_neutral, null)
.setPositiveButton(R.string.multi_msg_dialog_positive) { _, _ ->
mainPreferenceFr?.setMode(MODE_LEGACY)
}
.setNegativeButton(R.string.dont_show) { _, _ ->
sendBroadcast(Intent(this, StaticReceiver::class.java).also {
it.action = ACTION_MULTI_MSG_DONT_SHOW
})
}
.show()
}
}

Expand Down
Loading

0 comments on commit faf64bb

Please sign in to comment.