Merge pull request #7749 from osmandapp/DataStorage

Data Storage
This commit is contained in:
max-klaus 2019-10-25 14:28:19 +03:00 committed by GitHub
commit 764631fe9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 156 deletions

View file

@ -5,7 +5,6 @@ import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
@ -56,11 +55,10 @@ import static net.osmand.plus.settings.bottomsheets.SelectFolderBottomSheet.NEW_
public class DataStorageFragment extends BaseSettingsFragment implements DataStorageHelper.UpdateMemoryInfoUIAdapter {
public final static int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 500;
public final static int UI_REFRESH_TIME_MS = 500;
private final static String CHANGE_DIRECTORY_BUTTON = "change_directory";
private final static String OSMAND_USAGE = "osmand_usage";
private final static String CALCULATE_TILES_BTN_PRESSED = "calculate_tiles_btn_pressed";
private final static String ITEMS_HOLDER = "items_holder";
private ArrayList<DataStorageMenuItem> menuItems;
private ArrayList<DataStorageMemoryItem> memoryItems;
@ -68,7 +66,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private Preference changeButton;
private DataStorageMenuItem currentDataStorage;
private String tmpManuallySpecifiedPath;
private DataStorageHelper itemsHolder;
private DataStorageHelper dataStorageHelper;
private boolean calculateTilesBtnPressed;
private DataStorageHelper.RefreshMemoryUsedInfo calculateMemoryTask;
@ -76,18 +74,13 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private OsmandApplication app;
private OsmandActionBarActivity activity;
private OsmandSettings settings;
@Override
public void onCreate(Bundle savedInstanceState) {
app = getMyApplication();
activity = getMyActivity();
settings = app.getSettings();
if (savedInstanceState != null) {
calculateTilesBtnPressed = savedInstanceState.getBoolean(CALCULATE_TILES_BTN_PRESSED);
itemsHolder = savedInstanceState.getParcelable(ITEMS_HOLDER);
}
if (itemsHolder == null) {
if (dataStorageHelper == null) {
setRetainInstance(true);
refreshDataInfo();
}
super.onCreate(savedInstanceState);
@ -98,12 +91,12 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
PreferenceScreen screen = getPreferenceScreen();
if (screen == null || itemsHolder == null) {
if (screen == null || dataStorageHelper == null) {
return;
}
menuItems = itemsHolder.getStorageItems();
memoryItems = itemsHolder.getMemoryInfoItems();
menuItems = dataStorageHelper.getStorageItems();
memoryItems = dataStorageHelper.getMemoryInfoItems();
dataStorageRadioButtonsGroup = new ArrayList<>();
for (DataStorageMenuItem item : menuItems) {
@ -115,7 +108,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
dataStorageRadioButtonsGroup.add(preference);
}
currentDataStorage = itemsHolder.getCurrentStorage();
currentDataStorage = dataStorageHelper.getCurrentStorage();
changeButton = new Preference(app);
changeButton.setKey(CHANGE_DIRECTORY_BUTTON);
@ -163,7 +156,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
if (tmpManuallySpecifiedPath != null) {
DataStorageMenuItem manuallySpecified = null;
try {
manuallySpecified = (DataStorageMenuItem) itemsHolder.getManuallySpecified().clone();
manuallySpecified = (DataStorageMenuItem) dataStorageHelper.getManuallySpecified().clone();
manuallySpecified.setDirectory(tmpManuallySpecifiedPath);
} catch (CloneNotSupportedException e) {
return false;
@ -177,7 +170,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
//show necessary dialog
String key = preference.getKey();
if (key != null) {
DataStorageMenuItem newDataStorage = itemsHolder.getStorage(key);
DataStorageMenuItem newDataStorage = dataStorageHelper.getStorage(key);
if (newDataStorage != null) {
if (!currentDataStorage.getKey().equals(newDataStorage.getKey())) {
if (newDataStorage.getType() == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT
@ -198,13 +191,6 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
return false;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(CALCULATE_TILES_BTN_PRESSED, calculateTilesBtnPressed);
outState.putParcelable(ITEMS_HOLDER, itemsHolder);
}
@Override
protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) {
super.onBindPreferenceViewHolder(preference, holder);
@ -226,7 +212,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
final View itemView = holder.itemView;
if (preference instanceof CheckBoxPreference) {
DataStorageMenuItem item = itemsHolder.getStorage(key);
DataStorageMenuItem item = dataStorageHelper.getStorage(key);
if (item != null) {
TextView tvTitle = itemView.findViewById(android.R.id.title);
TextView tvSummary = itemView.findViewById(R.id.summary);
@ -277,7 +263,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
icon.setVisibility(View.INVISIBLE);
title.setText(R.string.shared_string_change);
} else if(key.equals(OSMAND_USAGE)) {
long totalUsageBytes = itemsHolder.getTotalUsedBytes();
long totalUsageBytes = dataStorageHelper.getTotalUsedBytes();
TextView tvSummary = itemView.findViewById(R.id.summary);
tvSummary.setText(DataStorageHelper.getFormattedMemoryInfo(totalUsageBytes, memoryUnitsFormats));
} else {
@ -293,7 +279,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
@Override
public void onClick(View v) {
calculateTilesBtnPressed = true;
calculateTilesMemoryTask = itemsHolder.calculateTilesMemoryUsed(DataStorageFragment.this);
calculateTilesMemoryTask = dataStorageHelper.calculateTilesMemoryUsed(DataStorageFragment.this);
updateAllSettings();
}
});
@ -340,7 +326,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
}
private void showFolderSelectionDialog() {
DataStorageMenuItem manuallySpecified = itemsHolder.getManuallySpecified();
DataStorageMenuItem manuallySpecified = dataStorageHelper.getManuallySpecified();
if (manuallySpecified != null) {
SelectFolderBottomSheet.showInstance(getFragmentManager(), manuallySpecified.getKey(),
manuallySpecified.getDirectory(), DataStorageFragment.this,
@ -451,8 +437,8 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private void refreshDataInfo() {
calculateTilesBtnPressed = false;
itemsHolder = DataStorageHelper.refreshInfo(app);
calculateMemoryTask = itemsHolder.calculateMemoryUsedInfo(this);
dataStorageHelper = new DataStorageHelper(app);
calculateMemoryTask = dataStorageHelper.calculateMemoryUsedInfo(this);
}
private String getSpaceDescription(String path) {
@ -483,7 +469,14 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
@Override
public void onMemoryInfoUpdate() {
updateAllSettings();
app.getSettings().OSMAND_USAGE_SPACE.set(itemsHolder.getTotalUsedBytes());
}
@Override
public void onFinishUpdating(String taskKey) {
updateAllSettings();
if (taskKey != null && taskKey.equals(TILES_MEMORY)) {
app.getSettings().OSMAND_USAGE_SPACE.set(dataStorageHelper.getTotalUsedBytes());
}
}
public static class MoveFilesToDifferentDirectory extends AsyncTask<Void, Void, Boolean> {

View file

@ -2,8 +2,6 @@ package net.osmand.plus.settings;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import net.osmand.IndexConstants;
import net.osmand.ValueHolder;
@ -15,11 +13,12 @@ import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import static net.osmand.plus.settings.DataStorageFragment.UI_REFRESH_TIME_MS;
import static net.osmand.plus.settings.DataStorageMemoryItem.Directory;
import static net.osmand.plus.settings.DataStorageMemoryItem.EXTENSIONS;
import static net.osmand.plus.settings.DataStorageMemoryItem.PREFIX;
public class DataStorageHelper implements Parcelable {
public class DataStorageHelper {
public final static String INTERNAL_STORAGE = "internal_storage";
public final static String EXTERNAL_STORAGE = "external_storage";
public final static String SHARED_STORAGE = "shared_storage";
@ -48,14 +47,10 @@ public class DataStorageHelper implements Parcelable {
private int currentStorageType;
private String currentStoragePath;
private DataStorageHelper(OsmandApplication app) {
public DataStorageHelper(OsmandApplication app) {
prepareData(app);
}
public static DataStorageHelper refreshInfo(OsmandApplication app) {
return new DataStorageHelper(app);
}
private void prepareData(OsmandApplication app) {
if (app == null) {
@ -274,14 +269,14 @@ public class DataStorageHelper implements Parcelable {
public RefreshMemoryUsedInfo calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(currentStoragePath);
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir, null, null);
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir, null, null, OTHER_MEMORY);
task.execute(mapsMemory, srtmAndHillshadeMemory, tracksMemory, notesMemory);
return task;
}
public RefreshMemoryUsedInfo calculateTilesMemoryUsed(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(tilesMemory.getDirectories()[0].getAbsolutePath());
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir, null, srtmAndHillshadeMemory.getPrefixes());
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir, null, srtmAndHillshadeMemory.getPrefixes(), TILES_MEMORY);
task.execute(tilesMemory);
return task;
}
@ -292,17 +287,21 @@ public class DataStorageHelper implements Parcelable {
private DataStorageMemoryItem otherMemory;
private String[] directoriesToAvoid;
private String[] prefixesToAvoid;
private String taskKey;
private long lastRefreshTime;
public RefreshMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener, DataStorageMemoryItem otherMemory, File rootDir, String[] directoriesToAvoid, String[] prefixesToAvoid) {
public RefreshMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener, DataStorageMemoryItem otherMemory, File rootDir, String[] directoriesToAvoid, String[] prefixesToAvoid, String taskKey) {
this.listener = listener;
this.otherMemory = otherMemory;
this.rootDir = rootDir;
this.directoriesToAvoid = directoriesToAvoid;
this.prefixesToAvoid = prefixesToAvoid;
this.taskKey = taskKey;
}
@Override
protected Void doInBackground(DataStorageMemoryItem... items) {
lastRefreshTime = System.currentTimeMillis();
if (rootDir.canRead()) {
calculateMultiTypes(rootDir, items);
}
@ -334,9 +333,13 @@ public class DataStorageHelper implements Parcelable {
}
for (Directory dir : directories) {
if (file.getAbsolutePath().equals(dir.getAbsolutePath())
|| (file.getAbsolutePath().startsWith(dir.getAbsolutePath()) && dir.isGoDeeper())) {
|| (file.getAbsolutePath().startsWith(dir.getAbsolutePath()))) {
if (dir.isGoDeeper()) {
calculateMultiTypes(file, items);
break nextFile;
} else if (dir.isSkipOther()) {
break nextFile;
}
}
}
}
@ -403,7 +406,7 @@ public class DataStorageHelper implements Parcelable {
otherMemory.addBytes(file.length());
}
}
publishProgress();
refreshUI();
}
}
@ -437,7 +440,15 @@ public class DataStorageHelper implements Parcelable {
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (listener != null) {
listener.onMemoryInfoUpdate();
listener.onFinishUpdating(taskKey);
}
}
private void refreshUI() {
long currentTime = System.currentTimeMillis();
if ((currentTime - lastRefreshTime) > UI_REFRESH_TIME_MS) {
lastRefreshTime = currentTime;
publishProgress();
}
}
}
@ -468,40 +479,7 @@ public class DataStorageHelper implements Parcelable {
void onMemoryInfoUpdate();
}
void onFinishUpdating(String taskKey);
@Override
public int describeContents() {
return 0;
}
private DataStorageHelper(Parcel in) {
menuItems = in.readArrayList(DataStorageMenuItem.class.getClassLoader());
currentDataStorage = in.readParcelable(DataStorageMenuItem.class.getClassLoader());
memoryItems = in.readArrayList(DataStorageMemoryItem.class.getClassLoader());
currentStorageType = in.readInt();
currentStoragePath = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeArray(menuItems.toArray());
dest.writeParcelable(currentDataStorage, flags);
dest.writeArray(memoryItems.toArray());
dest.writeInt(currentStorageType);
dest.writeString(currentStoragePath);
}
public static final Parcelable.Creator<DataStorageHelper> CREATOR = new Parcelable.Creator<DataStorageHelper>() {
@Override
public DataStorageHelper createFromParcel(Parcel source) {
return new DataStorageHelper(source);
}
@Override
public DataStorageHelper[] newArray(int size) {
return new DataStorageHelper[size];
}
};
}

View file

@ -1,9 +1,6 @@
package net.osmand.plus.settings;
import android.os.Parcel;
import android.os.Parcelable;
public class DataStorageMemoryItem implements Parcelable {
public class DataStorageMemoryItem {
public final static int EXTENSIONS = 0;
public final static int PREFIX = 1;
@ -21,14 +18,6 @@ public class DataStorageMemoryItem implements Parcelable {
this.directories = directories;
}
private DataStorageMemoryItem(Parcel in) {
key = in.readString();
in.readStringArray(extensions);
in.writeStringArray(prefixes);
directories = (Directory[]) in.readArray(Directory.class.getClassLoader());
usedMemoryBytes = in.readLong();
}
public String getKey() {
return key;
}
@ -61,33 +50,6 @@ public class DataStorageMemoryItem implements Parcelable {
this.usedMemoryBytes += bytes;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(key);
dest.writeStringArray(extensions);
dest.writeStringArray(prefixes);
dest.writeArray(directories);
dest.writeLong(usedMemoryBytes);
}
public static final Parcelable.Creator<DataStorageMemoryItem> CREATOR = new Parcelable.Creator<DataStorageMemoryItem>() {
@Override
public DataStorageMemoryItem createFromParcel(Parcel source) {
return new DataStorageMemoryItem(source);
}
@Override
public DataStorageMemoryItem[] newArray(int size) {
return new DataStorageMemoryItem[size];
}
};
public static class DataStorageMemoryItemBuilder {
private String key;
private String[] extensions;
@ -125,25 +87,12 @@ public class DataStorageMemoryItem implements Parcelable {
}
}
public static class Directory implements Parcelable {
public static class Directory {
private String absolutePath;
private boolean goDeeper;
private int checkingType;
private boolean skipOther;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(absolutePath);
dest.writeInt(goDeeper ? 1 : 0);
dest.writeInt(checkingType);
dest.writeInt(skipOther ? 1 : 0);
}
public Directory(String absolutePath, boolean goDeeper, int checkingType, boolean skipOther) {
this.absolutePath = absolutePath;
this.goDeeper = goDeeper;
@ -166,25 +115,5 @@ public class DataStorageMemoryItem implements Parcelable {
public boolean isSkipOther() {
return skipOther;
}
private Directory(Parcel in) {
absolutePath = in.readString();
goDeeper = in.readInt() == 1;
checkingType = in.readInt();
skipOther = in.readInt() == 1;
}
public static final Parcelable.Creator<Directory> CREATOR = new Parcelable.Creator<Directory>() {
@Override
public Directory createFromParcel(Parcel source) {
return new Directory(source);
}
@Override
public Directory[] newArray(int size) {
return new Directory[size];
}
};
}
}

View file

@ -137,7 +137,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment implements Send
Preference externalStorageDir = (Preference) findPreference(OsmandSettings.EXTERNAL_STORAGE_DIR);
externalStorageDir.setIcon(getContentIcon(R.drawable.ic_action_folder));
DataStorageHelper holder = DataStorageHelper.refreshInfo(app);
DataStorageHelper holder = new DataStorageHelper(app);
DataStorageMenuItem currentStorage = holder.getCurrentStorage();
long totalUsed = app.getSettings().OSMAND_USAGE_SPACE.get();
if (totalUsed > 0) {