Created
April 15, 2019 20:07
-
-
Save jeluchu/e0590dbc302437f30b3c6ecdd9b1f63e to your computer and use it in GitHub Desktop.
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter unused
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jeluchu.movimadrid.activity | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import android.graphics.Bitmap | |
import android.graphics.BitmapFactory | |
import android.os.AsyncTask | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.* | |
import androidx.appcompat.app.AppCompatActivity | |
import com.jeluchu.movimadrid.R | |
import com.jeluchu.movimadrid.data.DataModel | |
import kotlinx.android.synthetic.main.activity_cameras.* | |
import org.w3c.dom.Document | |
import org.w3c.dom.Element | |
import org.xml.sax.SAXException | |
import java.io.File | |
import java.io.FileNotFoundException | |
import java.io.IOException | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import javax.xml.parsers.DocumentBuilderFactory | |
import javax.xml.parsers.ParserConfigurationException | |
class CamerasActivity : AppCompatActivity(), AdapterView.OnItemClickListener { | |
private val URL_CAMERAS = "http://informo.madrid.es/informo/tmadrid/CCTV.kml" | |
private var listView: ListView? = null | |
private var imageView: ImageView? = null | |
private var adapter: ArrayAdapter<String>? = null | |
private val dataModel = DataModel() | |
private var selected_position = -1 | |
private var loaded_cam_image: Bitmap? = null | |
private val POSITION_KEY = "sel_pos" | |
private val BITMAP_FILENAME_KEY = "loaded_bmp" | |
private val CAMERA_IMAGE_FILENAME = "snapCam.jpg" | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_cameras) | |
} | |
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { | |
selected_position = Integer.parseInt(dataModel.getCameraIdFromName(parent.adapter.getItem(position).toString())) | |
val task = DownloadImageTask() | |
task.execute(dataModel.getCameraURLAtPosition(selected_position)) | |
} | |
private fun saveBitmapToFile(bmpToSave: Bitmap, fileName: String) { | |
try { | |
val f = File(filesDir, fileName) | |
if (f.exists()) { | |
f.delete() | |
} | |
val fos = openFileOutput(fileName, 0) | |
bmpToSave.compress(Bitmap.CompressFormat.PNG, 85, fos) | |
fos.close() | |
} catch (e: FileNotFoundException) { | |
e.printStackTrace() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
private fun retrieveBitmapFromFile(fileName: String?): Bitmap? { | |
File(filesDir, fileName) | |
return try { | |
val fis = openFileInput(fileName) | |
val readBmp = BitmapFactory.decodeStream(fis) | |
fis.close() | |
readBmp | |
} catch (e: FileNotFoundException) { | |
e.printStackTrace() | |
null | |
} catch (e: IOException) { | |
e.printStackTrace() | |
null | |
} | |
} | |
@SuppressLint("StaticFieldLeak") | |
private inner class DownloadKMLTask : AsyncTask<String, Void, Void>() { | |
private var contentType = "" | |
private var error = false | |
private var errorMessage: String? = null | |
override fun doInBackground(vararg urls: String): Void? { | |
val urlConnection: HttpURLConnection | |
try { | |
val url = URL(urls[0]) | |
urlConnection = url.openConnection() as HttpURLConnection | |
contentType = urlConnection.contentType | |
val `is` = urlConnection.inputStream | |
if (contentType.contentEquals("application/vnd.google-earth.kml+xml")) { | |
var document: Document? = null | |
try { | |
val factory = DocumentBuilderFactory.newInstance() | |
factory.isExpandEntityReferences = false | |
factory.isIgnoringComments = true | |
factory.isIgnoringElementContentWhitespace = true | |
val builder = factory.newDocumentBuilder() | |
document = builder.parse(`is`) | |
} catch (e: ParserConfigurationException) { | |
dataModel.addCameraData(e.toString(), "", "", "") | |
} catch (e: SAXException) { | |
dataModel.addCameraData(e.toString(), "", "", "") | |
} catch (e: IOException) { | |
dataModel.addCameraData(e.toString(), "", "", "") | |
} | |
val placemarkList = document!!.getElementsByTagName("Placemark") | |
dataModel.clearCameraData() | |
for (i in 0 until placemarkList.length) { | |
val placemark = placemarkList.item(i) as Element | |
val descriptionList = placemark.getElementsByTagName("description") | |
val description = descriptionList.item(0) as Element | |
var camera = description.textContent | |
camera = camera.substring(camera.indexOf("s/Camara")) | |
camera = camera.substring(0, camera.indexOf(".jpg") + 4) | |
var cameraURL = "http://informo.munimadrid.es/camera" | |
cameraURL += camera | |
val pointList = placemark.getElementsByTagName("Point") | |
val point = pointList.item(0) as Element | |
val coordinates = point.getElementsByTagName("coordinates") | |
val coordString = coordinates.item(0).textContent | |
val extendedDataList = placemark.getElementsByTagName("ExtendedData") | |
val extendedData = extendedDataList.item(0) as Element | |
val dataList = extendedData.getElementsByTagName("Data") | |
val data = dataList.item(1) as Element | |
val values = data.getElementsByTagName("Value") | |
val cameraName = values.item(0).textContent | |
dataModel.addCameraData(i.toString(), cameraName, coordString, cameraURL) | |
} | |
} else { | |
error = true | |
errorMessage = "$contentType not processed" | |
} | |
urlConnection.disconnect() | |
} catch (e: Exception) { | |
error = true | |
errorMessage = e.toString() | |
} | |
return null | |
} | |
override fun onPostExecute(unused: Void) { | |
if (error) { | |
Toast.makeText(this@CamerasActivity, errorMessage, Toast.LENGTH_SHORT).show() | |
} else { | |
adapter!!.clear() | |
adapter!!.addAll(dataModel.getCameraListNames()) | |
adapter!!.notifyDataSetChanged() | |
if (selected_position != -1) { | |
listView?.setSelection(selected_position) | |
listView!!.setItemChecked(selected_position, true) | |
listView!!.smoothScrollToPosition(selected_position) | |
if (loaded_cam_image != null) { | |
imageView!!.isClickable = true | |
imageView!!.setImageBitmap(loaded_cam_image) | |
} | |
} | |
listView!!.isEnabled = true | |
} | |
} | |
} | |
@SuppressLint("StaticFieldLeak") | |
private inner class DownloadImageTask : AsyncTask<String, Void, Bitmap>() { | |
private var contentType = "" | |
private var error = false | |
private var errorMessage: String? = null | |
override fun onPreExecute() { | |
listView!!.isEnabled = false | |
} | |
override fun doInBackground(vararg urls: String): Bitmap? { | |
var bitmap: Bitmap? = null | |
val urlConnection: HttpURLConnection? | |
try { | |
val url = URL(urls[0]) | |
urlConnection = url.openConnection() as HttpURLConnection | |
contentType = urlConnection.contentType | |
val `is` = urlConnection.inputStream | |
if (contentType.indexOf("image") != -1) { | |
bitmap = BitmapFactory.decodeStream(`is`) | |
} else { | |
error = true | |
errorMessage = "$contentType not processed" | |
} | |
urlConnection.disconnect() | |
} catch (e: Exception) { | |
error = true | |
errorMessage = e.toString() | |
} | |
return bitmap | |
} | |
override fun onPostExecute(bitmap: Bitmap) { | |
if (error) { | |
Toast.makeText(this@CamerasActivity, errorMessage, Toast.LENGTH_SHORT).show() | |
listView!!.isEnabled = true | |
} else { | |
loaded_cam_image = bitmap | |
imageView!!.setImageBitmap(loaded_cam_image) | |
listView!!.isEnabled = true | |
} | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
imgCamera.isClickable = false | |
val sharedPref = this.getPreferences(Context.MODE_PRIVATE) | |
selected_position = sharedPref.getInt(POSITION_KEY, -1) | |
if (sharedPref.contains(BITMAP_FILENAME_KEY)) { | |
loaded_cam_image = retrieveBitmapFromFile( | |
sharedPref.getString(BITMAP_FILENAME_KEY, CAMERA_IMAGE_FILENAME) | |
) | |
} | |
dataModel.addCameraData("Getting cameras list... Please wait!!!", "", "", "") | |
listCamera.onItemClickListener = this | |
adapter = ArrayAdapter(this, R.layout.item_camera, dataModel.getCameraListNames()) | |
listCamera.adapter = adapter | |
listCamera.choiceMode = ListView.CHOICE_MODE_SINGLE | |
listCamera.isEnabled = false | |
val task = DownloadKMLTask() | |
task.execute(URL_CAMERAS) | |
} | |
override fun onPause() { | |
super.onPause() | |
val sharedPref = this.getPreferences(Context.MODE_PRIVATE) | |
val editor = sharedPref.edit() | |
editor.putInt(POSITION_KEY, selected_position) | |
if (loaded_cam_image != null) { | |
editor.putString(BITMAP_FILENAME_KEY, CAMERA_IMAGE_FILENAME) | |
saveBitmapToFile(loaded_cam_image!!, CAMERA_IMAGE_FILENAME) | |
} | |
editor.apply() | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
Toast.makeText(this, "¡Pásalo bien!", Toast.LENGTH_SHORT).show() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment