Skip to content

Commit

Permalink
Code cleaning and add more NullPointers check
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuzakiKK committed Aug 11, 2018
1 parent fd3551a commit 78f7ad1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.Objects;


/**
* Display the custom choose password dialog.
Expand All @@ -42,9 +44,9 @@ public class ChoosePasswordDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final View view = getActivity().getLayoutInflater().inflate(R.layout.new_password, (ViewGroup) getView());
fieldEditText = (EditText) view.findViewById(R.id.field_password);
confFieldEditText = (EditText) view.findViewById(R.id.conf_field_password);
spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
fieldEditText = view.findViewById(R.id.field_password);
confFieldEditText = view.findViewById(R.id.conf_field_password);
spinner = view.findViewById(R.id.progressBar1);
path = getArguments().getString("path");
final String caller = getArguments().getString("caller");
if (caller != null && caller.equals("SelectDatabaseActivity")) {
Expand Down Expand Up @@ -77,14 +79,23 @@ public void onClick(final DialogInterface dialog, final int whichButton) {
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int whichButton) {
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(getArguments().getString("Context"));
im.hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
try {
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Objects.requireNonNull(getArguments().getString("Context")));
Objects.requireNonNull(im).hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
} catch (NullPointerException npe) {
// Something bad happened but preventing the app from crashing
// should be fine. Maybe we should log this event.
}
}
}
)
.create();

passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
try {
Objects.requireNonNull(passwordDialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} catch (NullPointerException npe) {
// Something bad happened but preventing the app from crashing
// should be fine. Maybe we should log this event.
}
return passwordDialog;
}

Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/notecrypt/ui/EditNoteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ protected void onCreate(final Bundle savedInstanceState) {
final String title = intent.getStringExtra(IDatabaseForNotes.TITLE);
final String tags = intent.getStringExtra(IDatabaseForNotes.TAGS);
final String note = App.getNote();
final TextView textViewTitle = (TextView) findViewById(R.id.editTextTitle);
final MultiAutoCompleteTextView macTextViewTags = (MultiAutoCompleteTextView) findViewById(R.id.editTextTags);
final TextView textViewNote = (TextView) findViewById(R.id.editTextNote);
final TextView textViewTitle = findViewById(R.id.editTextTitle);
final MultiAutoCompleteTextView macTextViewTags = findViewById(R.id.editTextTags);
final TextView textViewNote = findViewById(R.id.editTextNote);
textViewTitle.setText(title);
macTextViewTags.setText(tags);
textViewNote.setText(note);
Expand Down Expand Up @@ -110,7 +110,8 @@ public void run() {
}
if (App.getTimesInBackground() >= MainActivity.MAX_TIMES_BACKGROUND) {
EditNoteActivity.this.finish();
} else if (!isEditNoteActivityForeground) { //Check in loop until the user change app or return to this activity
} else if (!isEditNoteActivityForeground) {
//Check in loop until the user change app or return to this activity
mHandler.postDelayed(r, MainActivity.TIMEOUT_SPLITTED);
}
}
Expand All @@ -137,7 +138,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
}

/*
* Create an AlertDialog if the user press the back button.
* Create an AlertDialog if the user presses the back button.
*/
@Override
public void onBackPressed() {
Expand All @@ -159,9 +160,9 @@ public void onClick(final DialogInterface dialog, final int id) {
* Save in the extra the fields filled by the user and set the result to RESULT_OK.
*/
private void saveNote() {
final EditText fieldTitle = (EditText) findViewById(R.id.editTextTitle);
final EditText fieldTags = (EditText) findViewById(R.id.editTextTags);
final EditText fieldNote = (EditText) findViewById(R.id.editTextNote);
final EditText fieldTitle = findViewById(R.id.editTextTitle);
final EditText fieldTags = findViewById(R.id.editTextTags);
final EditText fieldNote = findViewById(R.id.editTextNote);
final Intent intent = getIntent();
intent.putExtra(IDatabaseForNotes.TITLE, fieldTitle.getText().toString());
intent.putExtra(IDatabaseForNotes.TAGS, fieldTags.getText().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.widget.EditText;
import android.widget.Toast;

import java.util.Objects;


/**
* Display the custom insert password dialog.
Expand All @@ -30,7 +32,7 @@ public class InsertPasswordDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final View view = getActivity().getLayoutInflater().inflate(R.layout.insert_password, (ViewGroup) getView());
fieldEditText = (EditText) view.findViewById(R.id.field_password);
fieldEditText = view.findViewById(R.id.field_password);
path = getArguments().getString("path");
fieldEditText.requestFocus();

Expand All @@ -55,13 +57,23 @@ public void onClick(final DialogInterface dialog, final int whichButton) {
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int whichButton) {
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(getArguments().getString("Context"));
im.hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
try {
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Objects.requireNonNull(getArguments().getString("Context")));
Objects.requireNonNull(im).hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
} catch (NullPointerException npe) {
// Something bad happened but preventing the app from crashing
// should be fine. Maybe we should log this event.
}
}
}
)
.create();
passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
try {
Objects.requireNonNull(passwordDialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} catch (NullPointerException npe) {
// Something bad happened but preventing the app from crashing
// should be fine. Maybe we should log this event.
}
return passwordDialog;
}
}
18 changes: 8 additions & 10 deletions app/src/main/java/com/notecrypt/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import com.notecrypt.utils.StringMethods;
import com.notecryptpro.R;

import android.annotation.TargetApi;
import android.graphics.Outline;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
Expand All @@ -31,7 +28,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
Expand Down Expand Up @@ -89,14 +85,14 @@ protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast0 = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_LONG);
spinner = (ProgressBar) findViewById(R.id.progressBar2);
spinner = findViewById(R.id.progressBar2);
spinner.setVisibility(View.VISIBLE);
final Intent intent = getIntent();
path = intent.getStringExtra("path");
setTitle(StringMethods.getInstance().getNameDB(path));
key = intent.getStringExtra("key");
db = App.getDatabase();
listview = (ListView) findViewById(android.R.id.list);
listview = findViewById(android.R.id.list);
final String[] from = {IDatabaseForNotes.TITLE, IDatabaseForNotes.STAR};
final int[] to = {R.id.txt, R.id.star};
db.initializeLists();
Expand Down Expand Up @@ -136,8 +132,8 @@ public void onClick(final DialogInterface dialog, final int id) {
return true;
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerList = findViewById(R.id.left_drawer);
arrayTagList = new ArrayList<>();
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, arrayTagList));
((BaseAdapter) mDrawerList.getAdapter()).notifyDataSetChanged();
Expand Down Expand Up @@ -223,7 +219,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSearchableInfo(searchManager != null ? searchManager.getSearchableInfo(getComponentName()) : null);
searchView.setIconifiedByDefault(false);
searchView.requestFocusFromTouch();
return true;
Expand Down Expand Up @@ -256,7 +252,9 @@ public boolean onOptionsItemSelected(final MenuItem item) {
return true;
case R.id.action_search:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(listview.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInputFromWindow(listview.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
default:
return super.onOptionsItemSelected(item);
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/notecrypt/ui/ReadNoteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_note);
//enable the ability to press the title as back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
final Intent intent = getIntent();
listViewLocation = intent.getIntExtra("listViewLocation", -1);
location = Long.parseLong(intent.getStringExtra("location"));
Expand All @@ -50,10 +52,10 @@ protected void onCreate(final Bundle savedInstanceState) {
isStarred = db.getNotes().get(location).isStarred();
//If the text of the note is empty a message will be displayed
if (textNote == null || textNote.equals("")) {
final TextView textViewNoNote = (TextView) findViewById(R.id.textViewNoNote);
final TextView textViewNoNote = findViewById(R.id.textViewNoNote);
textViewNoNote.setText(getString(R.string.no_textNote));
} else {
final TextView textViewNote = (TextView) findViewById(R.id.textViewNote);
final TextView textViewNote = findViewById(R.id.textViewNote);
textViewNote.setText(db.getNotes().get(location).getNote());
}
mHandler = new Handler();
Expand Down Expand Up @@ -212,7 +214,7 @@ protected void onResume() {

private void setSize() {
int prefDrawer = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("pref_textSize", "20"));
final TextView textViewNoNote = (TextView) findViewById(R.id.textViewNote);
final TextView textViewNoNote = findViewById(R.id.textViewNote);
textViewNoNote.setTextSize(TypedValue.COMPLEX_UNIT_SP, prefDrawer);
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/notecrypt/ui/SearchResultsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//enable the ability to press the title as back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
final Intent intent = getIntent();
final String query = intent.getStringExtra(SearchManager.QUERY);
setTitle("\"" + query + "\"");
db = App.getDatabase();
list = (List<Map<String, String>>) intent.getSerializableExtra("list");
final ListView listview = (ListView) findViewById(android.R.id.list);
final ListView listview = findViewById(android.R.id.list);
final String[] from = {IDatabaseForNotes.TITLE, IDatabaseForNotes.STAR};
final int[] to = {R.id.txt, R.id.star};
findViewById(R.id.empty).setVisibility(View.INVISIBLE);
Expand All @@ -72,7 +74,7 @@ public void onItemClick(final AdapterView<?> parent, final View view,
startActivityForResult(intent, DatabaseForNotesAsync.REQUEST_EDIT_NOTE);
}
});
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final DrawerLayout mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mHandler = new Handler();
r = new Runnable() {
Expand Down
51 changes: 30 additions & 21 deletions app/src/main/java/com/notecrypt/ui/SelectDatabaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onCreate(final Bundle savedInstanceState) {
if (getResources().getBoolean(R.bool.portrait_only)) { //Portrait only on phone (also landscape on tablet)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
listView = (ListView) findViewById(android.R.id.list);
listView = findViewById(android.R.id.list);
setRecent = new HashSet<>();
setRecent = getPreferences(MODE_PRIVATE).getStringSet("recent", null);
if (setRecent != null) {
Expand Down Expand Up @@ -104,9 +104,9 @@ public void onClick(DialogInterface dialog, int id) {
return true;
}
});
editText = (EditText) findViewById(R.id.editTextPath);
editText = findViewById(R.id.editTextPath);
editText.setText(getPreferences(MODE_PRIVATE).getString("path", DEFAULT_PATH));
final ImageButton buttonStorage = (ImageButton) findViewById(R.id.buttonStorage);
final ImageButton buttonStorage = findViewById(R.id.buttonStorage);
buttonStorage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Expand All @@ -119,7 +119,7 @@ public void onClick(final View v) {
}
});

final Button buttonOpen = (Button) findViewById(R.id.buttonOpen);
final Button buttonOpen = findViewById(R.id.buttonOpen);
buttonOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Expand All @@ -141,7 +141,7 @@ public void onClick(final View v) {
}
});

final Button buttonCreate = (Button) findViewById(R.id.buttonCreate);
final Button buttonCreate = findViewById(R.id.buttonCreate);
buttonCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Expand Down Expand Up @@ -272,9 +272,11 @@ public boolean onOptionsItemSelected(final MenuItem item) {
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
if (resultCode == RESULT_OK) {
final Uri uri = intent.getData();
final String path = convertMediaUriToPath(uri);
final EditText editText = (EditText) findViewById(R.id.editTextPath);
editText.setText(path);
if (uri != null) {
final String path = convertMediaUriToPath(uri);
final EditText editText = findViewById(R.id.editTextPath);
editText.setText(path);
}
}
}

Expand All @@ -285,20 +287,27 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
* @return path of the file
*/
private String convertMediaUriToPath(final Uri uri) {
if (uri.toString().substring(0, 4).equals("file")) {
//remove "file://" from the uri
return uri.toString().substring(7);
} else if (uri.toString().substring(0, 7).equals("content")) {
final String[] project = {MediaStore.Files.FileColumns.DATA};
final Cursor cursor = getContentResolver().query(uri, project, null, null, null);
final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
final String path = cursor.getString(columnIndex);
cursor.close();
return path;
final String sUri = uri.toString();
final int minLenght = "file://".length();
String path = "";

//uri should always begin with "file" or "content"
if (sUri.length() > minLenght) {
if (sUri.substring(0, 4).equals("file")) {
//remove "file://" from the uri
path = uri.toString().substring(7);
} else if (uri.toString().substring(0, 7).equals("content")) {
final String[] project = {MediaStore.Files.FileColumns.DATA};
final Cursor cursor = getContentResolver().query(uri, project, null, null, null);
if (cursor != null) {
final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(columnIndex);
cursor.close();
}
}
}
//uri will always begin with file or content, but with this is handled even if uri have something weird
return "";
return path;
}

private void updateRecentList(String item, boolean isDelete) {
Expand Down
Loading

0 comments on commit 78f7ad1

Please sign in to comment.