Skip to content

Commit 78f7ad1

Browse files
committed
Code cleaning and add more NullPointers check
1 parent fd3551a commit 78f7ad1

8 files changed

+100
-58
lines changed

app/src/main/java/com/notecrypt/ui/ChoosePasswordDialogFragment.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import android.widget.ProgressBar;
1919
import android.widget.Toast;
2020

21+
import java.util.Objects;
22+
2123

2224
/**
2325
* Display the custom choose password dialog.
@@ -42,9 +44,9 @@ public class ChoosePasswordDialogFragment extends DialogFragment {
4244
@Override
4345
public Dialog onCreateDialog(final Bundle savedInstanceState) {
4446
final View view = getActivity().getLayoutInflater().inflate(R.layout.new_password, (ViewGroup) getView());
45-
fieldEditText = (EditText) view.findViewById(R.id.field_password);
46-
confFieldEditText = (EditText) view.findViewById(R.id.conf_field_password);
47-
spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
47+
fieldEditText = view.findViewById(R.id.field_password);
48+
confFieldEditText = view.findViewById(R.id.conf_field_password);
49+
spinner = view.findViewById(R.id.progressBar1);
4850
path = getArguments().getString("path");
4951
final String caller = getArguments().getString("caller");
5052
if (caller != null && caller.equals("SelectDatabaseActivity")) {
@@ -77,14 +79,23 @@ public void onClick(final DialogInterface dialog, final int whichButton) {
7779
.setNegativeButton(android.R.string.cancel,
7880
new DialogInterface.OnClickListener() {
7981
public void onClick(final DialogInterface dialog, final int whichButton) {
80-
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(getArguments().getString("Context"));
81-
im.hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
82+
try {
83+
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Objects.requireNonNull(getArguments().getString("Context")));
84+
Objects.requireNonNull(im).hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
85+
} catch (NullPointerException npe) {
86+
// Something bad happened but preventing the app from crashing
87+
// should be fine. Maybe we should log this event.
88+
}
8289
}
8390
}
8491
)
8592
.create();
86-
87-
passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
93+
try {
94+
Objects.requireNonNull(passwordDialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
95+
} catch (NullPointerException npe) {
96+
// Something bad happened but preventing the app from crashing
97+
// should be fine. Maybe we should log this event.
98+
}
8899
return passwordDialog;
89100
}
90101

app/src/main/java/com/notecrypt/ui/EditNoteActivity.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ protected void onCreate(final Bundle savedInstanceState) {
4040
final String title = intent.getStringExtra(IDatabaseForNotes.TITLE);
4141
final String tags = intent.getStringExtra(IDatabaseForNotes.TAGS);
4242
final String note = App.getNote();
43-
final TextView textViewTitle = (TextView) findViewById(R.id.editTextTitle);
44-
final MultiAutoCompleteTextView macTextViewTags = (MultiAutoCompleteTextView) findViewById(R.id.editTextTags);
45-
final TextView textViewNote = (TextView) findViewById(R.id.editTextNote);
43+
final TextView textViewTitle = findViewById(R.id.editTextTitle);
44+
final MultiAutoCompleteTextView macTextViewTags = findViewById(R.id.editTextTags);
45+
final TextView textViewNote = findViewById(R.id.editTextNote);
4646
textViewTitle.setText(title);
4747
macTextViewTags.setText(tags);
4848
textViewNote.setText(note);
@@ -110,7 +110,8 @@ public void run() {
110110
}
111111
if (App.getTimesInBackground() >= MainActivity.MAX_TIMES_BACKGROUND) {
112112
EditNoteActivity.this.finish();
113-
} else if (!isEditNoteActivityForeground) { //Check in loop until the user change app or return to this activity
113+
} else if (!isEditNoteActivityForeground) {
114+
//Check in loop until the user change app or return to this activity
114115
mHandler.postDelayed(r, MainActivity.TIMEOUT_SPLITTED);
115116
}
116117
}
@@ -137,7 +138,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
137138
}
138139

139140
/*
140-
* Create an AlertDialog if the user press the back button.
141+
* Create an AlertDialog if the user presses the back button.
141142
*/
142143
@Override
143144
public void onBackPressed() {
@@ -159,9 +160,9 @@ public void onClick(final DialogInterface dialog, final int id) {
159160
* Save in the extra the fields filled by the user and set the result to RESULT_OK.
160161
*/
161162
private void saveNote() {
162-
final EditText fieldTitle = (EditText) findViewById(R.id.editTextTitle);
163-
final EditText fieldTags = (EditText) findViewById(R.id.editTextTags);
164-
final EditText fieldNote = (EditText) findViewById(R.id.editTextNote);
163+
final EditText fieldTitle = findViewById(R.id.editTextTitle);
164+
final EditText fieldTags = findViewById(R.id.editTextTags);
165+
final EditText fieldNote = findViewById(R.id.editTextNote);
165166
final Intent intent = getIntent();
166167
intent.putExtra(IDatabaseForNotes.TITLE, fieldTitle.getText().toString());
167168
intent.putExtra(IDatabaseForNotes.TAGS, fieldTags.getText().toString());

app/src/main/java/com/notecrypt/ui/InsertPasswordDialogFragment.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import android.widget.EditText;
1717
import android.widget.Toast;
1818

19+
import java.util.Objects;
20+
1921

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

@@ -55,13 +57,23 @@ public void onClick(final DialogInterface dialog, final int whichButton) {
5557
.setNegativeButton(android.R.string.cancel,
5658
new DialogInterface.OnClickListener() {
5759
public void onClick(final DialogInterface dialog, final int whichButton) {
58-
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(getArguments().getString("Context"));
59-
im.hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
60+
try {
61+
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Objects.requireNonNull(getArguments().getString("Context")));
62+
Objects.requireNonNull(im).hideSoftInputFromWindow(fieldEditText.getWindowToken(), 0);
63+
} catch (NullPointerException npe) {
64+
// Something bad happened but preventing the app from crashing
65+
// should be fine. Maybe we should log this event.
66+
}
6067
}
6168
}
6269
)
6370
.create();
64-
passwordDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
71+
try {
72+
Objects.requireNonNull(passwordDialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
73+
} catch (NullPointerException npe) {
74+
// Something bad happened but preventing the app from crashing
75+
// should be fine. Maybe we should log this event.
76+
}
6577
return passwordDialog;
6678
}
6779
}

app/src/main/java/com/notecrypt/ui/MainActivity.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import com.notecrypt.utils.StringMethods;
1414
import com.notecryptpro.R;
1515

16-
import android.annotation.TargetApi;
17-
import android.graphics.Outline;
18-
import android.os.Build;
1916
import android.os.Bundle;
2017
import android.os.Handler;
2118
import android.preference.PreferenceManager;
@@ -31,7 +28,6 @@
3128
import android.view.Menu;
3229
import android.view.MenuItem;
3330
import android.view.View;
34-
import android.view.ViewOutlineProvider;
3531
import android.view.inputmethod.InputMethodManager;
3632
import android.widget.AdapterView;
3733
import android.widget.AdapterView.OnItemClickListener;
@@ -89,14 +85,14 @@ protected void onCreate(final Bundle savedInstanceState) {
8985
super.onCreate(savedInstanceState);
9086
setContentView(R.layout.activity_main);
9187
toast0 = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_LONG);
92-
spinner = (ProgressBar) findViewById(R.id.progressBar2);
88+
spinner = findViewById(R.id.progressBar2);
9389
spinner.setVisibility(View.VISIBLE);
9490
final Intent intent = getIntent();
9591
path = intent.getStringExtra("path");
9692
setTitle(StringMethods.getInstance().getNameDB(path));
9793
key = intent.getStringExtra("key");
9894
db = App.getDatabase();
99-
listview = (ListView) findViewById(android.R.id.list);
95+
listview = findViewById(android.R.id.list);
10096
final String[] from = {IDatabaseForNotes.TITLE, IDatabaseForNotes.STAR};
10197
final int[] to = {R.id.txt, R.id.star};
10298
db.initializeLists();
@@ -136,8 +132,8 @@ public void onClick(final DialogInterface dialog, final int id) {
136132
return true;
137133
}
138134
});
139-
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
140-
mDrawerList = (ListView) findViewById(R.id.left_drawer);
135+
mDrawerLayout = findViewById(R.id.drawer_layout);
136+
mDrawerList = findViewById(R.id.left_drawer);
141137
arrayTagList = new ArrayList<>();
142138
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, arrayTagList));
143139
((BaseAdapter) mDrawerList.getAdapter()).notifyDataSetChanged();
@@ -223,7 +219,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
223219
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
224220
searchMenuItem = menu.findItem(R.id.action_search);
225221
SearchView searchView = (SearchView) searchMenuItem.getActionView();
226-
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
222+
searchView.setSearchableInfo(searchManager != null ? searchManager.getSearchableInfo(getComponentName()) : null);
227223
searchView.setIconifiedByDefault(false);
228224
searchView.requestFocusFromTouch();
229225
return true;
@@ -256,7 +252,9 @@ public boolean onOptionsItemSelected(final MenuItem item) {
256252
return true;
257253
case R.id.action_search:
258254
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
259-
inputMethodManager.toggleSoftInputFromWindow(listview.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
255+
if (inputMethodManager != null) {
256+
inputMethodManager.toggleSoftInputFromWindow(listview.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
257+
}
260258
default:
261259
return super.onOptionsItemSelected(item);
262260
}

app/src/main/java/com/notecrypt/ui/ReadNoteActivity.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ protected void onCreate(final Bundle savedInstanceState) {
3939
super.onCreate(savedInstanceState);
4040
setContentView(R.layout.activity_read_note);
4141
//enable the ability to press the title as back button
42-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
42+
if (getSupportActionBar() != null) {
43+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
44+
}
4345
final Intent intent = getIntent();
4446
listViewLocation = intent.getIntExtra("listViewLocation", -1);
4547
location = Long.parseLong(intent.getStringExtra("location"));
@@ -50,10 +52,10 @@ protected void onCreate(final Bundle savedInstanceState) {
5052
isStarred = db.getNotes().get(location).isStarred();
5153
//If the text of the note is empty a message will be displayed
5254
if (textNote == null || textNote.equals("")) {
53-
final TextView textViewNoNote = (TextView) findViewById(R.id.textViewNoNote);
55+
final TextView textViewNoNote = findViewById(R.id.textViewNoNote);
5456
textViewNoNote.setText(getString(R.string.no_textNote));
5557
} else {
56-
final TextView textViewNote = (TextView) findViewById(R.id.textViewNote);
58+
final TextView textViewNote = findViewById(R.id.textViewNote);
5759
textViewNote.setText(db.getNotes().get(location).getNote());
5860
}
5961
mHandler = new Handler();
@@ -212,7 +214,7 @@ protected void onResume() {
212214

213215
private void setSize() {
214216
int prefDrawer = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("pref_textSize", "20"));
215-
final TextView textViewNoNote = (TextView) findViewById(R.id.textViewNote);
217+
final TextView textViewNoNote = findViewById(R.id.textViewNote);
216218
textViewNoNote.setTextSize(TypedValue.COMPLEX_UNIT_SP, prefDrawer);
217219
}
218220

app/src/main/java/com/notecrypt/ui/SearchResultsActivity.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ protected void onCreate(final Bundle savedInstanceState) {
4646
super.onCreate(savedInstanceState);
4747
setContentView(R.layout.activity_main);
4848
//enable the ability to press the title as back button
49-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
49+
if (getSupportActionBar() != null) {
50+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
51+
}
5052
final Intent intent = getIntent();
5153
final String query = intent.getStringExtra(SearchManager.QUERY);
5254
setTitle("\"" + query + "\"");
5355
db = App.getDatabase();
5456
list = (List<Map<String, String>>) intent.getSerializableExtra("list");
55-
final ListView listview = (ListView) findViewById(android.R.id.list);
57+
final ListView listview = findViewById(android.R.id.list);
5658
final String[] from = {IDatabaseForNotes.TITLE, IDatabaseForNotes.STAR};
5759
final int[] to = {R.id.txt, R.id.star};
5860
findViewById(R.id.empty).setVisibility(View.INVISIBLE);
@@ -72,7 +74,7 @@ public void onItemClick(final AdapterView<?> parent, final View view,
7274
startActivityForResult(intent, DatabaseForNotesAsync.REQUEST_EDIT_NOTE);
7375
}
7476
});
75-
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
77+
final DrawerLayout mDrawerLayout = findViewById(R.id.drawer_layout);
7678
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
7779
mHandler = new Handler();
7880
r = new Runnable() {

app/src/main/java/com/notecrypt/ui/SelectDatabaseActivity.java

+30-21
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected void onCreate(final Bundle savedInstanceState) {
6464
if (getResources().getBoolean(R.bool.portrait_only)) { //Portrait only on phone (also landscape on tablet)
6565
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
6666
}
67-
listView = (ListView) findViewById(android.R.id.list);
67+
listView = findViewById(android.R.id.list);
6868
setRecent = new HashSet<>();
6969
setRecent = getPreferences(MODE_PRIVATE).getStringSet("recent", null);
7070
if (setRecent != null) {
@@ -104,9 +104,9 @@ public void onClick(DialogInterface dialog, int id) {
104104
return true;
105105
}
106106
});
107-
editText = (EditText) findViewById(R.id.editTextPath);
107+
editText = findViewById(R.id.editTextPath);
108108
editText.setText(getPreferences(MODE_PRIVATE).getString("path", DEFAULT_PATH));
109-
final ImageButton buttonStorage = (ImageButton) findViewById(R.id.buttonStorage);
109+
final ImageButton buttonStorage = findViewById(R.id.buttonStorage);
110110
buttonStorage.setOnClickListener(new View.OnClickListener() {
111111
@Override
112112
public void onClick(final View v) {
@@ -119,7 +119,7 @@ public void onClick(final View v) {
119119
}
120120
});
121121

122-
final Button buttonOpen = (Button) findViewById(R.id.buttonOpen);
122+
final Button buttonOpen = findViewById(R.id.buttonOpen);
123123
buttonOpen.setOnClickListener(new View.OnClickListener() {
124124
@Override
125125
public void onClick(final View v) {
@@ -141,7 +141,7 @@ public void onClick(final View v) {
141141
}
142142
});
143143

144-
final Button buttonCreate = (Button) findViewById(R.id.buttonCreate);
144+
final Button buttonCreate = findViewById(R.id.buttonCreate);
145145
buttonCreate.setOnClickListener(new View.OnClickListener() {
146146
@Override
147147
public void onClick(final View v) {
@@ -272,9 +272,11 @@ public boolean onOptionsItemSelected(final MenuItem item) {
272272
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
273273
if (resultCode == RESULT_OK) {
274274
final Uri uri = intent.getData();
275-
final String path = convertMediaUriToPath(uri);
276-
final EditText editText = (EditText) findViewById(R.id.editTextPath);
277-
editText.setText(path);
275+
if (uri != null) {
276+
final String path = convertMediaUriToPath(uri);
277+
final EditText editText = findViewById(R.id.editTextPath);
278+
editText.setText(path);
279+
}
278280
}
279281
}
280282

@@ -285,20 +287,27 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
285287
* @return path of the file
286288
*/
287289
private String convertMediaUriToPath(final Uri uri) {
288-
if (uri.toString().substring(0, 4).equals("file")) {
289-
//remove "file://" from the uri
290-
return uri.toString().substring(7);
291-
} else if (uri.toString().substring(0, 7).equals("content")) {
292-
final String[] project = {MediaStore.Files.FileColumns.DATA};
293-
final Cursor cursor = getContentResolver().query(uri, project, null, null, null);
294-
final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
295-
cursor.moveToFirst();
296-
final String path = cursor.getString(columnIndex);
297-
cursor.close();
298-
return path;
290+
final String sUri = uri.toString();
291+
final int minLenght = "file://".length();
292+
String path = "";
293+
294+
//uri should always begin with "file" or "content"
295+
if (sUri.length() > minLenght) {
296+
if (sUri.substring(0, 4).equals("file")) {
297+
//remove "file://" from the uri
298+
path = uri.toString().substring(7);
299+
} else if (uri.toString().substring(0, 7).equals("content")) {
300+
final String[] project = {MediaStore.Files.FileColumns.DATA};
301+
final Cursor cursor = getContentResolver().query(uri, project, null, null, null);
302+
if (cursor != null) {
303+
final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
304+
cursor.moveToFirst();
305+
path = cursor.getString(columnIndex);
306+
cursor.close();
307+
}
308+
}
299309
}
300-
//uri will always begin with file or content, but with this is handled even if uri have something weird
301-
return "";
310+
return path;
302311
}
303312

304313
private void updateRecentList(String item, boolean isDelete) {

0 commit comments

Comments
 (0)