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

The fix of issue #310 #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
The crash has been fixed. Now the code doesn't refer to the null sear…
…chview.
  • Loading branch information
ArionTheWanderer committed Jun 8, 2023
commit b36e5451a96e81f8581fcce2113e5bfea3af55cf
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.cursoradapter.widget.CursorAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand Down Expand Up @@ -84,13 +88,19 @@ public class HistoryActivity extends BaseActivity implements
private static final int SEARCH_TYPE_NOTE = 2;
private static final int SEARCH_TYPE_TEXT_ALL = 3;
private static final int SEARCH_TYPE_DATE = 4;
private static final String SHOULD_TEXT_BE_PAINTED_IN_RED_KEY = "SHOULD_TEXT_BE_PAINTED_IN_RED";


private HistoryRecyclerViewAdapter historyAdapter;
private DetailRecyclerViewAdapter detailAdapters[];
private MenuItem searchMenuItem;
private SearchView searchView;

// The flag will be set to "true" to paint the text
// after the rotate, when the UI will be definitely initialized, because onCreateOptionsMenu()
// is invoked only after onResume() is invoked
private boolean shouldTextBePaintedInRed = false;

ContentProviderClient client = ActivityDiaryApplication.getAppContext().getContentResolver().acquireContentProviderClient(ActivityDiaryContract.AUTHORITY);
ActivityDiaryContentProvider provider = (ActivityDiaryContentProvider) client.getLocalContentProvider();

Expand Down Expand Up @@ -148,6 +158,7 @@ public void onBackPressed() {
public boolean onQueryTextChange(String newText) {
// no dynamic change before starting the search...
setDefaultColorSearchText();
shouldTextBePaintedInRed = false;
return true;
}

Expand All @@ -164,6 +175,10 @@ private QHandler() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
shouldTextBePaintedInRed = savedInstanceState.getBoolean(SHOULD_TEXT_BE_PAINTED_IN_RED_KEY);
}

detailAdapters = new DetailRecyclerViewAdapter[5];

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Expand Down Expand Up @@ -192,12 +207,66 @@ protected void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SHOULD_TEXT_BE_PAINTED_IN_RED_KEY, shouldTextBePaintedInRed);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
// If the intent has wrong type of date, flag will be set to "true" to paint the text
// after the rotate, when the UI will be definitely initialized, because onCreateOptionsMenu()
// is invoked only after onResume() is invoked
// Otherwise, set the variable to "false"
boolean isWrongDate = checkIfIntentHasWrongDate(intent);
if (isWrongDate) {
shouldTextBePaintedInRed = true;
setWrongColorSearchText();
} else {
shouldTextBePaintedInRed = false;
}
handleIntent(intent);
}

private boolean checkIfIntentHasWrongDate(Intent intent) {
String query;
String action = intent.getAction();
if (ActivityDiaryContentProvider.SEARCH_DATE.equals(action)) {
Uri data = intent.getData();
if (data != null) {
query = data.getPath();
query = query.replaceFirst("/", "");
return isDateWrong(query);
}
}
return false;
}

private boolean isDateWrong(String date) {
String[] formats = {
getResources().getString(R.string.date_format), //get default format from strings.xml
((SimpleDateFormat) android.text.format.DateFormat.getDateFormat(getApplicationContext())).toLocalizedPattern() //locale format
};

SimpleDateFormat simpleDateFormat;

for (String format : formats) {
simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setLenient(false);
try {
simpleDateFormat.parse(date).getTime();
return false;
} catch (ParseException e) {
/* intentionally no further handling. We try the next date format and onyl if we cannot parse the date with any
* supported format we return null afterwards. */
}
}
return true;
}

private void handleIntent(Intent intent) {
String query = null;
String action = intent.getAction();
Expand Down Expand Up @@ -227,7 +296,7 @@ private void handleIntent(Intent intent) {
if (data != null) {

query = data.getPath();
query = query.replaceFirst("/","");
query = query.replaceFirst("/", "");
filterHistoryDates(query);
}
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Expand All @@ -245,7 +314,7 @@ private void handleIntent(Intent intent) {

getContentResolver().delete(uri,
ActivityDiaryContract.DiarySearchSuggestion.SUGGESTION + " LIKE ? AND "
+ ActivityDiaryContract.DiarySearchSuggestion.ACTION + " LIKE ?",
+ ActivityDiaryContract.DiarySearchSuggestion.ACTION + " LIKE ?",
new String[]{query, intent.getAction()});

values.put(ActivityDiaryContract.DiarySearchSuggestion.SUGGESTION, query);
Expand All @@ -254,9 +323,9 @@ private void handleIntent(Intent intent) {

getContentResolver().delete(uri,
ActivityDiaryContract.DiarySearchSuggestion._ID +
" IN (SELECT " + ActivityDiaryContract.DiarySearchSuggestion._ID +
" FROM " + ActivityDiaryContract.DiarySearchSuggestion.TABLE_NAME +
" ORDER BY " + ActivityDiaryContract.DiarySearchSuggestion._ID + " DESC LIMIT " + SEARCH_SUGGESTION_DISPLAY_COUNT + ",1)",
" IN (SELECT " + ActivityDiaryContract.DiarySearchSuggestion._ID +
" FROM " + ActivityDiaryContract.DiarySearchSuggestion.TABLE_NAME +
" ORDER BY " + ActivityDiaryContract.DiarySearchSuggestion._ID + " DESC LIMIT " + SEARCH_SUGGESTION_DISPLAY_COUNT + ",1)",
null);
}
}
Expand Down Expand Up @@ -293,8 +362,12 @@ public boolean onSuggestionClick(int position) {
});

searchView.setImeOptions(searchView.getImeOptions() | EditorInfo.IME_ACTION_SEARCH);
//TODO to make it look nice
// searchView.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));

if (shouldTextBePaintedInRed) {
setWrongColorSearchText();
}
// TODO to make it look nice
// searchView.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));

return true;
}
Expand Down Expand Up @@ -397,7 +470,7 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
} else {
return new CursorLoader(HistoryActivity.this,
ActivityDiaryContract.DiaryImage.CONTENT_URI,
new String[] {ActivityDiaryContract.DiaryImage._ID,
new String[]{ActivityDiaryContract.DiaryImage._ID,
ActivityDiaryContract.DiaryImage.URI},
ActivityDiaryContract.DiaryImage.DIARY_ID + "=? AND "
+ ActivityDiaryContract.DiaryImage._DELETED + "=0",
Expand Down Expand Up @@ -477,12 +550,14 @@ public void addDetailAdapter(long diaryEntryId, DetailRecyclerViewAdapter adapte

}

/** Checks date format and also checks date can be parsed (used for not existing dates like 35.13.2000)
/**
* Checks date format and also checks date can be parsed (used for not existing dates like 35.13.2000)
* (in case format not exists or date is incorrect Toast about wrong format is displayed)
*
* @param date input that is checked
* @return millis of parsed input
*/
private Long checkDateFormatAndParse(String date){
private Long checkDateFormatAndParse(String date) {
// TODO: generalize data format for search
String[] formats = {
getResources().getString(R.string.date_format), //get default format from strings.xml
Expand All @@ -491,36 +566,35 @@ private Long checkDateFormatAndParse(String date){

SimpleDateFormat simpleDateFormat;

for (String format: formats){
for (String format : formats) {
simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setLenient(false);
try {
return simpleDateFormat.parse(date).getTime();
} catch (ParseException e){
} catch (ParseException e) {
/* intentionally no further handling. We try the next date format and onyl if we cannot parse the date with any
* supported format we return null afterwards. */
}
}

setWrongColorSearchText();
Toast.makeText(getApplication().getBaseContext(), getResources().getString(R.string.wrongFormat), Toast.LENGTH_LONG).show();
return null;
}

/**
* Sets searched text to default color (white) in case it is set to red
*/
private void setDefaultColorSearchText(){
TextView textView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
private void setDefaultColorSearchText() {
TextView textView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
if (textView.getCurrentTextColor() == getResources().getColor(R.color.colorWrongText))
textView.setTextColor(getResources().getColor(R.color.activityTextColorLight));
}

/**
* Sets searched text to color which indicates wrong searching (red)
*/
private void setWrongColorSearchText(){
TextView textView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
private void setWrongColorSearchText() {
TextView textView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
textView.setTextColor(getResources().getColor(R.color.colorWrongText));
}

Expand Down