From 8ea16be125e9b57568c0eaa57c3e2c9a22ed866a Mon Sep 17 00:00:00 2001 From: Starcommander Date: Fri, 29 May 2020 18:53:03 +0200 Subject: [PATCH] Allow Export and Import of all data. See #110 Select maps for export. Dont keep old favourites-files. Catch NullPointer on TTS-Engine-Show. JsonWrapper must also override toString() --- PocketMaps/app/src/main/AndroidManifest.xml | 2 +- .../pocketmaps/activities/ExportActivity.java | 140 ++++++++++++++++-- .../activities/GeocodeActivity.java | 9 +- .../pocketmaps/activities/MainActivity.java | 3 +- .../activities/ShowLocationActivity.java | 1 + .../downloader/MapDownloadUnzip.java | 2 +- .../pocketmaps/downloader/MapUnzip.java | 134 ++++++++++++++--- .../downloader/ProgressPublisher.java | 2 +- .../pocketmaps/fragments/Dialog.java | 6 - .../com/junjunguo/pocketmaps/model/MyMap.java | 25 +--- .../pocketmaps/navigator/NaviVoice.java | 16 ++ .../com/junjunguo/pocketmaps/util/IO.java | 22 +++ .../pocketmaps/util/JsonWrapper.java | 6 + .../junjunguo/pocketmaps/util/Variable.java | 7 +- .../src/main/res/layout/activity_export.xml | 59 ++++++-- .../app/src/main/res/menu/menu_main.xml | 2 +- .../app/src/main/res/values-de/strings.xml | 3 +- .../app/src/main/res/values-it/strings.xml | 3 +- .../app/src/main/res/values-nl/strings.xml | 3 +- .../src/main/res/values-pt-rBR/strings.xml | 3 +- .../app/src/main/res/values/strings.xml | 3 +- 21 files changed, 371 insertions(+), 80 deletions(-) diff --git a/PocketMaps/app/src/main/AndroidManifest.xml b/PocketMaps/app/src/main/AndroidManifest.xml index c91cfde..f7d29a1 100644 --- a/PocketMaps/app/src/main/AndroidManifest.xml +++ b/PocketMaps/app/src/main/AndroidManifest.xml @@ -79,7 +79,7 @@ diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java index 17605bd..03a0a5f 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ExportActivity.java @@ -7,10 +7,15 @@ import android.util.Log; import android.view.View; import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.Toast; import com.junjunguo.pocketmaps.downloader.MapUnzip; @@ -21,37 +26,54 @@ import java.util.ArrayList; import java.util.GregorianCalendar; -public class ExportActivity extends AppCompatActivity implements OnClickListener +public class ExportActivity extends AppCompatActivity implements OnClickListener, OnItemSelectedListener, OnCheckedChangeListener { + public enum FileType { Tracking, Favourites, Setting, Map, Unknown } Spinner exSpinner; + Spinner exTypeSpinner; CheckBox exSetCb; CheckBox exFavCb; CheckBox exTrackCb; CheckBox exMapsCb; + LinearLayout lImport; + LinearLayout lExport; + LinearLayout lMaps; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_export); Button exOk = (Button) findViewById(R.id.exOk); - TextView exHeader = (TextView) findViewById(R.id.exHeader); exSpinner = (Spinner) findViewById(R.id.exSpinner); + exTypeSpinner = (Spinner) findViewById(R.id.exTypeSpinner); exSetCb = (CheckBox) findViewById(R.id.exSet_cb); exFavCb = (CheckBox) findViewById(R.id.exFav_cb); exTrackCb = (CheckBox) findViewById(R.id.exTrack_cb); exMapsCb = (CheckBox) findViewById(R.id.exMaps_cb); + lImport = (LinearLayout) findViewById(R.id.exLayout_import); + lExport = (LinearLayout) findViewById(R.id.exLayout_export); + lMaps = (LinearLayout) findViewById(R.id.exLayout_maps); exSetCb.setChecked(true); exFavCb.setChecked(true); exTrackCb.setChecked(true); fillSpinner(); - exHeader.setText("Export"); + fillTypeSpinner(); + fillMapList(); exOk.setOnClickListener(this); } @Override public void onClick(View v) { - if (v.getId()!=R.id.exOk) { return; } + if (v.getId()!=R.id.exOk) + { // Import a pmz file. + String dataDir = exSpinner.getSelectedItem().toString(); + String dataFile = ((Button)v).getText().toString(); + log("Import from: " + dataDir + "/" + dataFile); + new MapUnzip().unzipImport(new File(dataDir, dataFile).getPath(), this.getApplicationContext()); + finish(); + return; + } log("Selected: Export"); String targetDir = exSpinner.getSelectedItem().toString(); if (!new File(targetDir).canWrite()) @@ -61,29 +83,48 @@ public void onClick(View v) return; } ArrayList saveList = new ArrayList(); + ArrayList saveListDirs = new ArrayList(); + String exSettingsS = "Exported: "; if (exSetCb.isChecked()) { log("- Export settings."); - saveList.addAll(Variable.getVariable().getSavingFiles()); + boolean anySetting = false; + for (String savingFile : Variable.getVariable().getSavingFiles()) + { + saveList.add(savingFile); + saveListDirs.add(""); + anySetting = true; + } + if (anySetting) { exSettingsS += "[Settings] "; } } if (exFavCb.isChecked()) { log("- Export favourites."); String favFolder = Variable.getVariable().getMapsFolder().getParent(); - saveList.add(new File(favFolder, "Favourites.properties").getPath()); + File favFile = new File(favFolder, "Favourites.properties"); + if (favFile.isFile()) + { + saveList.add(favFile.getPath()); + saveListDirs.add("data"); + exSettingsS += "[Favourites] "; + } } if (exTrackCb.isChecked()) { log("- Export tracking-records."); File trDir = Variable.getVariable().getTrackingFolder(); + boolean anySetting = false; if (trDir.isDirectory()) { for (String fname : trDir.list()) { fname = new File(trDir, fname).getPath(); saveList.add(fname); + saveListDirs.add("data"); + anySetting = true; } } + if (anySetting) { exSettingsS += "[Tracking-recs] "; } } GregorianCalendar now = new GregorianCalendar(); int y = now.get(GregorianCalendar.YEAR); @@ -95,8 +136,9 @@ public void onClick(View v) if (saveList.isEmpty()) { logUser("Nothing to save."); } else { - new MapUnzip().compressFiles(saveList, zipFile.getPath(), "data", null, this); - logUser("Finish: Export base."); + new MapUnzip().compressFiles(saveList, saveListDirs, zipFile.getPath(), null, this); + ProgressPublisher pp = new ProgressPublisher(this.getApplicationContext()); + pp.updateTextFinal(exSettingsS); } if (exMapsCb.isChecked()) { @@ -111,22 +153,76 @@ public void onClick(View v) finish(); } + public static FileType getFileType(String file) + { + if (file.startsWith("/maps/")) { return FileType.Map; } + if (Variable.isSavingFile(file)) { return FileType.Setting; } + if (file.endsWith(".gpx")) { return FileType.Tracking; } + if (file.endsWith("Favourites.properties")) { return FileType.Favourites; } + return FileType.Unknown; + } + + @Override + public void onItemSelected(AdapterView av, View view, int i, long l) + { + if (exTypeSpinner.getSelectedItemId()==0) + { + lExport.setVisibility(View.VISIBLE); + lImport.setVisibility(View.GONE); + } + else + { + lExport.setVisibility(View.GONE); + lImport.setVisibility(View.VISIBLE); + lImport.removeAllViews(); + String dataDir = exSpinner.getSelectedItem().toString(); + for (String f : new File(dataDir).list()) + { + if (!f.endsWith(".pmz")) { continue; } + if (!new File(dataDir, f).isFile()) { continue; } + Button button = new Button(this); + button.setText(f); + button.setOnClickListener(this); + lImport.addView(button); + } + } + } + + @Override + public void onNothingSelected(AdapterView av) {} + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) + { + if (isChecked) { lMaps.setVisibility(View.VISIBLE); } + else { lMaps.setVisibility(View.GONE); } + } + private void exportMaps(File zipBaseFile) { - for (String dName : Variable.getVariable().getMapsFolder().list()) + long fullSize = 0; + for (View dView : lMaps.getTouchables()) { + CheckBox dCb = (CheckBox)dView; + if (!dCb.isChecked()) { continue; } + String dName = dCb.getText().toString(); int zipDotIdx = zipBaseFile.getPath().lastIndexOf("."); String zipFile = zipBaseFile.getPath().substring(0, zipDotIdx) + "-" + dName + ".pmz"; ArrayList mapFiles = new ArrayList(); + ArrayList mapSubDirs = new ArrayList(); File mDir = new File(Variable.getVariable().getMapsFolder(), dName); if (!mDir.isDirectory()) { continue; } + fullSize += IO.dirSize(mDir); + long freeSize = zipBaseFile.getParentFile().getFreeSpace() / (1000 * 1000); + if (fullSize > freeSize) { logUser("Out of disk-memory"); return; } for (String mFile : mDir.list()) - { + { // No directory allowed mapFiles.add(new File(mDir, mFile).getPath()); + mapSubDirs.add("/maps/" + dName); } if (mapFiles.size()==0) { continue; } ProgressPublisher pp = new ProgressPublisher(this.getApplicationContext()); - new MapUnzip().compressFiles(mapFiles, zipFile, "/maps/" + dName, pp, this); + new MapUnzip().compressFiles(mapFiles, mapSubDirs, zipFile, pp, this); } } @@ -150,6 +246,28 @@ private void fillSpinner() ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line); adapter.addAll(IO.listSelectionPaths(this, false)); exSpinner.setAdapter(adapter); + exSpinner.setOnItemSelectedListener(this); + } + private void fillTypeSpinner() + { + ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line); + adapter.add(getResources().getString(R.string.exp)); + adapter.add(getResources().getString(R.string.imp)); + exTypeSpinner.setAdapter(adapter); + exTypeSpinner.setSelection(0); + exTypeSpinner.setOnItemSelectedListener(this); + } + + private void fillMapList() + { + for (String dName : Variable.getVariable().getMapsFolder().list()) + { + CheckBox cb = new CheckBox(this); + cb.setText(dName); + cb.setChecked(true); + lMaps.addView(cb); + } + exMapsCb.setOnCheckedChangeListener(this); } } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/GeocodeActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/GeocodeActivity.java index d98dec5..fcb4f68 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/GeocodeActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/GeocodeActivity.java @@ -51,6 +51,7 @@ import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.UiThread; /** Shows the Favourites-List, AddressDetailsView, and SearchEngine. **/ public class GeocodeActivity extends AppCompatActivity implements OnClickListener @@ -636,6 +637,9 @@ private void storeGeocodeSettings(String searchText) if (bSetEn || bSetTx || bSetBit) { Variable.getVariable().saveVariables(VarType.Geocode); } } + + @UiThread + public static void resetFavourites() { favourites = null; } private void startFavAsync(final MyAddressAdapter adapter) { @@ -653,6 +657,7 @@ else if (favourites == null) loadProp = true; } final boolean loadPropFinal = loadProp; + final Properties favouritesFinal = favourites; new AsyncTask>() { String errMsg; @@ -664,7 +669,7 @@ protected List
doInBackground(Void... params) { try (FileInputStream fis = new FileInputStream(propFile)) { - favourites.load(fis); + favouritesFinal.load(fis); } catch (IOException e) { @@ -673,7 +678,7 @@ protected List
doInBackground(Void... params) } } boolean hasError = false; - for (Entry entry : favourites.entrySet()) + for (Entry entry : favouritesFinal.entrySet()) { Address addr = AddressLoc.readFromPropEntry(entry); if (addr == null) { hasError = true; } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/MainActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/MainActivity.java index 080df39..9b16085 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/MainActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/MainActivity.java @@ -481,7 +481,8 @@ protected void copyFavourites(File oldMapDir) return; } if (dataFavourites.isEmpty()) { return; } - IO.writeToFile(dataFavourites, newFavourites, false); + boolean success = IO.writeToFile(dataFavourites, newFavourites, false); + if (success) { oldFavourites.delete(); } } @Override protected void onResume() { diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ShowLocationActivity.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ShowLocationActivity.java index 068dc9e..a22a103 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ShowLocationActivity.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/activities/ShowLocationActivity.java @@ -12,6 +12,7 @@ import android.util.Log; import android.widget.Toast; +/** This Activity shows a location via MainActivity, selected from an other app. **/ public class ShowLocationActivity extends AppCompatActivity { public static String locationSearchString; diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapDownloadUnzip.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapDownloadUnzip.java index fd0d288..946d955 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapDownloadUnzip.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapDownloadUnzip.java @@ -20,7 +20,7 @@ public class MapDownloadUnzip { private static Thread unzipThread; - private static List unzipMapQueue = java.util.Collections.synchronizedList(new ArrayList<>()); + private static List unzipMapQueue = java.util.Collections.synchronizedList(new ArrayList()); public static void checkMap(Activity activity, MyMap tmpMap, StatusUpdate stUpdate) { diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapUnzip.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapUnzip.java index 8ba36dc..30be8c8 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapUnzip.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/MapUnzip.java @@ -19,6 +19,9 @@ import android.os.Build; import android.service.notification.StatusBarNotification; import android.util.Log; +import com.junjunguo.pocketmaps.activities.ExportActivity; +import com.junjunguo.pocketmaps.activities.GeocodeActivity; +import java.io.OutputStream; import org.oscim.utils.IOUtils; /** @@ -49,10 +52,11 @@ public void unzip(String zipFilePath, String mapName, ProgressPublisher pp) thro up++; long fSize = entry.getSize(); pp.updateText(false, "" + up + " Unzipping " + mapName, 0); - String filePath = mapFolder.getAbsolutePath() + File.separator + entry.getName(); + String mapFileName = new File(entry.getName()).getName(); + String filePath = mapFolder.getAbsolutePath() + File.separator + mapFileName; if (!entry.isDirectory()) { // if the entry is a file, extracts it - extractFile(zipIn, filePath, pp, "" + up + " Unzipping " + mapName, fSize); + extractFile(zipIn, filePath, pp, "" + up + " Unzipping " + mapName, fSize, null); } else { // if the entry is a directory, make the directory File dir = new File(filePath); @@ -68,10 +72,98 @@ public void unzip(String zipFilePath, String mapName, ProgressPublisher pp) thro } } + /** Unzip an exported-file (.pmz) and import it. **/ + public boolean unzipImport(final String zipFilePath, final Context appContext) + { + ZipInputStream zipIn = null; + try + { + zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); + ZipEntry entry = zipIn.getNextEntry(); + if (entry==null) { return false; } + if (ExportActivity.getFileType(entry.getName()) == ExportActivity.FileType.Map) + { // Is a map file! + String mapName = entry.getName().substring("/maps/".length()); + int index = mapName.indexOf("-gh/"); + if (index <= 0) { return false; } + mapName = mapName.substring(0,index); + final String mapNameFinal = mapName; + + //TODO: RefreshMapList, or use DownloadMapActivity.createStatusUpdater() --> BroadcastReceiver + Thread t = new Thread(new Runnable(){ public void run() + { // Because this may be a long running task, we dont use AsyncTask. + ProgressPublisher pp = new ProgressPublisher(appContext); + pp.updateText(false, "Unzipping " + mapNameFinal, 0); + String finishTxt = "Finish: "; + try + { + unzip(zipFilePath, mapNameFinal, pp); + } + catch (IOException e) { e.printStackTrace(); finishTxt = "Unzip error: "; } + pp.updateTextFinal(finishTxt + mapNameFinal); + }}); + t.start(); + return true; + } + boolean reSettings = false; + boolean reFav = false; + boolean reTrac = false; + while (entry != null) + { + if (ExportActivity.getFileType(entry.getName()) == ExportActivity.FileType.Setting) + { + extractFile(zipIn, entry.getName(), null, "", 0, appContext); + reSettings = true; + } + else if (ExportActivity.getFileType(entry.getName()) == ExportActivity.FileType.Favourites) + { + String favFolder = Variable.getVariable().getMapsFolder().getParent(); + String favFile = new File(favFolder, "Favourites.properties").getPath(); + extractFile(zipIn, favFile, null, "", 0, null); + GeocodeActivity.resetFavourites(); + reFav = true; + } + else if (ExportActivity.getFileType(entry.getName()) == ExportActivity.FileType.Tracking) + { + File tracFolder = Variable.getVariable().getTrackingFolder(); + String tracFile = new File(tracFolder, new File(entry.getName()).getName()).getPath(); + extractFile(zipIn, tracFile, null, "", 0, null); + reTrac = true; + } + entry = zipIn.getNextEntry(); + } + String reSettingsS = "Loaded: "; + if (reSettings) + { + for (Variable.VarType vt : Variable.VarType.values()) + { + Variable.getVariable().loadVariables(vt); + } + reSettingsS += "[Settings] "; + } + if (reFav) + { + reSettingsS += "[Favourites] "; + } + if (reTrac) + { + reSettingsS += "[Tracking-recs] "; + } + ProgressPublisher pp = new ProgressPublisher(appContext); + pp.updateTextFinal(reSettingsS); + } + catch (IOException e) { e.printStackTrace(); return false; } + finally + { + IOUtils.closeQuietly(zipIn); + } + return true; + } + /** Compress files. * @param context If any srcFile is internal, use this context. * @param pp ProgressPublisher may be null. **/ - public boolean compressFiles(ArrayList srcFiles, String tarZipFile, String zipSubDir, ProgressPublisher pp, Context context) + public boolean compressFiles(ArrayList srcFiles, ArrayList zipSubDirs, String tarZipFile, ProgressPublisher pp, Context context) { if (srcFiles.size()==0) { return true; } ZipOutputStream zout = null; @@ -82,34 +174,37 @@ public boolean compressFiles(ArrayList srcFiles, String tarZipFile, Stri byte[] bytesIn = new byte[BUFFER_SIZE]; int fcount = 0; int flen = srcFiles.size(); - for (String curFile : srcFiles) + for (int i=0; i srcFiles, String tarZipFile, Stri /** * Extracts a zip entry (file entry) * - * @param zipIn - * @param filePath - * @param pp - * @param mapName + * @param zipIn The zip + * @param filePath Target path, may be internal, when context not null + * @param pp Show Progress, or may be null + * @param mapName Map name + * @param fSize The file size for progress or 0 for 50. + * @param ppText Text for progress + * @param appContextWhenInternal When filePath is internal, or null for regular file. * @throws IOException */ private void extractFile(ZipInputStream zipIn, String filePath, ProgressPublisher pp, String ppText, - long fSize) throws IOException { + long fSize, + Context appContextWhenInternal) throws IOException { BufferedOutputStream bos = null; try{ - bos = new BufferedOutputStream(new FileOutputStream(filePath)); + if (appContextWhenInternal==null) { bos = new BufferedOutputStream(new FileOutputStream(filePath)); } + else { bos = new BufferedOutputStream(appContextWhenInternal.openFileOutput(filePath, Context.MODE_PRIVATE)); } byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; long readCounter = 0; @@ -145,12 +245,12 @@ private void extractFile(ZipInputStream zipIn, percent = ((float)readCounter) / ((float)fSize); percent = percent * 100.0f; } - pp.updateText(true, ppText, (int)percent); + if (pp!=null) { pp.updateText(true, ppText, (int)percent); } } } finally { - if (bos!=null) bos.close(); + IOUtils.closeQuietly(bos); } } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/ProgressPublisher.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/ProgressPublisher.java index 4b2d9f3..c826546 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/ProgressPublisher.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/downloader/ProgressPublisher.java @@ -94,7 +94,7 @@ private Notification.Builder createNotification(Context c, NotificationManager n } @TargetApi(26) //OREO - public void updateTextFinalOrio(String txt) + private void updateTextFinalOrio(String txt) { // Crazy: Android is blocking when too much notification updates, so final message does not receive. Try later :( int sleepTime = 300; for (int i=0; i<10; i++) diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/Dialog.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/Dialog.java index bbff205..222824f 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/Dialog.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/fragments/Dialog.java @@ -9,15 +9,9 @@ import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.provider.Settings; -import android.speech.tts.Voice; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; -import android.widget.TextView; -import com.junjunguo.pocketmaps.navigator.NaviEngine; -import com.junjunguo.pocketmaps.navigator.NaviVoice; -import com.junjunguo.pocketmaps.util.MyUtility; -import java.util.ArrayList; public class Dialog { diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/model/MyMap.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/model/MyMap.java index 33d3945..fdbf211 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/model/MyMap.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/model/MyMap.java @@ -50,7 +50,7 @@ public MyMap(String mapName) { generateContinentName(mapName); File file = new File(Variable.getVariable().getMapsFolder().getAbsolutePath(), mapName + "-gh"); setUrl(file.getAbsolutePath()); - long lSize = dirSize(file); + long lSize = IO.dirSize(file); if (lSize>1000) { lSize = lSize/100; @@ -59,7 +59,7 @@ public MyMap(String mapName) { } else { - setSize(dirSize(file) + "M"); + setSize(IO.dirSize(file) + "M"); } } @@ -256,25 +256,6 @@ private static String readTimeLocal(String mapName) return timeLocal; } - /** - * Return the size of a directory in megabytes - */ - public long dirSize(File dir) { - if (!dir.exists()) return 0; - - long result = 0; - File[] fileList = dir.listFiles(); - for (int i = 0; i < fileList.length; i++) { - // Recursive call if it's a directory - if (fileList[i].isDirectory()) { - result += dirSize(fileList[i]); - } else { - result += fileList[i].length(); - } - } - return result / (1024 * 1024); - } - public String getCountry() { return country; } @@ -398,7 +379,7 @@ private static void log(String str) { Log.i(MyMap.class.getName(), str); } - private void logUserLong(String str, Activity activity) { + private static void logUserLong(String str, Activity activity) { Log.i(MyMap.class.getName(), str); Toast.makeText(activity.getBaseContext(), str, Toast.LENGTH_LONG).show(); } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/navigator/NaviVoice.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/navigator/NaviVoice.java index 2e6d08e..8c57a00 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/navigator/NaviVoice.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/navigator/NaviVoice.java @@ -18,6 +18,8 @@ import android.speech.tts.Voice; import android.speech.tts.TextToSpeech.OnInitListener; import android.util.Log; +import android.widget.Toast; +import com.junjunguo.pocketmaps.model.MyMap; import java.util.ArrayList; public class NaviVoice @@ -218,9 +220,17 @@ public static void showTtsActivity(Activity activity) public static void showAppActivity(Activity activity, String packageName) { + try + { PackageManager pm = activity.getPackageManager(); Intent intent = pm.getLaunchIntentForPackage(packageName); activity.startActivity(intent); + } + catch (NullPointerException e) + { //Some TTS-Apps are available, but cannot show. (pico) + e.printStackTrace(); + logUserLong("Cannot show", activity); + } } private OnInitListener createInitListener() @@ -240,4 +250,10 @@ private void log(String txt) { Log.i(NaviVoice.class.getName(), txt); } + + private static void logUserLong(String str, Activity activity) + { + Log.i(MyMap.class.getName(), str); + Toast.makeText(activity.getBaseContext(), str, Toast.LENGTH_LONG).show(); + } } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/IO.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/IO.java index 43981bf..3b68e1e 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/IO.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/IO.java @@ -60,6 +60,28 @@ public static String readFromFile(File file, String lineBreak) return sb.toString(); } + /** + * Return the size of a directory in megabytes + */ + public static long dirSize(File dir) + { + if (!dir.exists()) return 0; + + long result = 0; + for (File file : dir.listFiles()) + { // Recursive call if it's a directory + if (file.isDirectory()) + { + result += dirSize(file); + } + else + { + result += file.length(); + } + } + return result / (1024 * 1024); + } + /** From Android 8 (Orio) there is a bug for downloading to sd-card. *
In this case use an internal storage for download. * @param requestedDir The external directory that is desired. */ diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/JsonWrapper.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/JsonWrapper.java index fcd266b..16ff647 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/JsonWrapper.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/JsonWrapper.java @@ -50,4 +50,10 @@ public String getStr(String key, String def) throws JSONException if (obj.has(key)) { return obj.getString(key); } return def; } + + @Override + public String toString() + { + return obj.toString(); + } } diff --git a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/Variable.java b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/Variable.java index 3ed9e8a..b8f2bf9 100644 --- a/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/Variable.java +++ b/PocketMaps/app/src/main/java/com/junjunguo/pocketmaps/util/Variable.java @@ -662,10 +662,15 @@ public ArrayList getSavingFiles() ArrayList list = new ArrayList(); for (String f : context.fileList()) { - if (f.startsWith("pocketmapssavedfile") && f.endsWith(".txt")) { list.add(f); } + if (isSavingFile(f)) { list.add(f); } } return list; } + + public static boolean isSavingFile(String f) + { + return f.startsWith("pocketmapssavedfile") && f.endsWith(".txt"); + } /** * @return read saved file and return it as a string diff --git a/PocketMaps/app/src/main/res/layout/activity_export.xml b/PocketMaps/app/src/main/res/layout/activity_export.xml index 826eba0..6d0a64e 100644 --- a/PocketMaps/app/src/main/res/layout/activity_export.xml +++ b/PocketMaps/app/src/main/res/layout/activity_export.xml @@ -4,50 +4,87 @@ android:orientation="vertical" > - + android:text="Import/Export" /> + + + - + + - - - -