Skip to content

Commit

Permalink
Using built in voice recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Oct 2, 2023
1 parent 6ba801c commit 4d837e5
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.gsantner.markor.format.plaintext.PlaintextActionButtons;
import net.gsantner.markor.format.todotxt.TodoTxtActionButtons;
import net.gsantner.markor.format.wikitext.WikitextActionButtons;
import net.gsantner.opoc.util.GsCollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -110,14 +111,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void saveNewOrder() {
final ArrayList<String> reorderedKeys = new ArrayList<>();

for (final int i : _adapter.order) {
reorderedKeys.add(_keys.get(i));
}

_textActions.saveActionOrder(reorderedKeys);
_textActions.saveDisabledActions(new ArrayList<>(_adapter._disabled));
_textActions.saveActionOrder(GsCollectionUtils.map(_adapter.order, i -> _keys.get(i)));
_textActions.saveDisabledActions(_adapter._disabled);
}

@Override
Expand Down Expand Up @@ -238,8 +233,8 @@ private ReorderCallback(OrderAdapter adapter) {

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
final int from = viewHolder.getAdapterPosition();
final int to = target.getAdapterPosition();
final int from = viewHolder.getAbsoluteAdapterPosition();
final int to = target.getAbsoluteAdapterPosition();

final int value = _adapter.order.get(from);
_adapter.order.remove(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
// Restore keyboard when we regain focus
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
_hlEditor.getViewTreeObserver().addOnWindowFocusChangeListener(hasFocus -> {
if (hasFocus) {
if (hasFocus && defocusImeState != null) {
_cu.showSoftKeyboard(getActivity(), defocusImeState, _hlEditor);
defocusImeState = null;
} else {
Expand Down
134 changes: 73 additions & 61 deletions app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsCollectionUtils;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -63,13 +66,13 @@

@SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
public abstract class ActionButtonBase {
private Activity m_activity;
private MarkorContextUtils m_cu;
private Activity _activity;
private MarkorContextUtils _cu;
private final int _buttonHorizontalMargin;
private String _lastSnip;

protected HighlightingEditor _hlEditor;
protected WebView m_webView;
protected WebView _webView;
protected Document _document;
protected AppSettings _appSettings;
protected int _indent;
Expand Down Expand Up @@ -97,7 +100,7 @@ public boolean onActionLongClick(final @StringRes int action) {

// Override to implement custom search action
public boolean onSearch() {
MarkorDialogFactory.showSearchDialog(getActivity(), _hlEditor);
MarkorDialogFactory.showSearchDialog(_activity, _hlEditor);
return true;
}

Expand Down Expand Up @@ -137,10 +140,10 @@ public List<String> getDisabledActions() {
* @return Map of String key -> Action
*/
public Map<String, ActionItem> getActiveActionMap() {
List<ActionItem> actionList = getActiveActionList();
List<String> keyList = getActiveActionKeys();
final List<ActionItem> actionList = getActiveActionList();
final List<String> keyList = getActiveActionKeys();

Map<String, ActionItem> map = new HashMap<String, ActionItem>();
final Map<String, ActionItem> map = new HashMap<String, ActionItem>();

for (int i = 0; i < actionList.size(); i++) {
map.put(keyList.get(i), actionList.get(i));
Expand All @@ -154,14 +157,7 @@ public Map<String, ActionItem> getActiveActionMap() {
* @return List or resource strings
*/
public List<String> getActiveActionKeys() {
final List<ActionItem> actionList = getActiveActionList();
final ArrayList<String> keys = new ArrayList<>();

for (ActionItem item : actionList) {
keys.add(rstr(item.keyId));
}

return keys;
return GsCollectionUtils.map(getActiveActionList(), item -> rstr(item.keyId));
}

/**
Expand All @@ -172,7 +168,7 @@ public List<String> getActiveActionKeys() {
*
* @param keys of keys (in order) to save
*/
public void saveDisabledActions(final List<String> keys) {
public void saveDisabledActions(final Collection<String> keys) {
saveActionPreference(DISABLED_SUFFIX, keys);
}

Expand All @@ -184,17 +180,13 @@ public void saveDisabledActions(final List<String> keys) {
*
* @param keys of keys (in order) to save
*/
public void saveActionOrder(final List<String> keys) {
public void saveActionOrder(final Collection<String> keys) {
saveActionPreference(ORDER_SUFFIX, keys);
}

private void saveActionPreference(final String suffix, List<String> values) {
// Remove any values not in current actions
values = new ArrayList<>(values);
values.retainAll(getActiveActionKeys());

SharedPreferences settings = getContext().getSharedPreferences(ACTION_ORDER_PREF_NAME, Context.MODE_PRIVATE);
String formatKey = rstr(getFormatActionsKey()) + suffix;
private void saveActionPreference(final String suffix, final Collection<String> values) {
final SharedPreferences settings = getContext().getSharedPreferences(ACTION_ORDER_PREF_NAME, Context.MODE_PRIVATE);
final String formatKey = rstr(getFormatActionsKey()) + suffix;
settings.edit().putString(formatKey, TextUtils.join(",", values)).apply();
}

Expand All @@ -220,30 +212,38 @@ private List<String> loadActionPreference(final String suffix) {
*/
public List<String> getActionOrder() {

ArrayList<String> definedKeys = new ArrayList<>(getActiveActionKeys());
List<String> prefKeys = new ArrayList<>(loadActionPreference(ORDER_SUFFIX));
final Set<String> order = new LinkedHashSet<>(loadActionPreference(ORDER_SUFFIX));

// Handle the case where order was stored without suffix. i.e. before this release.
if (prefKeys.size() == 0) {
prefKeys = new ArrayList<>(loadActionPreference(""));
if (order.isEmpty()) {
order.addAll(loadActionPreference(""));
}

Set<String> prefSet = new LinkedHashSet<>(prefKeys);
Set<String> defSet = new LinkedHashSet<>(definedKeys);
final Set<String> defined = new LinkedHashSet<>(getActiveActionKeys());
final Set<String> disabled = new LinkedHashSet<>(getDisabledActions());

// Any definedKeys which are not in prefs or disabled keys are added to disabled
final Set<String> existing = GsCollectionUtils.union(order, disabled);
final Set<String> added = GsCollectionUtils.setDiff(defined, existing);
final Set<String> removed = GsCollectionUtils.setDiff(existing, defined);

// Disable any new actions unless none existing (i.e. first run)
if (!existing.isEmpty()) {
disabled.addAll(added);
}

// Add any defined keys which are not in prefs
defSet.removeAll(prefSet);
prefKeys.addAll(defSet);
// Add new ones to order
order.addAll(added);

// Remove any pref keys which are not defined
prefSet.removeAll(definedKeys);
prefKeys.removeAll(prefSet);
// Removed removed from order and disabled
disabled.removeAll(removed);
order.removeAll(removed);

if (defSet.size() > 0 || prefSet.size() > 0) {
saveActionOrder(prefKeys);
if (!added.isEmpty() || !removed.isEmpty()) {
saveActionOrder(order);
}

return prefKeys;
return new ArrayList<>(order);
}

@SuppressWarnings("ConstantConditions")
Expand All @@ -263,7 +263,7 @@ public void recreateActionButtons(ViewGroup barLayout, ActionItem.DisplayMode di
}

protected void appendActionButtonToBar(ViewGroup barLayout, @NonNull ActionItem action) {
final ImageView btn = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.quick_keyboard_button, null);
final ImageView btn = (ImageView) _activity.getLayoutInflater().inflate(R.layout.quick_keyboard_button, null);
btn.setImageResource(action.iconId);
final String desc = rstr(action.stringId);
btn.setContentDescription(desc);
Expand Down Expand Up @@ -499,10 +499,10 @@ else if (((selectionEnd <= (_hlEditor.length() - _action.length())) &&


public ActionButtonBase setUiReferences(@Nullable final Activity activity, @Nullable final HighlightingEditor hlEditor, @Nullable final WebView webview) {
m_activity = activity;
_activity = activity;
_hlEditor = hlEditor;
m_webView = webview;
m_cu = new MarkorContextUtils(m_activity);
_webView = webview;
_cu = new MarkorContextUtils(_activity);
return this;
}

Expand All @@ -516,22 +516,22 @@ public ActionButtonBase setDocument(Document document) {
}

public Activity getActivity() {
return m_activity;
return _activity;
}

public Context getContext() {
return m_activity != null ? m_activity : _appSettings.getContext();
return _activity != null ? _activity : _appSettings.getContext();
}

public MarkorContextUtils getCu() {
return m_cu;
return _cu;
}

/**
* Callable from background thread!
*/
public void setEditorTextAsync(final String text) {
getActivity().runOnUiThread(() -> _hlEditor.setText(text));
_activity.runOnUiThread(() -> _hlEditor.setText(text));
}

protected void runIndentLines(final boolean deIndent) {
Expand Down Expand Up @@ -562,23 +562,23 @@ protected final boolean runCommonAction(final @StringRes int action) {
return true;
}
case R.string.abid_common_time: {
DatetimeFormatDialog.showDatetimeFormatDialog(getActivity(), _hlEditor);
DatetimeFormatDialog.showDatetimeFormatDialog(_activity, _hlEditor);
return true;
}
case R.string.abid_common_accordion: {
_hlEditor.insertOrReplaceTextOnCursor("<details markdown='1'><summary>" + rstr(R.string.expand_collapse) + "</summary>\n" + HighlightingEditor.PLACE_CURSOR_HERE_TOKEN + "\n\n</details>");
return true;
}
case R.string.abid_common_insert_audio: {
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.AUDIO_ACTION, _document.getFormat(), getActivity(), _hlEditor, _document.getFile());
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.AUDIO_ACTION, _document.getFormat(), _activity, text, _document.getFile());
return true;
}
case R.string.abid_common_insert_link: {
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.FILE_OR_LINK_ACTION, _document.getFormat(), getActivity(), _hlEditor, _document.getFile());
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.FILE_OR_LINK_ACTION, _document.getFormat(), _activity, text, _document.getFile());
return true;
}
case R.string.abid_common_insert_image: {
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.IMAGE_ACTION, _document.getFormat(), getActivity(), _hlEditor, _document.getFile());
AttachLinkOrFileDialog.showInsertImageOrLinkDialog(AttachLinkOrFileDialog.IMAGE_ACTION, _document.getFormat(), _activity, text, _document.getFile());
return true;
}
case R.string.abid_common_ordered_list_renumber: {
Expand All @@ -598,7 +598,7 @@ protected final boolean runCommonAction(final @StringRes int action) {
return true;
}
case R.string.abid_common_insert_snippet: {
MarkorDialogFactory.showInsertSnippetDialog(getActivity(), (snip) -> {
MarkorDialogFactory.showInsertSnippetDialog(_activity, (snip) -> {
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateEscapedDateTime(snip));
_lastSnip = snip;
});
Expand All @@ -610,7 +610,7 @@ protected final boolean runCommonAction(final @StringRes int action) {
if (url.endsWith(")")) {
url = url.substring(0, url.length() - 1);
}
getCu().openWebpageInExternalBrowser(getContext(), url);
_cu.openWebpageInExternalBrowser(getContext(), url);
}
return true;
}
Expand All @@ -636,15 +636,15 @@ protected final boolean runCommonAction(final @StringRes int action) {
return true;
}
case R.string.abid_common_web_jump_to_table_of_contents: {
m_webView.loadUrl("javascript:document.getElementsByClassName('toc')[0].scrollIntoView();");
_webView.loadUrl("javascript:document.getElementsByClassName('toc')[0].scrollIntoView();");
return true;
}
case R.string.abid_common_view_file_in_other_app: {
getCu().viewFileInOtherApp(getContext(), _document.getFile(), GsFileUtils.getMimeType(_document.getFile()));
_cu.viewFileInOtherApp(getContext(), _document.getFile(), GsFileUtils.getMimeType(_document.getFile()));
return true;
}
case R.string.abid_common_rotate_screen: {
getCu().nextScreenRotationSetting(getActivity());
_cu.nextScreenRotationSetting(_activity);
return true;
}
}
Expand All @@ -658,7 +658,7 @@ protected final boolean runCommonLongPressAction(@StringRes int action) {
switch (action) {
case R.string.abid_common_deindent:
case R.string.abid_common_indent: {
MarkorDialogFactory.showIndentSizeDialog(getActivity(), _indent, (size) -> {
MarkorDialogFactory.showIndentSizeDialog(_activity, _indent, (size) -> {
_indent = Integer.parseInt(size);
_appSettings.setDocumentIndentSize(_document.getPath(), _indent);
});
Expand Down Expand Up @@ -693,6 +693,18 @@ protected final boolean runCommonLongPressAction(@StringRes int action) {
}
return true;
}
case R.string.abid_common_insert_audio: {
AttachLinkOrFileDialog.insertAudioRecording(_activity, _document.getFormat(), _hlEditor.getText(), _document.getFile());
return true;
}
case R.string.abid_common_insert_link: {
AttachLinkOrFileDialog.insertGalleryPhoto(_activity, _document.getFormat(), _hlEditor.getText(), _document.getFile());
return true;
}
case R.string.abid_common_insert_image: {
AttachLinkOrFileDialog.insertCameraPhoto(_activity, _document.getFormat(), _hlEditor.getText(), _document.getFile());
return true;
}
}
return false;
}
Expand Down Expand Up @@ -865,11 +877,11 @@ public void runJumpBottomTopAction(ActionItem.DisplayMode displayMode) {
int pos = _hlEditor.getSelectionStart();
_hlEditor.setSelection(pos == 0 ? _hlEditor.getText().length() : 0);
} else if (displayMode == ActionItem.DisplayMode.VIEW) {
boolean top = m_webView.getScrollY() > 100;
m_webView.scrollTo(0, top ? 0 : m_webView.getContentHeight());
boolean top = _webView.getScrollY() > 100;
_webView.scrollTo(0, top ? 0 : _webView.getContentHeight());
if (!top) {
m_webView.scrollBy(0, 1000);
m_webView.scrollBy(0, 1000);
_webView.scrollBy(0, 1000);
_webView.scrollBy(0, 1000);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ public boolean onActionClick(final @StringRes int action) {
@Override
public boolean onActionLongClick(final @StringRes int action) {
switch (action) {
case R.string.abid_common_insert_image: {
int pos = _hlEditor.getSelectionStart();
_hlEditor.getText().insert(pos, "<img style=\"width:auto;max-height: 256px;\" src=\"\" />");
_hlEditor.setSelection(pos + 48);
return true;
}
case R.string.abid_markdown_table_insert_columns: {
MarkorDialogFactory.showInsertTableRowDialog(getActivity(), true, this::insertTableRow);
return true;
Expand Down
Loading

0 comments on commit 4d837e5

Please sign in to comment.