Skip to content

Commit

Permalink
#273: More information will be logged when the "memory low" event is …
Browse files Browse the repository at this point in the history
…triggered.

It's now possible to disable "memory low" detection, which seems to cause problems on some ROMs.

NOTE: The application will crash if this option is disabled but the event is triggered by the system.
  • Loading branch information
Keidan committed Oct 17, 2023
1 parent e26fd32 commit 5c18c36
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 10 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/fr/ralala/hexviewer/ApplicationCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,22 @@ public boolean isSequential() {
public int getMemoryThreshold() {
try {
String s = getPref(this).getString(SettingsKeys.CFG_MEMORY_THRESHOLD, mDefaultMemoryThreshold);
if(s.equals(getString(R.string.memory_thresholds_disable)))
return -1;
if (s.startsWith("~"))
s = s.substring(1);
if (s.endsWith("%"))
s = s.substring(0, s.length() - 1);
return Integer.parseInt(s);
} catch (Exception ignore) {
return Integer.parseInt(mDefaultMemoryThreshold);
return -1;
}
}

public String getDefaultMemoryThreshold() {
return mDefaultMemoryThreshold;
}

/**
* Changes the default state specifying whether the source section (LineEdit) is expanded or collapsed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mScreenOrientation.setValue(mApp.getScreenOrientationStr());
mNbBytesPerLine.setDefaultValue("" + mApp.getNbBytesPerLine());

String mem = mApp.getPref(getContext()).getString(SettingsKeys.CFG_MEMORY_THRESHOLD, mApp.getDefaultMemoryThreshold());
if (mem == null || !mem.startsWith("~")) {
ListPreference lp = findPreference(SettingsKeys.CFG_MEMORY_THRESHOLD);
if (lp != null)
lp.setValueIndex(0);
}
refreshUiAccordingToOrientation(null);

}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/fr/ralala/hexviewer/ui/tasks/TaskOpen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

import fr.ralala.hexviewer.ApplicationCtx;
Expand Down Expand Up @@ -225,9 +226,14 @@ private void evaluateShiftOffset(FileData fd, long totalSequential) {
}
}

public void onLowAppMemory() {
ApplicationCtx.addLog(mContext, "Open", "Low memory detected");
mLowMemory.set(true);
mCancel.set(true);
public void onLowAppMemory(boolean disabled, long available, long used, float percentUsed) {
if (disabled) {
ApplicationCtx.addLog(mContext, "Open", "Low memory disabled");
} else {
ApplicationCtx.addLog(mContext, "Open",
String.format(Locale.US, "Low memory detected (a: %d, u: %d, pu: %f", available, used, percentUsed));
mLowMemory.set(true);
mCancel.set(true);
}
}
}
15 changes: 10 additions & 5 deletions app/src/main/java/fr/ralala/hexviewer/utils/MemoryMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MemoryMonitor implements Runnable {
private MemoryListener mMemoryListener;

public interface MemoryListener {
void onLowAppMemory();
void onLowAppMemory(boolean disabled, long available, long used, float percentUsed);
}

public MemoryMonitor(final float threshold, final int checkFrequencyMs) {
Expand All @@ -41,8 +41,14 @@ public void start(final MemoryListener memoryListener, final boolean autoStop) {
stop();
mMemoryListener = memoryListener;
mAutoStop = autoStop;
mMemoryHandler = new Handler(Looper.getMainLooper());
run();
if (mMemoryListener != null) {
if (mThreshold == -1) {
mMemoryListener.onLowAppMemory(true, 0, 0, 0);
} else {
mMemoryHandler = new Handler(Looper.getMainLooper());
run();
}
}
}

/**
Expand All @@ -64,8 +70,7 @@ public void run() {
// Check for & and handle low memory state
float percentUsed = 100f * (1f - ((float) used / available));
if (percentUsed <= mThreshold) {
if (mMemoryListener != null)
mMemoryListener.onLowAppMemory();
mMemoryListener.onLowAppMemory(false, available, used, percentUsed);
if (mAutoStop)
stop();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restore</string>
<string name="dialog_restore_question">Do you really want to restore the default values\?</string>
<string name="dialog_restore_recent">Delete the list of recent files\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Wiederherstellen</string>
<string name="dialog_restore_question">Möchten Sie wirklich die Standardwerte wiederherstellen?</string>
<string name="dialog_restore_recent">Die Liste der letzten Dateien löschen?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restaurar</string>
<string name="dialog_restore_question">¿Realmente quieres restaurar los valores por defecto?</string>
<string name="dialog_restore_recent">¿Borrar la lista de archivos recientes?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr-rFR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restaurer</string>
<string name="dialog_restore_question">Voulez-vous vraiment restaurer les valeurs par défaut ?</string>
<string name="dialog_restore_recent">Effacer la liste des fichiers récents ?</string>
<string name="memory_thresholds_disable">Désactiver</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-hu-rHU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Visszaállítás</string>
<string name="dialog_restore_question">Valóban visszaállítja az alapértelmezett értékeket\?</string>
<string name="dialog_restore_recent">Törli a legutóbb megnyitott fájlok listáját\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_recent">Hapus daftar file terbaru\?</string>
<string name="action_line_numbers_title">Baris nomor</string>
<string name="error_no_file">File hilang (dihapus atau dipindah)</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-it-rIT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restore</string>
<string name="dialog_restore_question">Do you really want to restore the default values\?</string>
<string name="dialog_restore_recent">Delete the list of recent files\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-iw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">שחזור</string>
<string name="dialog_restore_question">האם אתה באמת רוצה לשחזר את ערכי ברירת המחדל\?</string>
<string name="dialog_restore_recent">למחוק את רשימת הקבצים האחרונים\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restore</string>
<string name="dialog_restore_question">Do you really want to restore the default values\?</string>
<string name="dialog_restore_recent">Delete the list of recent files\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-nb-rNO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restore</string>
<string name="dialog_restore_question">Do you really want to restore the default values\?</string>
<string name="dialog_restore_recent">Delete the list of recent files\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restaurar</string>
<string name="dialog_restore_question">Deseja realmente restaurar os valores ao padrão\?</string>
<string name="dialog_restore_recent">Excluir a lista de arquivos recentes\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rPT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Restaurar</string>
<string name="dialog_restore_question">Deseja realmente restaurar os valores ao padrão\?</string>
<string name="dialog_restore_recent">Apagar a lista de ficheiros recentes\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru-rRU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Сброс</string>
<string name="dialog_restore_question">Вы действительно хотите сбросить до значений по умолчанию\?</string>
<string name="dialog_restore_recent">Удалить список недавного файлов\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-tr-rTR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Geri yükle</string>
<string name="dialog_restore_question">Öntanımlı değerleri gerçekten geri yüklemek istiyor musunuz\?</string>
<string name="dialog_restore_recent">Son kullanılan dosyaların listesi silinsin mi\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-vi-rVN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">Khôi phục</string>
<string name="dialog_restore_question">Bạn có thực sự muốn khôi phục giá trị mặc định không\?</string>
<string name="dialog_restore_recent">Xoá danh sách tệp gần đây\?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@
<string name="dialog_restore_title">还原</string>
<string name="dialog_restore_question">你真想还原默认值吗?</string>
<string name="dialog_restore_recent">删除最近文件列表吗?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="memory_thresholds" translatable="false">
<item>@string/memory_thresholds_disable</item>
<item>~5%</item>
<item>~6%</item>
<item>~7%</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@
<string name="dialog_restore_title">Restore</string>
<string name="dialog_restore_question">Do you really want to restore the default values?</string>
<string name="dialog_restore_recent">Delete the list of recent files?</string>
<string name="memory_thresholds_disable">Disable</string>
</resources>

0 comments on commit 5c18c36

Please sign in to comment.