[android] Remove usage of MwmApplication.get.

Removed from classes: SplashActivity, Statistics, StorageUtils, StreetAdapter, StringUtils.
This commit is contained in:
velichkomarija 2020-12-02 10:48:49 +03:00 committed by Alexey
parent 29e1a8d4df
commit 145e8cb10e
15 changed files with 35 additions and 78 deletions

View file

@ -274,7 +274,8 @@ public class DownloadResourcesLegacyActivity extends BaseMwmFragmentActivity
private void setDownloadMessage(int bytesToDownload)
{
mTvMessage.setText(getString(R.string.download_resources, StringUtils.getFileSizeString(bytesToDownload)));
mTvMessage.setText(getString(R.string.download_resources,
StringUtils.getFileSizeString(getApplicationContext(), bytesToDownload)));
}
private boolean prepareFilesDownload(boolean showMap)

View file

@ -173,6 +173,7 @@ public class MwmApplication extends Application implements AppBackgroundTracker.
mMediator = new ExternalLibrariesMediator(this);
mMediator.initSensitiveDataToleranceLibraries();
mMediator.initSensitiveDataStrictLibrariesAsync();
Statistics.INSTANCE.initialize(this);
Statistics.INSTANCE.setMediator(mMediator);
initNotificationChannels();
@ -240,9 +241,6 @@ public class MwmApplication extends Application implements AppBackgroundTracker.
Config.setStatisticsEnabled(SharedPropertiesUtils.isStatisticsEnabled(this));
@SuppressWarnings("unused")
Statistics s = Statistics.INSTANCE;
if (!isInstallationIdFound)
mMediator.setInstallationIdToCrashlytics();

View file

@ -236,7 +236,7 @@ public class SplashActivity extends AppCompatActivity
// If external permissions are still granted we just need to check platform
// and core initialization, because we may be in the recovering process,
// i.e. method onResume() may not be invoked in that case.
if (!MwmApplication.get().arePlatformAndCoreInitialized())
if (!MwmApplication.from(getApplicationContext()).arePlatformAndCoreInitialized())
{
init();
}

View file

@ -97,7 +97,7 @@ class BottomPanel
private void setUpdateAllState(UpdateInfo info)
{
mButton.setText(String.format(Locale.US, "%s (%s)", mFragment.getString(R.string.downloader_update_all_button),
StringUtils.getFileSizeString(info.totalSize)));
StringUtils.getFileSizeString(mFragment.requireContext(), info.totalSize)));
mButton.setOnClickListener(mUpdateListener);
}

View file

@ -139,7 +139,7 @@ public class CountrySuggestFragment extends BaseMwmFragment implements View.OnCl
mBtnDownloadMap.setText(String.format(Locale.US, "%1$s (%2$s)",
getString(R.string.downloader_download_map),
StringUtils.getFileSizeString(mCurrentCountry.totalSize)));
StringUtils.getFileSizeString(requireContext(), mCurrentCountry.totalSize)));
}
private void initViews(View view)

View file

@ -614,7 +614,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
size = ((!mSearchResultsMode && mMyMapsMode) ? mItem.size : mItem.totalSize);
}
mSize.setText(StringUtils.getFileSizeString(size));
mSize.setText(StringUtils.getFileSizeString(mFragment.requireContext(), size));
mStatusIcon.update(mItem);
}
}

View file

@ -166,7 +166,7 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener
}
else
{
sizeText = StringUtils.getFileSizeString(mCurrentCountry.totalSize);
sizeText = StringUtils.getFileSizeString(mActivity.getApplicationContext(), mCurrentCountry.totalSize);
if (shouldAutoDownload &&
Config.isAutodownloadEnabled() &&

View file

@ -181,7 +181,7 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment
final Bundle args = new Bundle();
args.putBoolean(ARG_UPDATE_IMMEDIATELY, result == Framework.DO_AFTER_UPDATE_AUTO_UPDATE);
args.putString(ARG_TOTAL_SIZE, StringUtils.getFileSizeString(info.totalSize));
args.putString(ARG_TOTAL_SIZE, StringUtils.getFileSizeString(activity.getApplicationContext(), info.totalSize));
args.putLong(ARG_TOTAL_SIZE_BYTES, info.totalSize);
args.putStringArray(ARG_OUTDATED_MAPS, Framework.nativeGetOutdatedCountries());
@ -379,7 +379,7 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment
{
return getString(R.string.downloader_percent, progress + "%",
localSize / Constants.MB + getString(R.string.mb),
StringUtils.getFileSizeString(remoteSize));
StringUtils.getFileSizeString(requireContext(), remoteSize));
}
void setProgress(int progress, long localSize, long remoteSize)
@ -405,7 +405,7 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment
void updateTotalSizes(long totalSize)
{
mTotalSize = StringUtils.getFileSizeString(totalSize);
mTotalSize = StringUtils.getFileSizeString(requireContext(), totalSize);
mTotalSizeBytes = totalSize;
}

View file

@ -63,7 +63,7 @@ public class StreetAdapter extends RecyclerView.Adapter<StreetAdapter.BaseViewHo
private void addStreet()
{
final Resources resources = MwmApplication.get().getResources();
final Resources resources = MwmApplication.from(mFragment.requireContext()).getResources();
EditTextDialogFragment.show(resources.getString(R.string.street), null,
resources.getString(R.string.ok),
resources.getString(R.string.cancel), mFragment);

View file

@ -106,7 +106,7 @@ abstract class BaseRoutingErrorDialogFragment extends BaseMwmDialogFragment
((TextView) countryView.findViewById(R.id.tv__title)).setText(map.name);
final TextView szView = (TextView) countryView.findViewById(R.id.tv__size);
szView.setText(StringUtils.getFileSizeString(map.totalSize));
szView.setText(StringUtils.getFileSizeString(requireContext(), map.totalSize));
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) szView.getLayoutParams();
lp.rightMargin = 0;
szView.setLayoutParams(lp);
@ -167,7 +167,7 @@ abstract class BaseRoutingErrorDialogFragment extends BaseMwmDialogFragment
Map<String, String> group = new HashMap<>();
group.put(GROUP_NAME, getString(R.string.maps) + " (" + mMissingMaps.size() + ") ");
group.put(GROUP_SIZE, StringUtils.getFileSizeString(size));
group.put(GROUP_SIZE, StringUtils.getFileSizeString(requireContext(), size));
List<Map<String, String>> groups = new ArrayList<>();
groups.add(group);

View file

@ -155,10 +155,7 @@ public class StoragePathManager
{
Set<String> pathsFromConfig = new HashSet<>();
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
StorageUtils.parseKitkatStorages(pathsFromConfig);
else
StorageUtils.parseStorages(pathsFromConfig);
StorageUtils.parseStorages(pathsFromConfig);
mItems.clear();

View file

@ -143,56 +143,6 @@ final class StorageUtils
parseMountFile("/proc/mounts", MOUNTS_MODE, paths);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("ResultOfMethodCallIgnored")
static void parseKitkatStorages(Set<String> paths)
{
final File primaryStorage = MwmApplication.get().getExternalFilesDir(null);
final File[] storages = MwmApplication.get().getExternalFilesDirs(null);
if (storages != null)
{
for (File f : storages)
{
// add only secondary dirs
if (f != null && !f.equals(primaryStorage))
{
LOGGER.i(StoragePathManager.TAG, "Additional storage path: " + f.getPath());
paths.add(f.getPath());
}
}
}
Set<String> testStorages = new HashSet<>();
parseStorages(testStorages);
String suffix = String.format(Constants.STORAGE_PATH, BuildConfig.APPLICATION_ID, Constants.FILES_DIR);
for (String testStorage : testStorages)
{
LOGGER.i(StoragePathManager.TAG, "Test storage from config files : " + testStorage);
if (isDirWritable(testStorage))
{
LOGGER.i(StoragePathManager.TAG, "Found writable storage : " + testStorage);
paths.add(testStorage);
}
else
{
testStorage += suffix;
File file = new File(testStorage);
if (!file.exists()) // create directory for our package if it isn't created by any reason
{
LOGGER.i(StoragePathManager.TAG, "Try to create MWM path");
if (file.mkdirs())
LOGGER.i(StoragePathManager.TAG, "Created!");
}
if (isDirWritable(testStorage))
{
LOGGER.i(StoragePathManager.TAG, "Found writable storage : " + testStorage);
paths.add(testStorage);
}
}
}
}
static void copyFile(File source, File dest) throws IOException
{
int maxChunkSize = 10 * Constants.MB; // move file by smaller chunks to avoid OOM.

View file

@ -2077,7 +2077,7 @@ public class PlacePageView extends NestedScrollViewClickFixed
mDownloaderIcon.update(country);
StringBuilder sb = new StringBuilder(StringUtils.getFileSizeString(country.totalSize));
StringBuilder sb = new StringBuilder(StringUtils.getFileSizeString(getContext(), country.totalSize));
if (country.isExpandable())
sb.append(String.format(Locale.US, " • %s: %d", getContext().getString(R.string.downloader_status_maps), country.totalChildCount));

View file

@ -1,5 +1,6 @@
package com.mapswithme.util;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Pair;
@ -52,10 +53,11 @@ public class StringUtils
* Formats size in bytes to "x MB" or "x.x GB" format.
* Small values rounded to 1 MB without fractions.
*
* @param context context for getString()
* @param size size in bytes
* @return formatted string
*/
public static String getFileSizeString(long size)
public static String getFileSizeString(@NonNull Context context, long size)
{
if (size < Constants.GB)
{
@ -63,11 +65,13 @@ public class StringUtils
if (value == 0)
value = 1;
return String.format(Locale.US, "%1$d %2$s", value, MwmApplication.get().getString(R.string.mb));
return String.format(Locale.US, "%1$d %2$s", value,
MwmApplication.from(context).getString(R.string.mb));
}
float value = ((float)size / Constants.GB);
return String.format(Locale.US, "%1$.1f %2$s", value, MwmApplication.get().getString(R.string.gb));
float value = ((float) size / Constants.GB);
return String.format(Locale.US, "%1$.1f %2$s", value,
MwmApplication.from(context).getString(R.string.gb));
}
public static boolean isRtl()

View file

@ -22,6 +22,7 @@ import com.mapswithme.maps.ads.MwmNativeAd;
import com.mapswithme.maps.ads.NativeAdError;
import com.mapswithme.maps.analytics.ExternalLibrariesMediator;
import com.mapswithme.maps.api.ParsedMwmRequest;
import com.mapswithme.maps.base.Initializable;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.MapObject;
@ -186,7 +187,7 @@ import static com.mapswithme.util.statistics.Statistics.ParamValue.TRUE;
import static com.mapswithme.util.statistics.Statistics.ParamValue.UNKNOWN;
import static com.mapswithme.util.statistics.Statistics.ParamValue.VEHICLE;
public enum Statistics
public enum Statistics implements Initializable<Context>
{
INSTANCE;
@ -888,11 +889,11 @@ public enum Statistics
// Initialized once in constructor and does not change until the process restarts.
// In this way we can correctly finish all statistics sessions and completely
// avoid their initialization if user has disabled statistics collection.
private final boolean mEnabled;
private boolean mEnabled;
Statistics()
@Override
public void initialize(@Nullable Context context)
{
final Context context = MwmApplication.get();
mEnabled = SharedPropertiesUtils.isStatisticsEnabled(context);
// At the moment we need special handling for Alohalytics to enable/disable logging of events in core C++ code.
if (mEnabled)
@ -902,6 +903,12 @@ public enum Statistics
configure(context);
}
@Override
public void destroy()
{
// No op.
}
@SuppressWarnings("NullableProblems")
@NonNull
private ExternalLibrariesMediator mMediator;