Skip to content

Commit

Permalink
Cleaner widget logic, update widget on path change, by @harshad1 (PR #…
Browse files Browse the repository at this point in the history
…1660)


*  Closes #1661 , closes #822
  • Loading branch information
harshad1 authored Apr 22, 2022
1 parent 7146fb6 commit 79bcb47
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.arch.lifecycle.Lifecycle;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
Expand All @@ -38,7 +36,6 @@
import android.widget.TextView;
import android.widget.Toast;

import net.gsantner.markor.App;
import net.gsantner.markor.BuildConfig;
import net.gsantner.markor.R;
import net.gsantner.markor.format.TextConverter;
Expand Down Expand Up @@ -664,7 +661,6 @@ public boolean saveDocument(final boolean forceSaveEmpty) {
// Document is written iff content has changed
if (_isTextChanged && _document != null && _hlEditor != null && isAdded()) {
if (_document.saveContent(getContext(), _hlEditor.getText().toString(), _shareUtil, forceSaveEmpty)) {
updateLauncherWidgets();
checkTextChangeState();
return true;
} else {
Expand Down Expand Up @@ -698,13 +694,6 @@ public void onPause() {
super.onPause();
}

private void updateLauncherWidgets() {
Context c = App.get().getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(c);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(c, WrMarkorWidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_notes_list);
}

@Override
public void setUserVisibleHint(final boolean isVisibleToUser) {
// This function can be called _outside_ the normal lifecycle!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import butterknife.OnClick;
import butterknife.OnLongClick;
import butterknife.OnPageChange;
import other.writeily.widget.WrMarkorWidgetProvider;

public class MainActivity extends MarkorBaseActivity implements FilesystemViewerFragment.FilesystemFragmentOptionsListener, BottomNavigationView.OnNavigationItemSelectedListener {

Expand Down Expand Up @@ -173,7 +174,6 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -498,10 +498,15 @@ public HashMap<Integer, GsFragmentBase> getCachedFragments() {
}
}

@Override
protected void onPause() {
super.onPause();
WrMarkorWidgetProvider.updateLauncherWidgets();
}

@Override
protected void onStop() {
super.onStop();

restoreDefaultToolbar();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import other.writeily.widget.WrMarkorWidgetProvider;

public class SettingsActivity extends MarkorBaseActivity {

Expand Down Expand Up @@ -161,7 +162,7 @@ public String getFragmentTag() {
public void doUpdatePreferences() {
String remove = "/storage/emulated/0/";
updateSummary(R.string.pref_key__notebook_directory,
_cu.htmlToSpanned("<small><small>" + AppSettings.get().getNotebookDirectoryAsStr().replace(remove, "") + "</small></small>")
_cu.htmlToSpanned("<small><small>" + _as.getNotebookDirectoryAsStr().replace(remove, "") + "</small></small>")
);
updateSummary(R.string.pref_key__quicknote_filepath,
_cu.htmlToSpanned("<small><small>" + _as.getQuickNoteFile().getAbsolutePath().replace(remove, "") + "</small></small>")
Expand Down Expand Up @@ -235,6 +236,8 @@ protected void onPreferenceChanged(final SharedPreferences prefs, final String k
} catch (IllegalArgumentException e) {
Toast.makeText(context, e.getLocalizedMessage() + "\n\n" + getString(R.string.loading_default_value), Toast.LENGTH_SHORT).show();
}
} else if (eq(key, R.string.pref_key__notebook_directory, R.string.pref_key__quicknote_filepath, R.string.pref_key__todo_filepath)) {
WrMarkorWidgetProvider.updateLauncherWidgets();
}
}

Expand All @@ -250,9 +253,8 @@ public Boolean onPreferenceClicked(Preference preference, String key, int keyRes
FilesystemViewerCreator.showFolderDialog(new FilesystemViewerData.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
AppSettings as = AppSettings.get();
as.setSaveDirectory(file.getAbsolutePath());
as.setRecreateMainRequired(true);
_as.setSaveDirectory(file.getAbsolutePath());
_as.setRecreateMainRequired(true);
doUpdatePreferences();
}

Expand All @@ -273,16 +275,15 @@ public void onFsViewerConfig(FilesystemViewerData.Options dopt) {
FilesystemViewerCreator.showFileDialog(new FilesystemViewerData.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
AppSettings as = AppSettings.get();
as.setQuickNoteFile(file);
as.setRecreateMainRequired(true);
_as.setQuickNoteFile(file);
_as.setRecreateMainRequired(true);
doUpdatePreferences();
}

@Override
public void onFsViewerConfig(FilesystemViewerData.Options dopt) {
dopt.titleText = R.string.quicknote;
dopt.rootFolder = Environment.getExternalStorageDirectory();
dopt.rootFolder = _as.getNotebookDirectory();
}
}, fragManager, getActivity(), FilesystemViewerCreator.IsMimeText);
}
Expand All @@ -294,16 +295,15 @@ public void onFsViewerConfig(FilesystemViewerData.Options dopt) {
FilesystemViewerCreator.showFileDialog(new FilesystemViewerData.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
AppSettings as = AppSettings.get();
as.setTodoFile(file);
as.setRecreateMainRequired(true);
_as.setTodoFile(file);
_as.setRecreateMainRequired(true);
doUpdatePreferences();
}

@Override
public void onFsViewerConfig(FilesystemViewerData.Options dopt) {
dopt.titleText = R.string.todo;
dopt.rootFolder = Environment.getExternalStorageDirectory();
dopt.rootFolder = _as.getNotebookDirectory();
}
}, fragManager, getActivity(), FilesystemViewerCreator.IsMimeText);
}
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,9 @@ public class Document implements Serializable {

public Document(@NonNull final File file) {
_file = file;
final String name = _file.getName();
final int doti = name.lastIndexOf(".");
if (doti < 0) {
_fileExtension = "";
_title = name;
} else {
_fileExtension = name.substring(doti).toLowerCase();
_title = name.substring(0, doti);
}
_path = _file.getAbsolutePath();
_title = FileUtils.getFilenameWithoutExtension(_file);
_fileExtension = FileUtils.getFilenameExtension(_file);

// Set initial format
final String fnlower = _file.getName().toLowerCase();
Expand Down Expand Up @@ -117,10 +110,6 @@ public String getTitle() {
return _title;
}

public void setTitle(String title) {
_title = title == null ? "" : title;
}

public String getName() {
return _file.getName();
}
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/net/gsantner/markor/util/ShareUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,27 @@
import net.gsantner.markor.activity.MainActivity;
import net.gsantner.markor.activity.openeditor.OpenEditorFromShortcutOrWidgetActivity;
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.util.FileUtils;

import java.io.File;

public class ShareUtil extends net.gsantner.opoc.util.ShareUtil {
public ShareUtil(Context context) {
super(context);
setChooserTitle(_context.getString(R.string.share_to_arrow));
}

public void createLauncherDesktopShortcut(final Document document) {
public void createLauncherDesktopShortcut(final File file) {
// This is only allowed to call when direct file access is possible!!
// So basically only for java.io.File Objects. Virtual files, or content://
// in private/restricted space won't work - because of missing permission grant when re-launching
if (document != null && !TextUtils.isEmpty(document.getTitle())) {
final boolean isDir = document.getFile().isDirectory();
final String title = file != null ? FileUtils.getFilenameWithoutExtension(file) : null;
if (!TextUtils.isEmpty(title)) {
final boolean isDir = file.isDirectory();
final Class<?> klass = isDir ? MainActivity.class : OpenEditorFromShortcutOrWidgetActivity.class;
final Intent intent = new Intent(_context, klass).setData(Uri.fromFile(document.getFile()));
final Intent intent = new Intent(_context, klass).setData(Uri.fromFile(file));
final int iconRes = isDir ? R.mipmap.ic_shortcut_folder : R.mipmap.ic_shortcut_file;
super.createLauncherDesktopShortcut(intent, iconRes, document.getTitle());
super.createLauncherDesktopShortcut(intent, iconRes, title);
// Toast.makeText(_context, R.string.tried_to_create_shortcut_for_this_notice, Toast.LENGTH_LONG).show();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -454,23 +455,29 @@ private void onPreferenceScreenChangedPriv(PreferenceFragmentCompat preferenceFr
* Is key equal
*
* @param pref A preference
* @param resIdKey the resource id of the string
* @return if equals
* @param resIdKey one or more string resource ids
* @return if equals any of the resource ids
*/
public boolean eq(@Nullable Preference pref, @StringRes int resIdKey) {
return pref != null && getString(resIdKey).equals(pref.getKey());
public boolean eq(final @Nullable Preference pref, final @StringRes int... resIdKey) {
return pref != null && eq(pref.getKey(), resIdKey);
}


/**
* Is key equal
*
* @param key the key
* @param resIdKey the resource id of the string
* @return if equals
* @param resIdKey one or more string resource ids
* @return if equals any of the resource ids
*/
public boolean eq(@Nullable String key, @StringRes int resIdKey) {
return getString(resIdKey).equals(key);
public boolean eq(final @Nullable String key, @StringRes int... resIdKey) {
resIdKey = resIdKey != null ? resIdKey : new int[0];
for (final int id : resIdKey) {
if (getString(id).equals(key)) {
return true;
}
}
return false;
}

public boolean hasTitle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import other.writeily.model.WrMarkorSingleton;
import other.writeily.ui.WrConfirmDialog;
import other.writeily.ui.WrRenameDialog;
import other.writeily.widget.WrMarkorWidgetProvider;

public class FilesystemViewerFragment extends GsFragmentBase
implements FilesystemViewerData.SelectionListener {
Expand Down Expand Up @@ -374,12 +375,12 @@ public FilesystemViewerAdapter getAdapter() {
public boolean onOptionsItemSelected(MenuItem item) {
final PermissionChecker permc = new PermissionChecker(getActivity());

File folderToLoad = null;
final int _id = item.getItemId();

switch (item.getItemId()) {
switch (_id) {
case R.id.action_create_shortcut: {
final File file = _filesystemViewerAdapter.getCurrentSelection().iterator().next();
_shareUtil.createLauncherDesktopShortcut(new Document(file));
_shareUtil.createLauncherDesktopShortcut(file);
return true;
}
case R.id.action_sort_by_name: {
Expand Down Expand Up @@ -440,8 +441,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_go_to_appdata_sdcard_1:
case R.id.action_go_to_appdata_sdcard_2:
case R.id.action_go_to_appdata_public: {
folderToLoad = _appSettings.getFolderToLoadByMenuId(item.getItemId());
break;
final File folder = _appSettings.getFolderToLoadByMenuId(_id);
_filesystemViewerAdapter.setCurrentFolder(folder, true);
Toast.makeText(getContext(), folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();
return true;
}
case R.id.action_favourite:
case R.id.action_favourite_remove: {
Expand All @@ -468,12 +471,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

case R.id.action_move_selected_items: {
askForMoveOrCopy(true);
return true;
}
case R.id.action_move_selected_items:
case R.id.action_copy_selected_items: {
askForMoveOrCopy(false);
askForMoveOrCopy(_id == R.id.action_move_selected_items);
return true;
}

Expand Down Expand Up @@ -516,12 +516,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

if (folderToLoad != null) {
_filesystemViewerAdapter.setCurrentFolder(folderToLoad, true);
Toast.makeText(getContext(), folderToLoad.getAbsolutePath(), Toast.LENGTH_SHORT).show();
return true;
}

return false;
}

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/net/gsantner/opoc/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,18 @@ public static boolean fileExists(final File checkFile) {
}
return false;
}

// Get the title of the file
public static String getFilenameWithoutExtension(final File file) {
final String name = file.getName();
final int doti = name.lastIndexOf(".");
return (doti < 0) ? name : name.substring(0, doti);
}

/// Get the file extension of the file
public static String getFilenameExtension(final File file) {
final String name = file.getName();
final int doti = name.lastIndexOf(".");
return (doti < 0) ? "" : name.substring(doti).toLowerCase();
}
}
Loading

0 comments on commit 79bcb47

Please sign in to comment.