diff --git a/OsmAnd/src/net/osmand/plus/settings/DataStorageFragment.java b/OsmAnd/src/net/osmand/plus/settings/DataStorageFragment.java index 802db74758..2e357ac497 100644 --- a/OsmAnd/src/net/osmand/plus/settings/DataStorageFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/DataStorageFragment.java @@ -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 menuItems; private ArrayList 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,9 +469,16 @@ 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 { protected WeakReference activity; diff --git a/OsmAnd/src/net/osmand/plus/settings/DataStorageHelper.java b/OsmAnd/src/net/osmand/plus/settings/DataStorageHelper.java index f2b611f21e..757f551fdc 100644 --- a/OsmAnd/src/net/osmand/plus/settings/DataStorageHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/DataStorageHelper.java @@ -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())) { - calculateMultiTypes(file, items); - break nextFile; + || (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(); } } } @@ -467,41 +478,8 @@ public class DataStorageHelper implements Parcelable { public interface UpdateMemoryInfoUIAdapter { 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 CREATOR = new Parcelable.Creator() { - - @Override - public DataStorageHelper createFromParcel(Parcel source) { - return new DataStorageHelper(source); - } - - @Override - public DataStorageHelper[] newArray(int size) { - return new DataStorageHelper[size]; - } - }; } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/DataStorageMemoryItem.java b/OsmAnd/src/net/osmand/plus/settings/DataStorageMemoryItem.java index dd7576418f..3fcf9b3069 100644 --- a/OsmAnd/src/net/osmand/plus/settings/DataStorageMemoryItem.java +++ b/OsmAnd/src/net/osmand/plus/settings/DataStorageMemoryItem.java @@ -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 CREATOR = new Parcelable.Creator() { - - @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 CREATOR = new Parcelable.Creator() { - - @Override - public Directory createFromParcel(Parcel source) { - return new Directory(source); - } - - @Override - public Directory[] newArray(int size) { - return new Directory[size]; - } - }; } } diff --git a/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java index 561abc1146..1eb702376b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java @@ -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) {