From ac01274eb497d6c3810463535f40f868e3dfdf2b Mon Sep 17 00:00:00 2001 From: TranceLove Date: Mon, 23 Oct 2023 19:01:06 +0800 Subject: [PATCH] Chore fixes in TextEditorActivity and FtpNotification - FtpNotification use NotificationManagerCompat to ensure compatibility - TextEditorActivity added a couple of null checks to make linter happier Fixes #768 --- .../texteditor/TextEditorActivity.java | 164 ++++++++++-------- .../ui/notifications/FtpNotification.java | 10 +- 2 files changed, 96 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java b/app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java index f3bebaf489..be5fad7533 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java +++ b/app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java @@ -75,6 +75,7 @@ import android.widget.Toast; import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; import androidx.appcompat.widget.AppCompatEditText; import androidx.appcompat.widget.AppCompatImageButton; import androidx.lifecycle.ViewModelProvider; @@ -130,7 +131,9 @@ public void onCreate(Bundle savedInstanceState) { boolean useNewStack = getBoolean(PREFERENCE_TEXTEDITOR_NEWSTACK); - getSupportActionBar().setDisplayHomeAsUpEnabled(!useNewStack); + if (getSupportActionBar() != null) { + getSupportActionBar().setDisplayHomeAsUpEnabled(!useNewStack); + } mainTextView = findViewById(R.id.fname); scrollView = findViewById(R.id.editscroll); @@ -176,12 +179,13 @@ public void onCreate(Bundle savedInstanceState) { } @Override - protected void onSaveInstanceState(Bundle outState) { + protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); final TextEditorActivityViewModel viewModel = new ViewModelProvider(this).get(TextEditorActivityViewModel.class); - outState.putString(KEY_MODIFIED_TEXT, mainTextView.getText().toString()); + outState.putString( + KEY_MODIFIED_TEXT, mainTextView.getText() != null ? mainTextView.getText().toString() : ""); outState.putInt(KEY_INDEX, mainTextView.getScrollY()); outState.putString(KEY_ORIGINAL_TEXT, viewModel.getOriginal()); outState.putBoolean(KEY_MONOFONT, inputTypefaceMono.equals(mainTextView.getTypeface())); @@ -193,6 +197,7 @@ private void checkUnsavedChanges() { if (viewModel.getOriginal() != null && mainTextView.isShown() + && mainTextView.getText() != null && !viewModel.getOriginal().equals(mainTextView.getText().toString())) { new MaterialDialog.Builder(this) .title(R.string.unsaved_changes) @@ -293,10 +298,13 @@ public boolean onOptionsItemSelected(MenuItem item) { break; case R.id.save: // Make sure EditText is visible before saving! - saveFile(this, mainTextView.getText().toString()); + if (mainTextView.getText() != null) { + saveFile(this, mainTextView.getText().toString()); + } break; case R.id.details: if (editableFileAbstraction.scheme.equals(FILE) + && editableFileAbstraction.hybridFileParcelable.getFile() != null && editableFileAbstraction.hybridFileParcelable.getFile().exists()) { GeneralDialogCreation.showPropertiesDialogWithoutPermissions( editableFileAbstraction.hybridFileParcelable, this, getAppTheme()); @@ -316,7 +324,7 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.openwith: if (editableFileAbstraction.scheme.equals(FILE)) { File currentFile = editableFileAbstraction.hybridFileParcelable.getFile(); - if (currentFile.exists()) { + if (currentFile != null && currentFile.exists()) { boolean useNewStack = getBoolean(PREFERENCE_TEXTEDITOR_NEWSTACK); FileUtils.openWith(currentFile, this, useNewStack); } else { @@ -355,7 +363,8 @@ public void onDestroy() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { // condition to check if callback is called in search editText - if (searchEditText != null && charSequence.hashCode() == searchEditText.getText().hashCode()) { + if (searchEditText.getText() != null + && charSequence.hashCode() == searchEditText.getText().hashCode()) { final TextEditorActivityViewModel viewModel = new ViewModelProvider(this).get(TextEditorActivityViewModel.class); @@ -371,56 +380,61 @@ public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) @Override public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { - if (charSequence.hashCode() == mainTextView.getText().hashCode()) { - final TextEditorActivityViewModel viewModel = - new ViewModelProvider(this).get(TextEditorActivityViewModel.class); - final Timer oldTimer = viewModel.getTimer(); - viewModel.setTimer(null); - - if (oldTimer != null) { - oldTimer.cancel(); - oldTimer.purge(); - } - - final WeakReference textEditorActivityWR = new WeakReference<>(this); - - Timer newTimer = new Timer(); - newTimer.schedule( - new TimerTask() { - boolean modified; + if (mainTextView.getText() != null) { + if (charSequence.hashCode() == mainTextView.getText().hashCode()) { + final TextEditorActivityViewModel viewModel = + new ViewModelProvider(this).get(TextEditorActivityViewModel.class); + final Timer oldTimer = viewModel.getTimer(); + viewModel.setTimer(null); + + if (oldTimer != null) { + oldTimer.cancel(); + oldTimer.purge(); + } - @Override - public void run() { - final TextEditorActivity textEditorActivity = textEditorActivityWR.get(); - if (textEditorActivity == null) { - return; + final WeakReference textEditorActivityWR = new WeakReference<>(this); + + Timer newTimer = new Timer(); + newTimer.schedule( + new TimerTask() { + boolean modified; + + @Override + public void run() { + final TextEditorActivity textEditorActivity = textEditorActivityWR.get(); + if (textEditorActivity == null) { + return; + } + + final TextEditorActivityViewModel viewModel = + new ViewModelProvider(textEditorActivity) + .get(TextEditorActivityViewModel.class); + + modified = + textEditorActivity.mainTextView.getText() != null + && !textEditorActivity + .mainTextView + .getText() + .toString() + .equals(viewModel.getOriginal()); + if (viewModel.getModified() != modified) { + viewModel.setModified(modified); + invalidateOptionsMenu(); + } } + }, + 250); - final TextEditorActivityViewModel viewModel = - new ViewModelProvider(textEditorActivity).get(TextEditorActivityViewModel.class); - - modified = - !textEditorActivity - .mainTextView - .getText() - .toString() - .equals(viewModel.getOriginal()); - if (viewModel.getModified() != modified) { - viewModel.setModified(modified); - invalidateOptionsMenu(); - } - } - }, - 250); - - viewModel.setTimer(newTimer); + viewModel.setTimer(newTimer); + } } } @Override public void afterTextChanged(Editable editable) { // searchBox callback block - if (searchEditText != null && editable.hashCode() == searchEditText.getText().hashCode()) { + if (searchEditText.getText() != null + && editable.hashCode() == searchEditText.getText().hashCode()) { final WeakReference textEditorActivityWR = new WeakReference<>(this); final OnProgressUpdate onProgressUpdate = @@ -460,13 +474,15 @@ public void afterTextChanged(Editable editable) { } }; - searchTextTask = - new SearchTextTask( - mainTextView.getText().toString(), - editable.toString(), - onProgressUpdate, - onAsyncTaskFinished); - searchTextTask.execute(); + if (mainTextView.getText() != null) { + searchTextTask = + new SearchTextTask( + mainTextView.getText().toString(), + editable.toString(), + onProgressUpdate, + onAsyncTaskFinished); + searchTextTask.execute(); + } } } @@ -594,12 +610,14 @@ private void highlightCurrentSearchResult(final TextEditorActivityViewModel view colorSearchResult(keyValueNew, Utils.getColor(this, R.color.search_text_highlight)); // scrolling to the highlighted element - scrollView.scrollTo( - 0, - (Integer) keyValueNew.getLineNumber() - + mainTextView.getLineHeight() - + Math.round(mainTextView.getLineSpacingExtra()) - - getSupportActionBar().getHeight()); + if (getSupportActionBar() != null) { + scrollView.scrollTo( + 0, + (Integer) keyValueNew.getLineNumber() + + mainTextView.getLineHeight() + + Math.round(mainTextView.getLineSpacingExtra()) + - getSupportActionBar().getHeight()); + } } private void unhighlightSearchResult(SearchResultIndex resultIndex) { @@ -614,13 +632,15 @@ private void unhighlightSearchResult(SearchResultIndex resultIndex) { } private void colorSearchResult(SearchResultIndex resultIndex, @ColorInt int color) { - mainTextView - .getText() - .setSpan( - new BackgroundColorSpan(color), - (Integer) resultIndex.getStartCharNumber(), - (Integer) resultIndex.getEndCharNumber(), - Spanned.SPAN_INCLUSIVE_INCLUSIVE); + if (mainTextView.getText() != null) { + mainTextView + .getText() + .setSpan( + new BackgroundColorSpan(color), + (Integer) resultIndex.getStartCharNumber(), + (Integer) resultIndex.getEndCharNumber(), + Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } } private void cleanSpans(TextEditorActivityViewModel viewModel) { @@ -630,10 +650,12 @@ private void cleanSpans(TextEditorActivityViewModel viewModel) { viewModel.setLine(0); // clearing textView spans - BackgroundColorSpan[] colorSpans = - mainTextView.getText().getSpans(0, mainTextView.length(), BackgroundColorSpan.class); - for (BackgroundColorSpan colorSpan : colorSpans) { - mainTextView.getText().removeSpan(colorSpan); + if (mainTextView.getText() != null) { + BackgroundColorSpan[] colorSpans = + mainTextView.getText().getSpans(0, mainTextView.length(), BackgroundColorSpan.class); + for (BackgroundColorSpan colorSpan : colorSpans) { + mainTextView.getText().removeSpan(colorSpan); + } } } } diff --git a/app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotification.java b/app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotification.java index defb81ff26..7117f551d5 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotification.java +++ b/app/src/main/java/com/amaze/filemanager/ui/notifications/FtpNotification.java @@ -31,7 +31,6 @@ import com.amaze.filemanager.utils.NetworkUtil; import android.app.Notification; -import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -39,6 +38,7 @@ import androidx.annotation.StringRes; import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import androidx.preference.PreferenceManager; /** @@ -96,9 +96,7 @@ public static Notification startNotification(Context context, boolean noStopButt } public static void updateNotification(Context context, boolean noStopButton) { - String notificationService = Context.NOTIFICATION_SERVICE; - NotificationManager notificationManager = - (NotificationManager) context.getSystemService(notificationService); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); int port = sharedPreferences.getInt(FtpService.PORT_PREFERENCE_KEY, FtpService.DEFAULT_PORT); @@ -129,8 +127,6 @@ public static void updateNotification(Context context, boolean noStopButton) { } private static void removeNotification(Context context) { - String ns = Context.NOTIFICATION_SERVICE; - NotificationManager nm = (NotificationManager) context.getSystemService(ns); - nm.cancelAll(); + NotificationManagerCompat.from(context).cancelAll(); } }