Permalink
Browse files

Suggest app from Hello Android

  • Loading branch information...
0 parents commit 43c64d1c5022ddc805f079fd042027b15b00d2b3 @anthonymoralez committed Sep 7, 2012
@@ -0,0 +1,2 @@
+bin
+gen
1 .rvmrc
@@ -0,0 +1 @@
+rvm use jruby
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="org.example.suggest"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <uses-permission android:name="android.permission.INTERNET" />
+ <application android:label="@string/app_name"
+ android:debuggable="true"
+ android:icon="@drawable/ic_launcher">
+
+ <activity android:name="Suggest"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ </application>
+</manifest>
@@ -0,0 +1,4 @@
+source :gemcutter
+gem 'rake'
+gem "mirah"
+gem "pindah", :git => 'https://github.com/mirah/pindah.git'
@@ -0,0 +1,22 @@
+GIT
+ remote: https://github.com/mirah/pindah.git
+ revision: 835604f0e5ae3eeb58e9475e92ef9ae7d8c7bdca
+ specs:
+ pindah (0.1.3.dev)
+ mirah (>= 0.0.10.dev)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ bitescript (0.1.0)
+ mirah (0.0.12-java)
+ bitescript (>= 0.0.8)
+ rake (0.9.2.2)
+
+PLATFORMS
+ java
+
+DEPENDENCIES
+ mirah
+ pindah!
+ rake
@@ -0,0 +1,7 @@
+require "rubygems"
+require "pindah"
+
+Pindah.spec = {
+ :name => "suggest",
+ :target_version => "2.3.3"
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent" >
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/original_label" />
+ <EditText
+ android:id="@+id/original_text"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:hint="@string/original_hint"
+ android:inputType="textNoSuggestions"
+ android:padding="10dip"
+ android:textSize="18sp" />
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/result_label" />
+ <ListView
+ android:id="@+id/result_list"
+ android:layout_width="fill_parent"
+ android:layout_height="0dip"
+ android:layout_weight="1" />
+</LinearLayout>
+
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="app_name">Suggest</string>
+ <string name="original_hint">Enter partial text</string>
+ <string name="original_label">Original text</string>
+ <string name="result_label">Suggestions:</string>
+ <string name="working">Working...</string>
+ <string name="error">(Web service error)</string>
+ <string name="interrupted">(Web service interrupted)</string>
+ <string name="no_results">(No suggestions)</string>
+</resources>
@@ -0,0 +1,138 @@
+package org.example.suggest
+
+import android.app.Activity
+import android.app.SearchManager
+import android.content.Intent
+import android.os.Handler
+import android.text.TextWatcher
+import android.text.Editable
+import android.widget.ArrayAdapter
+import android.widget.EditText
+import android.widget.ListView
+import android.R.layout as AndroidLayout
+import android.util.Log
+
+import java.util.concurrent.Executors
+import java.util.ArrayList
+import java.util.List
+import java.util.concurrent.RejectedExecutionException
+import java.lang.InterruptedException
+
+class Suggest < Activity
+ class SuggestTextWatcher
+ implements TextWatcher
+ def initialize(outer:Suggest)
+ @outer = outer
+ end
+ def beforeTextChanged(s:CharSequence, start:int, before:int, count:int):void
+ #nop
+ end
+ def onTextChanged(s:CharSequence, start:int, before:int, count:int):void
+ Log.d("onTextChanged", "#{s}")
+ @outer.queueUpdate(1000)
+ end
+ def afterTextChanged(e:Editable)
+ #nop
+ end
+ end
+
+ class UpdateTask
+ implements Runnable
+ def initialize(outer:Suggest)
+ @outer = outer
+ end
+ def run:void
+ original = @outer.origText.getText.toString.trim
+ Log.d("UpdateTask", "#{original}")
+ @suggPending.cancel(true) if (@suggPending != nil)
+
+ if (original.length != 0)
+ @outer.setText(R.string.working)
+ begin
+ suggestTask = SuggestTask.new(@outer, original)
+ @suggPending = @outer.suggThread.submit(suggestTask)
+ rescue RejectedExecutionException => e
+ @outer.setText(R.string.error)
+ end
+ end
+ end
+ end
+
+ def onCreate(state)
+ super state
+ setContentView R.layout.main
+ initThreading
+ findViews
+ setListeners
+ setAdapters
+ end
+
+ def origText
+ @origText
+ end
+
+ def suggThread
+ @suggThread
+ end
+
+ def initThreading:void
+ @guiThread = Handler.new
+ @suggThread = Executors.newSingleThreadExecutor
+ @updateTask = UpdateTask.new(self)
+ end
+
+ def findViews:void
+ @origText = EditText(findViewById(R.id.original_text))
+ @suggList = ListView(findViewById(R.id.result_list))
+ end
+
+ def setListeners:void
+ @origText.addTextChangedListener(SuggestTextWatcher.new(self))
+ this = self
+ @suggList.setOnItemClickListener do |parent, view, position, id|
+ query = String(parent.getItemAtPosition(position))
+ this.doSearch(query)
+ end
+ end
+
+ def setAdapters:void
+ @items = ArrayList.new
+ @adapter = ArrayAdapter.new(self, AndroidLayout.simple_list_item_1, @items)
+ @suggList.setAdapter(@adapter)
+ end
+
+ def doSearch(query:String)
+ intent = Intent.new(Intent.ACTION_WEB_SEARCH)
+ intent.putExtra(SearchManager.QUERY, query)
+ startActivity(intent)
+ end
+
+ def queueUpdate(delayMillis:long)
+ @guiThread.removeCallbacks(@updateTask)
+ @guiThread.postDelayed(@updateTask, delayMillis)
+ end
+
+ def setText(id:int):void
+ @adapter.clear
+ @adapter.add(getResources.getString(id))
+ end
+
+ def setList(list:List):void
+ @adapter.clear
+ for item in list
+ @adapter.add(item)
+ end
+ end
+
+ def setSuggestions(suggestions:List):void
+ guiSetList(@suggList, suggestions)
+ end
+
+ def guiSetList(view:ListView, list:List):void
+ this = self
+ @guiThread.post do
+ this.setList(list)
+ end
+ end
+
+end
@@ -0,0 +1,100 @@
+package org.example.suggest
+
+import java.io.IOException
+import java.net.HttpURLConnection
+import java.net.URL
+import java.net.URLEncoder
+import java.util.LinkedList
+import java.util.concurrent.RejectedExecutionException
+import java.lang.InterruptedException
+import java.lang.Thread
+import org.xmlpull.v1.XmlPullParser
+import org.xmlpull.v1.XmlPullParserException
+import android.util.Log
+import android.util.Xml
+
+
+class SuggestTask
+ implements Runnable
+
+ def initialize(context:Suggest, original:String)
+ @suggest = context
+ @original = original
+ end
+
+ def run:void
+ suggestions = suggest(@original)
+ @suggest.setSuggestions(suggestions)
+ end
+
+ def con:HttpURLConnection
+ HttpURLConnection(@con)
+ end
+
+ def suggest(original:String)
+ messages = LinkedList.new
+ error = nil
+ @con = nil
+
+ begin
+ raise InterruptedException.new if (Thread.interrupted)
+
+ Log.d("SuggestTask", original)
+ q = URLEncoder.encode(original, "UTF-8")
+ url = URL.new("http://google.com/complete/search?output=toolbar&q=#{q}")
+
+ @con = HttpURLConnection(url.openConnection)
+ con.setReadTimeout(10000)#ms
+ con.setConnectTimeout(15000)#ms
+ con.setRequestMethod("GET")
+ con.addRequestProperty("Referer", "http://www.pragprog.com/titles/eband3/hello-android")
+ con.setDoInput(true)
+
+ Log.d("SuggestTask", "connecting")
+ con.connect
+ Log.d("SuggestTask", "connected")
+
+ raise InterruptedException.new if (Thread.interrupted)
+
+ Log.d("SuggestTask", "parsing")
+ parser = Xml.newPullParser
+ parser.setInput(con.getInputStream, nil)
+ eventType = parser.getEventType
+ while (eventType != XmlPullParser.END_DOCUMENT)
+ name = parser.getName
+ if (eventType == XmlPullParser.START_TAG && name.equalsIgnoreCase("suggestion"))
+ Log.d("SuggestTask", "suggestion")
+ i = 0
+ while i < parser.getAttributeCount
+ if (parser.getAttributeName(i).equalsIgnoreCase("data"))
+ Log.d("SuggestTask", "suggestion: #{parser.getAttributeValue(i)}")
+ messages.add(parser.getAttributeValue(i))
+ end
+ i+=1
+ end
+ end
+ eventType = parser.next
+ end
+ Log.d("SuggestTask", "parsed")
+ raise InterruptedException.new if (Thread.interrupted)
+ rescue IOException => e
+ error = @suggest.getResources.getString(R.string.error) + " #{e.toString}"
+ rescue XmlPullParserException => e
+ error = @suggest.getResources.getString(R.string.error) + " #{e.toString}"
+ rescue InterruptedException => e
+ error = @suggest.getResources.getString(R.string.interrupted) + " #{e.toString}"
+ ensure
+ con.disconnect if con != nil
+ end
+
+ if (error != nil)
+ Log.d("SuggestTask", error)
+ messages.clear
+ messages.add(error)
+ end
+
+ messages.add(@suggest.getResources.getString(R.string.no_results)) if (messages.size == 0)
+ Log.d("SuggestTask", "suggestions = #{messages.toString}")
+ messages
+ end
+end

0 comments on commit 43c64d1

Please sign in to comment.