Skip to content

Commit

Permalink
Merge pull request #36 from pakutoma/master
Browse files Browse the repository at this point in the history
ver2.2リリース
  • Loading branch information
pakutoma authored Apr 14, 2019
2 parents 14189dc + ce24876 commit 88b810c
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 195 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea
.DS_Store
/build
/captures
22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

3 changes: 1 addition & 2 deletions .idea/gradle.xml

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

11 changes: 0 additions & 11 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

8 changes: 8 additions & 0 deletions .idea/kotlinc.xml

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

25 changes: 14 additions & 11 deletions .idea/misc.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/modules.xml

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

1 change: 1 addition & 0 deletions .idea/vcs.xml

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

42 changes: 16 additions & 26 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
buildToolsVersion '26.0.2'
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "pakutoma.iijmiocouponwidget"
minSdkVersion 21
versionCode 3
versionName "2.1"
targetSdkVersion 27
versionCode 6
versionName "2.2"
targetSdkVersion 28
}
buildTypes {
release {
Expand All @@ -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'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="true"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/assets/about.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head><style> body { font-family: sans-serif; }</style></head>
<body>
<h3>mioWidget 2.1</h3>
<p>Copyright (c) 2018 @pakutoma</p>
<h3>mioWidget 2.2</h3>
<p>Copyright (c) 2019 @pakutoma</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,12 +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.withContext
import kotlinx.coroutines.*
import org.jetbrains.anko.db.*
import org.jetbrains.anko.defaultSharedPreferences
import pakutoma.iijmiocouponwidget.R
Expand Down Expand Up @@ -47,7 +43,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) {
Expand All @@ -56,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)
}
Expand All @@ -65,7 +61,7 @@ private suspend fun showResult(context: Context, change: Boolean, hasToken: Bool

private suspend fun fetchCouponInfo(context: Context, coupon: CouponAPI, change: Boolean): Triple<Boolean, Int, List<String>> {
val couponInfo = coupon.fetchCouponInfo()
launch { 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
Expand All @@ -85,6 +81,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)
}

Expand Down Expand Up @@ -136,6 +133,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()
}
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,15 +27,17 @@ 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)
}
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
changeToFetchingMode(applicationContext)
launch {
GlobalScope.launch {
fetchRemains(applicationContext)
stopSelf()
}
Expand Down
Loading

0 comments on commit 88b810c

Please sign in to comment.