[android] Remove usage of MwmApplication.get.

Remove from classes: TransitionListener, Utils, ViralFragment, WorkerService.
This commit is contained in:
velichkomarija 2020-12-04 12:50:30 +03:00 committed by Aleksandr Zatsepin
parent 5f89ebb642
commit befcd292a4
10 changed files with 97 additions and 85 deletions

View file

@ -22,8 +22,11 @@ std::string Platform::UniqueClientId() const
{
JNIEnv * env = jni::GetEnv();
static jmethodID const getInstallationId = jni::GetStaticMethodID(env, g_utilsClazz, "getInstallationId",
"()Ljava/lang/String;");
static jstring const installationId = (jstring)env->CallStaticObjectMethod(g_utilsClazz, getInstallationId);
"(Landroid/content/Context;)"
"Ljava/lang/String;");
jobject context = android::Platform::Instance().GetContext();
static jstring const installationId
= static_cast<jstring>(env->CallStaticObjectMethod(g_utilsClazz, getInstallationId, context));
static std::string const result = jni::ToNativeString(env, installationId);
return result;
}
@ -48,9 +51,13 @@ std::string Platform::MacAddress(bool md5Decoded) const
{
JNIEnv * env = jni::GetEnv();
static jmethodID const getMacAddressMethod = jni::GetStaticMethodID(env, g_utilsClazz, "getMacAddress",
"(Landroid/content/Context;)"
"(Z)Ljava/lang/String;");
jstring const macAddr = static_cast<jstring>(env->CallStaticObjectMethod(g_utilsClazz, getMacAddressMethod,
static_cast<jboolean>(md5Decoded)));
jobject context = android::Platform::Instance().GetContext();
auto const macAddr = static_cast<jstring>(env->CallStaticObjectMethod(g_utilsClazz,
getMacAddressMethod,
context,
static_cast<jboolean>(md5Decoded)));
return jni::ToNativeString(env, macAddr);
}
@ -320,9 +327,12 @@ void Platform::AndroidSecureStorage::Save(std::string const & key, std::string c
Init(env);
static jmethodID const saveMethodId =
jni::GetStaticMethodID(env, m_secureStorageClass, "save", "(Ljava/lang/String;Ljava/lang/String;)V");
jni::GetStaticMethodID(env, m_secureStorageClass, "save",
"(Landroid/content/Context;Ljava/lang/String;"
"Ljava/lang/String;)V");
jobject context = android::Platform::Instance().GetContext();
env->CallStaticVoidMethod(m_secureStorageClass, saveMethodId,
context,
jni::TScopedLocalRef(env, jni::ToJavaString(env, key)).get(),
jni::TScopedLocalRef(env, jni::ToJavaString(env, value)).get());
}
@ -336,9 +346,12 @@ bool Platform::AndroidSecureStorage::Load(std::string const & key, std::string &
Init(env);
static jmethodID const loadMethodId =
jni::GetStaticMethodID(env, m_secureStorageClass, "load", "(Ljava/lang/String;)Ljava/lang/String;");
auto const resultString = static_cast<jstring>(env->CallStaticObjectMethod(m_secureStorageClass, loadMethodId,
jni::GetStaticMethodID(env, m_secureStorageClass, "load",
"(Landroid/content/Context;Ljava/lang/String;)"
"Ljava/lang/String;");
jobject context = android::Platform::Instance().GetContext();
auto const resultString = static_cast<jstring>(env->CallStaticObjectMethod(m_secureStorageClass,
loadMethodId, context,
jni::TScopedLocalRef(env, jni::ToJavaString(env, key)).get()));
if (resultString == nullptr)
return false;
@ -356,9 +369,10 @@ void Platform::AndroidSecureStorage::Remove(std::string const & key)
Init(env);
static jmethodID const removeMethodId =
jni::GetStaticMethodID(env, m_secureStorageClass, "remove", "(Ljava/lang/String;)V");
env->CallStaticVoidMethod(m_secureStorageClass, removeMethodId,
jni::GetStaticMethodID(env, m_secureStorageClass, "remove",
"(Landroid/content/Context;Ljava/lang/String;)V");
jobject context = android::Platform::Instance().GetContext();
env->CallStaticVoidMethod(m_secureStorageClass, removeMethodId, context,
jni::TScopedLocalRef(env, jni::ToJavaString(env, key)).get());
}

View file

@ -1,6 +1,7 @@
package com.mapswithme.maps;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
@ -268,7 +269,9 @@ public class DownloadResourcesLegacyActivity extends BaseMwmFragmentActivity
private void suggestRemoveLiteOrSamsung()
{
if (Utils.isPackageInstalled(Constants.Package.MWM_LITE_PACKAGE) || Utils.isPackageInstalled(Constants.Package.MWM_SAMSUNG_PACKAGE))
Context context = getApplicationContext();
if (Utils.isPackageInstalled(context, Constants.Package.MWM_LITE_PACKAGE) ||
Utils.isPackageInstalled(context, Constants.Package.MWM_SAMSUNG_PACKAGE))
Toast.makeText(this, R.string.suggest_uninstall_lite, Toast.LENGTH_LONG).show();
}

View file

@ -109,7 +109,7 @@ public class ExternalLibrariesMediator
if (!isCrashlyticsEnabled())
return false;
final String installationId = Utils.getInstallationId();
final String installationId = Utils.getInstallationId(mApplication);
// If installation id is not found this means id was not
// generated by alohalytics yet and it is a first run.
if (TextUtils.isEmpty(installationId))

View file

@ -22,9 +22,6 @@ public class WorkerService extends JobIntentService
private static final String ACTION_UPLOAD_OSM_CHANGES = "com.mapswithme.maps.action.upload_osm_changes";
private static final String ACTION_UPLOAD_UGC = "com.mapswithme.maps.action.upload_ugc";
private final boolean mArePlatformAndCoreInitialized =
MwmApplication.get().arePlatformAndCoreInitialized();
/**
* Starts this service to upload map edits to osm servers.
*/
@ -50,8 +47,9 @@ public class WorkerService extends JobIntentService
@Override
protected void onHandleWork(@NonNull Intent intent)
{
final Context context = getApplicationContext();
String msg = "onHandleIntent: " + intent + " app in background = "
+ !MwmApplication.backgroundTracker(getApplicationContext()).isForeground();
+ !MwmApplication.backgroundTracker(context).isForeground();
LOGGER.i(TAG, msg);
CrashlyticsUtils.INSTANCE.log(Log.INFO, TAG, msg);
final String action = intent.getAction();
@ -59,13 +57,13 @@ public class WorkerService extends JobIntentService
if (TextUtils.isEmpty(action))
return;
if (!mArePlatformAndCoreInitialized)
if (!MwmApplication.from(context).arePlatformAndCoreInitialized())
return;
switch (action)
{
case ACTION_UPLOAD_OSM_CHANGES:
handleActionUploadOsmChanges(getApplicationContext());
handleActionUploadOsmChanges(context);
break;
case ACTION_UPLOAD_UGC:

View file

@ -1,6 +1,5 @@
package com.mapswithme.maps.editor;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
@ -25,11 +24,6 @@ public class ViralFragment extends BaseMwmDialogFragment
{
private static final String EXTRA_CONGRATS_SHOWN = "CongratsShown";
private String mViralText;
private final String viralChangesMsg = MwmApplication.get().getString(R.string.editor_done_dialog_1);
private final String viralRatingMsg = MwmApplication.get().getString(R.string.editor_done_dialog_2, getUserEditorRank());
@Nullable
private Runnable mDismissListener;
@ -51,41 +45,42 @@ public class ViralFragment extends BaseMwmDialogFragment
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
MwmApplication.prefs(requireContext()).edit().putBoolean(EXTRA_CONGRATS_SHOWN, true).apply();
@SuppressLint("InflateParams")
final View root = inflater.inflate(R.layout.fragment_editor_viral, null);
TextView viralText = (TextView) root.findViewById(R.id.viral);
initViralText();
viralText.setText(mViralText);
root.findViewById(R.id.tell_friend).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
share();
dismiss();
if (mDismissListener != null)
mDismissListener.run();
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_SHARE_CLICK);
}
});
root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dismiss();
if (mDismissListener != null)
mDismissListener.run();
}
});
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_SHARE_SHOW,
Statistics.params().add("showed", mViralText.equals(viralChangesMsg) ? "change" : "rating"));
return root;
return inflater.inflate(R.layout.fragment_editor_viral, null);
}
@Override
public void onCancel(DialogInterface dialog)
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
TextView viralTextView = (TextView) view.findViewById(R.id.viral);
Context context = requireContext();
String viralChangesMsg = context.getString(R.string.editor_done_dialog_1);
String viralRatingMsg = context.getString(R.string.editor_done_dialog_2, getUserEditorRank());
String viralText = new Random().nextBoolean() ? viralChangesMsg : viralRatingMsg;
viralTextView.setText(viralText);
view.findViewById(R.id.tell_friend).setOnClickListener(v -> {
share();
dismiss();
if (mDismissListener != null)
mDismissListener.run();
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_SHARE_CLICK);
});
view.findViewById(R.id.close).setOnClickListener(v -> {
dismiss();
if (mDismissListener != null)
mDismissListener.run();
});
Statistics.INSTANCE.trackEvent(Statistics.EventName.EDITOR_SHARE_SHOW,
Statistics.params()
.add("showed", viralText.equals(viralChangesMsg) ? "change" : "rating"));
}
@Override
public void onCancel(@NonNull DialogInterface dialog)
{
super.onCancel(dialog);
if (mDismissListener != null)
@ -102,11 +97,6 @@ public class ViralFragment extends BaseMwmDialogFragment
SharingHelper.shareViralEditor(getActivity(), R.drawable.img_sharing_editor, R.string.editor_sharing_title, R.string.whatsnew_editor_message_1);
}
private void initViralText()
{
mViralText = new Random().nextBoolean() ? viralChangesMsg : viralRatingMsg;
}
// Counts fake rank in the rating of editors.
private static int getUserEditorRank()
{

View file

@ -47,9 +47,6 @@ public enum LocationHelper implements Initializable<Context>
@NonNull
private Context mContext;
@NonNull
private final TransitionListener mOnTransition = new TransitionListener();
@NonNull
private final LocationListener mCoreLocationListener = new LocationListener()
{
@ -163,7 +160,8 @@ public enum LocationHelper implements Initializable<Context>
initProvider();
LocationState.nativeSetListener(mMyPositionModeListener);
LocationState.nativeSetLocationPendingTimeoutListener(mLocationPendingTimeoutListener);
MwmApplication.backgroundTracker(context).addListener(mOnTransition);
TransitionListener transitionListener = new TransitionListener(mContext);
MwmApplication.backgroundTracker(context).addListener(transitionListener);
}
@Override

View file

@ -1,5 +1,6 @@
package com.mapswithme.maps.location;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
@ -13,6 +14,14 @@ class TransitionListener implements AppBackgroundTracker.OnTransitionListener
@NonNull
private final GPSCheck mReceiver = new GPSCheck();
private boolean mReceiverRegistered;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private final Context mContext;
public TransitionListener(@NonNull Context context)
{
mContext = context;
}
@Override
public void onTransit(boolean foreground)
@ -23,14 +32,14 @@ class TransitionListener implements AppBackgroundTracker.OnTransitionListener
filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addCategory(Intent.CATEGORY_DEFAULT);
MwmApplication.get().registerReceiver(mReceiver, filter);
MwmApplication.from(mContext).registerReceiver(mReceiver, filter);
mReceiverRegistered = true;
return;
}
if (!foreground && mReceiverRegistered)
{
MwmApplication.get().unregisterReceiver(mReceiver);
MwmApplication.from(mContext).unregisterReceiver(mReceiver);
mReceiverRegistered = false;
}
}

View file

@ -13,27 +13,28 @@ public final class SecureStorage
{
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = SecureStorage.class.getSimpleName();
private static final SharedPreferences mPrefs
= MwmApplication.get().getSharedPreferences("secure", Context.MODE_PRIVATE);
private SecureStorage() {}
public static void save(@NonNull String key, @NonNull String value)
public static void save(@NonNull Context context, @NonNull String key, @NonNull String value)
{
LOGGER.d(TAG, "save: key = " + key);
mPrefs.edit().putString(key, value).apply();
SharedPreferences prefs = context.getSharedPreferences("secure", Context.MODE_PRIVATE);
prefs.edit().putString(key, value).apply();
}
@Nullable
public static String load(@NonNull String key)
public static String load(@NonNull Context context, @NonNull String key)
{
LOGGER.d(TAG, "load: key = " + key);
return mPrefs.getString(key, null);
SharedPreferences prefs = context.getSharedPreferences("secure", Context.MODE_PRIVATE);
return prefs.getString(key, null);
}
public static void remove(@NonNull String key)
public static void remove(@NonNull Context context, @NonNull String key)
{
LOGGER.d(TAG, "remove: key = " + key);
mPrefs.edit().remove(key).apply();
SharedPreferences prefs = context.getSharedPreferences("secure", Context.MODE_PRIVATE);
prefs.edit().remove(key).apply();
}
}

View file

@ -164,9 +164,9 @@ public class Utils
return "[" + joined + "]";
}
public static boolean isPackageInstalled(String packageUri)
public static boolean isPackageInstalled(@NonNull Context context, String packageUri)
{
PackageManager pm = MwmApplication.get().getPackageManager();
PackageManager pm = context.getPackageManager();
boolean installed;
try
{
@ -380,11 +380,11 @@ public class Utils
}).show();
}
public static String getInstallationId()
@NonNull
public static String getInstallationId(@NonNull Context context)
{
final Context context = MwmApplication.get();
final SharedPreferences sharedPrefs = context.getSharedPreferences(
org.alohalytics.Statistics.PREF_FILE, Context.MODE_PRIVATE);
org.alohalytics.Statistics.PREF_FILE, Context.MODE_PRIVATE);
// "UNIQUE_ID" is the value of org.alohalytics.Statistics.PREF_UNIQUE_ID, but it private.
String installationId = sharedPrefs.getString("UNIQUE_ID", null);
@ -403,9 +403,8 @@ public class Utils
}
@NonNull
public static String getMacAddress(boolean md5Decoded)
public static String getMacAddress(@NonNull Context context, boolean md5Decoded)
{
final Context context = MwmApplication.get();
byte[] macBytes = null;
String address = "";
try

View file

@ -179,7 +179,7 @@ class FileLoggerStrategy implements LoggerStrategy
fw.write("Android version: " + Build.VERSION.SDK_INT + "\n");
fw.write("Device: " + Utils.getFullDeviceModel() + "\n");
fw.write("App version: " + BuildConfig.APPLICATION_ID + " " + BuildConfig.VERSION_NAME + "\n");
fw.write("Installation ID: " + Utils.getInstallationId() + "\n");
fw.write("Installation ID: " + Utils.getInstallationId(application) + "\n");
fw.write("Locale : " + Locale.getDefault());
fw.write("\nNetworks : ");
final ConnectivityManager manager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);