Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 change notification #33

Merged
merged 2 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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())
}
}

}


4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_notification.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="84"
android:viewportWidth="84" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M42,84a41.737,41.737 0,0 1,-16.348 -3.3,41.859 41.859,0 0,1 -13.35,-9 41.859,41.859 0,0 1,-9 -13.35A41.736,41.736 0,0 1,0 42a41.739,41.739 0,0 1,3.3 -16.349,41.859 41.859,0 0,1 9,-13.35 41.859,41.859 0,0 1,13.35 -9A41.737,41.737 0,0 1,42 0a41.739,41.739 0,0 1,16.349 3.3,41.859 41.859,0 0,1 13.35,9 41.859,41.859 0,0 1,9 13.35A41.739,41.739 0,0 1,84 42a41.737,41.737 0,0 1,-3.3 16.348,41.859 41.859,0 0,1 -9,13.35 41.86,41.86 0,0 1,-13.35 9A41.739,41.739 0,0 1,42 84ZM22,27a14.9,14.9 0,0 0,-10.606 4.394A14.9,14.9 0,0 0,7 42a14.9,14.9 0,0 0,4.393 10.606A14.9,14.9 0,0 0,22 57h40a14.9,14.9 0,0 0,10.607 -4.393A14.9,14.9 0,0 0,77 42a14.9,14.9 0,0 0,-4.393 -10.607A14.9,14.9 0,0 0,62 27ZM22,54a12.013,12.013 0,0 1,-12 -12,12.014 12.014,0 0,1 12,-12 12.014,12.014 0,0 1,12 12A12.014,12.014 0,0 1,22 54Z"/>
</vector>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<string name="developerID">IilCI1xrAgqKrXV9Zt4</string>
<string name="authUri"><![CDATA[https://api.iijmio.jp/mobile/d/v1/authorization/?response_type=token&client_id=IilCI1xrAgqKrXV9Zt4&state=example_state&redirect_uri=pakutoma.iijmiocouponwidget://callback]]></string>
<string name="switch_service">クーポン切り替え・通信量取得</string>
<string name="switch_notification">通信中</string>
<string name="notification_title">mioWidget</string>
<string name="notification_text">通信しています</string>
<string name="fetch_remains_notification_text">クーポン残量を取得しています</string>
<string name="switch_coupon_notification_text">クーポンを切り替えています</string>
<string name="switch_coupon_notification_wait_text">クーポン切り替えを待機しています</string>
<string name="widget_settings">切替回線・表示プランの設定</string>
<string name="advanced_settings">詳細設定</string>
<string name="show_remains_plans">残量を表示するプラン</string>
Expand Down