Created
May 9, 2018 10:38
-
-
Save williesong/61e594b177ef4627c6f40fc9d5b8c296 to your computer and use it in GitHub Desktop.
Android debug utilities to export SQLite, Realm, SharedPreferences and Logcat to external files directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DebugUtils { | |
private val TAG = LogUtils.makeLogTag("DebugUtils") | |
/** | |
* Export SQLite DB to external files directory. | |
* | |
* @param context The context to use. | |
* @param name SQLite DB name. | |
*/ | |
fun exportSQLite(context: Context, name: String) { | |
LogUtils.LOGD(TAG, "exportSQLite") | |
try { | |
val srcDB = context.getDatabasePath(name) | |
val destDB = File(context.getExternalFilesDir(null), name) | |
copyFileUsingFileChannels(srcDB, destDB) | |
if (destDB.exists()) { | |
scanFile(context, destDB) | |
Toast.makeText(context, "SQLite Exported!", Toast.LENGTH_SHORT).show() | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
/** | |
* Export Realm DB to external files directory. | |
* | |
* @param context The context to use. | |
* @param name Realm DB name, or null for default Realm DB. | |
*/ | |
fun exportRealm(context: Context, name: String?) { | |
var name = name | |
LogUtils.LOGD(TAG, "exportRealm") | |
try { | |
if (name == null) { | |
name = "default.realm" | |
} | |
val srcDB = File(context.filesDir, name) | |
val destDB = File(context.getExternalFilesDir(null), name) | |
copyFileUsingFileChannels(srcDB, destDB) | |
if (destDB.exists()) { | |
scanFile(context, destDB) | |
Toast.makeText(context, "Realm Exported!", Toast.LENGTH_SHORT).show() | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
/** | |
* Export SharedPreferences to external files directory. | |
* | |
* @param context The context to use. | |
* @param name SharedPreferences name (without .xml), or null for default SharedPreferences. | |
*/ | |
fun exportSharedPreferences(context: Context, name: String?) { | |
var name = name | |
LogUtils.LOGD(TAG, "exportSharedPreferences") | |
try { | |
if (name == null) { | |
name = context.packageName + "_preferences.xml" | |
} else { | |
name += ".xml" | |
} | |
val srcSP = File(ContextCompat.getDataDir(context)!!.toString() + "/shared_prefs", name) | |
val destSP = File(context.getExternalFilesDir(null), name) | |
copyFileUsingFileChannels(srcSP, destSP) | |
if (destSP.exists()) { | |
scanFile(context, destSP) | |
Toast.makeText(context, "SharedPreferences Exported!", Toast.LENGTH_SHORT).show() | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
/** | |
* Export log to external files directory. | |
* | |
* @param context The context to use. | |
* @param name Output log name. | |
*/ | |
fun exportLogcat(context: Context, name: String) { | |
LogUtils.LOGD(TAG, "exportLogcat") | |
try { | |
val destLOG = File(context.getExternalFilesDir(null), name) | |
destLOG.createNewFile() | |
// http://developer.android.com/tools/help/logcat.html | |
val cmd = "logcat -d -f " + destLOG.absolutePath | |
Runtime.getRuntime().exec(cmd) | |
if (destLOG.exists()) { | |
scanFile(context, destLOG) | |
Toast.makeText(context, "Logcat Exported!", Toast.LENGTH_SHORT).show() | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
@Throws(IOException::class) | |
fun copyFileUsingFileChannels(source: File, dest: File) { | |
var inputChannel: FileChannel? = null | |
var outputChannel: FileChannel? = null | |
try { | |
inputChannel = FileInputStream(source).channel | |
outputChannel = FileOutputStream(dest).channel | |
outputChannel!!.transferFrom(inputChannel, 0, inputChannel!!.size()) | |
} finally { | |
if (inputChannel != null) { | |
inputChannel.close() | |
} | |
if (outputChannel != null) { | |
outputChannel.close() | |
} | |
} | |
} | |
fun scanFile(context: Context, file: File) { | |
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) | |
intent.data = Uri.fromFile(file) | |
context.sendBroadcast(intent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment