[android] Remove MwmApplication.prefs from OsmOAuth.

This commit is contained in:
velichkomarija 2020-11-29 23:56:54 +03:00 committed by Aleksandr Zatsepin
parent 3aab1663d8
commit c6d7f6461c
8 changed files with 49 additions and 37 deletions

View file

@ -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));

View file

@ -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()

View file

@ -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();

View file

@ -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;

View file

@ -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,

View file

@ -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)

View file

@ -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);

View file

@ -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())));
}