Skip to content

Commit fade958

Browse files
authored
Merge pull request commons-app#683 from dbrant/context1
Don't pass/store Activity instance in key places.
2 parents 0112a09 + 5ab0605 commit fade958

17 files changed

+117
-88
lines changed

app/src/main/java/fr/free/nrw/commons/MediaWikiImageView.java

+15
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
import android.content.Context;
44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
6+
import android.support.graphics.drawable.VectorDrawableCompat;
67
import android.text.TextUtils;
78
import android.util.AttributeSet;
89

10+
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
911
import com.facebook.drawee.view.SimpleDraweeView;
1012

1113
public class MediaWikiImageView extends SimpleDraweeView {
1214
private ThumbnailFetchTask currentThumbnailTask;
1315

1416
public MediaWikiImageView(Context context) {
1517
this(context, null);
18+
init();
1619
}
1720

1821
public MediaWikiImageView(Context context, AttributeSet attrs) {
1922
this(context, attrs, 0);
23+
init();
2024
}
2125

2226
public MediaWikiImageView(Context context, AttributeSet attrs, int defStyle) {
2327
super(context, attrs, defStyle);
28+
init();
2429
}
2530

2631
public void setMedia(Media media) {
@@ -48,6 +53,16 @@ protected void onDetachedFromWindow() {
4853
super.onDetachedFromWindow();
4954
}
5055

56+
private void init() {
57+
setHierarchy(GenericDraweeHierarchyBuilder
58+
.newInstance(getResources())
59+
.setPlaceholderImage(VectorDrawableCompat.create(getResources(),
60+
R.drawable.ic_image_black_24dp, getContext().getTheme()))
61+
.setFailureImage(VectorDrawableCompat.create(getResources(),
62+
R.drawable.ic_image_black_24dp, getContext().getTheme()))
63+
.build());
64+
}
65+
5166
private void setImageUrl(@Nullable String url) {
5267
setImageURI(url);
5368
}

app/src/main/java/fr/free/nrw/commons/WelcomeActivity.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class WelcomeActivity extends BaseActivity {
1313

1414
@BindView(R.id.welcomePager) ViewPager pager;
1515
@BindView(R.id.welcomePagerIndicator) CirclePageIndicator indicator;
16+
private WelcomePagerAdapter adapter = new WelcomePagerAdapter();
1617

1718
@Override
1819
public void onCreate(Bundle savedInstanceState) {
@@ -24,12 +25,19 @@ public void onCreate(Bundle savedInstanceState) {
2425
}
2526
ButterKnife.bind(this);
2627

27-
setUpAdapter();
28-
}
29-
30-
private void setUpAdapter() {
31-
WelcomePagerAdapter adapter = new WelcomePagerAdapter(this);
3228
pager.setAdapter(adapter);
3329
indicator.setViewPager(pager);
30+
adapter.setCallback(new WelcomePagerAdapter.Callback() {
31+
@Override
32+
public void onYesClicked() {
33+
finish();
34+
}
35+
});
36+
}
37+
38+
@Override
39+
public void onDestroy() {
40+
adapter.setCallback(null);
41+
super.onDestroy();
3442
}
3543
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.free.nrw.commons;
22

3-
import android.app.Activity;
4-
import android.content.Context;
3+
import android.support.annotation.Nullable;
54
import android.support.v4.view.PagerAdapter;
65
import android.view.LayoutInflater;
76
import android.view.View;
@@ -11,10 +10,12 @@
1110
import butterknife.OnClick;
1211

1312
public class WelcomePagerAdapter extends PagerAdapter {
14-
15-
private Context context;
16-
1713
private static final int PAGE_FINAL = 4;
14+
private Callback callback;
15+
16+
public interface Callback {
17+
void onYesClicked();
18+
}
1819

1920
static final int[] PAGE_LAYOUTS = new int[]{
2021
R.layout.welcome_wikipedia,
@@ -24,8 +25,8 @@ public class WelcomePagerAdapter extends PagerAdapter {
2425
R.layout.welcome_final
2526
};
2627

27-
public WelcomePagerAdapter(Context context) {
28-
this.context = context;
28+
public void setCallback(@Nullable Callback callback) {
29+
this.callback = callback;
2930
}
3031

3132
@Override
@@ -40,11 +41,11 @@ public boolean isViewFromObject(View view, Object object) {
4041

4142
@Override
4243
public Object instantiateItem(ViewGroup container, int position) {
43-
LayoutInflater inflater = LayoutInflater.from(context);
44+
LayoutInflater inflater = LayoutInflater.from(container.getContext());
4445
ViewGroup layout = (ViewGroup) inflater.inflate(PAGE_LAYOUTS[position], container, false);
4546

4647
if (position == PAGE_FINAL) {
47-
ViewHolder holder = new ViewHolder(layout, context);
48+
ViewHolder holder = new ViewHolder(layout);
4849
layout.setTag(holder);
4950
}
5051
container.addView(layout);
@@ -56,17 +57,16 @@ public void destroyItem(ViewGroup container, int position, Object obj) {
5657
container.removeView((View) obj);
5758
}
5859

59-
public static class ViewHolder {
60-
private Context context;
61-
62-
public ViewHolder(View view, Context context) {
60+
class ViewHolder {
61+
ViewHolder(View view) {
6362
ButterKnife.bind(this, view);
64-
this.context = context;
6563
}
6664

6765
@OnClick(R.id.welcomeYesButton)
6866
void onClicked() {
69-
((Activity) context).finish();
67+
if (callback != null) {
68+
callback.onYesClicked();
69+
}
7070
}
7171
}
7272
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public void onCreate(Bundle savedInstanceState) {
4343
app = CommonsApplication.getInstance();
4444

4545
setContentView(R.layout.activity_login);
46-
final LoginActivity that = this;
4746

4847
loginButton = (Button) findViewById(R.id.loginButton);
4948
Button signupButton = (Button) findViewById(R.id.signupButton);
@@ -62,12 +61,14 @@ public void onCreate(Bundle savedInstanceState) {
6261
loginButton.setOnClickListener(new View.OnClickListener() {
6362
@Override
6463
public void onClick(View v) {
65-
that.performLogin();
64+
performLogin();
6665
}
6766
});
6867
signupButton.setOnClickListener(new View.OnClickListener() {
6968
@Override
70-
public void onClick(View v) { that.signUp(v); }
69+
public void onClick(View v) {
70+
signUp(v);
71+
}
7172
});
7273
}
7374

@@ -115,7 +116,7 @@ public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent
115116
protected void onResume() {
116117
super.onResume();
117118
if (prefs.getBoolean("firstrun", true)) {
118-
this.startWelcomeIntent();
119+
startWelcomeIntent();
119120
prefs.edit().putBoolean("firstrun", false).apply();
120121
}
121122
if (app.getCurrentAccount() != null) {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void onServiceDisconnected(ComponentName componentName) {
8181

8282
@Override
8383
protected void onDestroy() {
84+
getSupportFragmentManager().removeOnBackStackChangedListener(this);
8485
super.onDestroy();
8586
if(isUploadServiceConnected) {
8687
unbindService(uploadServiceConnection);
@@ -227,7 +228,8 @@ public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
227228
@Override
228229
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
229230
if(contributionsList.getAdapter() == null) {
230-
contributionsList.setAdapter(new ContributionsListAdapter(this, cursor, 0));
231+
contributionsList
232+
.setAdapter(new ContributionsListAdapter(getApplicationContext(), cursor, 0));
231233
} else {
232234
((CursorAdapter)contributionsList.getAdapter()).swapCursor(cursor);
233235
}

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListAdapter.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package fr.free.nrw.commons.contributions;
22

3-
import android.app.Activity;
43
import android.content.Context;
54
import android.database.Cursor;
65
import android.support.v4.widget.CursorAdapter;
6+
import android.view.LayoutInflater;
77
import android.view.View;
88
import android.view.ViewGroup;
99

1010
import fr.free.nrw.commons.R;
1111

1212
class ContributionsListAdapter extends CursorAdapter {
13-
private Activity activity;
1413

15-
public ContributionsListAdapter(Activity activity, Cursor c, int flags) {
16-
super(activity, c, flags);
17-
this.activity = activity;
14+
public ContributionsListAdapter(Context context, Cursor c, int flags) {
15+
super(context, c, flags);
1816
}
1917

2018
@Override
2119
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
22-
View parent = activity.getLayoutInflater().inflate(R.layout.layout_contribution, viewGroup, false);
20+
View parent = LayoutInflater.from(context)
21+
.inflate(R.layout.layout_contribution, viewGroup, false);
2322
parent.setTag(new ContributionViewHolder(parent));
2423
return parent;
2524
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class FileUtils {
2323
*/
2424
// Can be safely suppressed, checks for isKitKat before running isDocumentUri
2525
@SuppressLint("NewApi")
26-
public static String getPath(final Context context, final Uri uri) {
26+
public static String getPath(Context context, Uri uri) {
2727

2828
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
2929

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515

16+
import fr.free.nrw.commons.CommonsApplication;
1617
import timber.log.Timber;
1718

1819
/**
@@ -26,23 +27,22 @@ public class GPSExtractor {
2627
private double decLatitude, decLongitude;
2728
private Double currentLatitude = null;
2829
private Double currentLongitude = null;
29-
private Context context;
3030
public boolean imageCoordsExists;
3131
private MyLocationListener myLocationListener;
3232
private LocationManager locationManager;
3333

3434

35-
public GPSExtractor(String filePath, Context context){
35+
public GPSExtractor(String filePath) {
3636
this.filePath = filePath;
37-
this.context = context;
3837
}
3938

4039
/**
4140
* Check if user enabled retrieval of their current location in Settings
4241
* @return true if enabled, false if disabled
4342
*/
4443
private boolean gpsPreferenceEnabled() {
45-
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
44+
SharedPreferences sharedPref
45+
= PreferenceManager.getDefaultSharedPreferences(CommonsApplication.getInstance());
4646
boolean gpsPref = sharedPref.getBoolean("allowGps", false);
4747
Timber.d("Gps pref set to: %b", gpsPref);
4848
return gpsPref;
@@ -52,7 +52,8 @@ private boolean gpsPreferenceEnabled() {
5252
* Registers a LocationManager to listen for current location
5353
*/
5454
protected void registerLocationManager() {
55-
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
55+
locationManager = (LocationManager) CommonsApplication.getInstance()
56+
.getSystemService(Context.LOCATION_SERVICE);
5657
Criteria criteria = new Criteria();
5758
String provider = locationManager.getBestProvider(criteria, true);
5859
myLocationListener = new MyLocationListener();

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private void multipleUploadBegins() {
115115

116116
Timber.d("Multiple upload begins");
117117

118-
final ProgressDialog dialog = new ProgressDialog(MultipleShareActivity.this);
118+
final ProgressDialog dialog = new ProgressDialog(this);
119119
dialog.setIndeterminate(false);
120120
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
121121
dialog.setMax(photosList.size());
@@ -145,12 +145,12 @@ public void onUploadStarted(Contribution contribution) {
145145

146146
uploadsList.setImageOnlyMode(true);
147147

148-
categorizationFragment = (CategorizationFragment) this.getSupportFragmentManager().findFragmentByTag("categorization");
148+
categorizationFragment = (CategorizationFragment) getSupportFragmentManager().findFragmentByTag("categorization");
149149
if(categorizationFragment == null) {
150150
categorizationFragment = new CategorizationFragment();
151151
}
152152
// FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
153-
View target = this.getCurrentFocus();
153+
View target = getCurrentFocus();
154154
if (target != null) {
155155
InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
156156
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
@@ -203,7 +203,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
203203
@Override
204204
protected void onCreate(Bundle savedInstanceState) {
205205
super.onCreate(savedInstanceState);
206-
uploadController = new UploadController(this);
206+
uploadController = new UploadController();
207207

208208
setContentView(R.layout.activity_multiple_uploads);
209209
app = CommonsApplication.getInstance();
@@ -221,18 +221,19 @@ protected void onCreate(Bundle savedInstanceState) {
221221
@Override
222222
protected void onDestroy() {
223223
super.onDestroy();
224+
getSupportFragmentManager().removeOnBackStackChangedListener(this);
224225
uploadController.cleanup();
225226
}
226227

227228
private void showDetail(int i) {
228229
if(mediaDetails == null ||!mediaDetails.isVisible()) {
229230
mediaDetails = new MediaDetailPagerFragment(true);
230-
this.getSupportFragmentManager()
231+
getSupportFragmentManager()
231232
.beginTransaction()
232233
.replace(R.id.uploadsFragmentContainer, mediaDetails)
233234
.addToBackStack(null)
234235
.commit();
235-
this.getSupportFragmentManager().executePendingTransactions();
236+
getSupportFragmentManager().executePendingTransactions();
236237
}
237238
mediaDetails.showImage(i);
238239
}
@@ -267,7 +268,7 @@ protected void onAuthCookieAcquired(String authCookie) {
267268
uploadsList = (MultipleUploadListFragment) getSupportFragmentManager().findFragmentByTag("uploadsList");
268269
if(uploadsList == null) {
269270
uploadsList = new MultipleUploadListFragment();
270-
this.getSupportFragmentManager()
271+
getSupportFragmentManager()
271272
.beginTransaction()
272273
.add(R.id.uploadsFragmentContainer, uploadsList, "uploadsList")
273274
.commit();

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import android.graphics.Point;
55
import android.net.Uri;
66
import android.os.Bundle;
7+
import android.support.graphics.drawable.VectorDrawableCompat;
78
import android.support.v4.app.Fragment;
9+
import android.support.v4.content.ContextCompat;
810
import android.text.Editable;
911
import android.text.TextUtils;
1012
import android.text.TextWatcher;
@@ -24,6 +26,7 @@
2426
import android.widget.RelativeLayout;
2527
import android.widget.TextView;
2628

29+
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
2730
import com.facebook.drawee.view.SimpleDraweeView;
2831

2932
import fr.free.nrw.commons.R;
@@ -82,7 +85,13 @@ public View getView(int i, View view, ViewGroup viewGroup) {
8285
holder.overlay = (RelativeLayout) view.findViewById(R.id.uploadOverlay);
8386

8487
holder.image.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, photoSize.y));
85-
88+
holder.image.setHierarchy(GenericDraweeHierarchyBuilder
89+
.newInstance(getResources())
90+
.setPlaceholderImage(VectorDrawableCompat.create(getResources(),
91+
R.drawable.ic_image_black_24dp, getContext().getTheme()))
92+
.setFailureImage(VectorDrawableCompat.create(getResources(),
93+
R.drawable.ic_error_outline_black_24dp, getContext().getTheme()))
94+
.build());
8695
view.setTag(holder);
8796
} else {
8897
holder = (UploadHolderView)view.getTag();

0 commit comments

Comments
 (0)