Skip to content

Commit accd7ed

Browse files
committed
Merge branch 'master' into login-screen-improvement
2 parents bcf6511 + 7e5d8c4 commit accd7ed

21 files changed

+161
-67
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ public void onCreate() {
147147
* @return Account|null
148148
*/
149149
public Account getCurrentAccount() {
150-
if(currentAccount == null) {
150+
if (currentAccount == null) {
151151
AccountManager accountManager = AccountManager.get(this);
152152
Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
153-
if(allAccounts.length != 0) {
153+
if (allAccounts.length != 0) {
154154
currentAccount = allAccounts[0];
155155
}
156156
}
@@ -161,7 +161,7 @@ public Boolean revalidateAuthToken() {
161161
AccountManager accountManager = AccountManager.get(this);
162162
Account curAccount = getCurrentAccount();
163163

164-
if(curAccount == null) {
164+
if (curAccount == null) {
165165
return false; // This should never happen
166166
}
167167

@@ -224,11 +224,13 @@ public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
224224
if (getIndex() == allAccounts.length) {
225225
Timber.d("All accounts have been removed");
226226
//TODO: fix preference manager
227-
PreferenceManager.getDefaultSharedPreferences(getInstance()).edit().clear().commit();
227+
PreferenceManager.getDefaultSharedPreferences(getInstance())
228+
.edit().clear().commit();
228229
SharedPreferences preferences = context
229230
.getSharedPreferences("fr.free.nrw.commons", MODE_PRIVATE);
230231
preferences.edit().clear().commit();
231-
context.getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().clear().commit();
232+
context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
233+
.edit().clear().commit();
232234
preferences.edit().putBoolean("firstrun", false).apply();
233235
updateAllDatabases();
234236
currentAccount = null;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import android.support.annotation.Nullable;
44

55
public class License {
6-
String key;
7-
String template;
8-
String url;
9-
String name;
6+
private String key;
7+
private String template;
8+
private String url;
9+
private String name;
1010

1111
public License(String key, String template, String url, String name) {
1212
if (key == null) {

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ public static String licenseUrlFor(String license) {
242242
public static boolean xmlFastForward(XmlPullParser parser, String namespace, String element) {
243243
try {
244244
while (parser.next() != XmlPullParser.END_DOCUMENT) {
245-
if (parser.getEventType() == XmlPullParser.START_TAG &&
246-
parser.getNamespace().equals(namespace) &&
247-
parser.getName().equals(element)) {
245+
if (parser.getEventType() == XmlPullParser.START_TAG
246+
&& parser.getNamespace().equals(namespace)
247+
&& parser.getName().equals(element)) {
248248
// We found it!
249249
return true;
250250
}
@@ -267,7 +267,8 @@ public static String fixExtension(String title, String extension) {
267267
extension = "jpg";
268268
}
269269
title = jpegPattern.matcher(title).replaceFirst(".jpg");
270-
if (extension != null && !title.toLowerCase(Locale.getDefault()).endsWith("." + extension.toLowerCase(Locale.ENGLISH))) {
270+
if (extension != null && !title.toLowerCase(Locale.getDefault())
271+
.endsWith("." + extension.toLowerCase(Locale.ENGLISH))) {
271272
title += "." + extension;
272273
}
273274
return title;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
public abstract class AuthenticatedActivity extends NavigationBaseActivity {
1616

17-
String accountType;
17+
private String accountType;
1818
CommonsApplication app;
1919

2020
private String authCookie;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
2525

2626
webView.setWebViewClient(new MyWebViewClient());
2727
WebSettings webSettings = webView.getSettings();
28-
//Needed to refresh Captcha. Might introduce XSS vulnerabilities, but we can trust Wikimedia's site... right?
28+
/*Needed to refresh Captcha. Might introduce XSS vulnerabilities, but we can
29+
trust Wikimedia's site... right?*/
2930
webSettings.setJavaScriptEnabled(true);
3031

3132
webView.loadUrl(BuildConfig.SIGNUP_LANDING_URL);

app/src/main/java/fr/free/nrw/commons/caching/CacheController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void setQtPoint(double decLongitude, double decLatitude) {
3232
public void cacheCategory() {
3333
List<String> pointCatList = new ArrayList<>();
3434
if (MwVolleyApi.GpsCatExists.getGpsCatExists()) {
35-
pointCatList.addAll(MwVolleyApi.getGpsCat());
35+
pointCatList.addAll(MwVolleyApi.getGpsCat());
3636
Timber.d("Categories being cached: %s", pointCatList);
3737
} else {
3838
Timber.d("No categories found, so no categories cached");

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void updateCategoryList(String filter) {
197197
.concatWith(
198198
searchAll(filter)
199199
.mergeWith(searchCategories(filter))
200-
.concatWith( TextUtils.isEmpty(filter)
200+
.concatWith(TextUtils.isEmpty(filter)
201201
? defaultCategories() : Observable.empty())
202202
)
203203
.filter(categoryItem -> !containsYear(categoryItem.getName()))

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
142142
public int update(@NonNull Uri uri, ContentValues contentValues, String selection,
143143
String[] selectionArgs) {
144144
/*
145-
SQL Injection warnings: First, note that we're not exposing this to the outside world (exported="false")
146-
Even then, we should make sure to sanitize all user input appropriately. Input that passes through ContentValues
145+
SQL Injection warnings: First, note that we're not exposing this to the
146+
outside world (exported="false"). Even then, we should make sure to sanitize
147+
all user input appropriately. Input that passes through ContentValues
147148
should be fine. So only issues are those that pass in via concating.
148149
149-
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
150+
In here, the only concat created argument is for id. It is cast to an int,
151+
and will error out otherwise.
150152
*/
151153
int uriType = uriMatcher.match(uri);
152154
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
154154
public int update(@NonNull Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
155155
/*
156156
SQL Injection warnings: First, note that we're not exposing this to the outside world (exported="false")
157-
Even then, we should make sure to sanitize all user input appropriately. Input that passes through ContentValues
158-
should be fine. So only issues are those that pass in via concating.
157+
Even then, we should make sure to sanitize all user input appropriately.
158+
Input that passes through ContentValuesshould be fine. So only issues are those that pass
159+
in via concating.
159160
160-
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
161+
In here, the only concat created argument is for id. It is cast to an int, and will
162+
error out otherwise.
161163
*/
162164
int uriType = uriMatcher.match(uri);
163165
CommonsApplication app = (CommonsApplication) getContext().getApplicationContext();

app/src/main/java/fr/free/nrw/commons/modifications/ModifierSequence.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ModifierSequence(Uri mediaUri) {
2727
public ModifierSequence(Uri mediaUri, JSONObject data) {
2828
this(mediaUri);
2929
JSONArray modifiersJSON = data.optJSONArray("modifiers");
30-
for(int i=0; i< modifiersJSON.length(); i++) {
30+
for (int i=0; i< modifiersJSON.length(); i++) {
3131
modifiers.add(PageModifier.fromJSON(modifiersJSON.optJSONObject(i)));
3232
}
3333
}
@@ -41,7 +41,7 @@ public void queueModifier(PageModifier modifier) {
4141
}
4242

4343
public String executeModifications(String pageName, String pageContents) {
44-
for(PageModifier modifier: modifiers) {
44+
for (PageModifier modifier: modifiers) {
4545
pageContents = modifier.doModification(pageName, pageContents);
4646
}
4747
return pageContents;
@@ -60,7 +60,7 @@ public JSONObject toJSON() {
6060
JSONObject data = new JSONObject();
6161
try {
6262
JSONArray modifiersJSON = new JSONArray();
63-
for(PageModifier modifier: modifiers) {
63+
for (PageModifier modifier: modifiers) {
6464
modifiersJSON.put(modifier.toJSON());
6565
}
6666
data.put("modifiers", modifiersJSON);
@@ -81,7 +81,8 @@ public static ModifierSequence fromCursor(Cursor cursor) {
8181
// Hardcoding column positions!
8282
ModifierSequence ms = null;
8383
try {
84-
ms = new ModifierSequence(Uri.parse(cursor.getString(1)), new JSONObject(cursor.getString(2)));
84+
ms = new ModifierSequence(Uri.parse(cursor.getString(1)),
85+
new JSONObject(cursor.getString(2)));
8586
} catch (JSONException e) {
8687
throw new RuntimeException(e);
8788
}

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public boolean logEvents(LogBuilder[] logBuilders) {
390390
public UploadResult uploadFile(String filename, InputStream file, long dataLength, String pageContents, String editSummary, final ProgressListener progressListener) throws IOException {
391391
ApiResult result = api.upload(filename, file, dataLength, pageContents, editSummary, progressListener::onProgress);
392392

393-
Log.e("WTF", "Result: "+result.toString());
393+
Log.e("WTF", "Result: " +result.toString());
394394

395395
String resultStatus = result.getString("/api/upload/@result");
396396
if (!resultStatus.equals("Success")) {

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

+50-22
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import android.Manifest;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.content.SharedPreferences;
67
import android.content.pm.PackageManager;
78
import android.location.LocationManager;
89
import android.net.Uri;
910
import android.os.AsyncTask;
1011
import android.os.Build;
1112
import android.os.Bundle;
13+
import android.preference.PreferenceManager;
1214
import android.support.annotation.NonNull;
1315
import android.support.v4.app.ActivityCompat;
1416
import android.support.v4.app.Fragment;
@@ -42,28 +44,46 @@ public class NearbyActivity extends NavigationBaseActivity {
4244

4345
@BindView(R.id.progressBar)
4446
ProgressBar progressBar;
45-
private boolean isMapViewActive = false;
4647
private static final int LOCATION_REQUEST = 1;
48+
private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed";
4749

4850
private LocationServiceManager locationManager;
4951
private LatLng curLatLang;
5052
private Bundle bundle;
5153
private NearbyAsyncTask nearbyAsyncTask;
54+
private SharedPreferences sharedPreferences;
55+
private NearbyActivityMode viewMode;
5256

5357
@Override
5458
protected void onCreate(Bundle savedInstanceState) {
5559
super.onCreate(savedInstanceState);
60+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
5661
setContentView(R.layout.activity_nearby);
5762
ButterKnife.bind(this);
5863
checkLocationPermission();
5964
bundle = new Bundle();
6065
initDrawer();
66+
initViewState();
67+
}
68+
69+
private void initViewState() {
70+
if (sharedPreferences.getBoolean(MAP_LAST_USED_PREFERENCE, false)) {
71+
viewMode = NearbyActivityMode.MAP;
72+
} else {
73+
viewMode = NearbyActivityMode.LIST;
74+
}
6175
}
6276

6377
@Override
6478
public boolean onCreateOptionsMenu(Menu menu) {
6579
MenuInflater inflater = getMenuInflater();
6680
inflater.inflate(R.menu.menu_nearby, menu);
81+
82+
if (viewMode.isMap()) {
83+
MenuItem item = menu.findItem(R.id.action_toggle_view);
84+
item.setIcon(viewMode.getIcon());
85+
}
86+
6787
return super.onCreateOptionsMenu(menu);
6888
}
6989

@@ -74,13 +94,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
7494
case R.id.action_refresh:
7595
refreshView();
7696
return true;
77-
case R.id.action_map:
78-
showMapView();
79-
if (isMapViewActive) {
80-
item.setIcon(R.drawable.ic_list_white_24dp);
81-
} else {
82-
item.setIcon(R.drawable.ic_map_white_24dp);
83-
}
97+
case R.id.action_toggle_view:
98+
viewMode = viewMode.toggle();
99+
item.setIcon(viewMode.getIcon());
100+
toggleView();
84101
return true;
85102
default:
86103
return super.onOptionsItemSelected(item);
@@ -158,15 +175,30 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
158175
if (progressBar != null) {
159176
progressBar.setVisibility(View.GONE);
160177
}
161-
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
162-
Fragment noPermissionsFragment = new NoPermissionsFragment();
163-
fragmentTransaction.replace(R.id.container, noPermissionsFragment);
164-
fragmentTransaction.commit();
178+
179+
showLocationPermissionDeniedErrorDialog();
165180
}
166181
}
167182
}
168183
}
169184

185+
private void showLocationPermissionDeniedErrorDialog() {
186+
new AlertDialog.Builder(this)
187+
.setMessage(R.string.nearby_needs_permissions)
188+
.setCancelable(false)
189+
.setPositiveButton(R.string.give_permission, (dialog, which) -> {
190+
//will ask for the location permission again
191+
checkLocationPermission();
192+
})
193+
.setNegativeButton(R.string.cancel, (dialog, which) -> {
194+
//dismiss dialog and finish activity
195+
dialog.cancel();
196+
finish();
197+
})
198+
.create()
199+
.show();
200+
}
201+
170202
private void checkGps() {
171203
LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
172204
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
@@ -198,20 +230,16 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
198230
}
199231
}
200232

201-
private void showMapView() {
233+
private void toggleView() {
202234
if (nearbyAsyncTask != null) {
203-
if (!isMapViewActive) {
204-
isMapViewActive = true;
205-
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
235+
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
236+
if (viewMode.isMap()) {
206237
setMapFragment();
207-
}
208-
209-
} else {
210-
isMapViewActive = false;
211-
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
238+
} else {
212239
setListFragment();
213240
}
214241
}
242+
sharedPreferences.edit().putBoolean(MAP_LAST_USED_PREFERENCE, viewMode.isMap()).apply();
215243
}
216244
}
217245

@@ -287,7 +315,7 @@ protected void onPostExecute(List<Place> placeList) {
287315
bundle.putString("CurLatLng", gsonCurLatLng);
288316

289317
// Begin the transaction
290-
if (isMapViewActive) {
318+
if (viewMode.isMap()) {
291319
setMapFragment();
292320
} else {
293321
setListFragment();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fr.free.nrw.commons.nearby;
2+
3+
import android.support.annotation.DrawableRes;
4+
5+
import fr.free.nrw.commons.R;
6+
7+
enum NearbyActivityMode {
8+
MAP(R.drawable.ic_list_white_24dp),
9+
LIST(R.drawable.ic_map_white_24dp);
10+
11+
@DrawableRes
12+
private final int icon;
13+
14+
NearbyActivityMode(int icon) {
15+
this.icon = icon;
16+
}
17+
18+
@DrawableRes
19+
public int getIcon() {
20+
return icon;
21+
}
22+
23+
public NearbyActivityMode toggle() {
24+
return isMap() ? LIST : MAP;
25+
}
26+
27+
public boolean isMap() {
28+
return MAP.equals(this);
29+
}
30+
}

0 commit comments

Comments
 (0)