Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore fixes in TextEditorActivity and FtpNotification #3964

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +380,8 @@ 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()) {
if (mainTextView.getText() != null
&& charSequence.hashCode() == mainTextView.getText().hashCode()) {
final TextEditorActivityViewModel viewModel =
new ViewModelProvider(this).get(TextEditorActivityViewModel.class);
final Timer oldTimer = viewModel.getTimer();
Expand Down Expand Up @@ -400,11 +410,12 @@ public void run() {
new ViewModelProvider(textEditorActivity).get(TextEditorActivityViewModel.class);

modified =
!textEditorActivity
.mainTextView
.getText()
.toString()
.equals(viewModel.getOriginal());
textEditorActivity.mainTextView.getText() != null
&& !textEditorActivity
.mainTextView
.getText()
.toString()
.equals(viewModel.getOriginal());
if (viewModel.getModified() != modified) {
viewModel.setModified(modified);
invalidateOptionsMenu();
Expand All @@ -420,7 +431,8 @@ public void run() {
@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 +472,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 +608,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 +630,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 +648,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();
}
}