Skip to content

Commit

Permalink
- 通信状況の確認タイミングを調整した。
Browse files Browse the repository at this point in the history
- Android 8.0でウィジェットをタップしてもアプリ画面を開かない不具合を修正した。
- Wi-Fi AP半強制モードの各種動作の実行間隔を設定から調整できるようにした。
  • Loading branch information
tateisu committed Sep 11, 2018
1 parent 0b1285d commit 55fbb55
Show file tree
Hide file tree
Showing 27 changed files with 1,440 additions and 978 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/tateisu.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
android:exported="false"
/>

<service
android:name="jp.juggler.fadownloader.NewFileService"
/>

<receiver
android:name="jp.juggler.fadownloader.NewFileWidget"
>
Expand Down
91 changes: 39 additions & 52 deletions app/src/main/java/jp/juggler/fadownloader/ActMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {

R.id.btnStop -> download_stop_button()

R.id.btnModeHelp -> openHelp(R.layout.help_mode)
R.id.btnModeHelp -> openHelpLayout(R.layout.help_mode)
}
}

Expand Down Expand Up @@ -528,9 +528,8 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {
stopService(intent)

try {
val pi = Utils.createAlarmPendingIntent(this)
val am = getSystemService(Context.ALARM_SERVICE) as? AlarmManager
am?.cancel(pi)
am?.cancel(Receiver1.piAlarm(this))
} catch(ex : Throwable) {
log.trace(ex, "createAlarmPendingIntent failed.")
}
Expand Down Expand Up @@ -616,12 +615,6 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {
return
}

val interval = Pref.uiInterval.getIntOrNull(pref) ?: - 1
if(repeat && interval < 1) {
Utils.showToast(this, true, getString(R.string.repeat_interval_not_ok))
return
}

val file_type = Pref.uiFileType(pref).trim()
if(file_type.isEmpty()) {
Utils.showToast(this, true, getString(R.string.file_type_empty))
Expand All @@ -634,34 +627,27 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {
return
}

var location_update_interval_desired = LocationTracker.DEFAULT_INTERVAL_DESIRED
var location_update_interval_min = LocationTracker.DEFAULT_INTERVAL_MIN

fun validSeconds(v : Int?) : Boolean {
return v != null && v > 0
}

if(repeat) {
if(! validSeconds(Pref.uiInterval.getIntOrNull(pref))) {
Utils.showToast(this, true, getString(R.string.repeat_interval_not_ok))
return
}
}

if(location_mode != LocationTracker.NO_LOCATION_UPDATE) {

fun x1000(v : Int?) = if(v != null) {
v.toLong() * 1000L
} else {
- 1L
if(! validSeconds(Pref.uiLocationIntervalDesired.getIntOrNull(pref))) {
Utils.showToast(this, true, getString(R.string.location_update_interval_not_ok))
return
}

location_update_interval_desired =
x1000(Pref.uiLocationIntervalDesired.getIntOrNull(pref))
location_update_interval_min = x1000(Pref.uiLocationIntervalMin.getIntOrNull(pref))

when {
! repeat -> {
}

location_update_interval_desired < 1000L -> {
Utils.showToast(this, true, getString(R.string.location_update_interval_not_ok))
return
}

location_update_interval_min < 1000L -> {
Utils.showToast(this, true, getString(R.string.location_update_interval_not_ok))
return
}
if(! validSeconds(Pref.uiLocationIntervalMin.getIntOrNull(pref))) {
Utils.showToast(this, true, getString(R.string.location_update_interval_not_ok))
return
}
}

Expand All @@ -678,9 +664,6 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {
}
}

val protected_only = Pref.uiProtectedOnly(pref)
val skip_already_download = Pref.uiSkipAlreadyDownload(pref)

// 最後に押したボタンを覚えておく
pref.edit()
.put(Pref.lastMode, if(repeat) Pref.LAST_MODE_REPEAT else Pref.LAST_MODE_ONCE)
Expand All @@ -691,28 +674,32 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {
val intent = Intent(this, DownloadService::class.java)
intent.action = DownloadService.ACTION_START

intent.putExtra(DownloadService.EXTRA_TARGET_TYPE, target_type)
intent.putExtra(DownloadService.EXTRA_REPEAT, repeat)
intent.put(pref, Pref.uiTetherSprayInterval)
intent.put(pref, Pref.uiTetherTestConnectionTimeout)
intent.put(pref, Pref.uiWifiChangeApInterval)
intent.put(pref, Pref.uiWifiScanInterval)
intent.put(pref, Pref.uiLocationIntervalDesired)
intent.put(pref, Pref.uiLocationIntervalMin)
intent.put(pref, Pref.uiInterval)

intent.put(pref, Pref.uiProtectedOnly)
intent.put(pref, Pref.uiSkipAlreadyDownload)
intent.put(pref, Pref.uiForceWifi)
intent.put(pref, Pref.uiRepeat)
intent.put(pref, Pref.uiLocationMode)

intent.put(ssid, Pref.uiSsid)
intent.put(folder_uri, Pref.uiFolderUri)
intent.put(file_type, Pref.uiFileType)

intent.put(target_type, Pref.uiTargetType)
intent.putExtra(DownloadService.EXTRA_TARGET_URL, target_url)
intent.putExtra(DownloadService.EXTRA_LOCAL_FOLDER, folder_uri)
intent.putExtra(DownloadService.EXTRA_INTERVAL, interval)
intent.putExtra(DownloadService.EXTRA_FILE_TYPE, file_type)

intent.putExtra(
DownloadService.EXTRA_LOCATION_INTERVAL_DESIRED,
location_update_interval_desired
)
intent.putExtra(DownloadService.EXTRA_LOCATION_INTERVAL_MIN, location_update_interval_min)
intent.putExtra(DownloadService.EXTRA_LOCATION_MODE, location_mode)
intent.putExtra(DownloadService.EXTRA_FORCE_WIFI, force_wifi)
intent.putExtra(DownloadService.EXTRA_SSID, ssid)
intent.putExtra(DownloadService.EXTRA_PROTECTED_ONLY, protected_only)
intent.putExtra(DownloadService.EXTRA_SKIP_ALREADY_DOWNLOAD, skip_already_download)

startService(intent)
}

internal fun openHelp(layout_id : Int) {
internal fun openHelpLayout(layout_id : Int) {
val v = layoutInflater.inflate(layout_id, null, false)
val d = Dialog(this)
d.requestWindowFeature(Window.FEATURE_NO_TITLE)
Expand Down
51 changes: 14 additions & 37 deletions app/src/main/java/jp/juggler/fadownloader/DownloadService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package jp.juggler.fadownloader

import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
Expand All @@ -20,31 +18,18 @@ import jp.juggler.fadownloader.tracker.NetworkTracker
import jp.juggler.fadownloader.tracker.WorkerTracker
import jp.juggler.fadownloader.util.LogWriter
import jp.juggler.fadownloader.util.NotificationHelper
import jp.juggler.fadownloader.util.Utils

class DownloadService : Service() {

companion object {
internal const val ACTION_BROADCAST_RECEIVED = "broadcast_received"
internal const val EXTRA_BROADCAST_INTENT = "broadcast_intent"

internal const val ACTION_START = "start"

internal const val EXTRA_REPEAT = "repeat"
internal const val EXTRA_TARGET_URL = "uri"
internal const val EXTRA_LOCAL_FOLDER = "folder_uri"
internal const val EXTRA_INTERVAL = "intervalSeconds"
internal const val EXTRA_FILE_TYPE = "file_type"
internal const val EXTRA_LOCATION_INTERVAL_DESIRED = "location_interval_desired"
internal const val EXTRA_LOCATION_INTERVAL_MIN = "location_interval_min"
internal const val EXTRA_LOCATION_MODE = "location_mode"
internal const val EXTRA_FORCE_WIFI = "force_wifi"
internal const val EXTRA_SSID = "ssid"
internal const val EXTRA_TARGET_TYPE = "target_type"

internal const val NOTIFICATION_ID_SERVICE = 1
const val EXTRA_PROTECTED_ONLY = "protected_only"
const val EXTRA_SKIP_ALREADY_DOWNLOAD = "skip_already_download"

internal var service_instance : DownloadService? = null

Expand All @@ -55,16 +40,17 @@ class DownloadService : Service() {
val sb = StringBuilder()
sb.append(context.getString(R.string.service_running))
sb.append("WakeLock=")
.append(if(service.wake_lock !!.isHeld) "ON" else "OFF")
.append(if(service.wake_lock?.isHeld == true) "ON" else "OFF")
.append(", ")
.append("WiFiLock=")
.append(if(service.wifi_lock !!.isHeld) "ON" else "OFF")
.append(if(service.wifi_lock?.isHeld == true) "ON" else "OFF")
.append(", ")
.append("Location=")
.append(service.location_tracker.status)
.append(", ")
.append("Network=")
service.wifi_tracker.getStatus(sb)
.append(service.wifi_tracker.getStatus())

sb.append('\n')

val worker = service.worker_tracker.worker
Expand Down Expand Up @@ -156,14 +142,7 @@ class DownloadService : Service() {
wifi_tracker.dispose()

if(cancel_alarm_on_destroy) {
try {
val pi = Utils.createAlarmPendingIntent(this)
val am = getSystemService(Context.ALARM_SERVICE) as? AlarmManager
am?.cancel(pi)
} catch(ex : Throwable) {
log.trace(ex,"cancel_alarm_on_destroy failed.")
}

Receiver1.cancelAlarm(this)
}

wake_lock?.release()
Expand All @@ -189,7 +168,7 @@ class DownloadService : Service() {
ACTION_START -> {
worker_tracker.start(intent)
}

ACTION_BROADCAST_RECEIVED -> {
val broadcast_intent = intent.getParcelableExtra<Intent>(EXTRA_BROADCAST_INTENT)
if(broadcast_intent != null) {
Expand Down Expand Up @@ -260,7 +239,7 @@ class DownloadService : Service() {
}

internal fun onThreadEnd(complete_and_no_repeat : Boolean) {
if(! is_alive ) return
if(! is_alive) return

if(complete_and_no_repeat) {
this@DownloadService.cancel_alarm_on_destroy = true
Expand All @@ -271,12 +250,12 @@ class DownloadService : Service() {
}
}

internal fun addHiddenDownloadCount(count : Long, log : LogWriter) {
NewFileService.addHiddenDownloadCount(this, count, log)
internal fun addHiddenDownloadCount(count : Long) {
NewFileWidget.addHiddenDownloadCount(this, count)
}

fun hasHiddenDownloadCount() : Boolean {
return NewFileService.hasHiddenDownloadCount(this)
return NewFileWidget.hasHiddenDownloadCount(this)
}

private fun setServiceNotification(status : String) {
Expand All @@ -290,8 +269,7 @@ class DownloadService : Service() {
"ServiceRunning",
"FA Downloader service",
"this notification is shown while FA Downloader service is active.",
NotificationManager.IMPORTANCE_LOW,
log
NotificationManager.IMPORTANCE_LOW
)
NotificationCompat.Builder(this, channel.id)
} else {
Expand All @@ -303,9 +281,8 @@ class DownloadService : Service() {
builder.setContentText(status)
builder.setOngoing(true)

val intent = Intent(this, ActMain::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
val pi = PendingIntent.getActivity(applicationContext, 567, intent, 0)
val pi = Receiver1.piActivity(this)

builder.setContentIntent(pi)

startForeground(NOTIFICATION_ID_SERVICE, builder.build())
Expand Down
Loading

0 comments on commit 55fbb55

Please sign in to comment.