From 1fbaa1bcc6258e944440f9f2d74e181b60c46703 Mon Sep 17 00:00:00 2001 From: Chiba Tairi <31642509+pakutoma@users.noreply.github.com> Date: Fri, 25 May 2018 21:00:36 +0900 Subject: [PATCH 01/13] =?UTF-8?q?=E4=BE=8B=E5=A4=96=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pakutoma/iijmiocouponwidget/service/CouponFunctions.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt index 11f3370..721925a 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt @@ -6,6 +6,7 @@ import android.widget.Toast import com.github.kittinunf.fuel.core.HttpException import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.launch +import kotlinx.coroutines.experimental.async import kotlinx.coroutines.experimental.withContext import org.jetbrains.anko.db.* import org.jetbrains.anko.defaultSharedPreferences @@ -47,7 +48,7 @@ private suspend fun accessCoupon(context: Context, change: Boolean) { } catch (e: NotFoundValidTokenException) { editor.putString("X-IIJmio-Authorization", "") showResult(context, change, false) - } catch (e: HttpException) { + } catch (e: Exception) { showResult(context, change, true, false) } if (change) { @@ -65,7 +66,7 @@ private suspend fun showResult(context: Context, change: Boolean, hasToken: Bool private suspend fun fetchCouponInfo(context: Context, coupon: CouponAPI, change: Boolean): Triple> { val couponInfo = coupon.fetchCouponInfo() - launch { updateDb(context, couponInfo) } + val db = async { updateDb(context, couponInfo) } val (enablePlans, enableLines) = getEnablePlansAndLines(context, couponInfo) val isCouponEnabledNow = if (enableLines.isEmpty()) { couponInfo.planInfoList[0].lineInfoList[0].couponUse @@ -85,6 +86,7 @@ private suspend fun fetchCouponInfo(context: Context, coupon: CouponAPI, change: couponInfo.planInfoList.flatMap { it.lineInfoList.filter { x -> enableLines.any { it == x.number } }.map { it.serviceCode } } } } + db.await() return Triple(isCouponEnabled, remains, serviceCodeList) } @@ -136,6 +138,7 @@ private fun sendToast(context: Context, hasToken: Boolean, couldChange: Boolean } if (!couldChange) { Toast.makeText(context, "切り替えに失敗しました。", Toast.LENGTH_SHORT).show() + return } Toast.makeText(context, "クーポンを" + (if (isCouponEnabled) "ON" else "OFF") + "に変更しました。", Toast.LENGTH_SHORT).show() } \ No newline at end of file From 2ca3ff7ec68c507650f29cd4b7f184f72884caef Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Fri, 1 Jun 2018 20:29:30 +0900 Subject: [PATCH 02/13] =?UTF-8?q?=E3=83=AA=E3=83=88=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=81=AE=E8=A8=AD=E5=AE=9A=E7=94=BB=E9=9D=A2=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 6 +++ .../activity/AdvancedSettingsActivity.kt | 38 +++++++++++++++++++ app/src/main/res/values/strings.xml | 6 ++- .../res/xml/activity_advanced_settings.xml | 15 ++++++++ .../main/res/xml/activity_main_settings.xml | 8 ++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt create mode 100644 app/src/main/res/xml/activity_advanced_settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 06c51d9..906d68b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -69,6 +69,12 @@ + + + + + diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt new file mode 100644 index 0000000..2908256 --- /dev/null +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt @@ -0,0 +1,38 @@ +package pakutoma.iijmiocouponwidget.activity + +import android.os.Bundle +import android.preference.PreferenceFragment +import android.support.v7.app.AppCompatActivity +import android.view.MenuItem +import pakutoma.iijmiocouponwidget.R + +/** + * Created by PAKUTOMA on 2018/05/25. + */ + +class AdvancedSettingsActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + fragmentManager.beginTransaction() + .replace(android.R.id.content, SettingsPreferenceFragment()) + .commit() + } + + class SettingsPreferenceFragment : PreferenceFragment() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + addPreferencesFromResource(R.xml.activity_advanced_settings) + } + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item?.itemId) { + android.R.id.home -> { + finish() + } + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bf2c98b..1943c32 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,8 +5,12 @@ クーポン切り替え・通信量取得 通信中 - 残量表示・スイッチの設定 + 切替回線・表示プランの設定 + 詳細設定 残量を表示するプラン 切り替えを行う回線 通信規制中 + クーポン切り替えのリトライ + 通信失敗時にリトライを行う + APIリミット時にリトライを行う diff --git a/app/src/main/res/xml/activity_advanced_settings.xml b/app/src/main/res/xml/activity_advanced_settings.xml new file mode 100644 index 0000000..d8cac35 --- /dev/null +++ b/app/src/main/res/xml/activity_advanced_settings.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/activity_main_settings.xml b/app/src/main/res/xml/activity_main_settings.xml index 0ba1eac..4da0163 100644 --- a/app/src/main/res/xml/activity_main_settings.xml +++ b/app/src/main/res/xml/activity_main_settings.xml @@ -12,6 +12,14 @@ android:targetClass="pakutoma.iijmiocouponwidget.activity.WidgetSettingsActivity" android:targetPackage="pakutoma.iijmiocouponwidget" /> + + + \ No newline at end of file From 5e9c7a757d563ced7dcf175784550af2fe360bdc Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Fri, 8 Jun 2018 19:44:20 +0900 Subject: [PATCH 03/13] =?UTF-8?q?JobService=E3=81=AEID=E3=82=92=E3=83=AA?= =?UTF-8?q?=E3=82=BD=E3=83=BC=E3=82=B9xml=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit リトライ用のJobServiceを追加するため。 --- .../iijmiocouponwidget/widget/SwitchWidget.kt | 22 +++++++++---------- app/src/main/res/values/jobs.xml | 4 ++++ 2 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 app/src/main/res/values/jobs.xml diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/widget/SwitchWidget.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/widget/SwitchWidget.kt index c614bec..e62c868 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/widget/SwitchWidget.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/widget/SwitchWidget.kt @@ -31,7 +31,6 @@ import java.util.concurrent.TimeUnit class SwitchWidget : AppWidgetProvider() { companion object { - private const val JOB_ID = 1 private const val NOTIFICATION_CHANNEL_ID = "switch_service" } @@ -60,7 +59,7 @@ class SwitchWidget : AppWidgetProvider() { override fun onDisabled(context: Context) { val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler - jobScheduler.cancel(JOB_ID) + jobScheduler.cancel(context.resources.getInteger(R.integer.periodic_fetch_remains)) super.onDisabled(context) } @@ -82,8 +81,9 @@ class SwitchWidget : AppWidgetProvider() { private fun registerFetchRemainsJobService(context: Context) { val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler val jobs = jobScheduler.allPendingJobs - if(jobs?.any { it.id == JOB_ID } != true) { - val jobInfo = JobInfo.Builder(JOB_ID, ComponentName(context, FetchRemainsJobService::class.java)) + val jobId = context.resources.getInteger(R.integer.periodic_fetch_remains) + if(jobs?.any { it.id == jobId } != true) { + val jobInfo = JobInfo.Builder(jobId, ComponentName(context, FetchRemainsJobService::class.java)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPeriodic(TimeUnit.MINUTES.toMillis(15)) .setPersisted(true) @@ -138,11 +138,10 @@ fun updateSwitchStatus(context: Context, hasToken: Boolean, couldGet: Boolean, r remoteViews.setTextViewText(R.id.data_traffic, convertPrefixString(remains)) } val clickIntent = Intent(context, SwitchCouponService::class.java) - val pendingIntent: PendingIntent - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - pendingIntent = PendingIntent.getForegroundService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) + val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + PendingIntent.getForegroundService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) } else { - pendingIntent = PendingIntent.getService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) + PendingIntent.getService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) } remoteViews.setOnClickPendingIntent(R.id.transparent_button, pendingIntent) val thisWidget = ComponentName(context, SwitchWidget::class.java) @@ -161,11 +160,10 @@ private fun convertPrefixString(traffic: Int): String { fun setSwitchPendingIntent(context: Context) { val remoteViews = RemoteViews(context.packageName, R.layout.switch_widget) val clickIntent = Intent(context, SwitchCouponService::class.java) - val pendingIntent: PendingIntent - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - pendingIntent = PendingIntent.getForegroundService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) + val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + PendingIntent.getForegroundService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) } else { - pendingIntent = PendingIntent.getService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) + PendingIntent.getService(context, 0, clickIntent, FLAG_CANCEL_CURRENT) } remoteViews.setOnClickPendingIntent(R.id.transparent_button, pendingIntent) val thisWidget = ComponentName(context, SwitchWidget::class.java) diff --git a/app/src/main/res/values/jobs.xml b/app/src/main/res/values/jobs.xml new file mode 100644 index 0000000..8d7f006 --- /dev/null +++ b/app/src/main/res/values/jobs.xml @@ -0,0 +1,4 @@ + + + 1 + \ No newline at end of file From 0da4d5c06a841125cd9b70e54ac2ae617864d674 Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Tue, 21 Aug 2018 00:43:44 +0900 Subject: [PATCH 04/13] =?UTF-8?q?#16=20#19=20=E3=82=AF=E3=83=BC=E3=83=9D?= =?UTF-8?q?=E3=83=B3=E5=88=87=E3=82=8A=E6=9B=BF=E3=81=88=E6=99=82=E3=81=AE?= =?UTF-8?q?=E5=BE=85=E3=81=A1=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + .../service/SwitchCouponService.kt | 86 +++++++++++++++---- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 906d68b..0da9120 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + = Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notification = Notification .Builder(applicationContext, "switch_service") .setContentTitle(applicationContext.getString(R.string.switch_notification)) @@ -45,30 +47,60 @@ class SwitchCouponService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val preferences = getSharedPreferences("iijmio_token", Context.MODE_PRIVATE) - val accessToken = preferences.getString("X-IIJmio-Authorization", "") - if (accessToken == "") { - val authIntent = Intent(applicationContext, OpenBrowserActivity::class.java) - authIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - startActivity(authIntent) - setSwitchPendingIntent(applicationContext) + val preferences = getSharedPreferences("iijmio_token", Context.MODE_PRIVATE); + if (preferences == null) { stopSelf() return START_NOT_STICKY } - val lastClickTime = preferences.getLong("last_click_time", 0) - if (System.currentTimeMillis() - lastClickTime < 1000 * 60) { - Toast.makeText(this, "API limit: あと" + java.lang.Long.toString((1000 * 60 - (System.currentTimeMillis() - lastClickTime)) / 1000) + "秒お待ち下さい。", Toast.LENGTH_SHORT).show() + val timeFromFinish = System.currentTimeMillis() - preferences.getLong("last_click_time", 0); + val editor = preferences.edit() + if (timeFromFinish > 3000 * 60) { + // サービス異常終了時への対処 + editor.putBoolean("SwitchCouponServiceIsRunning", false) + editor.apply() + } + + val running = preferences.getBoolean("SwitchCouponServiceIsRunning", false) + if (running) { + Toast.makeText(this, "クーポン切替中です", Toast.LENGTH_SHORT).show() + return START_NOT_STICKY + } + editor.putBoolean("SwitchCouponServiceIsRunning", true) + editor.apply() + + val accessToken = preferences.getString("X-IIJmio-Authorization", "") + if (accessToken == "") { + val authIntent = Intent(applicationContext, OpenBrowserActivity::class.java) + authIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(authIntent) setSwitchPendingIntent(applicationContext) - stopSelf() + exit(editor) return START_NOT_STICKY } changeToWaitMode(applicationContext, preferences.getBoolean("is_coupon_enabled", false)) + + val cm = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val networkRequest = NetworkRequest.Builder().build() launch { - switchCoupon(applicationContext) - stopSelf() + if (timeFromFinish < 1000 * 60) { + val remainingTime = 1000 * 60 - timeFromFinish + withContext(UI) { + Toast.makeText(applicationContext, "API limit: " + java.lang.Long.toString(remainingTime / 1000) + "秒後に切替を行います", Toast.LENGTH_SHORT).show() + } + delay(1000 * 60 - timeFromFinish) + } + val network = withTimeoutOrNull(1000 * 60) { + waitNetworkAvailable(cm, networkRequest) + } + if (network != null) { + switchCoupon(applicationContext) + } else { + Toast.makeText(applicationContext, "接続に失敗しました", Toast.LENGTH_SHORT).show() + } + exit(editor) } return Service.START_NOT_STICKY } @@ -77,4 +109,20 @@ class SwitchCouponService : Service() { return null } + private fun exit(editor: SharedPreferences.Editor) { + editor.putBoolean("SwitchCouponServiceIsRunning", false); + editor.apply() + stopSelf() + } + + private suspend fun waitNetworkAvailable(cm: ConnectivityManager, networkRequest: NetworkRequest): Network? = suspendCoroutine { cont -> + val callback = object : ConnectivityManager.NetworkCallback() { + override fun onAvailable(network: Network?) { + super.onAvailable(network) + cont.resume(network) + } + } + cm.registerNetworkCallback(networkRequest, callback) + } + } From 023626dbf8149274f154e1994168b0f4f490a4da Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Tue, 25 Sep 2018 11:03:55 +0900 Subject: [PATCH 05/13] =?UTF-8?q?=E3=83=AA=E3=83=AA=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E7=94=A8=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 22 ------------------- .idea/copyright/profiles_settings.xml | 3 --- .idea/encodings.xml | 6 ----- .idea/gradle.xml | 3 +-- .idea/inspectionProfiles/Project_Default.xml | 11 ---------- .../inspectionProfiles/profiles_settings.xml | 7 ------ .idea/kotlinc.xml | 8 +++++++ .idea/misc.xml | 10 +-------- .idea/modules.xml | 2 +- .idea/vcs.xml | 1 + app/build.gradle | 12 +++++----- .../iijmiocouponwidget/utility/CouponAPI.kt | 14 ++++++++---- 12 files changed, 28 insertions(+), 71 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/copyright/profiles_settings.xml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/kotlinc.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 97626ba..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index fe72da5..7ac24c7 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,9 +3,8 @@ - - - - - - - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index f05212f..90879b2 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..8306744 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index ff4e406..0db0c7f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android' android { compileSdkVersion 27 - buildToolsVersion '26.0.2' + buildToolsVersion '27.0.3' defaultConfig { applicationId "pakutoma.iijmiocouponwidget" minSdkVersion 21 - versionCode 3 - versionName "2.1" + versionCode 5 + versionName "2.1 Hotfix 2" targetSdkVersion 27 } buildTypes { @@ -39,10 +39,10 @@ dependencies { compile 'com.github.kittinunf.fuel:fuel-moshi:1.12.1' compile 'com.squareup.moshi:moshi:1.5.0' compile 'com.squareup.moshi:moshi-kotlin:1.5.0' - compile "org.jetbrains.anko:anko-sqlite:0.10.4" + compile 'org.jetbrains.anko:anko-sqlite:0.10.4' compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.3" - compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.3" + compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.3' + compile 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.3' } repositories { diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt index ff6dce5..8b6c269 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt @@ -1,6 +1,9 @@ package pakutoma.iijmiocouponwidget.utility +import android.util.Log +import android.widget.Toast import com.github.kittinunf.fuel.core.FuelManager +import com.github.kittinunf.fuel.core.interceptors.validatorResponseInterceptor import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.httpPut import com.github.kittinunf.fuel.moshi.responseObject @@ -8,6 +11,7 @@ import com.github.kittinunf.result.Result import com.github.kittinunf.result.getAs import com.squareup.moshi.Moshi import kotlinx.coroutines.experimental.CommonPool +import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.withContext import pakutoma.iijmiocouponwidget.exception.NotFoundValidTokenException import pakutoma.iijmiocouponwidget.exception.UndefinedPlanException @@ -35,11 +39,10 @@ class CouponAPI constructor(developerID: String, accessToken: String) { private suspend fun sendHttpGetCouponInfo(): CouponDataFromJson = suspendCoroutine { cont -> "/coupon/".httpGet() - .responseObject { _, _, result -> + .responseObject { _, response, result -> when (result) { is Result.Failure -> { - val (data, _) = result - if (data?.returnCode?.contains("User Authorization Failure") == true) { + if(response.statusCode == 403) { cont.resumeWithException(NotFoundValidTokenException("User Authorization Failure")) } cont.resumeWithException(result.getAs()!!) @@ -63,9 +66,12 @@ class CouponAPI constructor(developerID: String, accessToken: String) { "/coupon/".httpPut() .header("Content-Type" to "application/json") .body(json) - .responseObject { _, _, result -> + .responseObject { req, response, result -> when (result) { is Result.Failure -> { + if(response.statusCode == 403) { + cont.resumeWithException(NotFoundValidTokenException("User Authorization Failure")) + } cont.resumeWithException(result.getAs()!!) } is Result.Success -> { From c611ad4d05359d3114a5590107d114a2a30ae9cf Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Tue, 25 Sep 2018 11:15:07 +0900 Subject: [PATCH 06/13] =?UTF-8?q?=E6=9C=AA=E4=BD=BF=E7=94=A8=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 6 --- .../activity/AdvancedSettingsActivity.kt | 38 ------------------- .../iijmiocouponwidget/utility/CouponAPI.kt | 2 +- .../res/xml/activity_advanced_settings.xml | 15 -------- .../main/res/xml/activity_main_settings.xml | 8 ---- 5 files changed, 1 insertion(+), 68 deletions(-) delete mode 100644 app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt delete mode 100644 app/src/main/res/xml/activity_advanced_settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0da9120..f259e7e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -70,12 +70,6 @@ - - - - - diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt deleted file mode 100644 index 2908256..0000000 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/activity/AdvancedSettingsActivity.kt +++ /dev/null @@ -1,38 +0,0 @@ -package pakutoma.iijmiocouponwidget.activity - -import android.os.Bundle -import android.preference.PreferenceFragment -import android.support.v7.app.AppCompatActivity -import android.view.MenuItem -import pakutoma.iijmiocouponwidget.R - -/** - * Created by PAKUTOMA on 2018/05/25. - */ - -class AdvancedSettingsActivity : AppCompatActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - supportActionBar?.setDisplayHomeAsUpEnabled(true) - fragmentManager.beginTransaction() - .replace(android.R.id.content, SettingsPreferenceFragment()) - .commit() - } - - class SettingsPreferenceFragment : PreferenceFragment() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - addPreferencesFromResource(R.xml.activity_advanced_settings) - } - } - - override fun onOptionsItemSelected(item: MenuItem?): Boolean { - when (item?.itemId) { - android.R.id.home -> { - finish() - } - } - return super.onOptionsItemSelected(item) - } -} \ No newline at end of file diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt index 8b6c269..2b5feb2 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt @@ -66,7 +66,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { "/coupon/".httpPut() .header("Content-Type" to "application/json") .body(json) - .responseObject { req, response, result -> + .responseObject { _, response, result -> when (result) { is Result.Failure -> { if(response.statusCode == 403) { diff --git a/app/src/main/res/xml/activity_advanced_settings.xml b/app/src/main/res/xml/activity_advanced_settings.xml deleted file mode 100644 index d8cac35..0000000 --- a/app/src/main/res/xml/activity_advanced_settings.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/xml/activity_main_settings.xml b/app/src/main/res/xml/activity_main_settings.xml index 4da0163..0ba1eac 100644 --- a/app/src/main/res/xml/activity_main_settings.xml +++ b/app/src/main/res/xml/activity_main_settings.xml @@ -12,14 +12,6 @@ android:targetClass="pakutoma.iijmiocouponwidget.activity.WidgetSettingsActivity" android:targetPackage="pakutoma.iijmiocouponwidget" /> - - - \ No newline at end of file From bd269e8dfe6d718cbcd9371245ce0e23387f73c4 Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Wed, 20 Mar 2019 19:52:39 +0900 Subject: [PATCH 07/13] =?UTF-8?q?Fix:=20=E3=83=8D=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=83=AF=E3=83=BC=E3=82=AF=E3=82=B3=E3=83=BC=E3=83=AB=E3=83=90?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=AE=E7=99=BB=E9=8C=B2=E8=A7=A3=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt index 1857830..c49f16c 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt @@ -119,6 +119,7 @@ class SwitchCouponService : Service() { val callback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network?) { super.onAvailable(network) + cm.unregisterNetworkCallback(this); cont.resume(network) } } From bb0442459c7b91cda609eb83cdc8ea22475356f8 Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Fri, 22 Mar 2019 20:48:11 +0900 Subject: [PATCH 08/13] =?UTF-8?q?Kotlin=E3=81=AE=E3=83=90=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E3=83=A7=E3=83=B3=E3=82=921.3=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 15 +++++++++++-- .../service/CouponFunctions.kt | 11 +++------- .../service/FetchRemainsJobService.kt | 22 +++---------------- .../service/FetchRemainsService.kt | 14 +++--------- .../service/SwitchCouponService.kt | 13 +++++------ .../iijmiocouponwidget/utility/CouponAPI.kt | 19 ++++++++-------- .../utility/CouponAPITest.kt | 6 ++--- build.gradle | 5 +++-- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 9 files changed, 43 insertions(+), 66 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d1817bf..bd6c0e5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,21 +5,32 @@ diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt index 721925a..f4db411 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/CouponFunctions.kt @@ -1,13 +1,8 @@ package pakutoma.iijmiocouponwidget.service import android.content.Context -import android.database.sqlite.SQLiteException import android.widget.Toast -import com.github.kittinunf.fuel.core.HttpException -import kotlinx.coroutines.experimental.android.UI -import kotlinx.coroutines.experimental.launch -import kotlinx.coroutines.experimental.async -import kotlinx.coroutines.experimental.withContext +import kotlinx.coroutines.* import org.jetbrains.anko.db.* import org.jetbrains.anko.defaultSharedPreferences import pakutoma.iijmiocouponwidget.R @@ -57,7 +52,7 @@ private suspend fun accessCoupon(context: Context, change: Boolean) { editor.apply() } -private suspend fun showResult(context: Context, change: Boolean, hasToken: Boolean, couldGet: Boolean = false, remains: Int = -1, isCouponEnabled: Boolean = false) = withContext(UI) { +private suspend fun showResult(context: Context, change: Boolean, hasToken: Boolean, couldGet: Boolean = false, remains: Int = -1, isCouponEnabled: Boolean = false) = withContext(Dispatchers.Main) { if (change) { sendToast(context, hasToken, couldGet, isCouponEnabled) } @@ -66,7 +61,7 @@ private suspend fun showResult(context: Context, change: Boolean, hasToken: Bool private suspend fun fetchCouponInfo(context: Context, coupon: CouponAPI, change: Boolean): Triple> { val couponInfo = coupon.fetchCouponInfo() - val db = async { updateDb(context, couponInfo) } + val db = GlobalScope.async { updateDb(context, couponInfo) } val (enablePlans, enableLines) = getEnablePlansAndLines(context, couponInfo) val isCouponEnabledNow = if (enableLines.isEmpty()) { couponInfo.planInfoList[0].lineInfoList[0].couponUse diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsJobService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsJobService.kt index e5f238d..614057a 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsJobService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsJobService.kt @@ -1,30 +1,14 @@ package pakutoma.iijmiocouponwidget.service import android.app.job.JobParameters -import android.content.Context -import kotlinx.coroutines.experimental.launch -import pakutoma.iijmiocouponwidget.R - -import java.io.IOException +import kotlinx.coroutines.launch import android.app.job.JobService -import com.github.kittinunf.fuel.core.HttpException -import kotlinx.coroutines.experimental.android.UI -import kotlinx.coroutines.experimental.withContext - -import pakutoma.iijmiocouponwidget.utility.CouponAPI -import pakutoma.iijmiocouponwidget.exception.NotFoundValidTokenException -import pakutoma.iijmiocouponwidget.utility.CouponInfo -import pakutoma.iijmiocouponwidget.widget.updateSwitchStatus - +import kotlinx.coroutines.GlobalScope -/** - * Created by PAKUTOMA on 2016/06/21. - * Get Traffic Function - */ class FetchRemainsJobService : JobService() { override fun onStartJob(params: JobParameters?): Boolean { - launch { + GlobalScope.launch { fetchRemains(applicationContext) jobFinished(params, true) } diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt index 35d4c38..d54f79c 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt @@ -3,21 +3,13 @@ package pakutoma.iijmiocouponwidget.service import android.annotation.TargetApi import android.app.Notification import android.app.Service -import android.content.Context import android.content.Intent import android.os.Build import android.os.IBinder -import com.github.kittinunf.fuel.core.HttpException -import kotlinx.coroutines.experimental.android.UI -import kotlinx.coroutines.experimental.launch -import kotlinx.coroutines.experimental.withContext +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import pakutoma.iijmiocouponwidget.R -import pakutoma.iijmiocouponwidget.exception.NotFoundValidTokenException -import pakutoma.iijmiocouponwidget.utility.CouponAPI -import pakutoma.iijmiocouponwidget.utility.CouponInfo import pakutoma.iijmiocouponwidget.widget.changeToFetchingMode -import pakutoma.iijmiocouponwidget.widget.updateSwitchStatus -import java.io.IOException /** * Created by PAKUTOMA on 2018/03/06. @@ -43,7 +35,7 @@ class FetchRemainsService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { changeToFetchingMode(applicationContext) - launch { + GlobalScope.launch { fetchRemains(applicationContext) stopSelf() } diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt index c49f16c..57db994 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt @@ -11,17 +11,14 @@ import android.net.Network import android.net.NetworkRequest import android.os.IBinder import android.widget.Toast -import kotlinx.coroutines.experimental.android.UI -import kotlinx.coroutines.experimental.launch -import kotlinx.coroutines.experimental.withContext import pakutoma.iijmiocouponwidget.R import pakutoma.iijmiocouponwidget.activity.OpenBrowserActivity import pakutoma.iijmiocouponwidget.widget.changeToWaitMode import pakutoma.iijmiocouponwidget.widget.setSwitchPendingIntent import android.os.Build -import kotlinx.coroutines.experimental.delay -import kotlinx.coroutines.experimental.withTimeoutOrNull -import kotlin.coroutines.experimental.suspendCoroutine +import kotlinx.coroutines.* +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine /** @@ -84,10 +81,10 @@ class SwitchCouponService : Service() { val cm = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val networkRequest = NetworkRequest.Builder().build() - launch { + GlobalScope.launch { if (timeFromFinish < 1000 * 60) { val remainingTime = 1000 * 60 - timeFromFinish - withContext(UI) { + withContext(Dispatchers.Main) { Toast.makeText(applicationContext, "API limit: " + java.lang.Long.toString(remainingTime / 1000) + "秒後に切替を行います", Toast.LENGTH_SHORT).show() } delay(1000 * 60 - timeFromFinish) diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt index 2b5feb2..0b27a53 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt @@ -1,7 +1,5 @@ package pakutoma.iijmiocouponwidget.utility -import android.util.Log -import android.widget.Toast import com.github.kittinunf.fuel.core.FuelManager import com.github.kittinunf.fuel.core.interceptors.validatorResponseInterceptor import com.github.kittinunf.fuel.httpGet @@ -10,12 +8,13 @@ import com.github.kittinunf.fuel.moshi.responseObject import com.github.kittinunf.result.Result import com.github.kittinunf.result.getAs import com.squareup.moshi.Moshi -import kotlinx.coroutines.experimental.CommonPool -import kotlinx.coroutines.experimental.android.UI -import kotlinx.coroutines.experimental.withContext +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import pakutoma.iijmiocouponwidget.exception.NotFoundValidTokenException import pakutoma.iijmiocouponwidget.exception.UndefinedPlanException -import kotlin.coroutines.experimental.suspendCoroutine +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException +import kotlin.coroutines.suspendCoroutine /** * iijmio coupon switch api wrapper @@ -33,7 +32,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { } suspend fun fetchCouponInfo(): CouponInfo { - val fetchedData = withContext(CommonPool) { sendHttpGetCouponInfo() } + val fetchedData = withContext(Dispatchers.Default) { sendHttpGetCouponInfo() } return convert(fetchedData) } @@ -42,7 +41,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { .responseObject { _, response, result -> when (result) { is Result.Failure -> { - if(response.statusCode == 403) { + if (response.statusCode == 403) { cont.resumeWithException(NotFoundValidTokenException("User Authorization Failure")) } cont.resumeWithException(result.getAs()!!) @@ -59,7 +58,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { val moshi = Moshi.Builder().build() val adapter = moshi.adapter(CouponDataToJson::class.java) val json = adapter.toJson(couponDataToJson) - return withContext(CommonPool) { sendHttpPutCouponStatus(json) } + return withContext(Dispatchers.Default) { sendHttpPutCouponStatus(json) } } private suspend fun sendHttpPutCouponStatus(json: String): ReturnCodeFromJson = suspendCoroutine { cont -> @@ -69,7 +68,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { .responseObject { _, response, result -> when (result) { is Result.Failure -> { - if(response.statusCode == 403) { + if (response.statusCode == 403) { cont.resumeWithException(NotFoundValidTokenException("User Authorization Failure")) } cont.resumeWithException(result.getAs()!!) diff --git a/app/src/test/java/pakutoma/iijmiocouponwidget/utility/CouponAPITest.kt b/app/src/test/java/pakutoma/iijmiocouponwidget/utility/CouponAPITest.kt index 9e4e6ac..8c49936 100644 --- a/app/src/test/java/pakutoma/iijmiocouponwidget/utility/CouponAPITest.kt +++ b/app/src/test/java/pakutoma/iijmiocouponwidget/utility/CouponAPITest.kt @@ -1,8 +1,6 @@ package pakutoma.iijmiocouponwidget.utility -import kotlinx.coroutines.experimental.async -import kotlinx.coroutines.experimental.launch -import kotlinx.coroutines.experimental.runBlocking +import kotlinx.coroutines.runBlocking import org.junit.Test import org.junit.Assert.* @@ -11,7 +9,7 @@ import org.junit.Assert.* * Created by PAKUTOMA on 2018/02/21. */ class CouponAPITest { - private val accessToken = "Pns2pgN62W6po8wv23v2YHzW87E0kPn1519200200" + private val accessToken = "HfXcx3zYSrG3ukkQtZjsQATkqX191aw1553251721" private val developerID = "IilCI1xrAgqKrXV9Zt4" @Test fun fetchCouponInfo() { diff --git a/build.gradle b/build.gradle index 103cdda..36a9778 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.30' + ext.kotlin_version = '1.3.21' repositories { jcenter() google() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.3.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" @@ -19,6 +19,7 @@ buildscript { allprojects { repositories { + mavenCentral() jcenter() google() } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1a59dfa..7dcd8a5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Feb 18 15:01:11 JST 2018 +#Thu Mar 21 15:51:22 JST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip From c226b89682e13c7722b556ba6bff84d8dd074a9b Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Fri, 22 Mar 2019 20:49:17 +0900 Subject: [PATCH 09/13] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=83=96=E3=83=A9=E3=83=AA=E3=81=AE=E3=83=90=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=82=92=E6=9C=80=E6=96=B0=E7=89=88=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 32 +++++++------------ .../iijmiocouponwidget/utility/CouponAPI.kt | 5 ++- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0db0c7f..bebd408 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,25 +32,15 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:27.1.0' - compile 'com.github.kittinunf.fuel:fuel:1.12.1' - compile 'com.github.kittinunf.fuel:fuel-moshi:1.12.1' - compile 'com.squareup.moshi:moshi:1.5.0' - compile 'com.squareup.moshi:moshi-kotlin:1.5.0' - compile 'org.jetbrains.anko:anko-sqlite:0.10.4' - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.3' - compile 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.3' -} - -repositories { - mavenCentral() -} - -kotlin { - experimental { - coroutines "enable" - } + implementation fileTree(include: ['*.jar'], dir: 'libs') + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.github.kittinunf.fuel:fuel:2.0.1' + implementation 'com.github.kittinunf.fuel:fuel-moshi:2.0.1' + implementation 'com.squareup.moshi:moshi:1.8.0' + implementation 'com.squareup.moshi:moshi-kotlin:1.8.0' + implementation 'org.jetbrains.anko:anko-sqlite:0.10.8' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1' } diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt index 0b27a53..6151b3b 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/utility/CouponAPI.kt @@ -1,7 +1,7 @@ package pakutoma.iijmiocouponwidget.utility import com.github.kittinunf.fuel.core.FuelManager -import com.github.kittinunf.fuel.core.interceptors.validatorResponseInterceptor +import com.github.kittinunf.fuel.core.extensions.jsonBody import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.httpPut import com.github.kittinunf.fuel.moshi.responseObject @@ -63,8 +63,7 @@ class CouponAPI constructor(developerID: String, accessToken: String) { private suspend fun sendHttpPutCouponStatus(json: String): ReturnCodeFromJson = suspendCoroutine { cont -> "/coupon/".httpPut() - .header("Content-Type" to "application/json") - .body(json) + .jsonBody(json) .responseObject { _, response, result -> when (result) { is Result.Failure -> { From 9e94535d0779d062e407a11ea9e1d9670d06e8a5 Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Fri, 22 Mar 2019 20:51:24 +0900 Subject: [PATCH 10/13] =?UTF-8?q?=E3=83=93=E3=83=AB=E3=83=89=E3=82=BF?= =?UTF-8?q?=E3=83=BC=E3=82=B2=E3=83=83=E3=83=88=E3=82=92Pie=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 +++--- app/src/main/AndroidManifest.xml | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index bebd408..4b2633d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,14 +2,14 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { - compileSdkVersion 27 - buildToolsVersion '27.0.3' + compileSdkVersion 28 + buildToolsVersion '28.0.3' defaultConfig { applicationId "pakutoma.iijmiocouponwidget" minSdkVersion 21 versionCode 5 versionName "2.1 Hotfix 2" - targetSdkVersion 27 + targetSdkVersion 28 } buildTypes { release { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f259e7e..8b3da2c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + Date: Sun, 24 Mar 2019 17:42:10 +0900 Subject: [PATCH 11/13] Change .gigignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c6cbe56..5f94008 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ *.iml .gradle /local.properties -/.idea/workspace.xml -/.idea/libraries +/.idea .DS_Store /build /captures From b40999131c7b9d321f5b5af62040a0398f120cb8 Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Sun, 24 Mar 2019 17:45:43 +0900 Subject: [PATCH 12/13] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E3=81=AE=E6=96=87?= =?UTF-8?q?=E9=9D=A2=E3=81=8A=E3=82=88=E3=81=B3=E3=82=A2=E3=82=A4=E3=82=B3?= =?UTF-8?q?=E3=83=B3=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/FetchRemainsService.kt | 4 +- .../service/SwitchCouponService.kt | 39 ++++++++++++++----- app/src/main/res/drawable/ic_notification.xml | 4 ++ app/src/main/res/values/strings.xml | 6 ++- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/drawable/ic_notification.xml diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt index d54f79c..0eeeee0 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/FetchRemainsService.kt @@ -27,7 +27,9 @@ class FetchRemainsService : Service() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notification = Notification .Builder(applicationContext, "switch_service") - .setContentTitle(applicationContext.getString(R.string.switch_notification)) + .setSmallIcon(R.drawable.ic_notification) + .setContentTitle(applicationContext.getString(R.string.notification_title)) + .setContentText(applicationContext.getString(R.string.fetch_remains_notification_text)) .build() startForeground(FETCH_REMAINS_NOTIFICATION_ID, notification) } diff --git a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt index 57db994..03f468e 100644 --- a/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt +++ b/app/src/main/java/pakutoma/iijmiocouponwidget/service/SwitchCouponService.kt @@ -2,6 +2,7 @@ package pakutoma.iijmiocouponwidget.service import android.annotation.TargetApi import android.app.Notification +import android.app.NotificationManager import android.app.Service import android.content.Context import android.content.Intent @@ -29,22 +30,15 @@ class SwitchCouponService : Service() { companion object { private const val SWITCH_COUPON_NOTIFICATION_ID = 1 + private var builder: Notification.Builder? = null } override fun onCreate() { super.onCreate() - @TargetApi(Build.VERSION_CODES.O) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val notification = Notification - .Builder(applicationContext, "switch_service") - .setContentTitle(applicationContext.getString(R.string.switch_notification)) - .build() - startForeground(SWITCH_COUPON_NOTIFICATION_ID, notification) - } + builder = notifyInitialNotification() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val preferences = getSharedPreferences("iijmio_token", Context.MODE_PRIVATE); if (preferences == null) { stopSelf() @@ -78,6 +72,7 @@ class SwitchCouponService : Service() { } changeToWaitMode(applicationContext, preferences.getBoolean("is_coupon_enabled", false)) + updateNotification(builder, applicationContext.getString(R.string.switch_coupon_notification_text)) val cm = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val networkRequest = NetworkRequest.Builder().build() @@ -87,6 +82,7 @@ class SwitchCouponService : Service() { withContext(Dispatchers.Main) { Toast.makeText(applicationContext, "API limit: " + java.lang.Long.toString(remainingTime / 1000) + "秒後に切替を行います", Toast.LENGTH_SHORT).show() } + updateNotification(builder, applicationContext.getString(R.string.switch_coupon_notification_wait_text)) delay(1000 * 60 - timeFromFinish) } val network = withTimeoutOrNull(1000 * 60) { @@ -123,4 +119,29 @@ class SwitchCouponService : Service() { cm.registerNetworkCallback(networkRequest, callback) } + private fun notifyInitialNotification(): Notification.Builder? { + @TargetApi(Build.VERSION_CODES.O) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val builder = Notification.Builder(applicationContext, "switch_service") + builder.setSmallIcon(R.drawable.ic_notification) + .setContentTitle(applicationContext.getString(R.string.notification_title)) + .setContentText(applicationContext.getString(R.string.notification_text)) + startForeground(SwitchCouponService.SWITCH_COUPON_NOTIFICATION_ID, builder.build()) + return builder + } else { + return null + } + } + + private fun updateNotification(builder: Notification.Builder?, text: String) { + @TargetApi(Build.VERSION_CODES.O) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && builder != null) { + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + builder.setContentText(text) + notificationManager.notify(SWITCH_COUPON_NOTIFICATION_ID, builder.build()) + } + } + } + + diff --git a/app/src/main/res/drawable/ic_notification.xml b/app/src/main/res/drawable/ic_notification.xml new file mode 100644 index 0000000..ea54187 --- /dev/null +++ b/app/src/main/res/drawable/ic_notification.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1943c32..0071229 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,7 +4,11 @@ IilCI1xrAgqKrXV9Zt4 クーポン切り替え・通信量取得 - 通信中 + mioWidget + 通信しています + クーポン残量を取得しています + クーポンを切り替えています + クーポン切り替えを待機しています 切替回線・表示プランの設定 詳細設定 残量を表示するプラン From ce24876bae968afe18cbf92d59967ee46cb4e2ea Mon Sep 17 00:00:00 2001 From: Chiba Tairi Date: Sun, 14 Apr 2019 20:24:00 +0900 Subject: [PATCH 13/13] =?UTF-8?q?=E3=83=AA=E3=83=AA=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E6=BA=96=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- app/build.gradle | 4 ++-- app/src/main/assets/about.html | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index bd6c0e5..d7c5659 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -35,5 +35,5 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 4b2633d..392272d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { applicationId "pakutoma.iijmiocouponwidget" minSdkVersion 21 - versionCode 5 - versionName "2.1 Hotfix 2" + versionCode 6 + versionName "2.2" targetSdkVersion 28 } buildTypes { diff --git a/app/src/main/assets/about.html b/app/src/main/assets/about.html index bc4a1d5..0599b15 100644 --- a/app/src/main/assets/about.html +++ b/app/src/main/assets/about.html @@ -1,7 +1,7 @@ -

mioWidget 2.1

-

Copyright (c) 2018 @pakutoma

+

mioWidget 2.2

+

Copyright (c) 2019 @pakutoma

\ No newline at end of file