Skip to content

Commit

Permalink
Merge pull request #3604 from Navid200/Navid_2021_12_21b
Browse files Browse the repository at this point in the history
Save logs locally
  • Loading branch information
jamorham authored Aug 2, 2024
2 parents 31cc86e + aba9e7b commit 233cd68
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 21 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,12 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".utilitymodels.SaveLogs"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_save_logs"
android:noHistory="true">
</activity>
<activity android:name=".deposit.DepositActivity" />
<activity android:name=".utils.AndroidBarcode" />
<activity
Expand Down
27 changes: 20 additions & 7 deletions app/src/main/java/com/eveningoutpost/dexdrip/EventLogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.eveningoutpost.dexdrip.utilitymodels.Inevitable;
import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import com.eveningoutpost.dexdrip.utilitymodels.SaveLogs;
import com.eveningoutpost.dexdrip.utilitymodels.SendFeedBack;
import com.eveningoutpost.dexdrip.databinding.ActivityEventLogBinding;
import com.eveningoutpost.dexdrip.ui.helpers.BitmapUtil;
Expand All @@ -50,6 +51,7 @@
import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList;

import static com.eveningoutpost.dexdrip.Home.startWatchUpdaterService;
import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName;

/*
* New style event log viewer
Expand All @@ -65,6 +67,7 @@ public class EventLogActivity extends BaseAppCompatActivity {
private static final String TAG = EventLogActivity.class.getSimpleName();
private static final String PREF_SEVERITY_SELECTION = "event-log-severity-enabled-";
private static final String PREF_LAST_SEARCH = "event-log-last-search-";
private static int MAX_LOG_PACKAGE_SIZE = 200000;

static {
severitiesList.add(1);
Expand Down Expand Up @@ -291,20 +294,32 @@ private void updateToTopButtonVisibility(boolean force) {
}
}

// prepare current visible logs for upload
public synchronized void uploadEventLogs(View v) {
public synchronized void uploadEventLogs(View v) { // Send events log to JamOrHam
startActivity(new Intent(getApplicationContext(), SendFeedBack.class).putExtra("generic_text", packLogs()));
}

public synchronized void saveEventLog(View v) { // Save events log in mobile storage
startActivity(new Intent(getApplicationContext(), SaveLogs.class).putExtra("generic_text", packLogs()));
}

private String packLogs() { // Prepare current visible logs for upload or local save
final StringBuilder builder = new StringBuilder(50000);
builder.append("The following logs will be sent to the developers: \n\nPlease also include your email address or we will not know who they are from!\n\nFilter: "
builder.append("\n"
+ (model.allSeveritiesEnabled() ? "ALL" : model.whichSeveritiesEnabled()) + (model.getCurrentFilter() != "" ? " Search: " + model.getCurrentFilter() : "") + "\n\n");
for (UserError item : model.visible) {
builder.append(item.toString());
builder.append("\n");
if (builder.length() > 200000) {
if (builder.length() > MAX_LOG_PACKAGE_SIZE) {
JoH.static_toast_long(this, "Could not package up all logs, using most recent");
builder.append("\n\nOnly the most recent logs have been included to limit the file size.\n");
break;
}
}
startActivity(new Intent(getApplicationContext(), SendFeedBack.class).putExtra("generic_text", builder.toString()));

builder.insert(0, JoH.getDeviceDetails() + "\n" + JoH.getVersionDetails() + "\n" + getBestCollectorHardwareName() + "\n===\n" + "\nLog data:\n"); // Adds device, version and collector details before the log.
builder.append("\n\nCaptured: " + JoH.dateTimeText(JoH.tsl())); // Adds date and time of capture after the log.

return builder.toString();
}

// View model container - accessible binding methods must be declared public
Expand Down Expand Up @@ -636,5 +651,3 @@ public void onBindBinding(ViewDataBinding binding, int bindingVariable, @LayoutR
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.eveningoutpost.dexdrip.utilitymodels;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.eveningoutpost.dexdrip.BaseAppCompatActivity;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.UserError;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.eveningoutpost.dexdrip.utils.FileUtils.makeSureDirectoryExists;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

// Saves xDrip logs to storage.
// SendFeedBack sends logs to the lead developer.
// This does the same thing for saving logs to storage.
// Navid200
// July 2024

public class SaveLogs extends BaseAppCompatActivity {

private static final String TAG = "save logs";
private String LOG_FILE_PATH = "/Download/xDrip-export"; // Path to where we save the log file
private String LOG_FILE_NAME = "xDrip-log.txt"; // Log file name
private final static int MY_PERMISSIONS_REQUEST_STORAGE = 104;
private String log_data = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_logs);

Intent intent = getIntent();
if (intent != null) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
final String str2 = bundle.getString("generic_text");
if (str2 != null) {
log_data = str2;
((TextView) findViewById(R.id.yourSaveText)).setText(log_data.length() > 300 ? "\n\nAttached " + log_data.length() + " characters of log data. (hidden)\n\n" : log_data);
}
}
}
}

public void closeActivity(View myview) {
finish();
}

public void saveLogs(View myview) {
if (saveLogsToStorage(log_data)) {
UserError.Log.e(TAG, "Saved log file to /Downloads/xDrip-export/xDrip-log.txt");
} else {
UserError.Log.e(TAG, "Could not write log file");
}
log_data = "";
closeActivity(null); // Let's close the menu
}

public boolean saveLogsToStorage(String contents) {
if (isStorageWritable(this, MY_PERMISSIONS_REQUEST_STORAGE)) {
try {
final StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory().getAbsolutePath());
sb.append(LOG_FILE_PATH);
final String dir = sb.toString();
makeSureDirectoryExists(dir);
final String pathPlusFileName = dir + "/" + LOG_FILE_NAME;
final File myExternalFile = new File(pathPlusFileName);
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(contents.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
JoH.static_toast_long("getString(R.string.sdcard_not_writable_cannot_save)");
return false;
}
}

public static boolean isStorageWritable(Activity context, int request_code) { // Get write permission if not & return false. Return true if yes and not tied up.
if (ContextCompat.checkSelfPermission(context,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
request_code);
UserError.Log.e(TAG, "Did not have write permission, but should have it now");
return false;
}
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void onCreate(Bundle savedInstanceState) {
final String str2 = bundle.getString("generic_text");
if (str2 != null) {
log_data = str2;
((EditText) findViewById(R.id.yourText)).setText(log_data.length() > 300 ? "\n\nPlease describe what you think these logs may show? Explain the problem if there is one.\n\nAttached " + log_data.length() + " characters of log data. (hidden)\n\n" : log_data);
((EditText) findViewById(R.id.yourText)).setText(log_data.length() > 300 ? "\n\nPlease describe what you think these logs may show. Explain the problem if there is one.\n\nAttached " + log_data.length() + " characters of log data. (hidden)\n\n" : log_data);
type_of_message = "Log Push";
myrating.setVisibility(View.GONE);
ratingtext.setVisibility(View.GONE);
Expand Down Expand Up @@ -177,7 +177,7 @@ public void sendFeedback(View myview) {
try {
final RequestBody formBody = new FormEncodingBuilder()
.add("contact", contact.getText().toString())
.add("body", JoH.getDeviceDetails() + "\n" + JoH.getVersionDetails() + "\n" + getBestCollectorHardwareName() + "\n===\n\n" + yourtext.getText().toString() + " \n\n===\nType: " + type_of_message + "\nLog data:\n\n" + log_data + "\n\n\nSent: " + JoH.dateTimeText(JoH.tsl()))
.add("body",yourtext.getText().toString() + " \n\n===\nType: " + type_of_message + "\nLog data:\n\n" + log_data) // Adding "Your text" and type to the log
.add("rating", String.valueOf(myrating.getRating()))
.add("type", type_of_message)
.build();
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_event_log.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@
android:layout_weight="1"
android:onClick="uploadEventLogs"
android:text="@string/upload_logs"
android:textAllCaps="false"
android:textAlignment="center" />

<Button
android:id="@+id/event_save_logs_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="saveEventLog"
android:text="Save Logs"
android:textAllCaps="false"
android:textAlignment="center" />

<Button
Expand All @@ -148,6 +159,7 @@
android:layout_weight="1"
android:onClick="@{(v) -> viewModel.scrollToTop()}"
android:text="@string/top"
android:textAllCaps="false"
app:invisibleIfFalse="@{viewModel.showScrollToTop}" />

</LinearLayout>
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/activity_save_logs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.eveningoutpost.dexdrip.utilitymodels.SaveLogs">

<TextView
android:id="@+id/log_confidentail_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:text="@string/log_confidential_note"
android:textSize="18sp"/>

<TextView
android:id="@+id/yourSaveText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/log_confidentail_note"
android:textSize="18sp"/>

<Button
android:id="@+id/savelogbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="saveLogs"
android:text="@string/save_logs"
android:textAllCaps="false" />

<ImageButton
android:id="@+id/closebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:onClick="closeActivity"
android:src="@android:drawable/ic_delete" />
</RelativeLayout>
26 changes: 14 additions & 12 deletions app/src/main/res/layout/activity_send_feed_back.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.eveningoutpost.dexdrip.utilitymodels.SendFeedBack">

<EditText
<TextView
android:id="@+id/log_confidentail_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:text="@string/log_confidential_note"
android:textSize="18sp"/>

<EditText
android:id="@+id/yourText"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/log_confidentail_note"
android:hint="@string/please_enter_your_question_or_comments_here"
android:layout_below="@+id/contactText"
android:layout_above="@+id/ratingtext"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
android:inputType="textMultiLine"
android:text="" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/optional_contact_info_here_eg_email"
android:text=""
android:ems="10"
android:id="@+id/contactText"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
android:layout_below="@+id/yourText" />

<Button
android:layout_width="wrap_content"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<string name="title_activity_update">xDrip+ Update Available</string>
<string name="title_activity_display_qrcode">Share Settings via QR code</string>
<string name="title_activity_send_feed_back">Send Feedback to Jamorham</string>
<string name="title_activity_save_logs">Save Logs</string>
<string name="title_activity_maps">Parakeet Map</string>
<string name="title_activity_display_preferences">Display Preferences</string>

Expand Down Expand Up @@ -418,7 +419,9 @@
<string name="please_enter_your_question_or_comments_here">Please enter your question or comments here.\n\nIf you supply an email address you may get a response.\n\n</string>
<string name="optional_contact_info_here_eg_email">Optional contact info here, e.g. email</string>
<string name="send_message">Send Message</string>
<string name="save_logs">Save Logs</string>
<string name="please_indicate_what_you_think_of_the_app_generally">Please indicate what you think of the app generally</string>
<string name="log_confidential_note">Warning!\nThe logs may contain confidential information such as passwords or user IDs.</string>
<string name="preferences_saved_in_sdcard_downloads">Preferences saved in Internal storage/Download</string>
<string name="could_not_write_to_sdcard_check_perms">Couldn\'t write to SD card - check permissions?</string>
<string name="loaded_preferences_restarting">Loaded Preferences! - Restarting</string>
Expand Down

0 comments on commit 233cd68

Please sign in to comment.