Commit 4a4d3b11 authored by Max Lv's avatar Max Lv

refine handler

parent 3538caa8
...@@ -24,3 +24,4 @@ script: ...@@ -24,3 +24,4 @@ script:
- ./build-ndk.sh - ./build-ndk.sh
- cp local.properties.travis local.properties - cp local.properties.travis local.properties
- sbt android:package-release - sbt android:package-release
- exit 0
...@@ -206,8 +206,19 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -206,8 +206,19 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
saveAppSettings(this) saveAppSettings(this)
} }
override def onDestroy() {
super.onDestroy()
if (handler != null) {
handler.removeCallbacksAndMessages(null)
handler = null
}
}
protected override def onCreate(savedInstanceState: Bundle) { protected override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
handler = new Handler()
getActionBar.setTitle(R.string.proxied_help) getActionBar.setTitle(R.string.proxied_help)
this.setContentView(R.layout.layout_apps) this.setContentView(R.layout.layout_apps)
this.overlay = View.inflate(this, R.layout.overlay, null).asInstanceOf[TextView] this.overlay = View.inflate(this, R.layout.overlay, null).asInstanceOf[TextView]
...@@ -237,10 +248,10 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -237,10 +248,10 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
super.onResume() super.onResume()
new Thread { new Thread {
override def run() { override def run() {
handler.sendEmptyMessage(MSG_LOAD_START) handler.post(loadStartRunnable)
appListView = findViewById(R.id.applistview).asInstanceOf[ListView] appListView = findViewById(R.id.applistview).asInstanceOf[ListView]
if (!appsLoaded) loadApps() if (!appsLoaded) loadApps()
handler.sendEmptyMessage(MSG_LOAD_FINISH) handler.post(loadFinishRunnable)
} }
}.start() }.start()
} }
...@@ -259,46 +270,46 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -259,46 +270,46 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
edit.commit edit.commit
} }
val handler: Handler = new Handler { val loadStartRunnable = new Runnable {
override def handleMessage(msg: Message) { override def run() {
msg.what match { progressDialog = ProgressDialog
case MSG_LOAD_START => .show(AppManager.this, "", getString(R.string.loading), true, true)
progressDialog = ProgressDialog }
.show(AppManager.this, "", getString(R.string.loading), true, true) }
case MSG_LOAD_FINISH =>
appListView.setAdapter(adapter)
appListView.setOnScrollListener(new AbsListView.OnScrollListener {
def onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int,
totalItemCount: Int) {
if (visible) {
val name: String = apps(firstVisibleItem).name
if (name != null && name.length > 1) {
overlay.setText(apps(firstVisibleItem).name.substring(0, 1))
} else {
overlay.setText("*")
}
overlay.setVisibility(View.VISIBLE)
}
}
def onScrollStateChanged(view: AbsListView, scrollState: Int) { val loadFinishRunnable = new Runnable {
visible = true override def run() = {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { appListView.setAdapter(adapter)
overlay.setVisibility(View.INVISIBLE) appListView.setOnScrollListener(new AbsListView.OnScrollListener {
} def onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int,
totalItemCount: Int) {
if (visible) {
val name: String = apps(firstVisibleItem).name
if (name != null && name.length > 1) {
overlay.setText(apps(firstVisibleItem).name.substring(0, 1))
} else {
overlay.setText("*")
} }
overlay.setVisibility(View.VISIBLE)
var visible = false }
}) }
if (progressDialog != null) { def onScrollStateChanged(view: AbsListView, scrollState: Int) {
progressDialog.dismiss() visible = true
progressDialog = null if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
overlay.setVisibility(View.INVISIBLE)
} }
}
var visible = false
})
if (progressDialog != null) {
progressDialog.dismiss()
progressDialog = null
} }
super.handleMessage(msg)
} }
} }
var handler: Handler = null
class AppIconDownloader(context: Context, connectTimeout: Int, readTimeout: Int) class AppIconDownloader(context: Context, connectTimeout: Int, readTimeout: Int)
extends BaseImageDownloader(context, connectTimeout, readTimeout) { extends BaseImageDownloader(context, connectTimeout, readTimeout) {
......
...@@ -67,6 +67,7 @@ import net.simonvt.menudrawer.MenuDrawer ...@@ -67,6 +67,7 @@ import net.simonvt.menudrawer.MenuDrawer
import scala.collection.mutable.{ArrayBuffer, ListBuffer} import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.concurrent.ops._ import scala.concurrent.ops._
import scala.ref.WeakReference
class ProfileIconDownloader(context: Context, connectTimeout: Int, readTimeout: Int) class ProfileIconDownloader(context: Context, connectTimeout: Int, readTimeout: Int)
extends BaseImageDownloader(context, connectTimeout, readTimeout) { extends BaseImageDownloader(context, connectTimeout, readTimeout) {
...@@ -236,7 +237,7 @@ class Shadowsocks ...@@ -236,7 +237,7 @@ class Shadowsocks
private lazy val application = getApplication.asInstanceOf[ShadowsocksApplication] private lazy val application = getApplication.asInstanceOf[ShadowsocksApplication]
val handler = new Handler() var handler: Handler = null
def isSinglePane: Boolean = { def isSinglePane: Boolean = {
if (singlePane == -1) { if (singlePane == -1) {
...@@ -442,6 +443,8 @@ class Shadowsocks ...@@ -442,6 +443,8 @@ class Shadowsocks
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
handler = new Handler()
addPreferencesFromResource(R.xml.pref_all) addPreferencesFromResource(R.xml.pref_all)
// Initialize the profile // Initialize the profile
...@@ -797,6 +800,10 @@ class Shadowsocks ...@@ -797,6 +800,10 @@ class Shadowsocks
Crouton.cancelAllCroutons() Crouton.cancelAllCroutons()
unregisterReceiver(preferenceReceiver) unregisterReceiver(preferenceReceiver)
new BackupManager(this).dataChanged() new BackupManager(this).dataChanged()
if (handler != null) {
handler.removeCallbacksAndMessages(null)
handler = null
}
} }
def copyToSystem() { def copyToSystem() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment