Last active
January 2, 2016 20:07
-
-
Save cliff76/301b05e43176681aadad to your computer and use it in GitHub Desktop.
A conversion of my MainActivity.java
This file contains hidden or 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.cliftoncraig.apps.imagegallery | |
| import android.app.ListActivity | |
| import android.content.Context | |
| import android.net.Uri | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.view.KeyEvent | |
| import android.view.Menu | |
| import android.view.MenuItem | |
| import android.view.inputmethod.InputMethodManager | |
| import android.widget.EditText | |
| import android.widget.TextView | |
| import com.cliftoncraig.libs.parse.ResponseParse | |
| import java.io.BufferedReader | |
| import java.io.File | |
| import java.io.IOException | |
| import java.io.InputStream | |
| import java.io.InputStreamReader | |
| import java.net.MalformedURLException | |
| import java.net.URL | |
| import java.util.ArrayList | |
| import java.util.HashMap | |
| class MainActivity : ListActivity() { | |
| private var adapter: RowAdapter? = null | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val searchField = findViewById(R.id.searchField) as EditText | |
| handleEnterKey(searchField, Runnable { doSearch(searchField.text.toString()) }) | |
| adapter = RowAdapter(this) | |
| listAdapter = adapter | |
| } | |
| private fun createDataItems(): ArrayList<Map<String, String>> { | |
| val data = ArrayList<Map<String, String>>() | |
| data.add(makeItem( | |
| "image1", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image2", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image3", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg")) | |
| data.add(makeItem( | |
| "image1", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image2", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image3", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg")) | |
| data.add(makeItem( | |
| "image1", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image2", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg", | |
| "image3", "https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg")) | |
| return data | |
| } | |
| private fun createDataItems(parsedURLs: List<URL>): List<Map<String, String>> { | |
| val data = ArrayList<Map<String, String>>() | |
| val numberOfRows = parsedURLs.size / 3 | |
| val urls = parsedURLs.iterator() | |
| for (i in 0..numberOfRows - 1) { | |
| data.add(makeItem( | |
| "image1", urls.next().toString(), | |
| "image2", urls.next().toString(), | |
| "image3", urls.next().toString())) | |
| } | |
| val extra = parsedURLs.size % 3 | |
| if (extra > 0) { | |
| data.add(makeItem( | |
| "image1", if (extra > 0) urls.next().toString() else "", | |
| "image2", if (extra > 1) urls.next().toString() else "", | |
| "image3", if (extra > 2) urls.next().toString() else "")) | |
| } | |
| return data | |
| } | |
| private fun handleEnterKey(searchField: EditText, onEnterAction: Runnable) { | |
| searchField.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event -> | |
| if (event != null && event.action == KeyEvent.ACTION_DOWN) { | |
| val inputMethod = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | |
| inputMethod.hideSoftInputFromWindow(searchField.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) | |
| Thread(onEnterAction).start() | |
| return@OnEditorActionListener true | |
| } | |
| false | |
| }) | |
| } | |
| private fun doSearch(searchString: String) { | |
| // URL[] imageUrls = getImages(); | |
| val urlSpec = FLICKR_SEARCH_SPEC.format(searchString) | |
| val url: URL | |
| try { | |
| url = URL(urlSpec) | |
| } catch (e: MalformedURLException) { | |
| throw RuntimeException("Invalid search URL " + urlSpec, e) | |
| } | |
| val responseStream: InputStream | |
| try { | |
| responseStream = url.openStream() | |
| } catch (e: IOException) { | |
| throw RuntimeException("Could not load response for search URL " + urlSpec, e) | |
| } | |
| // logServerResponse(urlSpec, responseStream); | |
| adapter!!.setData(createDataItems(ResponseParse().parse(responseStream))) | |
| runOnUiThread { adapter!!.notifyDataSetChanged() } | |
| } | |
| private fun logServerResponse(urlSpec: String, responseStream: InputStream) { | |
| val response = asString(responseStream) | |
| Log.i(javaClass.getName(), "***Server response***") | |
| Log.i(javaClass.getName(), urlSpec) | |
| Log.i(javaClass.getName(), response) | |
| Log.i(javaClass.getName(), "***Server response***") | |
| } | |
| private fun asString(inputStream: InputStream): String { | |
| val reader = BufferedReader(InputStreamReader(inputStream)) | |
| val result = StringBuffer() | |
| try { | |
| var eachLine: String? = reader.readLine() | |
| while (eachLine != null) { | |
| result.append(eachLine) | |
| eachLine = reader.readLine() | |
| } | |
| } catch (e: IOException) { | |
| throw RuntimeException("Error readinf input stream.", e) | |
| } | |
| return result.toString() | |
| } | |
| private fun makeItem(key1: String, value1: String, key2: String, value2: String, key3: String, value3: String): HashMap<String, String> { | |
| val item = HashMap<String, String>() | |
| item.put(key1, value1) | |
| item.put(key2, value2) | |
| item.put(key3, value3) | |
| return item | |
| } | |
| override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| menuInflater.inflate(R.menu.menu_main, menu) | |
| return true | |
| } | |
| override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| val id = item.itemId | |
| //noinspection SimplifiableIfStatement | |
| if (id == R.id.action_settings) { | |
| return true | |
| } | |
| return super.onOptionsItemSelected(item) | |
| } | |
| companion object { | |
| val FLICKR_SEARCH_SPEC = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=af47a934f43272042601c3f903959027&format=json&nojsoncallback=1&text=%s" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment