refactor
This commit is contained in:
parent
4ee646687a
commit
448b542f82
|
@ -50,14 +50,14 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
fun onStatus(sv : String)
|
fun onStatus(sv : String)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class TrackingType(val str:String) {
|
enum class TrackingType(val str : String) {
|
||||||
All("all"),
|
All("all"),
|
||||||
Reply("reply"),
|
Reply("reply"),
|
||||||
NotReply("notReply");
|
NotReply("notReply");
|
||||||
|
|
||||||
companion object{
|
companion object {
|
||||||
fun parseStr(str:String?):TrackingType{
|
fun parseStr(str : String?) : TrackingType {
|
||||||
for( v in values()){
|
for(v in values()) {
|
||||||
if(v.str == str) return v
|
if(v.str == str) return v
|
||||||
}
|
}
|
||||||
return All
|
return All
|
||||||
|
@ -81,8 +81,7 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
const val EXTRA_DB_ID = "db_id"
|
const val EXTRA_DB_ID = "db_id"
|
||||||
const val EXTRA_TAG = "tag"
|
const val EXTRA_TAG = "tag"
|
||||||
const val EXTRA_TASK_ID = "task_id"
|
const val EXTRA_TASK_ID = "task_id"
|
||||||
const val EXTRA_NOTIFICATION_TYPE ="notification_type"
|
const val EXTRA_NOTIFICATION_TYPE = "notification_type"
|
||||||
|
|
||||||
|
|
||||||
const val APP_SERVER = "https://mastodon-msg.juggler.jp"
|
const val APP_SERVER = "https://mastodon-msg.juggler.jp"
|
||||||
|
|
||||||
|
@ -300,11 +299,11 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun queueNotificationDeleted(context : Context, db_id : Long,trackingType:String) {
|
fun queueNotificationDeleted(context : Context, db_id : Long, trackingType : String) {
|
||||||
try {
|
try {
|
||||||
val data = JSONObject()
|
val data = JSONObject()
|
||||||
data.putOpt(EXTRA_DB_ID, db_id)
|
data.putOpt(EXTRA_DB_ID, db_id)
|
||||||
data.putOpt(EXTRA_NOTIFICATION_TYPE,trackingType)
|
data.putOpt(EXTRA_NOTIFICATION_TYPE, trackingType)
|
||||||
addTask(context, true, TASK_NOTIFICATION_DELETE, data)
|
addTask(context, true, TASK_NOTIFICATION_DELETE, data)
|
||||||
} catch(ex : JSONException) {
|
} catch(ex : JSONException) {
|
||||||
log.trace(ex)
|
log.trace(ex)
|
||||||
|
@ -312,11 +311,11 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun queueNotificationClicked(context : Context, db_id : Long,trackingType:String) {
|
fun queueNotificationClicked(context : Context, db_id : Long, trackingType : String) {
|
||||||
try {
|
try {
|
||||||
val data = JSONObject()
|
val data = JSONObject()
|
||||||
data.putOpt(EXTRA_DB_ID, db_id)
|
data.putOpt(EXTRA_DB_ID, db_id)
|
||||||
data.putOpt(EXTRA_NOTIFICATION_TYPE,trackingType)
|
data.putOpt(EXTRA_NOTIFICATION_TYPE, trackingType)
|
||||||
addTask(context, true, TASK_NOTIFICATION_CLICK, data)
|
addTask(context, true, TASK_NOTIFICATION_CLICK, data)
|
||||||
} catch(ex : JSONException) {
|
} catch(ex : JSONException) {
|
||||||
log.trace(ex)
|
log.trace(ex)
|
||||||
|
@ -904,29 +903,31 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
TASK_NOTIFICATION_DELETE -> {
|
TASK_NOTIFICATION_DELETE -> {
|
||||||
val db_id = taskData.parseLong(EXTRA_DB_ID)
|
val db_id = taskData.parseLong(EXTRA_DB_ID)
|
||||||
val type = when(TrackingType.parseStr(taskData.parseString(EXTRA_NOTIFICATION_TYPE))){
|
val type =
|
||||||
TrackingType.Reply -> NotificationHelper.TRACKING_NAME_REPLY
|
when(TrackingType.parseStr(taskData.parseString(EXTRA_NOTIFICATION_TYPE))) {
|
||||||
else ->NotificationHelper.TRACKING_NAME_DEFAULT
|
TrackingType.Reply -> NotificationHelper.TRACKING_NAME_REPLY
|
||||||
}
|
else -> NotificationHelper.TRACKING_NAME_DEFAULT
|
||||||
|
}
|
||||||
log.d("Notification deleted! db_id=$db_id,type=$type")
|
log.d("Notification deleted! db_id=$db_id,type=$type")
|
||||||
if(db_id != null) {
|
if(db_id != null) {
|
||||||
NotificationTracking.updateRead(db_id,type)
|
NotificationTracking.updateRead(db_id, type)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
TASK_NOTIFICATION_CLICK -> {
|
TASK_NOTIFICATION_CLICK -> {
|
||||||
val db_id = taskData.parseLong(EXTRA_DB_ID)
|
val db_id = taskData.parseLong(EXTRA_DB_ID)
|
||||||
val type = when(TrackingType.parseStr(taskData.parseString(EXTRA_NOTIFICATION_TYPE))){
|
val type =
|
||||||
TrackingType.Reply -> NotificationHelper.TRACKING_NAME_REPLY
|
when(TrackingType.parseStr(taskData.parseString(EXTRA_NOTIFICATION_TYPE))) {
|
||||||
else ->NotificationHelper.TRACKING_NAME_DEFAULT
|
TrackingType.Reply -> NotificationHelper.TRACKING_NAME_REPLY
|
||||||
}
|
else -> NotificationHelper.TRACKING_NAME_DEFAULT
|
||||||
|
}
|
||||||
log.d("Notification clicked! db_id=$db_id,type=$type")
|
log.d("Notification clicked! db_id=$db_id,type=$type")
|
||||||
if(db_id != null) {
|
if(db_id != null) {
|
||||||
// 通知をキャンセル
|
// 通知をキャンセル
|
||||||
notification_manager.cancel(db_id.toString(), NOTIFICATION_ID)
|
notification_manager.cancel(db_id.toString(), NOTIFICATION_ID)
|
||||||
// DB更新処理
|
// DB更新処理
|
||||||
NotificationTracking.updateRead(db_id,type)
|
NotificationTracking.updateRead(db_id, type)
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -1030,7 +1031,7 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
val (instance, instanceResult) = TootInstance.get(client)
|
val (instance, instanceResult) = TootInstance.get(client)
|
||||||
if(instance == null) {
|
if(instance == null) {
|
||||||
if(instanceResult != null){
|
if(instanceResult != null) {
|
||||||
log.e("${instanceResult.error} ${instanceResult.requestInfo}".trim())
|
log.e("${instanceResult.error} ${instanceResult.requestInfo}".trim())
|
||||||
account.updateNotificationError("${instanceResult.error} ${instanceResult.requestInfo}".trim())
|
account.updateNotificationError("${instanceResult.error} ${instanceResult.requestInfo}".trim())
|
||||||
}
|
}
|
||||||
|
@ -1048,8 +1049,8 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
if(job.isJobCancelled) return
|
if(job.isJobCancelled) return
|
||||||
|
|
||||||
if(wps.flags == 0){
|
if(wps.flags == 0) {
|
||||||
if(account.last_notification_error != null){
|
if(account.last_notification_error != null) {
|
||||||
account.updateNotificationError(null)
|
account.updateNotificationError(null)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -1150,11 +1151,6 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 種別チェックより先に、cache中の最新のIDを「最後に表示した通知」に指定する
|
|
||||||
// nid_show は通知タップ時に参照されるので、通知を表示する際は必ず更新・保存する必要がある
|
|
||||||
// 種別チェックより優先する
|
|
||||||
if(cache.sinceId != null ) nr.nid_show = cache.sinceId
|
|
||||||
|
|
||||||
// 新しい順に並んでいる。先頭から10件までを処理する。ただし処理順序は古い方から
|
// 新しい順に並んでいる。先頭から10件までを処理する。ただし処理順序は古い方から
|
||||||
val size = min(10, jsonList.size)
|
val size = min(10, jsonList.size)
|
||||||
for(i in (0 until size).reversed()) {
|
for(i in (0 until size).reversed()) {
|
||||||
|
@ -1163,18 +1159,16 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
}
|
}
|
||||||
if(job.isJobCancelled) return
|
if(job.isJobCancelled) return
|
||||||
|
|
||||||
|
// 種別チェックより先に、cache中の最新のIDを「最後に表示した通知」に指定する
|
||||||
|
// nid_show は通知タップ時に参照されるので、通知を表示する際は必ず更新・保存する必要がある
|
||||||
|
// 種別チェックより優先する
|
||||||
|
if(cache.sinceId != null) nr.nid_show = cache.sinceId
|
||||||
nr.save()
|
nr.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun update_sub(src : JSONObject) {
|
private fun update_sub(src : JSONObject) {
|
||||||
|
|
||||||
// null値が残ってたらログをとる
|
|
||||||
if(nr.nid_read == null || nr.nid_show == null) {
|
|
||||||
log.d("update_sub[${account.db_id}], nid_read=${nr.nid_read}, nid_show=${nr.nid_show}")
|
|
||||||
}
|
|
||||||
|
|
||||||
val id = getEntityOrderId(account, src)
|
val id = getEntityOrderId(account, src)
|
||||||
|
|
||||||
if(id.isDefault || duplicate_check.contains(id)) return
|
if(id.isDefault || duplicate_check.contains(id)) return
|
||||||
duplicate_check.add(id)
|
duplicate_check.add(id)
|
||||||
|
|
||||||
|
@ -1207,11 +1201,11 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
|
|
||||||
internal fun updateNotification() {
|
internal fun updateNotification() {
|
||||||
|
|
||||||
val notification_tag = when(trackingName){
|
val notification_tag = when(trackingName) {
|
||||||
"" -> account.db_id.toString()
|
"" -> account.db_id.toString()
|
||||||
else-> "${account.db_id}/$trackingName"
|
else -> "${account.db_id}/$trackingName"
|
||||||
}
|
}
|
||||||
|
|
||||||
val dataList = dstListData
|
val dataList = dstListData
|
||||||
val item = dataList.firstOrNull()
|
val item = dataList.firstOrNull()
|
||||||
if(item == null) {
|
if(item == null) {
|
||||||
|
@ -1220,7 +1214,7 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val nt = NotificationTracking.load(account.db_id,trackingName)
|
val nt = NotificationTracking.load(account.db_id, trackingName)
|
||||||
|
|
||||||
if(item.notification.time_created_at == nt.post_time
|
if(item.notification.time_created_at == nt.post_time
|
||||||
&& item.notification.id == nt.post_id
|
&& item.notification.id == nt.post_id
|
||||||
|
@ -1248,16 +1242,16 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
intent_click.action = ActCallback.ACTION_NOTIFICATION_CLICK
|
intent_click.action = ActCallback.ACTION_NOTIFICATION_CLICK
|
||||||
intent_click.data =
|
intent_click.data =
|
||||||
"subwaytooter://notification_click/?db_id=${account.db_id}&type=${trackingType.str}"
|
"subwaytooter://notification_click/?db_id=${account.db_id}&type=${trackingType.str}"
|
||||||
.toUri()
|
.toUri()
|
||||||
|
|
||||||
// FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY を付与してはいけない
|
// FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY を付与してはいけない
|
||||||
intent_click.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
intent_click.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
|
||||||
val pi_click = PendingIntent.getActivity(
|
val pi_click = PendingIntent.getActivity(
|
||||||
context,
|
context,
|
||||||
when(trackingType){
|
when(trackingType) {
|
||||||
TrackingType.Reply-> midInt + account.db_id.toInt()
|
TrackingType.Reply -> midInt + account.db_id.toInt()
|
||||||
else-> 256 + account.db_id.toInt()
|
else -> 256 + account.db_id.toInt()
|
||||||
},
|
},
|
||||||
intent_click,
|
intent_click,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
@ -1270,11 +1264,12 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
intent_delete.putExtra(EXTRA_NOTIFICATION_TYPE, trackingType.str)
|
intent_delete.putExtra(EXTRA_NOTIFICATION_TYPE, trackingType.str)
|
||||||
val pi_delete = PendingIntent.getBroadcast(
|
val pi_delete = PendingIntent.getBroadcast(
|
||||||
context,
|
context,
|
||||||
when(trackingType){
|
when(trackingType) {
|
||||||
TrackingType.Reply->{
|
TrackingType.Reply -> {
|
||||||
midInt - account.db_id.toInt()
|
midInt - account.db_id.toInt()
|
||||||
}
|
}
|
||||||
else->{
|
|
||||||
|
else -> {
|
||||||
Integer.MAX_VALUE - account.db_id.toInt()
|
Integer.MAX_VALUE - account.db_id.toInt()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1287,7 +1282,11 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
val builder = if(Build.VERSION.SDK_INT >= 26) {
|
val builder = if(Build.VERSION.SDK_INT >= 26) {
|
||||||
// Android 8 から、通知のスタイルはユーザが管理することになった
|
// Android 8 から、通知のスタイルはユーザが管理することになった
|
||||||
// NotificationChannel を端末に登録しておけば、チャネルごとに管理画面が作られる
|
// NotificationChannel を端末に登録しておけば、チャネルごとに管理画面が作られる
|
||||||
val channel = NotificationHelper.createNotificationChannel(context, account,trackingName)
|
val channel = NotificationHelper.createNotificationChannel(
|
||||||
|
context,
|
||||||
|
account,
|
||||||
|
trackingName
|
||||||
|
)
|
||||||
NotificationCompat.Builder(context, channel.id)
|
NotificationCompat.Builder(context, channel.id)
|
||||||
} else {
|
} else {
|
||||||
NotificationCompat.Builder(context, "not_used")
|
NotificationCompat.Builder(context, "not_used")
|
||||||
|
@ -1459,11 +1458,10 @@ class PollingWorker private constructor(contextArg : Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun deleteCacheData(db_id : Long) {
|
private fun deleteCacheData(db_id : Long) {
|
||||||
SavedAccount.loadAccount(context,db_id) ?: return
|
SavedAccount.loadAccount(context, db_id) ?: return
|
||||||
NotificationCache.deleteCache(db_id)
|
NotificationCache.deleteCache(db_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun createErrorNotification(error_instance : ArrayList<String>) {
|
private fun createErrorNotification(error_instance : ArrayList<String>) {
|
||||||
if(error_instance.isEmpty()) {
|
if(error_instance.isEmpty()) {
|
||||||
return
|
return
|
||||||
|
|
|
@ -26,6 +26,7 @@ class NotificationCache(private val account_db_id : Long) {
|
||||||
// 通知のリスト
|
// 通知のリスト
|
||||||
var data = ArrayList<JSONObject>()
|
var data = ArrayList<JSONObject>()
|
||||||
|
|
||||||
|
// 次回以降の読み込み位置
|
||||||
var sinceId : EntityId? = null
|
var sinceId : EntityId? = null
|
||||||
|
|
||||||
companion object : TableCompanion {
|
companion object : TableCompanion {
|
||||||
|
@ -77,58 +78,6 @@ class NotificationCache(private val account_db_id : Long) {
|
||||||
|
|
||||||
private const val KEY_TIME_CREATED_AT = "<>KEY_TIME_CREATED_AT"
|
private const val KEY_TIME_CREATED_AT = "<>KEY_TIME_CREATED_AT"
|
||||||
|
|
||||||
// fun updateRead(account_db_id : Long) {
|
|
||||||
// try {
|
|
||||||
// val where_args = arrayOf(account_db_id.toString())
|
|
||||||
// App1.database.query(
|
|
||||||
// table,
|
|
||||||
// arrayOf(COL_NID_SHOW, COL_NID_READ),
|
|
||||||
// WHERE_AID,
|
|
||||||
// where_args,
|
|
||||||
// null,
|
|
||||||
// null,
|
|
||||||
// null
|
|
||||||
// )?.use { cursor ->
|
|
||||||
// when {
|
|
||||||
// ! cursor.moveToFirst() -> log.e("updateRead[${account_db_id}]: can't find the data row.")
|
|
||||||
//
|
|
||||||
// else -> {
|
|
||||||
// val nid_show = EntityId.from(cursor, COL_NID_SHOW)
|
|
||||||
// val nid_read = EntityId.from(cursor, COL_NID_READ)
|
|
||||||
// when {
|
|
||||||
// nid_show == null ->
|
|
||||||
// log.w("updateRead[${account_db_id}]: nid_show is null.")
|
|
||||||
// nid_read != null && nid_read >= nid_show ->
|
|
||||||
// log.d("updateRead[${account_db_id}]: nid_read already updated.")
|
|
||||||
//
|
|
||||||
// else -> {
|
|
||||||
// log.w("updateRead[${account_db_id}]: update nid_read as ${nid_show}...")
|
|
||||||
// val cv = ContentValues()
|
|
||||||
// nid_show.putTo(cv, COL_NID_READ) //変数名とキー名が異なるのに注意
|
|
||||||
// App1.database.update(table, cv, WHERE_AID, where_args)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch(ex : Throwable) {
|
|
||||||
// log.e(ex, "updateRead[${account_db_id}] failed.")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fun resetPostAll() {
|
|
||||||
// try {
|
|
||||||
// val cv = ContentValues()
|
|
||||||
// cv.putNull(COL_POST_ID)
|
|
||||||
// cv.put(COL_POST_TIME, 0)
|
|
||||||
// App1.database.update(table, cv, null, null)
|
|
||||||
//
|
|
||||||
// } catch(ex : Throwable) {
|
|
||||||
// log.e(ex, "resetPostAll failed.")
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
fun resetLastLoad(db_id : Long) {
|
fun resetLastLoad(db_id : Long) {
|
||||||
try {
|
try {
|
||||||
val cv = ContentValues()
|
val cv = ContentValues()
|
||||||
|
@ -251,11 +200,9 @@ class NotificationCache(private val account_db_id : Long) {
|
||||||
cv.put(COL_LAST_LOAD, last_load)
|
cv.put(COL_LAST_LOAD, last_load)
|
||||||
cv.put(COL_DATA, data.toJsonArray().toString())
|
cv.put(COL_DATA, data.toJsonArray().toString())
|
||||||
|
|
||||||
val sinceId = sinceId
|
when(val sinceId = sinceId) {
|
||||||
if(sinceId == null) {
|
null -> cv.putNull(COL_SINCE_ID)
|
||||||
cv.putNull(COL_SINCE_ID)
|
else -> sinceId.putTo(cv, COL_SINCE_ID)
|
||||||
} else {
|
|
||||||
sinceId.putTo(cv, COL_SINCE_ID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val rv = App1.database.replaceOrThrow(table, null, cv)
|
val rv = App1.database.replaceOrThrow(table, null, cv)
|
||||||
|
|
|
@ -9,17 +9,15 @@ import android.database.sqlite.SQLiteDatabase
|
||||||
fun Boolean.b2i() = if(this) 1 else 0
|
fun Boolean.b2i() = if(this) 1 else 0
|
||||||
|
|
||||||
// integer to boolean
|
// integer to boolean
|
||||||
fun Int.i2b() = this!=0
|
fun Int.i2b() = this != 0
|
||||||
|
|
||||||
|
fun Cursor.getBoolean(keyIdx : Int) =
|
||||||
fun Cursor.getBoolean(keyIdx:Int) =
|
|
||||||
getInt(keyIdx).i2b()
|
getInt(keyIdx).i2b()
|
||||||
|
|
||||||
fun Cursor.getBoolean(key:String) =
|
fun Cursor.getBoolean(key : String) =
|
||||||
getBoolean(getColumnIndex(key))
|
getBoolean(getColumnIndex(key))
|
||||||
|
|
||||||
|
interface TableCompanion {
|
||||||
interface TableCompanion{
|
|
||||||
fun onDBCreate(db : SQLiteDatabase)
|
fun onDBCreate(db : SQLiteDatabase)
|
||||||
fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int)
|
fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue