Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityExperiments.java
Show All 11 Lines | BusTO - Data components | ||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino; | package it.reyboz.bustorino; | ||||
import android.content.Context; | |||||
import android.util.Log; | |||||
import android.view.View; | |||||
import android.widget.Button; | |||||
import android.widget.Toast; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import it.reyboz.bustorino.backend.Fetcher; | import it.reyboz.bustorino.fragments.LinesDetailFragment; | ||||
import it.reyboz.bustorino.backend.gtfs.GtfsDataParser; | |||||
import it.reyboz.bustorino.backend.networkTools; | |||||
import it.reyboz.bustorino.data.gtfs.GtfsDatabase; | |||||
import it.reyboz.bustorino.data.gtfs.GtfsDBDao; | |||||
import it.reyboz.bustorino.middleware.GeneralActivity; | import it.reyboz.bustorino.middleware.GeneralActivity; | ||||
import java.io.*; | |||||
import java.net.MalformedURLException; | |||||
import java.net.URL; | |||||
import java.util.*; | |||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.concurrent.atomic.AtomicReference; | |||||
import java.util.zip.ZipEntry; | |||||
import java.util.zip.ZipFile; | |||||
public class ActivityExperiments extends GeneralActivity { | public class ActivityExperiments extends GeneralActivity { | ||||
ExecutorService executorService; | |||||
final static String DEBUG_TAG = "ExperimentsGTFS"; | 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); | if (savedInstanceState==null) { | ||||
if(deleteButton!=null) | getSupportFragmentManager().beginTransaction() | ||||
deleteButton.setOnClickListener(view -> { | .setReorderingAllowed(true) | ||||
File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | .add(R.id.fragment_container_view, LinesDetailFragment.class, | ||||
if(!saveFile.isDirectory() && saveFile.exists()){ | LinesDetailFragment.Companion.makeArgs("gtt:56U")) | ||||
//delete the file | .commit(); | ||||
if(saveFile.delete()) | |||||
Toast.makeText(this, "Gtfs zip deleted", Toast.LENGTH_SHORT).show(); | |||||
else | |||||
Toast.makeText(this, "Cannot delete gtfs zip", Toast.LENGTH_SHORT).show(); | |||||
} else | |||||
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){ | |||||
final Context appContext = v.getContext().getApplicationContext(); | |||||
Runnable run = new Runnable() { | |||||
@Override | |||||
public void run() { | |||||
AtomicReference<Fetcher.Result> res = new AtomicReference<>(); | |||||
//List<String> files = GtfsDataParser.readFilesList(res); | |||||
Date updateDate = GtfsDataParser.getLastGTFSUpdateDate(res); | |||||
Log.w( | |||||
"ExperimentGTFS", "Last update date is " + updateDate//utils.joinList(files, "\n") | |||||
); | |||||
//Toast.makeText(v.getContext(), "Gtfs data already downloaded", Toast.LENGTH_SHORT).show(); | |||||
GtfsDBDao dao = GtfsDatabase.Companion.getGtfsDatabase(appContext).gtfsDao(); | |||||
Log.d(DEBUG_TAG, String.valueOf(dao)); | |||||
dao.deleteAllStopTimes(); | |||||
File saveFile = new File(getFilesDir(), "gtfs_data.zip"); | |||||
if (!saveFile.isDirectory() && saveFile.exists()) { | |||||
Log.w(DEBUG_TAG, "Zip exists: " + saveFile); | |||||
try (ZipFile zipFile = new ZipFile(saveFile)) { | |||||
//ZipInputStream stream = new ZipInputStream(fileStream); | |||||
// now iterate through each item in the stream. The get next | |||||
// entry call will return a ZipEntry for each file in the | |||||
// stream | |||||
/* | |||||
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | |||||
ZipEntry entry; | |||||
String line; | |||||
//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()); | |||||
} | |||||
*/ | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
//saveFile.delete(); | |||||
} else | |||||
try { | |||||
//Toast.makeText(v.getContext(), "Downloading gtfs data", Toast.LENGTH_SHORT).show(); | |||||
networkTools.saveFileInCache(saveFile, new URL(GtfsDataParser.GTFS_ADDRESS)); | |||||
Log.w(DEBUG_TAG, "File saved"); | |||||
} catch (MalformedURLException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
}; | |||||
Toast.makeText(this, "Test disabled", Toast.LENGTH_SHORT).show(); | |||||
//Looper looper = new Looper(true); | |||||
//Handler handler = new Handler(); | |||||
//handler.post(run); | |||||
//executorService.execute(run); | |||||
} | |||||
public void deleteDatabase(View v){ | |||||
//final Context con = getApplicationContext().getApplicationContext(); | |||||
Toast.makeText(this, "Deleting GTFS DB contents isn't allowed anymore", Toast.LENGTH_SHORT).show(); | |||||
} | } | ||||
} | } | ||||
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