@Injected shared preferences where needed

This commit is contained in:
Paul Hawke 2017-09-27 21:30:41 -05:00
parent 1b840502a9
commit 9aa021695b
20 changed files with 137 additions and 100 deletions

View file

@ -8,7 +8,6 @@ import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
@ -26,6 +25,7 @@ import timber.log.Timber;
public class GPSExtractor {
private final Context context;
private SharedPreferences prefs;
private ExifInterface exif;
private double decLatitude;
private double decLongitude;
@ -41,8 +41,9 @@ public class GPSExtractor {
* @param context the context
*/
@RequiresApi(24)
public GPSExtractor(@NonNull FileDescriptor fileDescriptor, Context context) {
public GPSExtractor(@NonNull FileDescriptor fileDescriptor, Context context, SharedPreferences prefs) {
this.context = context;
this.prefs = prefs;
try {
exif = new ExifInterface(fileDescriptor);
} catch (IOException | IllegalArgumentException e) {
@ -55,7 +56,8 @@ public class GPSExtractor {
* @param path file path of the image
* @param context the context
*/
public GPSExtractor(@NonNull String path, Context context) {
public GPSExtractor(@NonNull String path, Context context, SharedPreferences prefs) {
this.prefs = prefs;
try {
exif = new ExifInterface(path);
} catch (IOException | IllegalArgumentException e) {
@ -69,9 +71,7 @@ public class GPSExtractor {
* @return true if enabled, false if disabled
*/
private boolean gpsPreferenceEnabled() {
SharedPreferences sharedPref
= PreferenceManager.getDefaultSharedPreferences(context);
boolean gpsPref = sharedPref.getBoolean("allowGps", false);
boolean gpsPref = prefs.getBoolean("allowGps", false);
Timber.d("Gps pref set to: %b", gpsPref);
return gpsPref;
}

View file

@ -6,6 +6,7 @@ import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.DataSetObserver;
import android.net.Uri;
@ -25,6 +26,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.ButterKnife;
import fr.free.nrw.commons.CommonsApplication;
@ -53,6 +55,8 @@ public class MultipleShareActivity extends AuthenticatedActivity
@Inject MediaWikiApi mwApi;
@Inject SessionManager sessionManager;
@Inject UploadController uploadController;
@Inject @Named("default_preferences") SharedPreferences prefs;
private ArrayList<Contribution> photosList = null;
@ -60,8 +64,6 @@ public class MultipleShareActivity extends AuthenticatedActivity
private MediaDetailPagerFragment mediaDetails;
private CategorizationFragment categorizationFragment;
private UploadController uploadController;
@Override
public Media getMediaAtPosition(int i) {
return photosList.get(i);
@ -179,7 +181,7 @@ public class MultipleShareActivity extends AuthenticatedActivity
// FIXME: Make sure that the content provider is up
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("categories-count", categories.size())
.param("files-count", photosList.size())
@ -204,7 +206,6 @@ public class MultipleShareActivity extends AuthenticatedActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uploadController = new UploadController(sessionManager, this);
setContentView(R.layout.activity_multiple_uploads);
ButterKnife.bind(this);
@ -289,7 +290,7 @@ public class MultipleShareActivity extends AuthenticatedActivity
public void onBackPressed() {
super.onBackPressed();
if (categorizationFragment != null && categorizationFragment.isVisible()) {
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("categories-count", categorizationFragment.getCurrentSelectedCount())
.param("files-count", photosList.size())
@ -297,7 +298,7 @@ public class MultipleShareActivity extends AuthenticatedActivity
.param("result", "cancelled")
.log();
} else {
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("source", getIntent().getStringExtra(UploadService.EXTRA_SOURCE))
.param("multiple", true)

View file

@ -3,6 +3,7 @@ package fr.free.nrw.commons.upload;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
@ -34,6 +35,7 @@ import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.ButterKnife;
import fr.free.nrw.commons.CommonsApplication;
@ -73,6 +75,8 @@ public class ShareActivity
@Inject MediaWikiApi mwApi;
@Inject CacheController cacheController;
@Inject SessionManager sessionManager;
@Inject UploadController uploadController;
@Inject @Named("default_preferences") SharedPreferences prefs;
private String source;
private String mimeType;
@ -81,8 +85,6 @@ public class ShareActivity
private Contribution contribution;
private SimpleDraweeView backgroundImageView;
private UploadController uploadController;
private boolean cacheFound;
private GPSExtractor imageObj;
@ -172,7 +174,7 @@ public class ShareActivity
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("categories-count", categories.size())
.param("files-count", 1)
@ -194,7 +196,7 @@ public class ShareActivity
public void onBackPressed() {
super.onBackPressed();
if(categorizationFragment != null && categorizationFragment.isVisible()) {
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("categories-count", categorizationFragment.getCurrentSelectedCount())
.param("files-count", 1)
@ -202,7 +204,7 @@ public class ShareActivity
.param("result", "cancelled")
.log();
} else {
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("source", getIntent().getStringExtra(UploadService.EXTRA_SOURCE))
.param("multiple", true)
@ -226,7 +228,7 @@ public class ShareActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uploadController = new UploadController(sessionManager, this);
setContentView(R.layout.activity_share);
ButterKnife.bind(this);
initBack();
@ -451,12 +453,12 @@ public class ShareActivity
= getContentResolver().openFileDescriptor(mediaUri, "r");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (descriptor != null) {
imageObj = new GPSExtractor(descriptor.getFileDescriptor(), this);
imageObj = new GPSExtractor(descriptor.getFileDescriptor(), this, prefs);
}
} else {
String filePath = getPathOfMediaOrCopy();
if (filePath != null) {
imageObj = new GPSExtractor(filePath, this);
imageObj = new GPSExtractor(filePath, this, prefs);
}
}
}

View file

@ -28,6 +28,9 @@ import android.widget.TextView;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
@ -50,7 +53,8 @@ public class SingleUploadFragment extends DaggerFragment {
@BindView(R.id.share_license_summary) TextView licenseSummaryView;
@BindView(R.id.licenseSpinner) Spinner licenseSpinner;
private SharedPreferences prefs;
@Inject @Named("default_preferences") SharedPreferences prefs;
private String license;
private OnUploadActionInitiated uploadActionInitiatedHandler;
private TitleTextWatcher textWatcher = new TitleTextWatcher();
@ -73,11 +77,10 @@ public class SingleUploadFragment extends DaggerFragment {
String desc = descEdit.getText().toString();
//Save the title/desc in short-lived cache so next time this fragment is loaded, we can access these
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = titleDesc.edit();
editor.putString("Title", title);
editor.putString("Desc", desc);
editor.apply();
prefs.edit()
.putString("Title", title)
.putString("Desc", desc)
.apply();
uploadActionInitiatedHandler.uploadActionInitiated(title, desc);
return true;
@ -91,7 +94,6 @@ public class SingleUploadFragment extends DaggerFragment {
View rootView = inflater.inflate(R.layout.fragment_single_upload, container, false);
ButterKnife.bind(this, rootView);
ArrayList<String> licenseItems = new ArrayList<>();
licenseItems.add(getString(R.string.license_name_cc0));
licenseItems.add(getString(R.string.license_name_cc_by));
@ -99,7 +101,6 @@ public class SingleUploadFragment extends DaggerFragment {
licenseItems.add(getString(R.string.license_name_cc_by_four));
licenseItems.add(getString(R.string.license_name_cc_by_sa_four));
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
license = prefs.getString(Prefs.DEFAULT_LICENSE, Prefs.Licenses.CC_BY_SA_3);
// check if this is the first time we have uploaded
@ -172,9 +173,9 @@ public class SingleUploadFragment extends DaggerFragment {
}
setLicenseSummary(license);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Prefs.DEFAULT_LICENSE, license);
editor.commit();
prefs.edit()
.putString(Prefs.DEFAULT_LICENSE, license)
.commit();
}
@OnTouch(R.id.share_license_summary)
@ -193,9 +194,8 @@ public class SingleUploadFragment extends DaggerFragment {
@OnClick(R.id.titleDescButton)
void setTitleDescButton() {
//Retrieve last title and desc entered
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
String title = titleDesc.getString("Title", "");
String desc = titleDesc.getString("Desc", "");
String title = prefs.getString("Title", "");
String desc = prefs.getString("Desc", "");
Timber.d("Title: %s, Desc: %s", title, desc);
titleEdit.setText(title);

View file

@ -10,7 +10,6 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.TextUtils;
@ -31,14 +30,16 @@ public class UploadController {
private UploadService uploadService;
private SessionManager sessionManager;
private Context context;
private SharedPreferences prefs;
public interface ContributionUploadProgress {
void onUploadStarted(Contribution contribution);
}
public UploadController(SessionManager sessionManager, Context context) {
public UploadController(SessionManager sessionManager, Context context, SharedPreferences sharedPreferences) {
this.sessionManager = sessionManager;
this.context = context;
this.prefs = sharedPreferences;
}
private boolean isUploadServiceConnected;
@ -85,8 +86,6 @@ public class UploadController {
}
public void startUpload(final Contribution contribution, final ContributionUploadProgress onComplete) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
//Set creator, desc, and license
if(TextUtils.isEmpty(contribution.getCreator())) {
contribution.setCreator(sessionManager.getCurrentAccount().name);

View file

@ -8,6 +8,7 @@ import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
@ -23,6 +24,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Named;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.HandlerService;
@ -50,6 +52,7 @@ public class UploadService extends HandlerService<Contribution> {
@Inject MediaWikiApi mwApi;
@Inject SessionManager sessionManager;
@Inject @Named("default_preferences") SharedPreferences prefs;
private NotificationManager notificationManager;
private ContentProviderClient contributionsProviderClient;
@ -247,7 +250,7 @@ public class UploadService extends HandlerService<Contribution> {
String resultStatus = uploadResult.getResultStatus();
if (!resultStatus.equals("Success")) {
showFailedNotification(contribution);
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("source", contribution.getSource())
.param("multiple", contribution.getMultiple())
@ -261,7 +264,7 @@ public class UploadService extends HandlerService<Contribution> {
contribution.setDateUploaded(uploadResult.getDateUploaded());
contribution.save();
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, getApplicationContext(), mwApi)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, mwApi, prefs)
.param("username", sessionManager.getCurrentAccount().name)
.param("source", contribution.getSource()) //FIXME
.param("filename", contribution.getFilename())