From c6d7f6461c2d6434f888b1e26791df8cb4eb6367 Mon Sep 17 00:00:00 2001 From: velichkomarija Date: Sun, 29 Nov 2020 23:56:54 +0300 Subject: [PATCH] [android] Remove MwmApplication.prefs from OsmOAuth. --- .../src/com/mapswithme/maps/MwmActivity.java | 7 +++-- .../maps/background/WorkerService.java | 6 ++-- .../com/mapswithme/maps/editor/Editor.java | 8 ++--- .../maps/editor/EditorHostFragment.java | 10 +++++-- .../maps/editor/OsmAuthFragmentDelegate.java | 2 +- .../com/mapswithme/maps/editor/OsmOAuth.java | 30 +++++++++++-------- .../maps/editor/ProfileFragment.java | 11 +++---- .../util/statistics/Statistics.java | 12 ++++---- 8 files changed, 49 insertions(+), 37 deletions(-) diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index cbbdd01c92..7642d91dca 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -66,6 +66,7 @@ import com.mapswithme.maps.editor.Editor; import com.mapswithme.maps.editor.EditorActivity; import com.mapswithme.maps.editor.EditorHostFragment; import com.mapswithme.maps.editor.FeatureCategoryActivity; +import com.mapswithme.maps.editor.OsmOAuth; import com.mapswithme.maps.editor.ReportFragment; import com.mapswithme.maps.gallery.Items; import com.mapswithme.maps.guides.GuidesGalleryListener; @@ -489,7 +490,8 @@ public class MwmActivity extends BaseMwmFragmentActivity { // TODO(yunikkk) think about refactoring. It probably should be called in editor. Editor.nativeStartEdit(); - Statistics.INSTANCE.trackEditorLaunch(false); + Statistics.INSTANCE.trackEditorLaunch(false, + String.valueOf(OsmOAuth.isAuthorized(getApplicationContext()))); if (mIsTabletLayout) replaceFragment(EditorHostFragment.class, null, null); else @@ -754,7 +756,8 @@ public class MwmActivity extends BaseMwmFragmentActivity mPositionChooser.findViewById(R.id.done).setOnClickListener( v -> { - Statistics.INSTANCE.trackEditorLaunch(true); + Statistics.INSTANCE.trackEditorLaunch(true, + String.valueOf(OsmOAuth.isAuthorized(getApplicationContext()))); hidePositionChooser(); if (Framework.nativeIsDownloadedMapAtScreenCenter()) startActivity(new Intent(MwmActivity.this, FeatureCategoryActivity.class)); diff --git a/android/src/com/mapswithme/maps/background/WorkerService.java b/android/src/com/mapswithme/maps/background/WorkerService.java index f4c96e7729..5624eda2cc 100644 --- a/android/src/com/mapswithme/maps/background/WorkerService.java +++ b/android/src/com/mapswithme/maps/background/WorkerService.java @@ -65,7 +65,7 @@ public class WorkerService extends JobIntentService switch (action) { case ACTION_UPLOAD_OSM_CHANGES: - handleActionUploadOsmChanges(); + handleActionUploadOsmChanges(getApplicationContext()); break; case ACTION_UPLOAD_UGC: @@ -74,9 +74,9 @@ public class WorkerService extends JobIntentService } } - private static void handleActionUploadOsmChanges() + private static void handleActionUploadOsmChanges(@NonNull Context context) { - Editor.uploadChanges(); + Editor.uploadChanges(context); } private static void handleUploadUGC() diff --git a/android/src/com/mapswithme/maps/editor/Editor.java b/android/src/com/mapswithme/maps/editor/Editor.java index c0d0494212..f2a28b7960 100644 --- a/android/src/com/mapswithme/maps/editor/Editor.java +++ b/android/src/com/mapswithme/maps/editor/Editor.java @@ -52,11 +52,11 @@ public final class Editor } @WorkerThread - public static void uploadChanges() + public static void uploadChanges(@NonNull Context context) { - if (nativeHasSomethingToUpload() && OsmOAuth.isAuthorized()) - nativeUploadChanges(OsmOAuth.getAuthToken(), OsmOAuth.getAuthSecret(), BuildConfig.VERSION_NAME, - BuildConfig.APPLICATION_ID); + if (nativeHasSomethingToUpload() && OsmOAuth.isAuthorized(context)) + nativeUploadChanges(OsmOAuth.getAuthToken(context), OsmOAuth.getAuthSecret(context), + BuildConfig.VERSION_NAME, BuildConfig.APPLICATION_ID); } public static native boolean nativeShouldShowEditPlace(); diff --git a/android/src/com/mapswithme/maps/editor/EditorHostFragment.java b/android/src/com/mapswithme/maps/editor/EditorHostFragment.java index 52644149e5..c5278b308c 100644 --- a/android/src/com/mapswithme/maps/editor/EditorHostFragment.java +++ b/android/src/com/mapswithme/maps/editor/EditorHostFragment.java @@ -1,6 +1,7 @@ package com.mapswithme.maps.editor; import android.app.Activity; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; @@ -315,14 +316,17 @@ public class EditorHostFragment extends BaseMwmToolbarFragment private void processNoFeatures() { - Statistics.INSTANCE.trackEditorError(mIsNewObject); + Statistics.INSTANCE.trackEditorError(mIsNewObject, + String.valueOf(OsmOAuth.isAuthorized(requireContext()))); DialogUtils.showAlertDialog(getActivity(), R.string.downloader_no_space_title); } private void processEditedFeatures() { - Statistics.INSTANCE.trackEditorSuccess(mIsNewObject); - if (OsmOAuth.isAuthorized() || !ConnectionState.isConnected()) + Context context = requireContext(); + Statistics.INSTANCE.trackEditorSuccess(mIsNewObject, + String.valueOf(OsmOAuth.isAuthorized(context))); + if (OsmOAuth.isAuthorized(context) || !ConnectionState.isConnected()) { Utils.navigateToParent(getActivity()); return; diff --git a/android/src/com/mapswithme/maps/editor/OsmAuthFragmentDelegate.java b/android/src/com/mapswithme/maps/editor/OsmAuthFragmentDelegate.java index 38716b4732..7cec96f239 100644 --- a/android/src/com/mapswithme/maps/editor/OsmAuthFragmentDelegate.java +++ b/android/src/com/mapswithme/maps/editor/OsmAuthFragmentDelegate.java @@ -70,7 +70,7 @@ public abstract class OsmAuthFragmentDelegate implements View.OnClickListener return; } - OsmOAuth.setAuthorization(auth[0], auth[1], username); + OsmOAuth.setAuthorization(mFragment.requireContext(), auth[0], auth[1], username); if (mFragment.isAdded()) Utils.navigateToParent(mFragment.getActivity()); Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT, diff --git a/android/src/com/mapswithme/maps/editor/OsmOAuth.java b/android/src/com/mapswithme/maps/editor/OsmOAuth.java index 22728f543c..bab859f83c 100644 --- a/android/src/com/mapswithme/maps/editor/OsmOAuth.java +++ b/android/src/com/mapswithme/maps/editor/OsmOAuth.java @@ -1,6 +1,9 @@ package com.mapswithme.maps.editor; +import android.content.Context; + import androidx.annotation.IntDef; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.Size; import androidx.annotation.WorkerThread; @@ -71,39 +74,40 @@ public final class OsmOAuth public static final String URL_PARAM_VERIFIER = "oauth_verifier"; - public static boolean isAuthorized() + public static boolean isAuthorized(@NonNull Context context) { - return MwmApplication.prefs().contains(PREF_OSM_TOKEN) && - MwmApplication.prefs().contains(PREF_OSM_SECRET); + return MwmApplication.prefs(context).contains(PREF_OSM_TOKEN) && + MwmApplication.prefs(context).contains(PREF_OSM_SECRET); } - public static String getAuthToken() + public static String getAuthToken(@NonNull Context context) { - return MwmApplication.prefs().getString(PREF_OSM_TOKEN, ""); + return MwmApplication.prefs(context).getString(PREF_OSM_TOKEN, ""); } - public static String getAuthSecret() + public static String getAuthSecret(@NonNull Context context) { - return MwmApplication.prefs().getString(PREF_OSM_SECRET, ""); + return MwmApplication.prefs(context).getString(PREF_OSM_SECRET, ""); } - public static String getUsername() + public static String getUsername(@NonNull Context context) { - return MwmApplication.prefs().getString(PREF_OSM_USERNAME, ""); + return MwmApplication.prefs(context).getString(PREF_OSM_USERNAME, ""); } - public static void setAuthorization(String token, String secret, String username) + public static void setAuthorization(@NonNull Context context, String token, + String secret, String username) { - MwmApplication.prefs().edit() + MwmApplication.prefs(context).edit() .putString(PREF_OSM_TOKEN, token) .putString(PREF_OSM_SECRET, secret) .putString(PREF_OSM_USERNAME, username) .apply(); } - public static void clearAuthorization() + public static void clearAuthorization(@NonNull Context context) { - MwmApplication.prefs().edit() + MwmApplication.prefs(context).edit() .remove(PREF_OSM_TOKEN) .remove(PREF_OSM_SECRET) .remove(PREF_OSM_USERNAME) diff --git a/android/src/com/mapswithme/maps/editor/ProfileFragment.java b/android/src/com/mapswithme/maps/editor/ProfileFragment.java index 45b44cf3b0..be05a58c08 100644 --- a/android/src/com/mapswithme/maps/editor/ProfileFragment.java +++ b/android/src/com/mapswithme/maps/editor/ProfileFragment.java @@ -1,5 +1,6 @@ package com.mapswithme.maps.editor; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; @@ -41,14 +42,14 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene @Override void invoke(final ProfileFragment fragment) { - new AlertDialog.Builder(fragment.getContext()) + new AlertDialog.Builder(fragment.requireContext()) .setMessage(R.string.are_you_sure) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - OsmOAuth.clearAuthorization(); + OsmOAuth.clearAuthorization(fragment.requireContext()); fragment.refreshViews(); } }) @@ -63,7 +64,7 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene @Override void invoke(ProfileFragment fragment) { - OsmOAuth.nativeUpdateOsmUserStats(OsmOAuth.getUsername(), true /* forceUpdate */); + OsmOAuth.nativeUpdateOsmUserStats(OsmOAuth.getUsername(fragment.requireContext()), true /* forceUpdate */); } }; @@ -87,7 +88,7 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene initViews(view); refreshViews(); OsmOAuth.setUserStatsListener(this); - OsmOAuth.nativeUpdateOsmUserStats(OsmOAuth.getUsername(), false /* forceUpdate */); + OsmOAuth.nativeUpdateOsmUserStats(OsmOAuth.getUsername(requireContext()), false /* forceUpdate */); } private void initViews(View view) @@ -109,7 +110,7 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene private void refreshViews() { - if (OsmOAuth.isAuthorized()) + if (OsmOAuth.isAuthorized(requireContext())) { UiUtils.show(mMore, mRatingBlock, mSentBlock); UiUtils.hide(mAuthBlock); diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java index 7b903618aa..237d787df3 100644 --- a/android/src/com/mapswithme/util/statistics/Statistics.java +++ b/android/src/com/mapswithme/util/statistics/Statistics.java @@ -1083,10 +1083,10 @@ public enum Statistics .add(EventParam.TO, Statistics.getPointType(to))); } - public void trackEditorLaunch(boolean newObject) + public void trackEditorLaunch(boolean newObject, String valueOfIsAuthorized) { trackEvent(newObject ? EventName.EDITOR_START_CREATE : EventName.EDITOR_START_EDIT, - editorMwmParams().add(EventParam.IS_AUTHENTICATED, String.valueOf(OsmOAuth.isAuthorized())) + editorMwmParams().add(EventParam.IS_AUTHENTICATED, valueOfIsAuthorized) .add(EventParam.IS_ONLINE, String.valueOf(ConnectionState.isConnected()))); if (newObject) @@ -1102,17 +1102,17 @@ public enum Statistics trackEvent(EventName.MAP_LAYERS_CLICK, builder); } - public void trackEditorSuccess(boolean newObject) + public void trackEditorSuccess(boolean newObject, String valueOfIsAuthorized) { trackEvent(newObject ? EventName.EDITOR_SUCCESS_CREATE : EventName.EDITOR_SUCCESS_EDIT, - editorMwmParams().add(EventParam.IS_AUTHENTICATED, String.valueOf(OsmOAuth.isAuthorized())) + editorMwmParams().add(EventParam.IS_AUTHENTICATED, valueOfIsAuthorized) .add(EventParam.IS_ONLINE, String.valueOf(ConnectionState.isConnected()))); } - public void trackEditorError(boolean newObject) + public void trackEditorError(boolean newObject, String valueOfIsAuthorized) { trackEvent(newObject ? EventName.EDITOR_ERROR_CREATE : EventName.EDITOR_ERROR_EDIT, - editorMwmParams().add(EventParam.IS_AUTHENTICATED, String.valueOf(OsmOAuth.isAuthorized())) + editorMwmParams().add(EventParam.IS_AUTHENTICATED, valueOfIsAuthorized) .add(EventParam.IS_ONLINE, String.valueOf(ConnectionState.isConnected()))); }