From 27374aea3f9edfadf91f9bf58fd82e1257bf4dfb Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 20 Mar 2019 14:06:29 +0100 Subject: [PATCH] Add DebugReceiver --- .../riotredesign/receivers/DebugReceiver.kt | 36 ++++++++ .../riotredesign/receivers/DebugReceiver.kt | 73 +++++++++++++++ .../core/platform/RiotActivity.kt | 26 ++++++ .../riotredesign/core/utils/FileUtils.kt | 89 +++++++++++++++++++ tools/debug_alter_scalar_token.sh | 2 +- tools/debug_dump_filesystem.sh | 2 +- tools/debug_dump_prefs.sh | 2 +- 7 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 app/release/java/im/vector/riotredesign/receivers/DebugReceiver.kt create mode 100644 app/src/debug/java/im/vector/riotredesign/receivers/DebugReceiver.kt create mode 100644 app/src/main/java/im/vector/riotredesign/core/utils/FileUtils.kt diff --git a/app/release/java/im/vector/riotredesign/receivers/DebugReceiver.kt b/app/release/java/im/vector/riotredesign/receivers/DebugReceiver.kt new file mode 100644 index 0000000000..ccb2821306 --- /dev/null +++ b/app/release/java/im/vector/riotredesign/receivers/DebugReceiver.kt @@ -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() + } +} diff --git a/app/src/debug/java/im/vector/riotredesign/receivers/DebugReceiver.kt b/app/src/debug/java/im/vector/riotredesign/receivers/DebugReceiver.kt new file mode 100644 index 0000000000..ff6e1d5395 --- /dev/null +++ b/app/src/debug/java/im/vector/riotredesign/receivers/DebugReceiver.kt @@ -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) + } + } +} diff --git a/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt b/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt index db1aa476fb..2938252c0b 100644 --- a/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt +++ b/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt @@ -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() @@ -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 + } + } + } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/core/utils/FileUtils.kt b/app/src/main/java/im/vector/riotredesign/core/utils/FileUtils.kt new file mode 100644 index 0000000000..c0a759701f --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/core/utils/FileUtils.kt @@ -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) +} + diff --git a/tools/debug_alter_scalar_token.sh b/tools/debug_alter_scalar_token.sh index e48163d3f8..f8716706bb 100755 --- a/tools/debug_alter_scalar_token.sh +++ b/tools/debug_alter_scalar_token.sh @@ -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 diff --git a/tools/debug_dump_filesystem.sh b/tools/debug_dump_filesystem.sh index e148e125a9..efe1745aea 100755 --- a/tools/debug_dump_filesystem.sh +++ b/tools/debug_dump_filesystem.sh @@ -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 diff --git a/tools/debug_dump_prefs.sh b/tools/debug_dump_prefs.sh index 9f2f652d72..3b5ecadcda 100755 --- a/tools/debug_dump_prefs.sh +++ b/tools/debug_dump_prefs.sh @@ -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