Skip to content

Commit

Permalink
Allow Export and Import of all data. See #110
Browse files Browse the repository at this point in the history
Select maps for export.
Dont keep old favourites-files.
Catch NullPointer on TTS-Engine-Show.
JsonWrapper must also override toString()
  • Loading branch information
Starcommander committed May 29, 2020
1 parent bda7e21 commit 8ea16be
Show file tree
Hide file tree
Showing 21 changed files with 371 additions and 80 deletions.
2 changes: 1 addition & 1 deletion PocketMaps/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</activity>
<activity
android:name=".activities.ExportActivity"
android:label="@string/export"
android:label="@string/exp"
android:screenOrientation="portrait"
android:theme="@style/MYAppTheme" >
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -61,29 +83,48 @@ public void onClick(View v)
return;
}
ArrayList<String> saveList = new ArrayList<String>();
ArrayList<String> saveListDirs = new ArrayList<String>();
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);
Expand All @@ -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())
{
Expand All @@ -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<String> mapFiles = new ArrayList<String>();
ArrayList<String> mapSubDirs = new ArrayList<String>();
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);
}
}

Expand All @@ -150,6 +246,28 @@ private void fillSpinner()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
adapter.addAll(IO.listSelectionPaths(this, false));
exSpinner.setAdapter(adapter);
exSpinner.setOnItemSelectedListener(this);
}
private void fillTypeSpinner()
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -653,6 +657,7 @@ else if (favourites == null)
loadProp = true;
}
final boolean loadPropFinal = loadProp;
final Properties favouritesFinal = favourites;
new AsyncTask<Void, Void, List<Address>>()
{
String errMsg;
Expand All @@ -664,7 +669,7 @@ protected List<Address> doInBackground(Void... params)
{
try (FileInputStream fis = new FileInputStream(propFile))
{
favourites.load(fis);
favouritesFinal.load(fis);
}
catch (IOException e)
{
Expand All @@ -673,7 +678,7 @@ protected List<Address> doInBackground(Void... params)
}
}
boolean hasError = false;
for (Entry<Object, Object> entry : favourites.entrySet())
for (Entry<Object, Object> entry : favouritesFinal.entrySet())
{
Address addr = AddressLoc.readFromPropEntry(entry);
if (addr == null) { hasError = true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class MapDownloadUnzip
{
private static Thread unzipThread;
private static List<MyMap> unzipMapQueue = java.util.Collections.synchronizedList(new ArrayList<>());
private static List<MyMap> unzipMapQueue = java.util.Collections.synchronizedList(new ArrayList<MyMap>());

public static void checkMap(Activity activity, MyMap tmpMap, StatusUpdate stUpdate)
{
Expand Down
Loading

0 comments on commit 8ea16be

Please sign in to comment.