Last active
May 9, 2018 10:31
-
-
Save williesong/79dc4b7c2d8dcce7420a93fa3043f30c 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
public class DebugUtils { | |
private static final String TAG = LogUtils.makeLogTag("DebugUtils"); | |
private DebugUtils() { | |
} | |
/** | |
* Export SQLite DB to external files directory. | |
* | |
* @param context The context to use. | |
* @param name SQLite DB name. | |
*/ | |
public static void exportSQLite(Context context, String name) { | |
LogUtils.LOGD(TAG, "exportSQLite"); | |
try { | |
File srcDB = context.getDatabasePath(name); | |
File destDB = new File(context.getExternalFilesDir(null), name); | |
copyFileUsingFileChannels(srcDB, destDB); | |
if (destDB.exists()) { | |
scanFile(context, destDB); | |
Toast.makeText(context, "SQLite Exported!", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (IOException e) { | |
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. | |
*/ | |
public static void exportRealm(Context context, String name) { | |
LogUtils.LOGD(TAG, "exportRealm"); | |
try { | |
if (name == null) { | |
name = "default.realm"; | |
} | |
File srcDB = new File(context.getFilesDir(), name); | |
File destDB = new File(context.getExternalFilesDir(null), name); | |
copyFileUsingFileChannels(srcDB, destDB); | |
if (destDB.exists()) { | |
scanFile(context, destDB); | |
Toast.makeText(context, "Realm Exported!", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (IOException e) { | |
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. | |
*/ | |
public static void exportSharedPreferences(Context context, String name) { | |
LogUtils.LOGD(TAG, "exportSharedPreferences"); | |
try { | |
if (name == null) { | |
name = context.getPackageName() + "_preferences.xml"; | |
} else { | |
name += ".xml"; | |
} | |
File srcSP = new File(ContextCompat.getDataDir(context) + "/shared_prefs", name); | |
File destSP = new File(context.getExternalFilesDir(null), name); | |
copyFileUsingFileChannels(srcSP, destSP); | |
if (destSP.exists()) { | |
scanFile(context, destSP); | |
Toast.makeText(context, "SharedPreferences Exported!", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Export log to external files directory. | |
* | |
* @param context The context to use. | |
* @param name Output log name. | |
*/ | |
public static void exportLogcat(Context context, String name) { | |
LogUtils.LOGD(TAG, "exportLogcat"); | |
try { | |
File destLOG = new File(context.getExternalFilesDir(null), name); | |
destLOG.createNewFile(); | |
// http://developer.android.com/tools/help/logcat.html | |
String cmd = "logcat -d -f " + destLOG.getAbsolutePath(); | |
Runtime.getRuntime().exec(cmd); | |
if (destLOG.exists()) { | |
scanFile(context, destLOG); | |
Toast.makeText(context, "Logcat Exported!", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void copyFileUsingFileChannels(File source, File dest) throws IOException { | |
FileChannel inputChannel = null; | |
FileChannel outputChannel = null; | |
try { | |
inputChannel = new FileInputStream(source).getChannel(); | |
outputChannel = new FileOutputStream(dest).getChannel(); | |
outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); | |
} finally { | |
if (inputChannel != null) { | |
inputChannel.close(); | |
} | |
if (outputChannel != null) { | |
outputChannel.close(); | |
} | |
} | |
} | |
private static void scanFile(Context context, File file) { | |
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); | |
intent.setData(Uri.fromFile(file)); | |
context.sendBroadcast(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment