Skip to content

Commit

Permalink
caffeine in MIUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Dec 22, 2018
1 parent 07cda21 commit c107410
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 42 deletions.
12 changes: 6 additions & 6 deletions .idea/misc.xml

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

14 changes: 8 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "soko.ekibun.tilesbox"
minSdkVersion 24
targetSdkVersion 27
targetSdkVersion 28
versionCode 2
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -21,15 +21,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:support-v4:27.1.0"
implementation "com.google.zxing:core:3.3.0"
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.zxing:core:3.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package soko.ekibun.tilesbox.service
import android.service.quicksettings.TileService
import android.service.quicksettings.Tile
import android.preference.PreferenceManager
import android.provider.Settings
import android.widget.Toast
import soko.ekibun.tilesbox.R


Expand All @@ -17,24 +19,37 @@ class CaffeineQuickTileService: TileService() {

override// 点击
fun onClick() {
if (CaffeineService.isServiceRunning(this)) {
qsTile.state = Tile.STATE_INACTIVE
//关闭服务
CaffeineService.stopService(applicationContext)
if (Settings.System.canWrite(applicationContext)) {
val lastTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
if (lastTime == Int.MAX_VALUE) {
qsTile.state = Tile.STATE_INACTIVE
//关闭服务
CaffeineService.stopService(applicationContext)
} else {
qsTile.state = Tile.STATE_ACTIVE
//启动服务
CaffeineService.startService(applicationContext)
}
} else {
qsTile.state = Tile.STATE_ACTIVE
//启动服务
CaffeineService.startService(applicationContext)
qsTile.state = Tile.STATE_UNAVAILABLE
Toast.makeText(this, R.string.toast_no_permit, Toast.LENGTH_LONG).show()
}
qsTile.updateTile()//更新Tile
}

private fun refreshState() {
qsTile.label = PreferenceManager.getDefaultSharedPreferences(this).getString("caffeine_tile_label", getString(R.string.pref_caffeine_cate))
if (CaffeineService.isServiceRunning(this)) {
qsTile.state = Tile.STATE_ACTIVE
if (Settings.System.canWrite(applicationContext)) {
val lastTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
if (lastTime == Int.MAX_VALUE) {
CaffeineService.startService(applicationContext)
qsTile.state = Tile.STATE_ACTIVE
} else {
CaffeineService.stopService(applicationContext)
qsTile.state = Tile.STATE_INACTIVE
}
} else {
qsTile.state = Tile.STATE_INACTIVE
qsTile.state = Tile.STATE_UNAVAILABLE
}
qsTile.updateTile()//更新Tile
}
Expand Down
39 changes: 24 additions & 15 deletions app/src/main/java/soko/ekibun/tilesbox/service/CaffeineService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import android.app.PendingIntent
import android.app.Service
import android.content.*
import android.preference.PreferenceManager
import android.os.PowerManager
import android.os.IBinder
import android.provider.Settings
import android.widget.Toast
import soko.ekibun.tilesbox.R
import soko.ekibun.tilesbox.util.AppUtil
import soko.ekibun.tilesbox.util.NotificationUtil


@Suppress("DEPRECATION")
class CaffeineService : Service() {
private val mWakeLock: PowerManager.WakeLock by lazy{
(getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "keep_screen_on_tag")
}

private var myReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (Intent.ACTION_SCREEN_OFF == intent.action) {//当按下电源键,屏幕变黑的时候
Expand All @@ -36,37 +33,49 @@ class CaffeineService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startid: Int): Int {
if (intent != null) {
if (intent.getBooleanExtra("acquireCaffeine", false))
acquireCaffeine()
else if (intent.getBooleanExtra("notif_click", false))
releaseCaffeine()
if (Settings.System.canWrite(applicationContext)) {
if (intent != null) {
if (intent.getBooleanExtra("acquireCaffeine", false))
acquireCaffeine()
else if (intent.getBooleanExtra("notifyClick", false))
releaseCaffeine()
}
} else {
Toast.makeText(this, R.string.toast_no_permit, Toast.LENGTH_LONG).show()
}
return super.onStartCommand(intent, flags, startid)
}

private fun acquireCaffeine() {
mWakeLock.acquire()

val sp = PreferenceManager.getDefaultSharedPreferences(this)

val lastTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
if(lastTime != Int.MAX_VALUE){
sp.edit().putString("caffeine_timeout", (lastTime / 1000).toString()).apply()
}
Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, Int.MAX_VALUE)

if (sp.getBoolean("caffeine_show_notif", true)) {
val title = sp.getString("caffeine_notif_title", getString(R.string.caffeine_notif_title))
val title = sp.getString("caffeine_notif_title", getString(R.string.caffeine_notif_title))!!
val description = sp.getString("caffeine_notif_text", getString(R.string.caffeine_notif_text))
val builder = NotificationUtil.builder(this, "caffeine", title, NotificationManager.IMPORTANCE_LOW)
.setSmallIcon(R.drawable.ic_caffeine_tile)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(PendingIntent.getService(this, 0, Intent(this, CaffeineService::class.java).putExtra("notif_click", true), 0))
.setContentIntent(PendingIntent.getService(this, 0, Intent(this, CaffeineService::class.java)
.putExtra("notifyClick", true), 0))
startForeground(233, builder.build())
}
}

fun releaseCaffeine() {
val sp = PreferenceManager.getDefaultSharedPreferences(this)
Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, sp.getString("caffeine_timeout", "30")!!.toInt() * 1000)
stopSelf()
}

override fun onDestroy() {
mWakeLock.release()
releaseCaffeine()
unregisterReceiver(myReceiver)
stopForeground(true)
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere
override fun onPreferenceTreeClick(preferenceScreen: PreferenceScreen, preference: Preference): Boolean {
when(preference.key) {
"version_code" -> (activity as SettingsActivity).showInfo()
"caffeine_permit_write_system",
"rotation_permit_write_system" -> PermissionUtil.requestWriteSystemSettings(activity)
"corner_permit_float_window",
"rotation_permit_float_window" -> PermissionUtil.requestShowOverlay(activity)
Expand Down Expand Up @@ -94,10 +95,15 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere
textPref = findPreference("corner_size") as EditTextPreference
textPref.summary = textPref.text

textPref = findPreference("caffeine_timeout") as EditTextPreference
textPref.summary = textPref.text

var permitPref = findPreference("rotation_permit_float_window") as SwitchPreference
permitPref.isChecked = PermissionUtil.checkShowOverlay(context)
permitPref = findPreference("rotation_permit_write_system") as SwitchPreference
permitPref.isChecked = PermissionUtil.checkWriteSystemSettings(context)
permitPref = findPreference("caffeine_permit_write_system") as SwitchPreference
permitPref.isChecked = PermissionUtil.checkWriteSystemSettings(context)
permitPref = findPreference("corner_permit_float_window") as SwitchPreference
permitPref.isChecked = PermissionUtil.checkShowOverlay(context)

Expand Down
19 changes: 15 additions & 4 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
<PreferenceScreen
android:title="@string/pref_caffeine_cate"
android:icon="@drawable/ic_caffeine_tile">
<EditTextPreference
android:key="caffeine_tile_label"
android:title="@string/pref_tile_label"
android:defaultValue="@string/pref_caffeine_cate" />
<PreferenceCategory android:title="@string/pref_cate_permit">
<SwitchPreference
android:contextClickable="false"
android:key="caffeine_permit_write_system"
android:title="@string/permit_write_system" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_cate_other">
<EditTextPreference
android:key="caffeine_tile_label"
android:title="@string/pref_tile_label"
android:defaultValue="@string/pref_caffeine_cate" />
<SwitchPreference
android:key="caffeine_show_notif"
android:defaultValue="true"
Expand All @@ -22,6 +28,11 @@
android:dependency="caffeine_show_notif"
android:title="@string/caffeine_pref_notif_text"
android:defaultValue="@string/caffeine_notif_text"/>
<EditTextPreference
android:key="caffeine_timeout"
android:inputType="number"
android:title="默认时间(s)"
android:defaultValue="30" />
</PreferenceCategory>
</PreferenceScreen>
<PreferenceScreen
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip

0 comments on commit c107410

Please sign in to comment.