Skip to content

Commit 35f05be

Browse files
fix memory leaks (they happened due to hiding keyboard) and lint null pointer warnings in the same code (commons-app#1471)
1 parent 6d2c41b commit 35f05be

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void onCreate(Bundle savedInstanceState) {
128128

129129
forgotPasswordText.setOnClickListener(view -> forgotPassword());
130130

131-
if(BuildConfig.FLAVOR == "beta"){
131+
if(BuildConfig.FLAVOR.equals("beta")){
132132
loginCredentials.setText(getString(R.string.login_credential));
133133
} else {
134134
loginCredentials.setVisibility(View.GONE);
@@ -145,8 +145,12 @@ void onPrivacyPolicyClicked() {
145145
}
146146

147147
public void hideKeyboard(View view) {
148-
InputMethodManager inputMethodManager =(InputMethodManager)this.getSystemService(Activity.INPUT_METHOD_SERVICE);
149-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
148+
if (view != null) {
149+
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
150+
if (inputMethodManager != null) {
151+
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
152+
}
153+
}
150154
}
151155

152156

app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.text.Editable;
1111
import android.text.TextUtils;
1212
import android.text.TextWatcher;
13-
import android.util.Log;
1413
import android.view.LayoutInflater;
1514
import android.view.Menu;
1615
import android.view.MenuInflater;
@@ -43,7 +42,6 @@
4342
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
4443
import fr.free.nrw.commons.mwapi.MediaWikiApi;
4544
import fr.free.nrw.commons.upload.MwVolleyApi;
46-
import fr.free.nrw.commons.upload.SingleUploadFragment;
4745
import fr.free.nrw.commons.utils.StringSortingUtils;
4846
import io.reactivex.Observable;
4947
import io.reactivex.android.schedulers.AndroidSchedulers;
@@ -131,8 +129,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
131129
}
132130

133131
public void hideKeyboard(View view) {
134-
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
135-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
132+
133+
if (view != null) {
134+
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
135+
if (inputMethodManager != null) {
136+
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
137+
}
138+
}
136139
}
137140

138141
@Override

app/src/main/java/fr/free/nrw/commons/upload/MultipleShareActivity.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ private void multipleUploadBegins() {
166166
View target = getCurrentFocus();
167167
if (target != null) {
168168
InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
169-
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
169+
if (imm != null)
170+
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
170171
}
171172
getSupportFragmentManager().beginTransaction()
172173
.add(R.id.uploadsFragmentContainer, categorizationFragment, "categorization")

app/src/main/java/fr/free/nrw/commons/upload/MultipleUploadListFragment.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public View getView(int i, View view, ViewGroup viewGroup) {
8888
if (view == null) {
8989
view = LayoutInflater.from(getContext()).inflate(R.layout.layout_upload_item, viewGroup, false);
9090
holder = new UploadHolderView();
91-
holder.image = (SimpleDraweeView) view.findViewById(R.id.uploadImage);
92-
holder.title = (TextView) view.findViewById(R.id.uploadTitle);
93-
holder.overlay = (RelativeLayout) view.findViewById(R.id.uploadOverlay);
91+
holder.image = view.findViewById(R.id.uploadImage);
92+
holder.title = view.findViewById(R.id.uploadTitle);
93+
holder.overlay = view.findViewById(R.id.uploadOverlay);
9494

9595
holder.image.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, photoSize.y));
9696
holder.image.setHierarchy(GenericDraweeHierarchyBuilder
@@ -128,11 +128,8 @@ public void onStop() {
128128
super.onStop();
129129

130130
// FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
131-
View target = getView().findFocus();
132-
if (target != null) {
133-
InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
134-
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
135-
}
131+
View target = getActivity().getCurrentFocus();
132+
hideKeyboard(target);
136133
}
137134

138135
// FIXME: Wrong result type
@@ -168,8 +165,8 @@ public void setImageOnlyMode(boolean mode) {
168165
@Override
169166
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
170167
View view = inflater.inflate(R.layout.fragment_multiple_uploads_list, container, false);
171-
photosGrid = (GridView) view.findViewById(R.id.multipleShareBackground);
172-
baseTitle = (EditText) view.findViewById(R.id.multipleBaseTitle);
168+
photosGrid = view.findViewById(R.id.multipleShareBackground);
169+
baseTitle = view.findViewById(R.id.multipleBaseTitle);
173170

174171
photosAdapter = new PhotoDisplayAdapter();
175172
photosGrid.setAdapter(photosAdapter);
@@ -189,8 +186,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
189186
}
190187

191188
public void hideKeyboard(View view) {
192-
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
193-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
189+
if (view != null) {
190+
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
191+
if (inputMethodManager != null) {
192+
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
193+
}
194+
}
194195
}
195196

196197
@Override

app/src/main/java/fr/free/nrw/commons/upload/SingleUploadFragment.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
import android.annotation.SuppressLint;
44
import android.app.Activity;
55

6-
import android.content.Context;
76
import android.content.Intent;
87
import android.content.SharedPreferences;
98
import android.graphics.Color;
10-
import android.net.Uri;
119
import android.os.Bundle;
1210
import android.preference.PreferenceManager;
1311
import android.support.annotation.NonNull;
14-
import android.support.design.widget.FloatingActionButton;
1512
import android.support.v4.view.ViewCompat;
1613
import android.support.v7.app.AlertDialog;
14+
1715
import android.text.Editable;
1816
import android.text.Html;
1917
import android.text.TextWatcher;
2018
import android.text.method.LinkMovementMethod;
21-
import android.util.Log;
19+
2220
import android.view.LayoutInflater;
2321
import android.view.Menu;
2422
import android.view.MenuInflater;
@@ -186,9 +184,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
186184
}
187185

188186
public void hideKeyboard(View view) {
189-
Log.i("hide", "hideKeyboard: ");
190-
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
191-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
187+
if (view != null) {
188+
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
189+
if (inputMethodManager != null) {
190+
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
191+
}
192+
}
192193
}
193194

194195
@Override
@@ -303,11 +304,8 @@ public void onStop() {
303304
super.onStop();
304305

305306
// FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
306-
View target = getView().findFocus();
307-
if (target != null) {
308-
InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
309-
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
310-
}
307+
View target = getActivity().getCurrentFocus();
308+
hideKeyboard(target);
311309
}
312310

313311
@NonNull

0 commit comments

Comments
 (0)