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.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; 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 class DataStorageFragment extends BaseSettingsFragment implements DataStorageHelper.UpdateMemoryInfoUIAdapter {
public final static int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 500; 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 CHANGE_DIRECTORY_BUTTON = "change_directory";
private final static String OSMAND_USAGE = "osmand_usage"; 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<DataStorageMenuItem> menuItems;
private ArrayList<DataStorageMemoryItem> memoryItems; private ArrayList<DataStorageMemoryItem> memoryItems;
@ -68,7 +66,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private Preference changeButton; private Preference changeButton;
private DataStorageMenuItem currentDataStorage; private DataStorageMenuItem currentDataStorage;
private String tmpManuallySpecifiedPath; private String tmpManuallySpecifiedPath;
private DataStorageHelper itemsHolder; private DataStorageHelper dataStorageHelper;
private boolean calculateTilesBtnPressed; private boolean calculateTilesBtnPressed;
private DataStorageHelper.RefreshMemoryUsedInfo calculateMemoryTask; private DataStorageHelper.RefreshMemoryUsedInfo calculateMemoryTask;
@ -76,18 +74,13 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private OsmandApplication app; private OsmandApplication app;
private OsmandActionBarActivity activity; private OsmandActionBarActivity activity;
private OsmandSettings settings;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
app = getMyApplication(); app = getMyApplication();
activity = getMyActivity(); activity = getMyActivity();
settings = app.getSettings(); if (dataStorageHelper == null) {
if (savedInstanceState != null) { setRetainInstance(true);
calculateTilesBtnPressed = savedInstanceState.getBoolean(CALCULATE_TILES_BTN_PRESSED);
itemsHolder = savedInstanceState.getParcelable(ITEMS_HOLDER);
}
if (itemsHolder == null) {
refreshDataInfo(); refreshDataInfo();
} }
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -98,12 +91,12 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
PreferenceScreen screen = getPreferenceScreen(); PreferenceScreen screen = getPreferenceScreen();
if (screen == null || itemsHolder == null) { if (screen == null || dataStorageHelper == null) {
return; return;
} }
menuItems = itemsHolder.getStorageItems(); menuItems = dataStorageHelper.getStorageItems();
memoryItems = itemsHolder.getMemoryInfoItems(); memoryItems = dataStorageHelper.getMemoryInfoItems();
dataStorageRadioButtonsGroup = new ArrayList<>(); dataStorageRadioButtonsGroup = new ArrayList<>();
for (DataStorageMenuItem item : menuItems) { for (DataStorageMenuItem item : menuItems) {
@ -115,7 +108,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
dataStorageRadioButtonsGroup.add(preference); dataStorageRadioButtonsGroup.add(preference);
} }
currentDataStorage = itemsHolder.getCurrentStorage(); currentDataStorage = dataStorageHelper.getCurrentStorage();
changeButton = new Preference(app); changeButton = new Preference(app);
changeButton.setKey(CHANGE_DIRECTORY_BUTTON); changeButton.setKey(CHANGE_DIRECTORY_BUTTON);
@ -163,7 +156,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
if (tmpManuallySpecifiedPath != null) { if (tmpManuallySpecifiedPath != null) {
DataStorageMenuItem manuallySpecified = null; DataStorageMenuItem manuallySpecified = null;
try { try {
manuallySpecified = (DataStorageMenuItem) itemsHolder.getManuallySpecified().clone(); manuallySpecified = (DataStorageMenuItem) dataStorageHelper.getManuallySpecified().clone();
manuallySpecified.setDirectory(tmpManuallySpecifiedPath); manuallySpecified.setDirectory(tmpManuallySpecifiedPath);
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
return false; return false;
@ -177,7 +170,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
//show necessary dialog //show necessary dialog
String key = preference.getKey(); String key = preference.getKey();
if (key != null) { if (key != null) {
DataStorageMenuItem newDataStorage = itemsHolder.getStorage(key); DataStorageMenuItem newDataStorage = dataStorageHelper.getStorage(key);
if (newDataStorage != null) { if (newDataStorage != null) {
if (!currentDataStorage.getKey().equals(newDataStorage.getKey())) { if (!currentDataStorage.getKey().equals(newDataStorage.getKey())) {
if (newDataStorage.getType() == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT if (newDataStorage.getType() == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT
@ -198,13 +191,6 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
return false; return false;
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(CALCULATE_TILES_BTN_PRESSED, calculateTilesBtnPressed);
outState.putParcelable(ITEMS_HOLDER, itemsHolder);
}
@Override @Override
protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) { protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) {
super.onBindPreferenceViewHolder(preference, holder); super.onBindPreferenceViewHolder(preference, holder);
@ -226,7 +212,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
final View itemView = holder.itemView; final View itemView = holder.itemView;
if (preference instanceof CheckBoxPreference) { if (preference instanceof CheckBoxPreference) {
DataStorageMenuItem item = itemsHolder.getStorage(key); DataStorageMenuItem item = dataStorageHelper.getStorage(key);
if (item != null) { if (item != null) {
TextView tvTitle = itemView.findViewById(android.R.id.title); TextView tvTitle = itemView.findViewById(android.R.id.title);
TextView tvSummary = itemView.findViewById(R.id.summary); TextView tvSummary = itemView.findViewById(R.id.summary);
@ -277,7 +263,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
icon.setVisibility(View.INVISIBLE); icon.setVisibility(View.INVISIBLE);
title.setText(R.string.shared_string_change); title.setText(R.string.shared_string_change);
} else if(key.equals(OSMAND_USAGE)) { } else if(key.equals(OSMAND_USAGE)) {
long totalUsageBytes = itemsHolder.getTotalUsedBytes(); long totalUsageBytes = dataStorageHelper.getTotalUsedBytes();
TextView tvSummary = itemView.findViewById(R.id.summary); TextView tvSummary = itemView.findViewById(R.id.summary);
tvSummary.setText(DataStorageHelper.getFormattedMemoryInfo(totalUsageBytes, memoryUnitsFormats)); tvSummary.setText(DataStorageHelper.getFormattedMemoryInfo(totalUsageBytes, memoryUnitsFormats));
} else { } else {
@ -293,7 +279,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
@Override @Override
public void onClick(View v) { public void onClick(View v) {
calculateTilesBtnPressed = true; calculateTilesBtnPressed = true;
calculateTilesMemoryTask = itemsHolder.calculateTilesMemoryUsed(DataStorageFragment.this); calculateTilesMemoryTask = dataStorageHelper.calculateTilesMemoryUsed(DataStorageFragment.this);
updateAllSettings(); updateAllSettings();
} }
}); });
@ -340,7 +326,7 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
} }
private void showFolderSelectionDialog() { private void showFolderSelectionDialog() {
DataStorageMenuItem manuallySpecified = itemsHolder.getManuallySpecified(); DataStorageMenuItem manuallySpecified = dataStorageHelper.getManuallySpecified();
if (manuallySpecified != null) { if (manuallySpecified != null) {
SelectFolderBottomSheet.showInstance(getFragmentManager(), manuallySpecified.getKey(), SelectFolderBottomSheet.showInstance(getFragmentManager(), manuallySpecified.getKey(),
manuallySpecified.getDirectory(), DataStorageFragment.this, manuallySpecified.getDirectory(), DataStorageFragment.this,
@ -451,8 +437,8 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
private void refreshDataInfo() { private void refreshDataInfo() {
calculateTilesBtnPressed = false; calculateTilesBtnPressed = false;
itemsHolder = DataStorageHelper.refreshInfo(app); dataStorageHelper = new DataStorageHelper(app);
calculateMemoryTask = itemsHolder.calculateMemoryUsedInfo(this); calculateMemoryTask = dataStorageHelper.calculateMemoryUsedInfo(this);
} }
private String getSpaceDescription(String path) { private String getSpaceDescription(String path) {
@ -483,7 +469,14 @@ public class DataStorageFragment extends BaseSettingsFragment implements DataSto
@Override @Override
public void onMemoryInfoUpdate() { public void onMemoryInfoUpdate() {
updateAllSettings(); 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> { 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.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.ValueHolder; import net.osmand.ValueHolder;
@ -15,11 +13,12 @@ import java.io.File;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; 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.Directory;
import static net.osmand.plus.settings.DataStorageMemoryItem.EXTENSIONS; import static net.osmand.plus.settings.DataStorageMemoryItem.EXTENSIONS;
import static net.osmand.plus.settings.DataStorageMemoryItem.PREFIX; 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 INTERNAL_STORAGE = "internal_storage";
public final static String EXTERNAL_STORAGE = "external_storage"; public final static String EXTERNAL_STORAGE = "external_storage";
public final static String SHARED_STORAGE = "shared_storage"; public final static String SHARED_STORAGE = "shared_storage";
@ -48,14 +47,10 @@ public class DataStorageHelper implements Parcelable {
private int currentStorageType; private int currentStorageType;
private String currentStoragePath; private String currentStoragePath;
private DataStorageHelper(OsmandApplication app) { public DataStorageHelper(OsmandApplication app) {
prepareData(app); prepareData(app);
} }
public static DataStorageHelper refreshInfo(OsmandApplication app) {
return new DataStorageHelper(app);
}
private void prepareData(OsmandApplication app) { private void prepareData(OsmandApplication app) {
if (app == null) { if (app == null) {
@ -274,14 +269,14 @@ public class DataStorageHelper implements Parcelable {
public RefreshMemoryUsedInfo calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener) { public RefreshMemoryUsedInfo calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(currentStoragePath); 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); task.execute(mapsMemory, srtmAndHillshadeMemory, tracksMemory, notesMemory);
return task; return task;
} }
public RefreshMemoryUsedInfo calculateTilesMemoryUsed(UpdateMemoryInfoUIAdapter listener) { public RefreshMemoryUsedInfo calculateTilesMemoryUsed(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(tilesMemory.getDirectories()[0].getAbsolutePath()); 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); task.execute(tilesMemory);
return task; return task;
} }
@ -292,17 +287,21 @@ public class DataStorageHelper implements Parcelable {
private DataStorageMemoryItem otherMemory; private DataStorageMemoryItem otherMemory;
private String[] directoriesToAvoid; private String[] directoriesToAvoid;
private String[] prefixesToAvoid; 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.listener = listener;
this.otherMemory = otherMemory; this.otherMemory = otherMemory;
this.rootDir = rootDir; this.rootDir = rootDir;
this.directoriesToAvoid = directoriesToAvoid; this.directoriesToAvoid = directoriesToAvoid;
this.prefixesToAvoid = prefixesToAvoid; this.prefixesToAvoid = prefixesToAvoid;
this.taskKey = taskKey;
} }
@Override @Override
protected Void doInBackground(DataStorageMemoryItem... items) { protected Void doInBackground(DataStorageMemoryItem... items) {
lastRefreshTime = System.currentTimeMillis();
if (rootDir.canRead()) { if (rootDir.canRead()) {
calculateMultiTypes(rootDir, items); calculateMultiTypes(rootDir, items);
} }
@ -334,9 +333,13 @@ public class DataStorageHelper implements Parcelable {
} }
for (Directory dir : directories) { for (Directory dir : directories) {
if (file.getAbsolutePath().equals(dir.getAbsolutePath()) if (file.getAbsolutePath().equals(dir.getAbsolutePath())
|| (file.getAbsolutePath().startsWith(dir.getAbsolutePath()) && dir.isGoDeeper())) { || (file.getAbsolutePath().startsWith(dir.getAbsolutePath()))) {
if (dir.isGoDeeper()) {
calculateMultiTypes(file, items); calculateMultiTypes(file, items);
break nextFile; break nextFile;
} else if (dir.isSkipOther()) {
break nextFile;
}
} }
} }
} }
@ -403,7 +406,7 @@ public class DataStorageHelper implements Parcelable {
otherMemory.addBytes(file.length()); otherMemory.addBytes(file.length());
} }
} }
publishProgress(); refreshUI();
} }
} }
@ -437,7 +440,15 @@ public class DataStorageHelper implements Parcelable {
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid); super.onPostExecute(aVoid);
if (listener != null) { 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 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; package net.osmand.plus.settings;
import android.os.Parcel; public class DataStorageMemoryItem {
import android.os.Parcelable;
public class DataStorageMemoryItem implements Parcelable {
public final static int EXTENSIONS = 0; public final static int EXTENSIONS = 0;
public final static int PREFIX = 1; public final static int PREFIX = 1;
@ -21,14 +18,6 @@ public class DataStorageMemoryItem implements Parcelable {
this.directories = directories; 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() { public String getKey() {
return key; return key;
} }
@ -61,33 +50,6 @@ public class DataStorageMemoryItem implements Parcelable {
this.usedMemoryBytes += bytes; 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 { public static class DataStorageMemoryItemBuilder {
private String key; private String key;
private String[] extensions; 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 String absolutePath;
private boolean goDeeper; private boolean goDeeper;
private int checkingType; private int checkingType;
private boolean skipOther; 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) { public Directory(String absolutePath, boolean goDeeper, int checkingType, boolean skipOther) {
this.absolutePath = absolutePath; this.absolutePath = absolutePath;
this.goDeeper = goDeeper; this.goDeeper = goDeeper;
@ -166,25 +115,5 @@ public class DataStorageMemoryItem implements Parcelable {
public boolean isSkipOther() { public boolean isSkipOther() {
return skipOther; 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); Preference externalStorageDir = (Preference) findPreference(OsmandSettings.EXTERNAL_STORAGE_DIR);
externalStorageDir.setIcon(getContentIcon(R.drawable.ic_action_folder)); externalStorageDir.setIcon(getContentIcon(R.drawable.ic_action_folder));
DataStorageHelper holder = DataStorageHelper.refreshInfo(app); DataStorageHelper holder = new DataStorageHelper(app);
DataStorageMenuItem currentStorage = holder.getCurrentStorage(); DataStorageMenuItem currentStorage = holder.getCurrentStorage();
long totalUsed = app.getSettings().OSMAND_USAGE_SPACE.get(); long totalUsed = app.getSettings().OSMAND_USAGE_SPACE.get();
if (totalUsed > 0) { if (totalUsed > 0) {