Skip to content

Commit

Permalink
TaskerのActionとConditionのPluginを用意した。ネットワーク強制のひどいバグを修正。
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Sep 13, 2018
1 parent c2753d5 commit 71483a4
Show file tree
Hide file tree
Showing 14 changed files with 1,568 additions and 146 deletions.
4 changes: 4 additions & 0 deletions .idea/dictionaries/tateisu.xml

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

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
minSdkVersion min_sdk_version
targetSdkVersion target_sdk_version

versionCode 21
versionName "1.14.1"
versionCode 22
versionName "1.14.2"
applicationId "jp.juggler.fadownloader"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
40 changes: 39 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@
android:name="jp.juggler.fadownloader.Receiver1"
android:exported="true"
>

<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING"/>
</intent-filter>

<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.QUERY_CONDITION" />
</intent-filter>

</receiver>


Expand All @@ -67,11 +77,39 @@
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name="jp.juggler.fadownloader.ActTaskerSettingAction"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
android:theme="@style/AppTheme"
android:label="@string/tasker_setting_action"
>
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING"/>
</intent-filter>
</activity>

<activity
android:name="jp.juggler.fadownloader.ActTaskerSettingCondition"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
android:theme="@style/AppTheme"
android:label="@string/tasker_setting_condition"
>
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_CONDITION" />
</intent-filter>
</activity>


<activity
android:name="jp.juggler.fadownloader.picker.FolderPicker"
android:icon="@mipmap/ic_launcher"
Expand Down
136 changes: 4 additions & 132 deletions app/src/main/java/jp/juggler/fadownloader/ActMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -520,20 +520,7 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {

// 転送サービスを停止
private fun download_stop_button() {
pref.edit()
.put(Pref.lastMode, Pref.LAST_MODE_STOP)
.put(Pref.lastModeUpdate, System.currentTimeMillis())
.apply()
val intent = Intent(this, DownloadService::class.java)
stopService(intent)

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

Receiver1.actionStop(this)
}

// 転送サービスを開始
Expand Down Expand Up @@ -579,125 +566,10 @@ open class ActMain : AppCompatActivity(), View.OnClickListener {

private fun startDownloadService() {

// LocationSettingを確認する前のrepeat引数の値を思い出す
val repeat = Pref.uiRepeat(pref)

// 設定から値を読んでバリデーション

val target_type = Pref.uiTargetType(pref)
if(target_type < 0) {
Utils.showToast(this, true, getString(R.string.target_type_invalid))
return
}

val target_url = Pref.loadTargetUrl(pref, target_type)
if(target_url.isEmpty()) {
Utils.showToast(this, true, getString(R.string.target_url_not_ok))
return
}

var folder_uri = ""
val sv = Pref.uiFolderUri(pref)
if(sv.isNotEmpty()) {
if(Build.VERSION.SDK_INT >= LocalFile.DOCUMENT_FILE_VERSION) {
val folder = DocumentFile.fromTreeUri(this, Uri.parse(sv))
if(folder != null) {
if(folder.exists() && folder.canWrite()) {
folder_uri = sv
}
}
} else {
folder_uri = sv
}
}
if(folder_uri.isEmpty()) {
Utils.showToast(this, true, getString(R.string.local_folder_not_ok))
return
}

val file_type = Pref.uiFileType(pref).trim()
if(file_type.isEmpty()) {
Utils.showToast(this, true, getString(R.string.file_type_empty))
return
}

val location_mode = Pref.uiLocationMode(pref)
if(location_mode < 0 || location_mode > LocationTracker.LOCATION_HIGH_ACCURACY) {
Utils.showToast(this, true, getString(R.string.location_mode_invalid))
return
val error = Receiver1.actionStart(this)
if( error != null) {
Utils.showToast(this, true, error)
}


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) {

if(! validSeconds(Pref.uiLocationIntervalDesired.getIntOrNull(pref))) {
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
}
}

val force_wifi = Pref.uiForceWifi(pref)

val ssid : String
if(! force_wifi) {
ssid = ""
} else {
ssid = Pref.uiSsid(pref).trim()
if(ssid.isEmpty()) {
Utils.showToast(this, true, getString(R.string.ssid_empty))
return
}
}

// 最後に押したボタンを覚えておく
pref.edit()
.put(Pref.lastMode, if(repeat) Pref.LAST_MODE_REPEAT else Pref.LAST_MODE_ONCE)
.put(Pref.lastModeUpdate, System.currentTimeMillis())
.apply()

// 転送サービスを開始
val intent = Intent(this, DownloadService::class.java)
intent.action = DownloadService.ACTION_START

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.uiStopWhenTetheringOff)
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)


startService(intent)
}

internal fun openHelpLayout(layout_id : Int) {
Expand Down
114 changes: 114 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/ActTaskerSettingAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package jp.juggler.fadownloader

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import jp.juggler.fadownloader.util.LogTag

class ActTaskerSettingAction : AppCompatActivity(), View.OnClickListener {

companion object {
val log = LogTag("ActTaskerSettingAction")

const val STATE_ACTION = "action"
}

private lateinit var spAction : Spinner

override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)

try {
val callingApplicationLabel = packageManager.getApplicationLabel(
packageManager.getApplicationInfo(callingPackage, 0)
)
title = if(callingApplicationLabel?.isNotEmpty() == true) {
"$callingApplicationLabel > ${getString(R.string.tasker_setting_action)}"
}else{
getString(R.string.tasker_setting_action)
}
} catch(ex : PackageManager.NameNotFoundException) {
log.e(ex, "Calling package couldn't be found")
}


setContentView(R.layout.act_tasker_setting_action)

spAction = findViewById(R.id.spAction)
val btnSave : View = findViewById(R.id.btnSave)

btnSave.setOnClickListener(this)

initSpinner(
spAction,
arrayOf(
getString(R.string.repeat),
getString(R.string.once),
getString(R.string.stop)
),
null
)
if(savedInstanceState != null) {
spAction.setSelection(savedInstanceState.getInt(STATE_ACTION, 0))
} else {
val b = intent?.extras
val actionInt = b?.getInt(Receiver1.EXTRA_ACTION, - 1)
when(actionInt) {
Pref.LAST_MODE_REPEAT -> spAction.setSelection(0)
Pref.LAST_MODE_ONCE -> spAction.setSelection(1)
Pref.LAST_MODE_STOP -> spAction.setSelection(2)
}

}
}

override fun onSaveInstanceState(outState : Bundle?) {
outState ?: return
super.onSaveInstanceState(outState)
outState.putInt(STATE_ACTION, spAction.selectedItemPosition)
}

override fun onClick(v : View?) {
when(v?.id) {
R.id.btnSave -> {
val actionInt = when(spAction.selectedItemPosition) {
0 -> Pref.LAST_MODE_REPEAT
1 -> Pref.LAST_MODE_ONCE
else -> Pref.LAST_MODE_STOP
}
val actionString = when(spAction.selectedItemPosition) {
0 -> "REPEAT"
1 -> "ONCE"
else -> "STOP"
}
val b = Bundle()
b.putInt(Receiver1.EXTRA_ACTION, actionInt)
val intent = Intent()
intent.putExtra(Receiver1.EXTRA_TASKER_BUNDLE, b)
intent.putExtra(
"com.twofortyfouram.locale.intent.extra.BLURB",
"action=$actionString"
)
setResult(RESULT_OK, intent)
finish()
}
}
}

private fun initSpinner(
sp : Spinner,
choices : Array<CharSequence>,
action : AdapterView.OnItemSelectedListener?
) {
val adapter = ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(R.layout.spinner_dropdown)
adapter.addAll(*choices)
sp.adapter = adapter
sp.onItemSelectedListener = action
}
}
Loading

0 comments on commit 71483a4

Please sign in to comment.