調整專案結構
This commit is contained in:
parent
e5ba0fe34b
commit
6aaea610b8
@ -27,12 +27,12 @@
|
|||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".AlarmService"
|
android:name=".ClockUpdateService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="true" />
|
android:exported="true" />
|
||||||
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".IOSClockWidget"
|
android:name=".receiver.IOSClockWidget"
|
||||||
android:exported="false">
|
android:exported="false">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||||
@ -44,7 +44,7 @@
|
|||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".AutoStartReceiver"
|
android:name=".receiver.AutoStartReceiver"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="true" >
|
android:exported="true" >
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
|||||||
@ -5,23 +5,24 @@ import android.app.*
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.PowerManager.WakeLock
|
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
|
import com.ray650128.iosclockwidget.receiver.IOSClockWidget
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
class AlarmService : Service() {
|
class ClockUpdateService : Service() {
|
||||||
|
|
||||||
private val timerThread = Thread {
|
private val timerThread = Thread {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!isServiceRunning) break
|
if (!isServiceRunning) break
|
||||||
val calendar = Calendar.getInstance()
|
val calendar = Calendar.getInstance()
|
||||||
val second = calendar[Calendar.SECOND]
|
val second = calendar[Calendar.SECOND]
|
||||||
|
val millisecond = calendar[Calendar.MILLISECOND]
|
||||||
// 計算時、分、秒的旋轉角度
|
// 計算時、分、秒的旋轉角度
|
||||||
secondAngle = (second * 360f / 60f)
|
secondAngle = ((second + (millisecond / 1000f)) * 360f / 60f)//(second * 360f / 60f)
|
||||||
sendWidgetIntent()
|
sendWidgetIntent()
|
||||||
Thread.sleep(1000)
|
Thread.sleep(100)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +102,7 @@ class AlarmService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val TAG = AlarmService::class.java.simpleName
|
private val TAG = ClockUpdateService::class.java.simpleName
|
||||||
|
|
||||||
const val SERVICE_ID = 0x101
|
const val SERVICE_ID = 0x101
|
||||||
|
|
||||||
@ -5,15 +5,17 @@ import android.content.Intent
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import com.ray650128.iosclockwidget.utils.PermissionUtil
|
||||||
|
import com.ray650128.iosclockwidget.utils.PreferenceUtil
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private val callback = object : PermissionUtil.PermissionResultCallback {
|
private val callback = object : PermissionUtil.PermissionResultCallback {
|
||||||
override fun onGrant() {
|
override fun onGrant() {
|
||||||
PreferenceUtil.isGranted = true
|
PreferenceUtil.isGranted = true
|
||||||
if (AlarmService.isServiceRunning) return
|
if (ClockUpdateService.isServiceRunning) return
|
||||||
startForegroundService(
|
startForegroundService(
|
||||||
Intent(this@MainActivity, AlarmService::class.java)
|
Intent(this@MainActivity, ClockUpdateService::class.java)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,9 +48,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
PreferenceUtil.isGranted = true
|
PreferenceUtil.isGranted = true
|
||||||
if (AlarmService.isServiceRunning) return
|
if (ClockUpdateService.isServiceRunning) return
|
||||||
startForegroundService(
|
startForegroundService(
|
||||||
Intent(this@MainActivity, AlarmService::class.java)
|
Intent(this@MainActivity, ClockUpdateService::class.java)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
package com.ray650128.iosclockwidget
|
package com.ray650128.iosclockwidget.receiver
|
||||||
|
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.ray650128.iosclockwidget.ClockUpdateService
|
||||||
|
import com.ray650128.iosclockwidget.utils.PreferenceUtil
|
||||||
|
|
||||||
class AutoStartReceiver : BroadcastReceiver() {
|
class AutoStartReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
@ -11,7 +13,7 @@ class AutoStartReceiver : BroadcastReceiver() {
|
|||||||
Log.d("AutoStartReceiver", "intent: ${intent?.action}")
|
Log.d("AutoStartReceiver", "intent: ${intent?.action}")
|
||||||
if (context == null || intent == null) return
|
if (context == null || intent == null) return
|
||||||
if (!PreferenceUtil.isGranted) return
|
if (!PreferenceUtil.isGranted) return
|
||||||
val i = Intent(context, AlarmService::class.java)
|
val i = Intent(context, ClockUpdateService::class.java)
|
||||||
context.startForegroundService(i)
|
context.startForegroundService(i)
|
||||||
Log.d("AutoStartReceiver", "Boot completed.")
|
Log.d("AutoStartReceiver", "Boot completed.")
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.iosclockwidget
|
package com.ray650128.iosclockwidget.receiver
|
||||||
|
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetProvider
|
import android.appwidget.AppWidgetProvider
|
||||||
@ -9,6 +9,8 @@ import android.graphics.Bitmap
|
|||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
|
import com.ray650128.iosclockwidget.ClockUpdateService
|
||||||
|
import com.ray650128.iosclockwidget.R
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of App Widget functionality.
|
* Implementation of App Widget functionality.
|
||||||
@ -21,7 +23,7 @@ class IOSClockWidget : AppWidgetProvider() {
|
|||||||
val appWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, IOSClockWidget::class.java))
|
val appWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, IOSClockWidget::class.java))
|
||||||
|
|
||||||
when (intent?.action) {
|
when (intent?.action) {
|
||||||
AlarmService.SECOND_CHANGED -> {
|
ClockUpdateService.SECOND_CHANGED -> {
|
||||||
for (appWidgetId in appWidgetIds) {
|
for (appWidgetId in appWidgetIds) {
|
||||||
updateAppWidget(context, appWidgetManager, appWidgetId)
|
updateAppWidget(context, appWidgetManager, appWidgetId)
|
||||||
}
|
}
|
||||||
@ -58,10 +60,12 @@ internal fun updateAppWidget(
|
|||||||
// Construct the RemoteViews object
|
// Construct the RemoteViews object
|
||||||
val views = RemoteViews(context.packageName, R.layout.i_o_s_clock_widget)
|
val views = RemoteViews(context.packageName, R.layout.i_o_s_clock_widget)
|
||||||
views.setTextViewText(R.id.appwidget_text, widgetText)
|
views.setTextViewText(R.id.appwidget_text, widgetText)
|
||||||
val bmpOriginal = BitmapFactory.decodeResource(context.applicationContext.resources, R.drawable.img_second_hand)
|
val bmpOriginal = BitmapFactory.decodeResource(context.applicationContext.resources,
|
||||||
|
R.drawable.img_second_hand
|
||||||
|
)
|
||||||
val bmpResult = Bitmap.createBitmap(285, 285, Bitmap.Config.ARGB_8888)
|
val bmpResult = Bitmap.createBitmap(285, 285, Bitmap.Config.ARGB_8888)
|
||||||
val tempCanvas = Canvas(bmpResult)
|
val tempCanvas = Canvas(bmpResult)
|
||||||
tempCanvas.rotate(AlarmService.secondAngle, 285 / 2.toFloat(), 285 / 2.toFloat())
|
tempCanvas.rotate(ClockUpdateService.secondAngle, 285 / 2.toFloat(), 285 / 2.toFloat())
|
||||||
tempCanvas.drawBitmap(bmpOriginal, 0f, 0f, null)
|
tempCanvas.drawBitmap(bmpOriginal, 0f, 0f, null)
|
||||||
views.setImageViewBitmap(R.id.imageView, bmpResult)
|
views.setImageViewBitmap(R.id.imageView, bmpResult)
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.iosclockwidget
|
package com.ray650128.iosclockwidget.utils
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package com.ray650128.iosclockwidget
|
package com.ray650128.iosclockwidget.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import java.lang.reflect.Type
|
import com.ray650128.iosclockwidget.MyApplication
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,7 +12,8 @@ object PreferenceUtil {
|
|||||||
private const val MAIN_KEY = "iOS_Clock_Widget"
|
private const val MAIN_KEY = "iOS_Clock_Widget"
|
||||||
private const val IS_GRANTED = "IS_GRANTED"
|
private const val IS_GRANTED = "IS_GRANTED"
|
||||||
|
|
||||||
private val sharedPreferences = MyApplication.getAppContext().getSharedPreferences(MAIN_KEY, Context.MODE_PRIVATE)
|
private val sharedPreferences = MyApplication.getAppContext()
|
||||||
|
.getSharedPreferences(MAIN_KEY, Context.MODE_PRIVATE)
|
||||||
|
|
||||||
var isGranted: Boolean
|
var isGranted: Boolean
|
||||||
get() = sharedPreferences.getBoolean(IS_GRANTED, false)
|
get() = sharedPreferences.getBoolean(IS_GRANTED, false)
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.ray650128.iosclockwidget
|
package com.ray650128.iosclockwidget.view
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.*
|
import android.graphics.*
|
||||||
@ -6,7 +6,7 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<com.ray650128.iosclockwidget.IOSClockView
|
<com.ray650128.iosclockwidget.view.IOSClockView
|
||||||
android:id="@+id/IOSClockView2"
|
android:id="@+id/IOSClockView2"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user