Created
July 12, 2021 13:06
-
-
Save abhishekgargx/08b144b99a8611e5af43c830de2e1664 to your computer and use it in GitHub Desktop.
Android user data logger - log data in file and sent it via email or other app easily - works with android 11 as well
This file contains hidden or 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
// Note : you must have storage permission before using this utility | |
// <uses-permission android:name="android.permission.READ_LOGS" | |
// tools:ignore="ProtectedPermissions" /> | |
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
// <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Environment; | |
import android.webkit.MimeTypeMap; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import androidx.core.content.FileProvider; | |
import com.MyApp.MyApplication; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URLConnection; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
public class LoggerUtils { | |
private static final String TAG = "LoggerUtils"; | |
@NonNull | |
public static File getStorageDirectory() { | |
if (Environment.getExternalStorageState() == null) { | |
File f = new File(Environment.getDataDirectory().getAbsolutePath() + "/Download/AppLogs/"); | |
if(!f.exists()) | |
f.mkdirs(); | |
return f; | |
} else A | |
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/AppLogs/"); | |
if(!f.exists()) | |
f.mkdirs(); | |
return f; | |
} | |
} | |
private static File getLogFile(){ | |
File baseDir = getStorageDirectory(); | |
String fileName = "myapp_log"; | |
return new File(baseDir, fileName + ".txt"); | |
} | |
public static void addLog(String tag, String message){ | |
try { | |
File baseDir = getStorageDirectory(); | |
String fileName = "myapp_log"; | |
File logFile = new File(baseDir, fileName + ".txt"); | |
FileWriter writer; | |
if (logFile.exists() && !logFile.isDirectory()) { | |
writer = new FileWriter(logFile, true); | |
} else { | |
writer = new FileWriter(logFile); | |
} | |
writer.append(DateUtil.getLoggerDate(new Date().getTime())) | |
.append(" ---->> ") | |
.append(tag) | |
.append(" : ") | |
.append(message) | |
.append("\n\n"); | |
writer.close(); | |
}catch (Exception e){ | |
DominosLog.e(TAG, "add logs exception" + (e.getMessage() != null ? e.getMessage() : "")); | |
} | |
DominosLog.i(tag, message); | |
} | |
public static void wipeLogs(){ | |
try { | |
File baseDir = getStorageDirectory(); | |
String fileName = "myapp_log"; | |
File logFile = new File(baseDir, fileName + ".txt"); | |
FileWriter writer; | |
if (logFile.exists() && !logFile.isDirectory()) { | |
writer = new FileWriter(logFile, false); | |
} else { | |
writer = new FileWriter(logFile); | |
} | |
writer.write(""); | |
writer.close(); | |
}catch (Exception e){ | |
Toast.makeText(MyApplication.getInstance(), e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
private static File generateAdbLog() { | |
File baseDir = getStorageDirectory(); | |
String fileName = "app" + new Date().getTime(); | |
File logFile = new File(baseDir, fileName + ".txt"); | |
//write log to file | |
int pid = android.os.Process.myPid(); | |
try { | |
String command = String.format("logcat -d *:E"); | |
Process process = Runtime.getRuntime().exec(command); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
StringBuilder result = new StringBuilder(); | |
String currentLine = null; | |
while ((currentLine = reader.readLine()) != null) { | |
if (currentLine != null && currentLine.contains(String.valueOf(pid))) { | |
result.append(currentLine); | |
result.append("\n"); | |
} | |
} | |
FileWriter writer; | |
if(logFile.exists() && !logFile.isDirectory()) { | |
writer = new FileWriter(logFile , true); | |
} else { | |
writer = new FileWriter(logFile); | |
} | |
writer.write(result.toString()); | |
writer.close(); | |
} catch (IOException e) { | |
Toast.makeText(MyApplication.getInstance(), e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
//clear the log | |
try { | |
Runtime.getRuntime().exec("logcat -c"); | |
} catch (IOException e) { | |
Toast.makeText(MyApplication.getInstance(), e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
return logFile; | |
} | |
public static void generateAndSendLogs(Activity activity) { | |
if (activity == null) { | |
return; | |
} | |
File logFile = getLogFile(); | |
if (logFile == null){ | |
Util.showCustomToast(activity, "issues in log file", Toast.LENGTH_SHORT); | |
return; | |
} | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
Uri fileUri; | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ | |
fileUri = FileProvider.getUriForFile(activity, | |
MyApplication.getInstance().getPackageName() + ".provider", logFile); | |
}else{ | |
fileUri = Uri.fromFile(logFile); | |
} | |
String mime= null; | |
try { | |
mime = URLConnection.guessContentTypeFromStream(new FileInputStream(logFile)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if(mime==null) mime=URLConnection.guessContentTypeFromName(logFile.getName()); | |
intent.putExtra(Intent.EXTRA_STREAM, fileUri); | |
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); | |
intent.putExtra(Intent.EXTRA_SUBJECT, "App Logs"); | |
intent.putExtra(Intent.EXTRA_TEXT, "Hi Team, Please app logs attached to this email."); | |
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | |
// intent.setType("multipart/"); | |
intent.setDataAndType(fileUri, mime); | |
if (intent.resolveActivity(MyApplication.getInstance().getPackageManager()) != null) { | |
activity.startActivity(Intent.createChooser(intent, "Share logs Via")); | |
}else{ | |
Util.showCustomToast(MyApplication.getInstance(), "No Apps is install to send email", Toast.LENGTH_SHORT); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment