Fix check for multi-user storage

This commit is contained in:
Vitaliy 2021-04-20 17:36:32 +03:00
parent 658b157bb2
commit 2e9238aff8
9 changed files with 56 additions and 106 deletions

View file

@ -22,6 +22,8 @@ import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.StatFs;
@ -264,6 +266,11 @@ public class AndroidUtils {
return "";
}
public static String getFreeSpace(Context ctx, File dir) {
long size = AndroidUtils.getAvailableSpace(dir);
return AndroidUtils.formatSize(ctx, size);
}
public static View findParentViewById(View view, int id) {
ViewParent viewParent = view.getParent();
@ -856,11 +863,39 @@ public class AndroidUtils {
return result;
}
public static long getAvailableSpace(@NonNull OsmandApplication app) {
return getAvailableSpace(app.getAppPath(null));
}
public static long getTotalSpace(@NonNull OsmandApplication app) {
return getTotalSpace(app.getAppPath(null));
}
public static long getAvailableSpace(@Nullable File dir) {
if (dir != null && dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
return fs.getAvailableBlocksLong() * fs.getBlockSize();
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
return fs.getAvailableBlocksLong() * fs.getBlockSizeLong();
} else {
return fs.getAvailableBlocks() * fs.getBlockSize();
}
} catch (IllegalArgumentException e) {
LOG.error(e);
}
}
return -1;
}
public static long getTotalSpace(@Nullable File dir) {
if (dir != null && dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
return fs.getBlockCountLong() * fs.getBlockSizeLong();
} else {
return fs.getBlockCount() * fs.getBlockSize();
}
} catch (IllegalArgumentException e) {
LOG.error(e);
}
@ -887,13 +922,6 @@ public class AndroidUtils {
return -1;
}
public static float getUsedSpaceGb(File dir) {
if (dir.canRead()) {
return getTotalSpaceGb(dir) - getFreeSpaceGb(dir);
}
return -1;
}
public static CharSequence getStyledString(CharSequence baseString, CharSequence stringToInsertAndStyle,
CharacterStyle baseStyle, CharacterStyle replaceStyle) {
int indexOfPlaceholder = baseString.toString().indexOf(STRING_PLACEHOLDER);

View file

@ -22,7 +22,6 @@ import android.media.MediaRecorder;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.StatFs;
import android.provider.MediaStore;
import android.view.Display;
import android.view.KeyEvent;
@ -1607,13 +1606,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
double bitrate = (((p.videoBitRate + p.audioBitRate) / 8f) * 60f) / (1 << 30); // gigabytes per minute
double clipSpace = bitrate * AV_RS_CLIP_LENGTH.get();
double storageSize = AV_RS_STORAGE_SIZE.get();
double availableSpace = storageSize;
File dir = app.getAppPath("").getParentFile();
if (dir.canRead()) {
StatFs fs = new StatFs(dir.getAbsolutePath());
availableSpace = (double) (fs.getAvailableBlocks()) * fs.getBlockSize() / (1 << 30) - clipSpace;
}
double availableSpace = (double) AndroidUtils.getAvailableSpace(app) / (1 << 30) - clipSpace;
if (usedSpace + clipSpace > storageSize || clipSpace > availableSpace) {
Arrays.sort(files, new Comparator<File>() {

View file

@ -11,7 +11,6 @@ import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
@ -42,7 +41,6 @@ import net.osmand.plus.widgets.style.CustomTypefaceSpan;
import org.apache.commons.logging.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@ -381,16 +379,7 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
private void setupStorageSizePref(AudioVideoNotesPlugin plugin) {
ListPreferenceEx storageSize = (ListPreferenceEx) findPreference(plugin.AV_RS_STORAGE_SIZE.getId());
File dir = app.getAppPath("").getParentFile();
long size = 0;
if (dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
size = ((long) fs.getBlockSize() * (long) fs.getBlockCount()) / (1 << 30);
} catch (IllegalArgumentException e) {
log.error(e);
}
}
long size = AndroidUtils.getTotalSpace(app) / (1 << 30);
if (size > 0) {
int value = 1;
ArrayList<Integer> gbList = new ArrayList<>();

View file

@ -11,7 +11,6 @@ import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@ -34,10 +33,10 @@ import net.osmand.FileUtils;
import net.osmand.PlatformUtil;
import net.osmand.ValueHolder;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.ProgressImplementation;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -93,18 +92,6 @@ public class DashChooseAppDirFragment {
selectePathTemp = null;
}
private String getFreeSpace(File dir) {
if (dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
return AndroidUtils.formatSize(activity, (long) fs.getAvailableBlocks() * fs.getBlockSize());
} catch (IllegalArgumentException e) {
LOG.error(e);
}
}
return "";
}
public void updateView() {
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_INTERNAL_FILE) {
locationPath.setText(R.string.storage_directory_internal_app);
@ -117,7 +104,7 @@ public class DashChooseAppDirFragment {
} else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) {
locationPath.setText(R.string.storage_directory_manual);
}
locationDesc.setText(selectedFile.getAbsolutePath() + " \u2022 " + getFreeSpace(selectedFile));
locationDesc.setText(selectedFile.getAbsolutePath() + " \u2022 " + AndroidUtils.getFreeSpace(activity, selectedFile));
boolean copyFiles = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()) && !mapsCopied;
warningReadonly.setVisibility(copyFiles ? View.VISIBLE : View.GONE);
if (copyFiles) {

View file

@ -652,7 +652,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
TextView messageTextView = (TextView) view.findViewById(R.id.leftTextView);
ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.progressBar);
File dir = activity.getMyApplication().getAppPath("").getParentFile();
File dir = activity.getMyApplication().getAppPath(null);
String size = "";
int percent = 0;
if (dir.canRead()) {

View file

@ -9,7 +9,6 @@ import android.net.TrafficStats;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.StatFs;
import android.view.View;
import android.widget.Toast;
@ -17,13 +16,12 @@ import androidx.annotation.UiThread;
import androidx.appcompat.app.AlertDialog;
import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.map.WorldRegion;
import net.osmand.map.WorldRegion.RegionParams;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.OsmandPreference;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.base.BasicProgressAsyncTask;
@ -31,6 +29,8 @@ import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning;
import net.osmand.plus.helpers.DatabaseHelper;
import net.osmand.plus.notifications.OsmandNotification;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.settings.backend.OsmandPreference;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -302,19 +302,8 @@ public class DownloadIndexesThread {
return null;
}
@SuppressWarnings("deprecation")
public double getAvailableSpace() {
File dir = app.getAppPath("").getParentFile();
double asz = -1;
if (dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
asz = (((long) fs.getAvailableBlocks()) * fs.getBlockSize()) / (1 << 20);
} catch (IllegalArgumentException e) {
LOG.error(e);
}
}
return asz;
return AndroidUtils.getAvailableSpace(app) / (1 << 20);
}
/// PRIVATE IMPL

View file

@ -5,7 +5,6 @@ import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
@ -17,8 +16,6 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.ibm.icu.impl.IllegalIcuArgumentException;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
@ -28,12 +25,12 @@ import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import net.osmand.plus.OnDismissDialogFragmentListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.dashboard.DashChooseAppDirFragment;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.settings.backend.OsmandSettings;
import org.apache.commons.logging.Log;
@ -119,7 +116,7 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
deviceStorageImageView.setImageDrawable(getContentIcon(R.drawable.ic_action_phone));
TextView deviceStorageDescription = (TextView) view.findViewById(R.id.deviceMemoryDescription);
deviceStorageDescription.setText(deviceStorageName);
deviceStorageDescription.setText(getFreeSpace(deviceStorage));
deviceStorageDescription.setText(AndroidUtils.getFreeSpace(activity, deviceStorage));
View sharedMemoryRow = view.findViewById(R.id.sharedMemoryRow);
if (hasExternalStoragePermission && sharedStorage != null) {
@ -127,7 +124,7 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
ImageView sharedMemoryImageView = (ImageView) view.findViewById(R.id.sharedMemoryImageView);
sharedMemoryImageView.setImageDrawable(getContentIcon(R.drawable.ic_action_phone));
TextView sharedMemoryDescription = (TextView) view.findViewById(R.id.sharedMemoryDescription);
sharedMemoryDescription.setText(getFreeSpace(sharedStorage));
sharedMemoryDescription.setText(AndroidUtils.getFreeSpace(activity, sharedStorage));
} else {
view.findViewById(R.id.divSharedStorage).setVisibility(View.GONE);
sharedMemoryRow.setVisibility(View.GONE);
@ -139,7 +136,7 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
ImageView memoryStickImageView = (ImageView) view.findViewById(R.id.memoryStickImageView);
memoryStickImageView.setImageDrawable(getContentIcon(R.drawable.ic_sdcard));
TextView memoryStickDescription = (TextView) view.findViewById(R.id.memoryStickDescription);
memoryStickDescription.setText(getFreeSpace(cardStorage));
memoryStickDescription.setText(AndroidUtils.getFreeSpace(activity, cardStorage));
} else {
view.findViewById(R.id.divExtStorage).setVisibility(View.GONE);
memoryStickRow.setVisibility(View.GONE);
@ -192,23 +189,6 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
.getDefaultInternalStorage();
}
private String getFreeSpace(File dir) {
String sz = "";
if (dir != null && dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
@SuppressWarnings("deprecation")
long size = (long) fs.getAvailableBlocks() * fs.getBlockSize();
if (size > 0) {
sz = AndroidUtils.formatSize(getActivity(), size);
}
} catch (IllegalIcuArgumentException e) {
LOG.error(e);
}
}
return sz;
}
private void checkAssets() {
getMyApplication().getResourceManager().checkAssets(IProgress.EMPTY_PROGRESS, true);
}

View file

@ -5,7 +5,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.LayoutInflater;
@ -37,7 +36,6 @@ import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity;
@ -51,12 +49,12 @@ import net.osmand.plus.download.IndexItem;
import net.osmand.plus.download.ui.DataStoragePlaceDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
@ -697,7 +695,7 @@ public class FirstUsageWizardFragment extends BaseOsmAndFragment implements OsmA
TextView freeSpaceValue = (TextView) storageView.findViewById(R.id.storage_free_space_value);
String freeSpaceStr = getString(R.string.storage_free_space) + ": ";
freeSpace.setText(freeSpaceStr);
freeSpaceValue.setText(getFreeSpace(settings.getExternalStorageDirectory()));
freeSpaceValue.setText(AndroidUtils.getFreeSpace(storageView.getContext(), settings.getExternalStorageDirectory()));
AppCompatButton changeStorageButton = (AppCompatButton) storageView.findViewById(R.id.storage_change_button);
if (wizardType == WizardType.MAP_DOWNLOAD) {
@ -737,18 +735,6 @@ public class FirstUsageWizardFragment extends BaseOsmAndFragment implements OsmA
}
}
private String getFreeSpace(File dir) {
if (dir.canRead()) {
try {
StatFs fs = new StatFs(dir.getAbsolutePath());
return AndroidUtils.formatSize(getActivity(), (long) fs.getAvailableBlocks() * fs.getBlockSize());
} catch (IllegalArgumentException e) {
LOG.error(e);
}
}
return "";
}
public static void showSearchLocationFragment(FragmentActivity activity, boolean searchByIp) {
Fragment fragment = new FirstUsageWizardFragment();
Bundle args = new Bundle();

View file

@ -36,7 +36,6 @@ import net.osmand.plus.settings.fragments.ExportSettingsAdapter.OnItemSelectedLi
import net.osmand.plus.widgets.TextViewEx;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.LinkedHashMap;
@ -221,8 +220,7 @@ public abstract class BaseSettingsListFragment extends BaseOsmAndFragment implem
if (calculatedSize != 0) {
selectedItemsSize.setText(AndroidUtils.formatSize(app, calculatedSize));
File dir = app.getAppPath("").getParentFile();
long availableSizeBytes = AndroidUtils.getAvailableSpace(dir);
long availableSizeBytes = AndroidUtils.getAvailableSpace(app);
if (calculatedSize > availableSizeBytes) {
String availableSize = AndroidUtils.formatSize(app, availableSizeBytes);
availableSpaceDescr.setText(getString(R.string.export_not_enough_space_descr, availableSize));