Skip to content

Commit

Permalink
Chore fixes in TextEditorActivity and FtpNotification
Browse files Browse the repository at this point in the history
- FtpNotification use NotificationManagerCompat to ensure compatibility
- TextEditorActivity added a couple of null checks to make linter happier

Fixes #768
  • Loading branch information
TranceLove committed Oct 23, 2023
1 parent 6783e4d commit ac01274
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()));
Expand All @@ -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)
Expand Down Expand Up @@ -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());
Expand All @@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -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<TextEditorActivity> textEditorActivityWR = new WeakReference<>(this);

Timer newTimer = new Timer();
newTimer.schedule(
new TimerTask() {
boolean modified;
if (mainTextView.getText() != null) {
if (charSequence.hashCode() == mainTextView.getText().hashCode()) {

Check warning on line 384 in app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/ui/activities/texteditor/TextEditorActivity.java#L384

These nested if statements could be combined
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<TextEditorActivity> 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<TextEditorActivity> textEditorActivityWR = new WeakReference<>(this);

final OnProgressUpdate<SearchResultIndex> onProgressUpdate =
Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
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;
import android.content.SharedPreferences;

import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.preference.PreferenceManager;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}

0 comments on commit ac01274

Please sign in to comment.