add OsmAnd Usage

This commit is contained in:
Nazar 2019-10-16 13:27:45 +03:00
parent 86d5bea322
commit e7fc83b78b
8 changed files with 650 additions and 31 deletions

View file

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="?attr/list_background_color"
android:layout_width="match_parent"
android:layout_height="@dimen/card_row_min_height"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:id="@android:id/icon"
android:layout_width="@dimen/standard_icon_size"
android:layout_height="@dimen/standard_icon_size"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding"
android:src="@drawable/mm_storage_tank"
android:tint="?attr/default_icon_color"
tools:tint="?attr/default_icon_color" />
<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_desc_text_size"
android:paddingLeft="@dimen/content_padding"
osmand:typeface="@string/font_roboto_regular"
tools:text="Internal application memory" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/memory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:clickable="true"
android:focusable="true"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_regular"
tools:text="6.7 GB" />
</LinearLayout>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/list_background_color">
<View
android:id="@+id/topSpace"
android:layout_width="match_parent"
android:layout_height="@dimen/content_padding_half" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/dashboard_divider"
android:focusable="false" />
<View
android:id="@+id/bottomSpace"
android:layout_width="match_parent"
android:layout_height="@dimen/content_padding_half" />
</LinearLayout>

View file

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/card_row_min_height"
android:background="?attr/list_background_color"
android:gravity="center_vertical"
android:minHeight="@dimen/bottom_sheet_cancel_button_height_small"
android:orientation="horizontal">
<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="@color/preference_category_title"
android:layout_gravity="center_vertical|start"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:text="@string/plugin_settings" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:text="summary" />
</LinearLayout>

View file

@ -11,9 +11,17 @@
Thx - Hardy
-->
<string name="shared_string_calculate">Calcutale</string>
<string name="shared_string_osmand_usage">OsmAnd Usage</string>
<string name="shared_sting_tiles">Tiles</string>
<string name="shared_string_maps">Maps</string>
<string name="shared_string_memory_tb_desc">%1$s TB</string>
<string name="shared_string_memory_gb_desc">%1$s GB</string>
<string name="shared_string_memory_mb_desc">%1$s MB</string>
<string name="shared_string_memory_kb_desc">%1$s KB</string>
<string name="change_data_storage_full_description">Move OsmAnd data files to the new destination?\n%1$s > %2$s</string>
<string name="data_storage_preference_summary">%1$s • Used %2$s GB</string>
<string name="data_storage_space_description">Free %1$s • %2$s / %3$s GB</string>
<string name="data_storage_space_description">Free %1$s GB • Total %2$s GB</string>
<string name="enter_path_to_folder">Enter path to the folder</string>
<string name="shared_string_select_folder">Select folder</string>
<string name="paste_Osmand_data_folder_path">Paste path to the folder with OsmAnd data</string>

View file

@ -2,6 +2,44 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/application_dir">
<Preference
android:key="osmand_usage"
android:layout="@layout/preference_category_with_right_text"
android:title="@string/shared_string_osmand_usage" />
<Preference
android:key="maps_memory_used"
android:layout="@layout/data_storage_memory_used_item"
android:icon="@drawable/ic_map"
android:title="@string/shared_string_maps"/>
<Preference
android:key="tracks_memory_used"
android:layout="@layout/data_storage_memory_used_item"
android:icon="@drawable/ic_action_polygom_dark"
android:title="@string/shared_string_gpx_tracks"/>
<Preference
android:key="notes_memory_used"
android:layout="@layout/data_storage_memory_used_item"
android:icon="@drawable/ic_action_photo_dark"
android:title="@string/audionotes_plugin_name"/>
<Preference
android:key="tiles_memory_used"
android:layout="@layout/data_storage_memory_used_item"
android:icon="@drawable/ic_world_globe_dark"
android:title="@string/shared_sting_tiles"/>
<Preference
android:key="other_memory_used"
android:layout="@layout/data_storage_memory_used_item"
android:icon="@drawable/ic_action_folder"
android:title="@string/shared_string_other"/>
<Preference
android:layout="@layout/divider_padding_half" />
<PreferenceCategory
android:key="application_profiles"
android:layout="@layout/preference_category_with_descr"

View file

@ -5,7 +5,6 @@ import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
@ -46,39 +45,61 @@ import java.util.Locale;
import static net.osmand.plus.settings.DataStorageItemsHolder.INTERNAL_STORAGE;
import static net.osmand.plus.settings.DataStorageItemsHolder.MANUALLY_SPECIFIED;
import static net.osmand.plus.settings.DataStorageItemsHolder.TILES_MEMORY;
import static net.osmand.plus.settings.bottomsheets.ChangeDataStorageBottomSheet.CHOSEN_DIRECTORY;
import static net.osmand.plus.settings.bottomsheets.ChangeDataStorageBottomSheet.MOVE_DATA;
import static net.osmand.plus.settings.bottomsheets.SelectFolderBottomSheet.PATH_CHANGED;
import static net.osmand.plus.settings.bottomsheets.SelectFolderBottomSheet.NEW_PATH;
public class DataStorageFragment extends BaseSettingsFragment {
public class DataStorageFragment extends BaseSettingsFragment implements DataStorageItemsHolder.UpdateMemoryInfoUIAdapter {
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;
private ArrayList<CheckBoxPreference> dataStorageRadioButtonsGroup;
private Preference changeButton;
private DataStorageMenuItem currentDataStorage;
private String tmpManuallySpecifiedPath;
private DataStorageItemsHolder itemsHolder;
private boolean calculateTilesBtnPressed;
private DataStorageItemsHolder.RefreshMemoryUsedInfo calculateMemoryTask;
private DataStorageItemsHolder.RefreshMemoryUsedInfo calculateTilesMemoryTask;
private OsmandApplication app;
private OsmandActionBarActivity activity;
private OsmandSettings settings;
@Override
protected void setupPreferences() {
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) {
refreshDataInfo();
}
super.onCreate(savedInstanceState);
}
@Override
protected void setupPreferences() {
PreferenceScreen screen = getPreferenceScreen();
if (screen == null || app == null || activity == null) {
if (screen == null || itemsHolder == null) {
return;
}
settings = app.getSettings();
itemsHolder = DataStorageItemsHolder.refreshInfo(app);
menuItems = itemsHolder.getStorageItems();
memoryItems = itemsHolder.getMemoryInfoItems();
dataStorageRadioButtonsGroup = new ArrayList<>();
for (DataStorageMenuItem item : menuItems) {
@ -173,6 +194,13 @@ public class DataStorageFragment extends BaseSettingsFragment {
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);
@ -180,7 +208,11 @@ public class DataStorageFragment extends BaseSettingsFragment {
if (key == null) {
return;
}
View itemView = holder.itemView;
int activeColorResId = isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
int activeColor = ContextCompat.getColor(app, activeColorResId);
int primaryTextColorResId = isNightMode() ? R.color.text_color_primary_dark : R.color.text_color_primary_light;
int primaryTextColor = ContextCompat.getColor(app, primaryTextColorResId);
final View itemView = holder.itemView;
if (preference instanceof CheckBoxPreference) {
DataStorageMenuItem item = itemsHolder.getStorage(key);
if (item != null) {
@ -222,15 +254,59 @@ public class DataStorageFragment extends BaseSettingsFragment {
} else if (key.equals(CHANGE_DIRECTORY_BUTTON)) {
ImageView icon = itemView.findViewById(R.id.button_icon);
TextView title = itemView.findViewById(R.id.button_text);
int colorResId = isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
int color = ContextCompat.getColor(app, colorResId);
Drawable drawable = UiUtilities.getColoredSelectableDrawable(app, color, 0.3f);
Drawable drawable = UiUtilities.getColoredSelectableDrawable(app, activeColor, 0.3f);
AndroidUtils.setBackground(itemView, drawable);
icon.setVisibility(View.INVISIBLE);
title.setText(R.string.shared_string_change);
} else if(key.equals(OSMAND_USAGE)) {
long totalUsageBytes = 0;
for (DataStorageMemoryItem mi : memoryItems) {
totalUsageBytes += mi.getUsedMemoryBytes();
}
TextView tvSummary = itemView.findViewById(R.id.summary);
tvSummary.setText(getFormattedMemoryUsedInfo(totalUsageBytes));
} else {
for (DataStorageMemoryItem mi : memoryItems) {
if (key.equals(mi.getKey())) {
TextView tvMemory = itemView.findViewById(R.id.memory);
String summary = "";
int color = 0;
if (mi.getKey().equals(TILES_MEMORY) && !calculateTilesBtnPressed) {
summary = getString(R.string.shared_string_calculate);
color = activeColor;
tvMemory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateTilesBtnPressed = true;
calculateTilesMemoryTask = itemsHolder.calculateTilesMemoryUsed(DataStorageFragment.this);
updateAllSettings();
}
});
} else {
tvMemory.setOnClickListener(null);
color = primaryTextColor;
summary = getFormattedMemoryUsedInfo(mi.getUsedMemoryBytes());
}
tvMemory.setTextColor(color);
tvMemory.setText(summary);
}
}
}
}
@Override
public void onDestroy() {
if (!activity.isChangingConfigurations()) {
if (calculateMemoryTask != null) {
calculateMemoryTask.cancel(true);
}
if (calculateTilesMemoryTask != null) {
calculateTilesMemoryTask.cancel(true);
}
}
super.onDestroy();
}
private void updateView(String key) {
//selection set up
for (CheckBoxPreference preference : dataStorageRadioButtonsGroup) {
@ -348,8 +424,15 @@ public class DataStorageFragment extends BaseSettingsFragment {
Toast.makeText(activity, R.string.specified_directiory_not_writeable,
Toast.LENGTH_LONG).show();
}
refreshDataInfo();
updateAllSettings();
}
private void refreshDataInfo() {
calculateTilesBtnPressed = false;
itemsHolder = DataStorageItemsHolder.refreshInfo(app);
calculateMemoryTask = itemsHolder.calculateMemoryUsedInfo(this);
}
private String getSpaceDescription(String path) {
File dir = new File(path);
@ -362,16 +445,40 @@ public class DataStorageFragment extends BaseSettingsFragment {
DecimalFormat formatter = new DecimalFormat("#.##");
return String.format(getString(R.string.data_storage_space_description),
formatter.format(AndroidUtils.getFreeSpaceGb(dir)),
formatter.format(AndroidUtils.getUsedSpaceGb(dir)),
formatter.format(AndroidUtils.getTotalSpaceGb(dir)));
}
return "";
}
private String getFormattedMemoryUsedInfo(long bytes) {
int type = 1;
double used = (double) bytes / 1024;
while (used > 1024) {
++type;
used = used / 1024;
}
String formattedUsed = new DecimalFormat("#.##").format(used);
if (type == 1) {
return String.format(getString(R.string.shared_string_memory_kb_desc), formattedUsed);
} else if (type == 2) {
return String.format(getString(R.string.shared_string_memory_mb_desc), formattedUsed);
} else if (type == 3){
return String.format(getString(R.string.shared_string_memory_gb_desc), formattedUsed);
} else if (type == 4){
return String.format(getString(R.string.shared_string_memory_tb_desc), formattedUsed);
}
return "";
}
protected void reloadData() {
new ReloadData(activity, getMyApplication()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}
@Override
public void onMemoryInfoUpdate() {
updateAllSettings();
}
public static class MoveFilesToDifferentDirectory extends AsyncTask<Void, Void, Boolean> {
protected WeakReference<OsmandActionBarActivity> activity;

View file

@ -1,7 +1,11 @@
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;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -10,39 +14,49 @@ import net.osmand.plus.R;
import java.io.File;
import java.util.ArrayList;
public class DataStorageItemsHolder {
public class DataStorageItemsHolder implements Parcelable {
public final static String INTERNAL_STORAGE = "internal_storage";
public final static String EXTERNAL_STORAGE = "external_storage";
public final static String SHARED_STORAGE = "shared_storage";
public final static String MULTIUSER_STORAGE = "multiuser_storage";
public final static String MANUALLY_SPECIFIED = "manually_specified";
private ArrayList<DataStorageMenuItem> menuItems;
public final static String MAPS_MEMORY = "maps_memory_used";
public final static String TRACKS_MEMORY = "tracks_memory_used";
public final static String NOTES_MEMORY = "notes_memory_used";
public final static String TILES_MEMORY = "tiles_memory_used";
public final static String OTHER_MEMORY = "other_memory_used";
private ArrayList<DataStorageMenuItem> menuItems = new ArrayList<>();
private DataStorageMenuItem currentDataStorage;
private DataStorageMenuItem manuallySpecified;
private ArrayList<DataStorageMemoryItem> memoryItems = new ArrayList<>();
private DataStorageMemoryItem mapsMemory;
private DataStorageMemoryItem tracksMemory;
private DataStorageMemoryItem notesMemory;
private DataStorageMemoryItem tilesMemory;
private DataStorageMemoryItem otherMemory;
private int currentStorageType;
private String currentStoragePath;
private OsmandApplication app;
private OsmandSettings settings;
private DataStorageItemsHolder(OsmandApplication app) {
this.app = app;
this.settings = app.getSettings();
prepareData();
prepareData(app);
}
public static DataStorageItemsHolder refreshInfo(OsmandApplication app) {
return new DataStorageItemsHolder(app);
}
private void prepareData() {
private void prepareData(OsmandApplication app) {
if (app == null) {
return;
}
OsmandSettings settings = app.getSettings();
if (settings.getExternalStorageDirectoryTypeV19() >= 0) {
currentStorageType = settings.getExternalStorageDirectoryTypeV19();
} else {
@ -55,8 +69,6 @@ public class DataStorageItemsHolder {
}
currentStoragePath = settings.getExternalStorageDirectory().getAbsolutePath();
menuItems = new ArrayList<>();
String path;
File dir;
int iconId;
@ -70,9 +82,9 @@ public class DataStorageItemsHolder {
DataStorageMenuItem internalStorageItem = DataStorageMenuItem.builder()
.buildKey(INTERNAL_STORAGE)
.buildTitle(getString(R.string.storage_directory_internal_app))
.buildTitle(app.getString(R.string.storage_directory_internal_app))
.buildDirectory(path)
.buildDescription(getString(R.string.internal_app_storage_description))
.buildDescription(app.getString(R.string.internal_app_storage_description))
.buildType(OsmandSettings.EXTERNAL_STORAGE_TYPE_INTERNAL_FILE)
.buildIconResId(iconId)
.build();
@ -85,7 +97,7 @@ public class DataStorageItemsHolder {
DataStorageMenuItem sharedStorageItem = DataStorageMenuItem.builder()
.buildKey(SHARED_STORAGE)
.buildTitle(getString(R.string.storage_directory_shared))
.buildTitle(app.getString(R.string.storage_directory_shared))
.buildDirectory(path)
.buildType(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT)
.buildIconResId(iconId)
@ -104,7 +116,7 @@ public class DataStorageItemsHolder {
iconId = getIconForStorageType(dir);
DataStorageMenuItem externalStorageItem = DataStorageMenuItem.builder()
.buildKey(EXTERNAL_STORAGE + i)
.buildTitle(getString(R.string.storage_directory_external) + " " + i)
.buildTitle(app.getString(R.string.storage_directory_external) + " " + i)
.buildDirectory(path)
.buildType(OsmandSettings.EXTERNAL_STORAGE_TYPE_EXTERNAL_FILE)
.buildIconResId(iconId)
@ -126,7 +138,7 @@ public class DataStorageItemsHolder {
iconId = getIconForStorageType(dir);
DataStorageMenuItem multiuserStorageItem = DataStorageMenuItem.builder()
.buildKey(MULTIUSER_STORAGE + i)
.buildTitle(getString(R.string.storage_directory_multiuser) + " " + i)
.buildTitle(app.getString(R.string.storage_directory_multiuser) + " " + i)
.buildDirectory(path)
.buildType(OsmandSettings.EXTERNAL_STORAGE_TYPE_OBB)
.buildIconResId(iconId)
@ -140,7 +152,7 @@ public class DataStorageItemsHolder {
//manually specified storage
manuallySpecified = DataStorageMenuItem.builder()
.buildKey(MANUALLY_SPECIFIED)
.buildTitle(getString(R.string.storage_directory_manual))
.buildTitle(app.getString(R.string.storage_directory_manual))
.buildDirectory(currentStoragePath)
.buildType(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED)
.buildIconResId(R.drawable.ic_action_folder)
@ -150,10 +162,49 @@ public class DataStorageItemsHolder {
if (currentDataStorage == null) {
currentDataStorage = manuallySpecified;
}
initMemoryUsed(app);
}
private String getString(int resId) {
return app.getString(resId);
private void initMemoryUsed(OsmandApplication app) {
mapsMemory = DataStorageMemoryItem.builder()
.buildKey(MAPS_MEMORY)
.buildExtensions(IndexConstants.BINARY_MAP_INDEX_EXT, IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)
.buildDirectories(
app.getAppPath(IndexConstants.MAPS_PATH).getAbsolutePath(),
app.getAppPath(IndexConstants.ROADS_INDEX_DIR).getAbsolutePath(),
app.getAppPath(IndexConstants.SRTM_INDEX_DIR).getAbsolutePath(),
app.getAppPath(IndexConstants.WIKI_INDEX_DIR).getAbsolutePath(),
app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR).getAbsolutePath(),
app.getAppPath(IndexConstants.BACKUP_INDEX_DIR).getAbsolutePath())
.build();
memoryItems.add(mapsMemory);
tracksMemory = DataStorageMemoryItem.builder()
.buildKey(TRACKS_MEMORY)
// .buildExtensions(".gpx", ".gpx.bz2")
.buildDirectories(app.getAppPath(IndexConstants.GPX_INDEX_DIR).getAbsolutePath())
.build();
memoryItems.add(tracksMemory);
notesMemory = DataStorageMemoryItem.builder()
.buildKey(NOTES_MEMORY)
// .buildExtensions("")
.buildDirectories(app.getAppPath(IndexConstants.AV_INDEX_DIR).getAbsolutePath())
.build();
memoryItems.add(notesMemory);
tilesMemory = DataStorageMemoryItem.builder()
.buildKey(TILES_MEMORY)
// .buildExtensions("")
.buildDirectories(app.getAppPath(IndexConstants.TILES_INDEX_DIR).getAbsolutePath())
.build();
memoryItems.add(tilesMemory);
otherMemory = DataStorageMemoryItem.builder()
.buildKey(OTHER_MEMORY)
.build();
memoryItems.add(otherMemory);
}
public ArrayList<DataStorageMenuItem> getStorageItems() {
@ -197,4 +248,188 @@ public class DataStorageItemsHolder {
public String getCurrentPath() {
return currentStoragePath;
}
public ArrayList<DataStorageMemoryItem> getMemoryInfoItems() {
return memoryItems;
}
public RefreshMemoryUsedInfo calculateMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(currentStoragePath);
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir, tilesMemory.getDirectories());
task.execute(mapsMemory, tracksMemory, notesMemory);
return task;
}
public RefreshMemoryUsedInfo calculateTilesMemoryUsed(UpdateMemoryInfoUIAdapter listener) {
File rootDir = new File(tilesMemory.getDirectories()[0]);
RefreshMemoryUsedInfo task = new RefreshMemoryUsedInfo(listener, otherMemory, rootDir);
task.execute(tilesMemory);
return task;
}
public static class RefreshMemoryUsedInfo extends AsyncTask<DataStorageMemoryItem, Void, Void> {
private UpdateMemoryInfoUIAdapter listener;
private File rootDir;
private DataStorageMemoryItem otherMemory;
private String[] directoriesToAvoid;
public RefreshMemoryUsedInfo(UpdateMemoryInfoUIAdapter listener, DataStorageMemoryItem otherMemory, File rootDir, String... directoriesToAvoid) {
this.listener = listener;
this.otherMemory = otherMemory;
this.rootDir = rootDir;
this.directoriesToAvoid = directoriesToAvoid;
}
@Override
protected Void doInBackground(DataStorageMemoryItem... items) {
if (items.length == 1) {
DataStorageMemoryItem item = items[0];
item.addBytes(getDirectorySize(rootDir, item.getExtensions()));
} else {
calculateMultiTypes(items);
}
return null;
}
private void calculateMultiTypes(DataStorageMemoryItem[] items) {
File[] files = rootDir.listFiles();
for (File f : files) {
boolean matched = false;
if (f.isDirectory()) {
boolean avoid = false;
for (String directoryToAvoid : directoriesToAvoid) {
if (f.getAbsolutePath().equals(directoryToAvoid)) {
avoid = true;
break;
}
}
if (!avoid) {
for (DataStorageMemoryItem item : items) {
String[] directories = item.getDirectories();
if (directories != null) {
for (String directory : directories) {
if (f.getAbsolutePath().equals(directory)) {
item.addBytes(getDirectorySize(f, item.getExtensions()));
matched = true;
break;
}
}
}
}
if (!matched) {
otherMemory.addBytes(getDirectorySize(f, null));
}
}
} else if (f.isFile()) {
for (DataStorageMemoryItem item : items) {
String[] extensions = item.getExtensions();
if (extensions != null) {
for (String extension : extensions) {
if (f.getAbsolutePath().endsWith(extension)) {
item.addBytes(f.length());
matched = true;
break;
}
}
}
}
if (!matched) {
otherMemory.addBytes(f.length());
}
}
}
}
private long getDirectorySize(File dir, String[] extensions) {
long bytes = 0;
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File file : files) {
if (isCancelled()) {
break;
}
if (file.isDirectory()) {
bytes += getDirectorySize(file, extensions);
} else if (file.isFile()) {
//check file extension
boolean matched = false;
if (extensions != null) {
for (String extension : extensions) {
if (file.getName().endsWith(extension)) {
matched = true;
break;
}
}
} else {
matched = true;
}
if (matched) {
bytes += file.length();
} else {
otherMemory.addBytes(file.length());
}
}
}
}
return bytes;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
if (listener != null) {
listener.onMemoryInfoUpdate();
}
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (listener != null) {
listener.onMemoryInfoUpdate();
}
}
}
public interface UpdateMemoryInfoUIAdapter {
void onMemoryInfoUpdate();
}
@Override
public int describeContents() {
return 0;
}
private DataStorageItemsHolder(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<DataStorageItemsHolder> CREATOR = new Parcelable.Creator<DataStorageItemsHolder>() {
@Override
public DataStorageItemsHolder createFromParcel(Parcel source) {
return new DataStorageItemsHolder(source);
}
@Override
public DataStorageItemsHolder[] newArray(int size) {
return new DataStorageItemsHolder[size];
}
};
}

View file

@ -0,0 +1,122 @@
package net.osmand.plus.settings;
import android.os.Parcel;
import android.os.Parcelable;
public class DataStorageMemoryItem implements Parcelable {
private String key;
private String[] extensions;
private String[] directories;
private long usedMemoryBytes;
private DataStorageMemoryItem(String key, String[] extensions, long usedMemoryBytes, String[] directories) {
this.key = key;
this.extensions = extensions;
this.usedMemoryBytes = usedMemoryBytes;
this.directories = directories;
}
private DataStorageMemoryItem(Parcel in) {
key = in.readString();
in.readStringArray(extensions);
in.readStringArray(directories);
usedMemoryBytes = in.readLong();
}
public String getKey() {
return key;
}
public long getUsedMemoryBytes() {
return usedMemoryBytes;
}
public void setKey(String key) {
this.key = key;
}
public void setUsedMemoryBytes(long usedMemoryBytes) {
this.usedMemoryBytes = usedMemoryBytes;
}
public static DataStorageMemoryItemBuilder builder() {
return new DataStorageMemoryItemBuilder();
}
public String[] getExtensions() {
return extensions;
}
public void setExtensions(String[] extensions) {
this.extensions = extensions;
}
public String[] getDirectories() {
return directories;
}
public void setDirectories(String[] directories) {
this.directories = directories;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(key);
dest.writeStringArray(extensions);
dest.writeStringArray(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;
private String[] directories;
private long usedMemoryBytes;
public DataStorageMemoryItemBuilder buildKey(String key) {
this.key = key;
return this;
}
public DataStorageMemoryItemBuilder buildExtensions(String ... extensions) {
this.extensions = extensions;
return this;
}
public DataStorageMemoryItemBuilder buildDirectories(String ... directories) {
this.directories = directories;
return this;
}
public DataStorageMemoryItemBuilder buildUsedMemoryBytes(long usedMemoryBytes) {
this.usedMemoryBytes = usedMemoryBytes;
return this;
}
public DataStorageMemoryItem build() {
return new DataStorageMemoryItem(key, extensions, usedMemoryBytes, directories);
}
}
public void addBytes(long bytes) {
this.usedMemoryBytes += bytes;
}
}