Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Dmitry Brant 2017-05-19 15:14:29 +02:00
commit a3f3ee7fdb
85 changed files with 523 additions and 191 deletions

View file

@ -5,8 +5,12 @@ import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
@ -19,7 +23,14 @@ import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.category.Category;
import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.data.DBOpenHelper;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.nearby.NearbyPlaces;
import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
@ -34,9 +45,10 @@ import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import java.io.File;
import java.io.IOException;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.utils.FileUtils;
import timber.log.Timber;
// TODO: Use ProGuard to rip out reporting when publishing
@ -50,7 +62,6 @@ import timber.log.Timber;
)
public class CommonsApplication extends Application {
private MWApi api;
private Account currentAccount = null; // Unlike a savings account...
public static final String API_URL = "https://commons.wikimedia.org/w/api.php";
public static final String IMAGE_URL_BASE = "https://upload.wikimedia.org/wikipedia/commons";
@ -69,13 +80,37 @@ public class CommonsApplication extends Application {
public static final String FEEDBACK_EMAIL = "commons-app-android@googlegroups.com";
public static final String FEEDBACK_EMAIL_SUBJECT = "Commons Android App (%s) Feedback";
public RequestQueue volleyQueue;
private static CommonsApplication instance = null;
private AbstractHttpClient httpClient = null;
private MWApi api = null;
private CacheController cacheData = null;
private RequestQueue volleyQueue = null;
private DBOpenHelper dbOpenHelper = null;
private NearbyPlaces nearbyPlaces = null;
public CacheController cacheData;
/**
* This should not be called by ANY application code (other than the magic Android glue)
* Use CommonsApplication.getInstance() instead to get the singleton.
*/
public CommonsApplication() {
CommonsApplication.instance = this;
}
public static CommonsApplication app;
public static CommonsApplication getInstance() {
if (instance == null) {
instance = new CommonsApplication();
}
return instance;
}
public static AbstractHttpClient createHttpClient() {
public AbstractHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = newHttpClient();
}
return httpClient;
}
private AbstractHttpClient newHttpClient() {
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
@ -86,14 +121,50 @@ public class CommonsApplication extends Application {
return new DefaultHttpClient(cm, params);
}
public static MWApi createMWApi() {
return new MWApi(API_URL, createHttpClient());
public MWApi getMWApi() {
if (api == null) {
api = newMWApi();
}
return api;
}
private MWApi newMWApi() {
return new MWApi(API_URL, getHttpClient());
}
public CacheController getCacheData() {
if (cacheData == null) {
cacheData = new CacheController();
}
return cacheData;
}
public RequestQueue getVolleyQueue() {
if (volleyQueue == null) {
DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 16 * 1024 * 1024);
volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack()));
volleyQueue.start();
}
return volleyQueue;
}
public synchronized DBOpenHelper getDBOpenHelper() {
if (dbOpenHelper == null) {
dbOpenHelper = new DBOpenHelper(this);
}
return dbOpenHelper;
}
public synchronized NearbyPlaces getNearbyPlaces() {
if (nearbyPlaces == null) {
nearbyPlaces = new NearbyPlaces();
}
return nearbyPlaces;
}
@Override
public void onCreate() {
super.onCreate();
app = this;
Timber.plant(new Timber.DebugTree());
@ -104,9 +175,8 @@ public class CommonsApplication extends Application {
}
// Fire progress callbacks for every 3% of uploaded content
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0");
api = createMWApi();
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(getApplicationContext())
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.discCache(new TotalSizeLimitedDiscCache(StorageUtils.getCacheDirectory(this), 128 * 1024 * 1024))
.build();
ImageLoader.getInstance().init(imageLoaderConfiguration);
@ -129,7 +199,7 @@ public class CommonsApplication extends Application {
}
/**
* @return Accout|null
* @return Account|null
*/
public Account getCurrentAccount() {
if(currentAccount == null) {
@ -150,21 +220,12 @@ public class CommonsApplication extends Application {
return false; // This should never happen
}
accountManager.invalidateAuthToken(AccountUtil.accountType(), api.getAuthCookie());
accountManager.invalidateAuthToken(AccountUtil.accountType(), getMWApi().getAuthCookie());
try {
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
api.setAuthCookie(authCookie);
getMWApi().setAuthCookie(authCookie);
return true;
} catch (OperationCanceledException e) {
e.printStackTrace();
return false;
} catch (AuthenticatorException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (NullPointerException e) {
} catch (OperationCanceledException | NullPointerException | IOException | AuthenticatorException e) {
e.printStackTrace();
return false;
}
@ -175,4 +236,47 @@ public class CommonsApplication extends Application {
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) ||
pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
}
public void clearApplicationData(Context context) {
File cacheDirectory = context.getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
FileUtils.deleteFile(new File(applicationDirectory, fileName));
}
}
}
AccountManager accountManager = AccountManager.get(this);
Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
for (int index = 0; index < allAccounts.length; index++) {
accountManager.removeAccount(allAccounts[index], null, null);
}
//TODO: fix preference manager
PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit();
SharedPreferences preferences = context
.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE);
preferences.edit().clear().commit();
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();
preferences.edit().putBoolean("firstrun", false).apply();
updateAllDatabases(context);
currentAccount = null;
}
/**
* Deletes all tables and re-creates them.
* @param context context
*/
public void updateAllDatabases(Context context) {
DBOpenHelper dbOpenHelper = CommonsApplication.getInstance().getDBOpenHelper();
dbOpenHelper.getReadableDatabase().close();
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ModifierSequence.Table.onDelete(db);
Category.Table.onDelete(db);
Contribution.Table.onDelete(db);
}
}

View file

@ -6,6 +6,7 @@ import android.os.Build;
import android.preference.PreferenceManager;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.AbstractHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
@ -30,10 +31,10 @@ public class EventLog {
boolean allSuccess = true;
// Not using the default URL connection, since that seems to have different behavior than the rest of the code
for(LogBuilder logBuilder: logBuilders) {
HttpURLConnection conn;
try {
URL url = logBuilder.toUrl();
HttpResponse response = Http.get(url.toString()).use(CommonsApplication.createHttpClient()).asResponse();
AbstractHttpClient httpClient = CommonsApplication.getInstance().getHttpClient();
HttpResponse response = Http.get(url.toString()).use(httpClient).asResponse();
if(response.getStatusLine().getStatusCode() != 204) {
allSuccess = false;

View file

@ -173,7 +173,7 @@ public class Media implements Parcelable {
}
public void setCategories(List<String> categories) {
this.categories.removeAll(this.categories);
this.categories.clear();
this.categories.addAll(categories);
}

View file

@ -66,7 +66,7 @@ public class MediaDataExtractor {
throw new IllegalStateException("Tried to call MediaDataExtractor.fetch() again.");
}
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result = api.action("query")
.param("prop", "revisions")
.param("titles", filename)

View file

@ -11,7 +11,6 @@ public class MediaWikiImageView extends SimpleDraweeView {
private ThumbnailFetchTask currentThumbnailTask;
LruCache<String, String> thumbnailUrlCache = new LruCache<>(1024);
public MediaWikiImageView(Context context) {
this(context, null);
}

View file

@ -55,7 +55,7 @@ public class AccountUtil {
@NonNull
private static CommonsApplication app() {
return CommonsApplication.app;
return CommonsApplication.getInstance();
}
}

View file

@ -132,7 +132,7 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (CommonsApplication)this.getApplicationContext();
app = CommonsApplication.getInstance();
if(savedInstanceState != null) {
authCookie = savedInstanceState.getString("authCookie");
}

View file

@ -40,7 +40,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (CommonsApplication) getApplicationContext();
app = CommonsApplication.getInstance();
setContentView(R.layout.activity_login);
final LoginActivity that = this;
@ -199,7 +199,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
}
private void showUserToast( int resId ) {
Toast.makeText(getApplicationContext(), resId, Toast.LENGTH_LONG).show();
Toast.makeText(this, resId, Toast.LENGTH_LONG).show();
}
public void showSuccessToastAndDismissDialog() {

View file

@ -3,16 +3,16 @@ package fr.free.nrw.commons.auth;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.IOException;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.EventLog;
import fr.free.nrw.commons.R;
import timber.log.Timber;
import java.io.IOException;
class LoginTask extends AsyncTask<String, String, String> {
private LoginActivity loginActivity;
@ -26,7 +26,7 @@ class LoginTask extends AsyncTask<String, String, String> {
this.username = username;
this.password = password;
this.twoFactorCode = twoFactorCode;
app = (CommonsApplication) loginActivity.getApplicationContext();
app = CommonsApplication.getInstance();
}
@Override
@ -44,9 +44,9 @@ class LoginTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
try {
if (twoFactorCode.isEmpty()) {
return app.getApi().login(username, password);
return app.getMWApi().login(username, password);
} else {
return app.getApi().login(username, password, twoFactorCode);
return app.getMWApi().login(username, password, twoFactorCode);
}
} catch (IOException e) {
// Do something better!

View file

@ -7,6 +7,7 @@ import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.theme.BaseActivity;
import timber.log.Timber;
@ -39,10 +40,14 @@ public class SignupActivity extends BaseActivity {
//Signup success, so clear cookies, notify user, and load LoginActivity again
Timber.d("Overriding URL %s", url);
Toast toast = Toast.makeText(getApplicationContext(), "Account created!", Toast.LENGTH_LONG);
Toast toast = Toast.makeText(
CommonsApplication.getInstance(),
"Account created!",
Toast.LENGTH_LONG
);
toast.show();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(CommonsApplication.getInstance(), LoginActivity.class);
startActivity(intent);
return true;
} else {

View file

@ -75,7 +75,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
}
private String getAuthCookie(String username, String password) throws IOException {
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
//TODO add 2fa support here
String result = api.login(username, password);
if(result.equals("PASS")) {

View file

@ -115,6 +115,11 @@ public class Category {
db.execSQL(CREATE_TABLE_STATEMENT);
}
public static void onDelete(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public static void onUpdate(SQLiteDatabase db, int from, int to) {
if(from == to) {
return;

View file

@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.data.DBOpenHelper;
import timber.log.Timber;
@ -36,7 +37,7 @@ public class CategoryContentProvider extends ContentProvider {
private DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
dbOpenHelper = DBOpenHelper.getInstance(getContext());
dbOpenHelper = CommonsApplication.getInstance().getDBOpenHelper();
return false;
}

View file

@ -79,7 +79,7 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
protected ArrayList<String> doInBackground(Void... voids) {
//otherwise if user has typed something in that isn't in cache, search API for matching categories
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> categories = new ArrayList<>();

View file

@ -95,7 +95,7 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
//otherwise if user has typed something in that isn't in cache, search API for matching categories
//URL: https://commons.wikimedia.org/w/api.php?action=query&list=allcategories&acprefix=filter&aclimit=25
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> categories = new ArrayList<>();
try {

View file

@ -34,7 +34,7 @@ public class TitleCategories extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... voids) {
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> items = new ArrayList<>();

View file

@ -331,6 +331,11 @@ public class Contribution extends Media {
db.execSQL(CREATE_TABLE_STATEMENT);
}
public static void onDelete(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public static void onUpdate(SQLiteDatabase db, int from, int to) {
if(from == to) {
return;

View file

@ -109,7 +109,7 @@ public class ContributionsActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
// Do a sync everytime we get here!
ContentResolver.requestSync(((CommonsApplication) getApplicationContext()).getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
ContentResolver.requestSync(CommonsApplication.getInstance().getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
Intent uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent);
@ -219,7 +219,7 @@ public class ContributionsActivity
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PreferenceManager.getDefaultSharedPreferences(this);
int uploads = sharedPref.getInt(Prefs.UPLOADS_SHOWING, 100);
return new CursorLoader(this, ContributionsContentProvider.BASE_URI,
Contribution.Table.ALL_FIELDS, CONTRIBUTION_SELECTION, null,

View file

@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.data.DBOpenHelper;
import timber.log.Timber;
@ -35,7 +36,7 @@ public class ContributionsContentProvider extends ContentProvider{
private DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
dbOpenHelper = DBOpenHelper.getInstance(getContext());
dbOpenHelper = CommonsApplication.getInstance().getDBOpenHelper();
return false;
}

View file

@ -159,8 +159,7 @@ public class ContributionsListFragment extends Fragment {
menu.clear(); // See http://stackoverflow.com/a/8495697/17865
inflater.inflate(R.menu.fragment_contributions_list, menu);
CommonsApplication app = (CommonsApplication)getActivity().getApplicationContext();
if (!app.deviceHasCamera()) {
if (!CommonsApplication.getInstance().deviceHasCamera()) {
menu.findItem(R.id.menu_from_camera).setEnabled(false);
}
}

View file

@ -61,7 +61,7 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
// This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
String user = account.name;
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
SharedPreferences prefs = this.getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
String lastModified = prefs.getString("lastSyncTimestamp", "");
Date curTime = new Date();

View file

@ -15,7 +15,7 @@ public class ContributionsSyncService extends Service {
super.onCreate();
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new ContributionsSyncAdapter(getApplicationContext(), true);
sSyncAdapter = new ContributionsSyncAdapter(this, true);
}
}
}

View file

@ -12,16 +12,11 @@ public class DBOpenHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "commons.db";
private static final int DATABASE_VERSION = 6;
private static DBOpenHelper singleton = null;
public static synchronized DBOpenHelper getInstance(Context context) {
if ( singleton == null ) {
singleton = new DBOpenHelper(context);
}
return singleton;
}
private DBOpenHelper(Context context) {
/**
* Do not use, please call CommonsApplication.getDBOpenHelper()
*/
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

View file

@ -1,15 +1,18 @@
package fr.free.nrw.commons.hamburger;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
@ -21,6 +24,7 @@ import fr.free.nrw.commons.AboutActivity;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.nearby.NearbyActivity;
import fr.free.nrw.commons.settings.SettingsActivity;
@ -31,22 +35,22 @@ public class NavigationBaseFragment extends Fragment {
ImageView pictureOfTheDay;
@BindView(R.id.upload_item)
TextView uploadItem;
LinearLayout uploadItem;
@BindView(R.id.nearby_item)
TextView nearbyItem;
LinearLayout nearbyItem;
@BindView(R.id.about_item)
TextView aboutItem;
LinearLayout aboutItem;
@BindView(R.id.settings_item)
TextView settingsItem;
LinearLayout settingsItem;
@BindView(R.id.feedback_item)
TextView feedbackItem;
LinearLayout feedbackItem;
@BindView(R.id.logout_item)
TextView logoutItem;
LinearLayout logoutItem;
private DrawerLayout drawerLayout;
private RelativeLayout drawerPane;
@ -120,6 +124,34 @@ public class NavigationBaseFragment extends Fragment {
}
}
@OnClick(R.id.logout_item)
protected void onLogoutItemClicked() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setMessage(R.string.logout_verification)
.setCancelable(false)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((CommonsApplication)getActivity().getApplicationContext())
.clearApplicationData(getContext());
Intent nearbyIntent = new Intent
(getActivity(), LoginActivity.class);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(nearbyIntent);
getActivity().finish();
}
});
alertDialogBuilder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
private void closeDrawer() {
if (drawerLayout != null) {
drawerLayout.closeDrawer(drawerPane);

View file

@ -6,6 +6,8 @@ public class LatLng {
public final double longitude;
/** Accepts latitude and longitude.
* North and South values are cut off at 90°
*
* @param latitude double value
* @param longitude double value
*/
@ -44,14 +46,15 @@ public class LatLng {
}
/**
* Rounds the float to 4 digits.
* Rounds the float to 4 digits and returns absolute value.
*
* @param coordinate A coordinate value as string.
* @return String of the rounded number.
*/
private String formatCoordinate(double coordinate) {
double roundedNumber = Math.round(coordinate * 10000d) / 10000d;
return String.valueOf(roundedNumber);
double absoluteNumber = Math.abs(roundedNumber);
return String.valueOf(absoluteNumber);
}
/**
@ -73,7 +76,7 @@ public class LatLng {
* @return "E" or "W".
*/
private String getEastWest() {
if (this.longitude < 180) {
if (this.longitude >= 0 && this.longitude < 180) {
return "E";
}

View file

@ -231,7 +231,7 @@ public class MediaDetailFragment extends Fragment {
coordinates.setText(prettyCoordinates(media));
uploadedDate.setText(prettyUploadedDate(media));
categoryNames.removeAll(categoryNames);
categoryNames.clear();
categoryNames.addAll(media.getCategories());
categoriesLoaded = true;
@ -276,7 +276,7 @@ public class MediaDetailFragment extends Fragment {
desc.setText(prettyDescription(media));
license.setText(prettyLicense(media));
categoryNames.removeAll(categoryNames);
categoryNames.clear();
categoryNames.addAll(media.getCategories());
categoriesLoaded = true;
@ -399,8 +399,7 @@ public class MediaDetailFragment extends Fragment {
return "Uploaded date not available";
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
String formattedDate = formatter.format(date);
return formattedDate;
return formatter.format(date);
}
/**
@ -409,8 +408,6 @@ public class MediaDetailFragment extends Fragment {
* @return Coordinates as text.
*/
private String prettyCoordinates(Media media) {
String coordinates = media.getCoordinates();
return coordinates;
return media.getCoordinates();
}
}

View file

@ -123,7 +123,7 @@ public class MediaDetailPagerFragment extends Fragment implements ViewPager.OnPa
if(savedInstanceState != null) {
editable = savedInstanceState.getBoolean("editable");
}
app = (CommonsApplication)getActivity().getApplicationContext();
app = CommonsApplication.getInstance();
setHasOptionsMenu(true);
}

View file

@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.data.DBOpenHelper;
import timber.log.Timber;
@ -35,7 +36,7 @@ public class ModificationsContentProvider extends ContentProvider{
private DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
dbOpenHelper = DBOpenHelper.getInstance(getContext());
dbOpenHelper = CommonsApplication.getInstance().getDBOpenHelper();
return false;
}

View file

@ -61,7 +61,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
return;
}
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
api.setAuthCookie(authCookie);
String editToken;

View file

@ -15,7 +15,7 @@ public class ModificationsSyncService extends Service {
super.onCreate();
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new ModificationsSyncAdapter(getApplicationContext(), true);
sSyncAdapter = new ModificationsSyncAdapter(this, true);
}
}
}

View file

@ -142,5 +142,10 @@ public class ModifierSequence {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public static void onDelete(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}

View file

@ -25,6 +25,7 @@ import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
@ -184,7 +185,7 @@ public class NearbyActivity extends NavigationBaseActivity {
@Override
protected List<Place> doInBackground(Void... params) {
return NearbyController
.loadAttractionsFromLocation(curLatLang, getApplicationContext()
.loadAttractionsFromLocation(curLatLang, CommonsApplication.getInstance()
);
}

View file

@ -15,6 +15,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import timber.log.Timber;
@ -37,13 +38,14 @@ public class NearbyController {
if (curLatLng == null) {
return Collections.emptyList();
}
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> places = prefs.getBoolean("useWikidata", true)
? NearbyPlaces.getInstance().getFromWikidataQuery(
? nearbyPlaces.getFromWikidataQuery(
context,
curLatLng,
Locale.getDefault().getLanguage())
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
: nearbyPlaces.getFromWikiNeedsPictures();
if (curLatLng != null) {
Timber.d("Sorting places by distance...");
final Map<Place, Double> distances = new HashMap<>();

View file

@ -1,6 +1,5 @@
package fr.free.nrw.commons.nearby;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
@ -9,22 +8,19 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UriDeserializer;
import java.lang.reflect.Type;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UriDeserializer;
import timber.log.Timber;
public class NearbyListFragment extends ListFragment {

View file

@ -29,13 +29,9 @@ public class NearbyPlaces {
private static final double MAX_RADIUS = 300.0; // in kilometer
private static final double RADIUS_MULTIPLIER = 1.618;
private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}";
private static NearbyPlaces singleton;
private double radius = INITIAL_RADIUS;
private List<Place> places;
private NearbyPlaces(){
}
List<Place> getFromWikidataQuery(Context context,
LatLng curLatLng,
String lang) {
@ -199,17 +195,4 @@ public class NearbyPlaces {
}
return places;
}
/**
* Get the singleton instance of this class.
* The instance is created upon the first invocation of this method, and then reused.
*
* @return The singleton instance
*/
public static synchronized NearbyPlaces getInstance() {
if (singleton == null) {
singleton = new NearbyPlaces();
}
return singleton;
}
}

View file

@ -11,6 +11,7 @@ import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
@ -46,7 +47,7 @@ public class SettingsFragment extends PreferenceFragment {
final EditTextPreference uploadLimit = (EditTextPreference) findPreference("uploads");
final SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(getActivity().getApplicationContext());
.getDefaultSharedPreferences(CommonsApplication.getInstance());
int uploads = sharedPref.getInt(Prefs.UPLOADS_SHOWING, 100);
uploadLimit.setText(uploads + "");
uploadLimit.setSummary(uploads + "");

View file

@ -38,7 +38,7 @@ public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
MWApi api = CommonsApplication.createMWApi();
MWApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
// https://commons.wikimedia.org/w/api.php?action=query&list=allimages&format=xml&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.upload;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
@ -20,6 +21,8 @@ public class FileUtils {
* @param uri The Uri to query.
* @author paulburke
*/
// Can be safely suppressed, checks for isKitKat before running isDocumentUri
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

View file

@ -132,7 +132,11 @@ public class MultipleShareActivity
dialog.setProgress(uploadCount);
if(uploadCount == photosList.size()) {
dialog.dismiss();
Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG);
Toast startingToast = Toast.makeText(
CommonsApplication.getInstance(),
R.string.uploading_started,
Toast.LENGTH_LONG
);
startingToast.show();
}
}
@ -202,9 +206,9 @@ public class MultipleShareActivity
uploadController = new UploadController(this);
setContentView(R.layout.activity_multiple_uploads);
app = CommonsApplication.getInstance();
ButterKnife.bind(this);
initDrawer();
app = (CommonsApplication)this.getApplicationContext();
if(savedInstanceState != null) {
photosList = savedInstanceState.getParcelableArrayList("uploadsList");
@ -241,7 +245,7 @@ public class MultipleShareActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
app.getApi().setAuthCookie(authCookie);
app.getMWApi().setAuthCookie(authCookie);
Intent intent = getIntent();
if(intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {

View file

@ -101,7 +101,7 @@ public class MwVolleyApi {
private static RequestQueue getQueue(Context context) {
if (REQUEST_QUEUE == null) {
REQUEST_QUEUE = Volley.newRequestQueue(context.getApplicationContext());
REQUEST_QUEUE = Volley.newRequestQueue(context);
}
return REQUEST_QUEUE;
}

View file

@ -106,12 +106,16 @@ public class ShareActivity
getFileMetadata(false);
}
Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG);
Toast startingToast = Toast.makeText(
CommonsApplication.getInstance(),
R.string.uploading_started,
Toast.LENGTH_LONG
);
startingToast.show();
if (!cacheFound) {
//Has to be called after apiCall.request()
app.cacheData.cacheCategory();
app.getCacheData().cacheCategory();
Timber.d("Cache the categories found");
}
@ -189,7 +193,7 @@ public class ShareActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
app.getApi().setAuthCookie(authCookie);
app.getMWApi().setAuthCookie(authCookie);
shareView = (SingleUploadFragment) getSupportFragmentManager().findFragmentByTag("shareView");
categorizationFragment = (CategorizationFragment) getSupportFragmentManager().findFragmentByTag("categorization");
@ -217,7 +221,7 @@ public class ShareActivity
setContentView(R.layout.activity_share);
ButterKnife.bind(this);
initDrawer();
app = (CommonsApplication)this.getApplicationContext();
app = CommonsApplication.getInstance();
backgroundImageView = (ImageView)findViewById(R.id.backgroundImage);
//Receive intent from ContributionController.java when user selects picture to upload
@ -398,12 +402,12 @@ public class ShareActivity
if (imageObj.imageCoordsExists) {
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
app.cacheData.setQtPoint(decLongitude, decLatitude);
app.getCacheData().setQtPoint(decLongitude, decLatitude);
}
MwVolleyApi apiCall = new MwVolleyApi(this);
List<String> displayCatList = app.cacheData.findCategory();
List<String> displayCatList = app.getCacheData().findCategory();
boolean catListEmpty = displayCatList.isEmpty();
// If no categories found in cache, call MediaWiki API to match image coords with nearby Commons categories

View file

@ -36,7 +36,7 @@ public class UploadController {
public UploadController(Activity activity) {
this.activity = activity;
app = (CommonsApplication)activity.getApplicationContext();
app = CommonsApplication.getInstance();
}
private boolean isUploadServiceConnected;
@ -55,7 +55,7 @@ public class UploadController {
};
public void prepareService() {
Intent uploadServiceIntent = new Intent(activity.getApplicationContext(), UploadService.class);
Intent uploadServiceIntent = new Intent(activity, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
activity.startService(uploadServiceIntent);
activity.bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE);

View file

@ -115,7 +115,7 @@ public class UploadService extends HandlerService<Contribution> {
super.onCreate();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
app = (CommonsApplication) this.getApplicationContext();
app = CommonsApplication.getInstance();
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
}
@ -176,7 +176,7 @@ public class UploadService extends HandlerService<Contribution> {
}
private void uploadContribution(Contribution contribution) {
MWApi api = app.getApi();
MWApi api = app.getMWApi();
ApiResult result;
InputStream file = null;
@ -201,7 +201,7 @@ public class UploadService extends HandlerService<Contribution> {
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setOngoing(true)
.setProgress(100, 0, true)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(this, ContributionsActivity.class), 0))
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
@ -282,7 +282,7 @@ public class UploadService extends HandlerService<Contribution> {
toUpload--;
if(toUpload == 0) {
// Sync modifications right after all uplaods are processed
ContentResolver.requestSync(((CommonsApplication) getApplicationContext()).getCurrentAccount(), ModificationsContentProvider.AUTHORITY, new Bundle());
ContentResolver.requestSync((CommonsApplication.getInstance()).getCurrentAccount(), ModificationsContentProvider.AUTHORITY, new Bundle());
stopForeground(true);
}
}
@ -304,7 +304,7 @@ public class UploadService extends HandlerService<Contribution> {
}
private String findUniqueFilename(String fileName) throws IOException {
MWApi api = app.getApi();
MWApi api = app.getMWApi();
String sequenceFileName;
for ( int sequenceNumber = 1; true; sequenceNumber++ ) {
if (sequenceNumber == 1) {

View file

@ -3,6 +3,7 @@ package fr.free.nrw.commons.utils;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
@ -32,4 +33,26 @@ public class FileUtils {
}
return stringBuilder;
}
/**
* Deletes files.
* @param file context
*/
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
}