Skip to content

Commit

Permalink
- テザリングモード時の確認アクセスをマルチスレッドで行う。
Browse files Browse the repository at this point in the history
- テザリングがOFFになったらサービスを停止するオプションを追加
  • Loading branch information
tateisu committed Sep 11, 2018
1 parent 55fbb55 commit 8038c3f
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 204 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/jp/juggler/fadownloader/ActMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {

intent.put(pref, Pref.uiProtectedOnly)
intent.put(pref, Pref.uiSkipAlreadyDownload)
intent.put(pref, Pref.uiStopWhenTetheringOff)
intent.put(pref, Pref.uiForceWifi)
intent.put(pref, Pref.uiRepeat)
intent.put(pref, Pref.uiLocationMode)
Expand Down
46 changes: 31 additions & 15 deletions app/src/main/java/jp/juggler/fadownloader/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,22 @@ class DownloadService : Service() {
this.log = log
log.d(getString(R.string.service_start))

val pm = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
wake_lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, packageName)
wake_lock !!.setReferenceCounted(false)
val pm = applicationContext.getSystemService(Context.POWER_SERVICE)
as PowerManager

wake_lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, packageName).apply{
setReferenceCounted(false)
}

val wm = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifi_lock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, packageName)
wifi_lock !!.setReferenceCounted(false)
val wm = applicationContext.getSystemService(Context.WIFI_SERVICE)
as WifiManager

wifi_lock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, packageName).apply {
setReferenceCounted(false)
}

mNotificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager

setServiceNotification(getString(R.string.service_idle))

Expand All @@ -116,16 +122,26 @@ class DownloadService : Service() {
media_tracker = MediaScannerTracker(this, log)

wifi_tracker =
NetworkTracker(this, log) { is_connected, cause ->
if(is_connected) {
val last_mode = Pref.lastMode(Pref.pref(this@DownloadService))
if(last_mode != Pref.LAST_MODE_STOP) {
worker_tracker.wakeup(cause)
}
NetworkTracker(this, log, wifi_tracker_callback)

worker_tracker = WorkerTracker(this, log)
}

private val wifi_tracker_callback = object:NetworkTracker.Callback{
override fun onConnectionStatus(is_connected : Boolean, cause : String) {
if(is_connected) {
val last_mode = Pref.lastMode(Pref.pref(this@DownloadService))
if(last_mode != Pref.LAST_MODE_STOP) {
worker_tracker.wakeup(cause)
}
}
}

worker_tracker = WorkerTracker(this, log)
override fun onTetheringOff() {
log.e(R.string.stop_when_tethering_off)
this@DownloadService.cancel_alarm_on_destroy = true
stopSelf()
}
}

override fun onDestroy() {
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/jp/juggler/fadownloader/DownloadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ class DownloadWorker : WorkerBase {
tetherSprayInterval = x1000Safe(Pref.uiTetherSprayInterval.getInt(intent)),
tetherTestConnectionTimeout = x1000Safe(Pref.uiTetherTestConnectionTimeout.getInt(intent)),
wifiChangeApInterval = x1000Safe(Pref.uiWifiChangeApInterval.getInt(intent)),
wifiScanInterval = x1000Safe(Pref.uiWifiScanInterval.getInt(intent))
wifiScanInterval = x1000Safe(Pref.uiWifiScanInterval.getInt(intent)),

stopWhenTetheringOff = Pref.uiStopWhenTetheringOff(intent)
)

this.location_setting = LocationTracker.Setting(
Expand All @@ -220,6 +222,7 @@ class DownloadWorker : WorkerBase {
.put(Pref.workerTetherTestConnectionTimeout, network_setting.tetherTestConnectionTimeout)
.put(Pref.workerWifiChangeApInterval, network_setting.wifiChangeApInterval)
.put(Pref.workerWifiScanInterval, network_setting.wifiScanInterval)
.put(Pref.workerStopWhenTetheringOff, network_setting.stopWhenTetheringOff)
.apply()

this.file_type_list = file_type_parse()
Expand Down Expand Up @@ -253,7 +256,9 @@ class DownloadWorker : WorkerBase {
tetherSprayInterval = Pref.workerTetherSprayInterval(pref),
tetherTestConnectionTimeout = Pref.workerTetherTestConnectionTimeout(pref),
wifiChangeApInterval = Pref.workerWifiChangeApInterval(pref),
wifiScanInterval = Pref.workerWifiScanInterval(pref)
wifiScanInterval = Pref.workerWifiScanInterval(pref),

stopWhenTetheringOff = Pref.workerStopWhenTetheringOff(pref)
)

this.location_setting = LocationTracker.Setting(
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/PageSetting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PageSetting(activity : Activity, ignored : View) :
private lateinit var swCopyBeforeViewSend : Switch
private lateinit var swProtectedOnly : Switch
private lateinit var swSkipAlreadyDownload : Switch
private lateinit var swStopWhenTetheringOff : Switch

private lateinit var btnSSIDPicker : View
internal var bLoading : Boolean = false
Expand All @@ -66,6 +67,7 @@ class PageSetting(activity : Activity, ignored : View) :
btnSSIDPicker = root.findViewById(R.id.btnSSIDPicker)
swProtectedOnly = root.findViewById(R.id.swProtectedOnly)
swSkipAlreadyDownload = root.findViewById(R.id.swSkipAlreadyDownload)
swStopWhenTetheringOff = root.findViewById(R.id.swStopWhenTetheringOff)
etTetherSprayInterval = root.findViewById(R.id.etTetherSprayInterval)
etTetherTestConnectionTimeout = root.findViewById(R.id.etTetherTestConnectionTimeout)
etWifiChangeApInterval = root.findViewById(R.id.etWifiChangeApInterval)
Expand Down Expand Up @@ -93,6 +95,7 @@ class PageSetting(activity : Activity, ignored : View) :
root.findViewById<View>(R.id.btnTetherTestConnectionTimeoutHelp).setOnClickListener(this)
root.findViewById<View>(R.id.btnWifiScanIntervalHelp).setOnClickListener(this)
root.findViewById<View>(R.id.btnWifiChangeApIntervalHelp).setOnClickListener(this)
root.findViewById<View>(R.id.btnStopWhenTetheringOffHelp).setOnClickListener(this)

val location_mode_adapter = ArrayAdapter<CharSequence>(
activity, android.R.layout.simple_spinner_item
Expand Down Expand Up @@ -229,6 +232,7 @@ class PageSetting(activity : Activity, ignored : View) :
R.id.btnTetherTestConnectionTimeoutHelp -> openHelp(R.string.tether_test_connection_timeout_help)
R.id.btnWifiScanIntervalHelp -> openHelp(R.string.wifi_scan_interval_help)
R.id.btnWifiChangeApIntervalHelp -> openHelp(R.string.wifi_change_ap_interval_help)
R.id.btnStopWhenTetheringOffHelp -> openHelp(R.string.stop_when_tethering_off_help)
}
}

Expand All @@ -244,6 +248,7 @@ class PageSetting(activity : Activity, ignored : View) :
swCopyBeforeViewSend.isChecked = Pref.uiCopyBeforeSend(pref)
swProtectedOnly.isChecked = Pref.uiProtectedOnly(pref)
swSkipAlreadyDownload.isChecked = Pref.uiSkipAlreadyDownload(pref)
swStopWhenTetheringOff.isChecked = Pref.uiStopWhenTetheringOff(pref)

// string
etInterval.setText(Pref.uiInterval(pref))
Expand Down Expand Up @@ -300,6 +305,8 @@ class PageSetting(activity : Activity, ignored : View) :
.put(Pref.uiSsid, etSSID.text.toString())
.put(Pref.uiProtectedOnly, swProtectedOnly.isChecked)
.put(Pref.uiSkipAlreadyDownload, swSkipAlreadyDownload.isChecked)
.put(Pref.uiStopWhenTetheringOff, swStopWhenTetheringOff.isChecked)

// .apply() は呼び出し側で行う
}

Expand Down
Loading

0 comments on commit 8038c3f

Please sign in to comment.