Add DebugReceiver

This commit is contained in:
Benoit Marty 2019-03-20 14:06:29 +01:00
parent a4ef259bd2
commit 27374aea3f
7 changed files with 227 additions and 3 deletions

View file

@ -0,0 +1,36 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotredesign.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
/**
* No Op version
*/
class DebugReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// No op
}
companion object {
fun getIntentFilter(context: Context) = IntentFilter()
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotredesign.receivers
import android.content.*
import android.preference.PreferenceManager
import androidx.core.content.edit
import im.vector.riotredesign.core.utils.lsFiles
import timber.log.Timber
/**
* Receiver to handle some command from ADB
*/
class DebugReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Timber.d("Received debug action: ${intent.action}")
intent.action?.let {
when {
it.endsWith(DEBUG_ACTION_DUMP_FILESYSTEM) -> lsFiles(context)
it.endsWith(DEBUG_ACTION_DUMP_PREFERENCES) -> dumpPreferences(context)
it.endsWith(DEBUG_ACTION_ALTER_SCALAR_TOKEN) -> alterScalarToken(context)
}
}
}
private fun dumpPreferences(context: Context) {
logPrefs("DefaultSharedPreferences", PreferenceManager.getDefaultSharedPreferences(context))
}
private fun logPrefs(name: String, sharedPreferences: SharedPreferences?) {
Timber.d("SharedPreferences $name:")
sharedPreferences?.let { prefs ->
prefs.all.keys.forEach { key ->
Timber.d("$key : ${prefs.all[key]}")
}
}
}
private fun alterScalarToken(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
// putString("SCALAR_TOKEN_PREFERENCE_KEY" + Matrix.getInstance(context).defaultSession.myUserId, "bad_token")
}
}
companion object {
private const val DEBUG_ACTION_DUMP_FILESYSTEM = ".DEBUG_ACTION_DUMP_FILESYSTEM"
private const val DEBUG_ACTION_DUMP_PREFERENCES = ".DEBUG_ACTION_DUMP_PREFERENCES"
private const val DEBUG_ACTION_ALTER_SCALAR_TOKEN = ".DEBUG_ACTION_ALTER_SCALAR_TOKEN"
fun getIntentFilter(context: Context) = IntentFilter().apply {
addAction(context.packageName + DEBUG_ACTION_DUMP_FILESYSTEM)
addAction(context.packageName + DEBUG_ACTION_DUMP_PREFERENCES)
addAction(context.packageName + DEBUG_ACTION_ALTER_SCALAR_TOKEN)
}
}
}

View file

@ -20,11 +20,16 @@ import android.os.Bundle
import androidx.annotation.MainThread
import com.airbnb.mvrx.BaseMvRxActivity
import com.bumptech.glide.util.Util
import im.vector.riotredesign.BuildConfig
import im.vector.riotredesign.receivers.DebugReceiver
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
abstract class RiotActivity : BaseMvRxActivity() {
// For debug only
private var debugReceiver: DebugReceiver? = null
private val uiDisposables = CompositeDisposable()
private val restorables = ArrayList<Restorable>()
@ -50,4 +55,25 @@ abstract class RiotActivity : BaseMvRxActivity() {
return this
}
override fun onResume() {
super.onResume()
DebugReceiver
.getIntentFilter(this)
.takeIf { BuildConfig.DEBUG }
?.let {
debugReceiver = DebugReceiver()
registerReceiver(debugReceiver, it)
}
}
override fun onPause() {
super.onPause()
debugReceiver?.let {
unregisterReceiver(debugReceiver)
debugReceiver = null
}
}
}

View file

@ -0,0 +1,89 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotredesign.core.utils
import android.content.Context
import timber.log.Timber
import java.io.File
// Implementation should return true in case of success
typealias ActionOnFile = (file: File) -> Boolean
/* ==========================================================================================
* Delete
* ========================================================================================== */
fun deleteAllFiles(context: Context) {
Timber.v("Delete cache dir:")
recursiveActionOnFile(context.cacheDir, ::deleteAction)
Timber.v("Delete files dir:")
recursiveActionOnFile(context.filesDir, ::deleteAction)
}
private fun deleteAction(file: File): Boolean {
if (file.exists()) {
Timber.v("deleteFile: $file")
return file.delete()
}
return true
}
/* ==========================================================================================
* Log
* ========================================================================================== */
fun lsFiles(context: Context) {
Timber.v("Content of cache dir:")
recursiveActionOnFile(context.cacheDir, ::logAction)
Timber.v("Content of files dir:")
recursiveActionOnFile(context.filesDir, ::logAction)
}
private fun logAction(file: File): Boolean {
if (file.isDirectory) {
Timber.d(file.toString())
} else {
Timber.d(file.toString() + " " + file.length() + " bytes")
}
return true
}
/* ==========================================================================================
* Private
* ========================================================================================== */
/**
* Return true in case of success
*/
private fun recursiveActionOnFile(file: File, action: ActionOnFile): Boolean {
if (file.isDirectory) {
file.list().forEach {
val result = recursiveActionOnFile(File(file, it), action)
if (!result) {
// Break the loop
return false
}
}
}
return action.invoke(file)
}

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
adb shell am broadcast -a im.vector.receiver.DEBUG_ACTION_ALTER_SCALAR_TOKEN
adb shell am broadcast -a im.vector.riotredesign.DEBUG_ACTION_ALTER_SCALAR_TOKEN

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
adb shell am broadcast -a im.vector.receiver.DEBUG_ACTION_DUMP_FILESYSTEM
adb shell am broadcast -a im.vector.riotredesign.DEBUG_ACTION_DUMP_FILESYSTEM

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
adb shell am broadcast -a im.vector.receiver.DEBUG_ACTION_DUMP_PREFERENCES
adb shell am broadcast -a im.vector.riotredesign.DEBUG_ACTION_DUMP_PREFERENCES