Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityExperiments.java
Show All 25 Lines | |||||
import android.widget.Button; | import android.widget.Button; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import it.reyboz.bustorino.backend.Fetcher; | import it.reyboz.bustorino.backend.Fetcher; | ||||
import it.reyboz.bustorino.backend.gtfs.GtfsDataParser; | import it.reyboz.bustorino.backend.gtfs.GtfsDataParser; | ||||
import it.reyboz.bustorino.backend.networkTools; | import it.reyboz.bustorino.backend.networkTools; | ||||
import it.reyboz.bustorino.backend.utils; | import it.reyboz.bustorino.backend.utils; | ||||
import it.reyboz.bustorino.data.gtfs.GtfsDatabase; | |||||
import it.reyboz.bustorino.data.gtfs.StaticGtfsDao; | |||||
import it.reyboz.bustorino.middleware.GeneralActivity; | import it.reyboz.bustorino.middleware.GeneralActivity; | ||||
import java.io.*; | import java.io.*; | ||||
import java.net.MalformedURLException; | import java.net.MalformedURLException; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.util.Date; | import java.util.*; | ||||
import java.util.List; | |||||
import java.util.Locale; | |||||
import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | ||||
import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
import java.util.zip.ZipEntry; | import java.util.zip.ZipEntry; | ||||
import java.util.zip.ZipFile; | import java.util.zip.ZipFile; | ||||
import java.util.zip.ZipInputStream; | import java.util.zip.ZipInputStream; | ||||
public class ActivityExperiments extends GeneralActivity { | public class ActivityExperiments extends GeneralActivity { | ||||
ExecutorService executorService; | |||||
final static String DEBUG_TAG = "ExperimentsGTFS"; | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.activity_experiments); | setContentView(R.layout.activity_experiments); | ||||
Button deleteButton = findViewById(R.id.deleteButton); | Button deleteButton = findViewById(R.id.deleteButton); | ||||
if(deleteButton!=null) | if(deleteButton!=null) | ||||
deleteButton.setOnClickListener(view -> { | deleteButton.setOnClickListener(view -> { | ||||
File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | ||||
if(!saveFile.isDirectory() && saveFile.exists()){ | if(!saveFile.isDirectory() && saveFile.exists()){ | ||||
//delete the file | //delete the file | ||||
if(saveFile.delete()) | if(saveFile.delete()) | ||||
Toast.makeText(this, "Gtfs zip deleted", Toast.LENGTH_SHORT).show(); | Toast.makeText(this, "Gtfs zip deleted", Toast.LENGTH_SHORT).show(); | ||||
else | else | ||||
Toast.makeText(this, "Cannot delete gtfs zip", Toast.LENGTH_SHORT).show(); | Toast.makeText(this, "Cannot delete gtfs zip", Toast.LENGTH_SHORT).show(); | ||||
} else | } else | ||||
Toast.makeText(this, "Gtfs data zip not present", Toast.LENGTH_SHORT).show(); | Toast.makeText(this, "Gtfs data zip not present", Toast.LENGTH_SHORT).show(); | ||||
}); | }); | ||||
Button cleanDBButton = findViewById(R.id.deleteDbButton); | |||||
if(cleanDBButton!=null) | |||||
cleanDBButton.setOnClickListener(this::deleteDatabase); | |||||
executorService = Executors.newFixedThreadPool(2); | |||||
} | } | ||||
public void runExp(View v){ | public void runExp(View v){ | ||||
final Context appContext = v.getContext().getApplicationContext(); | final Context appContext = v.getContext().getApplicationContext(); | ||||
Runnable run = new Runnable() { | Runnable run = new Runnable() { | ||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
final String DEBUG_TAG = "ExperimentsGTFS"; | |||||
AtomicReference<Fetcher.Result> res = new AtomicReference<>(); | AtomicReference<Fetcher.Result> res = new AtomicReference<>(); | ||||
//List<String> files = GtfsDataParser.readFilesList(res); | //List<String> files = GtfsDataParser.readFilesList(res); | ||||
Date updateDate = GtfsDataParser.getLastGTFSUpdateDate(res); | Date updateDate = GtfsDataParser.getLastGTFSUpdateDate(res); | ||||
Log.w( | Log.w( | ||||
"ExperimentGTFS", "Last update date is " + updateDate//utils.joinList(files, "\n") | "ExperimentGTFS", "Last update date is " + updateDate//utils.joinList(files, "\n") | ||||
); | ); | ||||
//Toast.makeText(v.getContext(), "Gtfs data already downloaded", Toast.LENGTH_SHORT).show(); | //Toast.makeText(v.getContext(), "Gtfs data already downloaded", Toast.LENGTH_SHORT).show(); | ||||
StaticGtfsDao dao = GtfsDatabase.Companion.getGtfsDatabase(appContext).gtfsDao(); | |||||
Log.d(DEBUG_TAG, String.valueOf(dao)); | |||||
dao.deleteAllStopTimes(); | |||||
File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | ||||
if (!saveFile.isDirectory() && saveFile.exists()) { | if (!saveFile.isDirectory() && saveFile.exists()) { | ||||
Log.w(DEBUG_TAG, "Zip exists: " + saveFile); | Log.w(DEBUG_TAG, "Zip exists: " + saveFile); | ||||
try (FileInputStream fileStream = new FileInputStream(saveFile)) { | |||||
ZipInputStream stream = new ZipInputStream(fileStream); | |||||
try (ZipFile zipFile = new ZipFile(saveFile)) { | |||||
//ZipInputStream stream = new ZipInputStream(fileStream); | |||||
// now iterate through each item in the stream. The get next | // now iterate through each item in the stream. The get next | ||||
// entry call will return a ZipEntry for each file in the | // entry call will return a ZipEntry for each file in the | ||||
// stream | // stream | ||||
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | |||||
ZipEntry entry; | ZipEntry entry; | ||||
String line; | String line; | ||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); | //final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); | ||||
HashSet<ZipEntry> readLater = new HashSet<>(); | |||||
while(entries.hasMoreElements()){ | |||||
entry = entries.nextElement(); | |||||
//String tableName = entry.getName().split("\\.")[0].trim(); | |||||
if(entry.getName().trim().equals("stop_times.txt")) { | |||||
readLater.add(entry); | |||||
continue; | |||||
} | |||||
GtfsDataParser.readGtfsZipEntry(entry, zipFile, v.getContext().getApplicationContext()); | |||||
} | |||||
for(ZipEntry laterEntry: readLater){ | |||||
GtfsDataParser.readGtfsZipEntry(laterEntry, zipFile, v.getContext().getApplicationContext()); | |||||
} | |||||
//Toast.makeText(appContext, "D", Toast.LENGTH_SHORT).show(); | |||||
/* | |||||
while ((entry = stream.getNextEntry()) != null) { | while ((entry = stream.getNextEntry()) != null) { | ||||
String s = String.format(Locale.ENGLISH, "Entry: %s len %d added", | String s = String.format(Locale.ENGLISH, "Entry: %s len %d added", | ||||
entry.getName(), | entry.getName(), | ||||
entry.getSize() | entry.getSize() | ||||
); | ); | ||||
if(entry.getName().contains("stop_times.")){ | |||||
//skip and do later | |||||
} | |||||
//Toast.makeText(v.getContext(), "File: " + entry.getName(), Toast.LENGTH_SHORT).show(); | //Toast.makeText(v.getContext(), "File: " + entry.getName(), Toast.LENGTH_SHORT).show(); | ||||
Log.d(DEBUG_TAG, s); | Log.d(DEBUG_TAG, s); | ||||
//read data in table | //read data in table | ||||
final String tableName = entry.getName().split("\\.")[0].trim(); | final String tableName = entry.getName().split("\\.")[0].trim(); | ||||
GtfsDataParser.readCSVWithColumns(reader, tableName, v.getContext().getApplicationContext()); | |||||
// Once we get the entry from the stream, the stream is | // Once we get the entry from the stream, the stream is | ||||
// positioned read to read the raw data, and we keep | // positioned read to read the raw data, and we keep | ||||
// reading until read returns 0 or less. | // reading until read returns 0 or less. | ||||
//result.add(entry.getName()); | //result.add(entry.getName()); | ||||
} | } | ||||
stream.close(); | stream.close(); | ||||
*/ | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
} | } | ||||
//saveFile.delete(); | //saveFile.delete(); | ||||
} else | } else | ||||
try { | try { | ||||
//Toast.makeText(v.getContext(), "Downloading gtfs data", Toast.LENGTH_SHORT).show(); | //Toast.makeText(v.getContext(), "Downloading gtfs data", Toast.LENGTH_SHORT).show(); | ||||
networkTools.saveFileInCache(saveFile, new URL(GtfsDataParser.GTFS_ADDRESS)); | networkTools.saveFileInCache(saveFile, new URL(GtfsDataParser.GTFS_ADDRESS)); | ||||
Log.w(DEBUG_TAG, "File saved"); | Log.w(DEBUG_TAG, "File saved"); | ||||
} catch (MalformedURLException e) { | } catch (MalformedURLException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
ExecutorService executorService = Executors.newFixedThreadPool(2); | |||||
Toast.makeText(this, "Launching, no result will show", Toast.LENGTH_SHORT).show(); | |||||
//Looper looper = new Looper(true); | //Looper looper = new Looper(true); | ||||
//Handler handler = new Handler(); | //Handler handler = new Handler(); | ||||
//handler.post(run); | //handler.post(run); | ||||
executorService.execute(run); | executorService.execute(run); | ||||
} | } | ||||
public void deleteDatabase(View v){ | |||||
final Context con = getApplicationContext().getApplicationContext(); | |||||
Toast.makeText(this, "Deleting GTFS DB contents, wait a few seconds", Toast.LENGTH_SHORT).show(); | |||||
Runnable deleteDB = new Runnable() { | |||||
@Override | |||||
public void run() { | |||||
StaticGtfsDao dao = GtfsDatabase.Companion.getGtfsDatabase(con).gtfsDao(); | |||||
Log.d(DEBUG_TAG, String.valueOf(dao)); | |||||
dao.deleteAllStopTimes(); | |||||
dao.deleteAllTrips(); | |||||
dao.deleteAllRoutes(); | |||||
dao.deleteAllStops(); | |||||
dao.deleteAllServices(); | |||||
Log.d(DEBUG_TAG, "Deleted stuff"); | |||||
} | |||||
}; | |||||
executorService.execute(deleteDB); | |||||
} | |||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register