Skip to content

Commit

Permalink
feat: try to add light control
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Dec 25, 2022
1 parent 4f5ed51 commit abc3c22
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "info.plateaukao.quickrotate"
minSdk 25
targetSdk 32
versionCode 1090
versionName "1.9"
versionCode 2000
versionName "2.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity
android:name=".FrontLightActivity"
android:exported="false">
</activity>
<activity
android:name=".RefreshModeActivity"
android:exported="false" />
Expand Down Expand Up @@ -166,6 +170,16 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
<!-- <service-->
<!-- android:name=".service.FrontLightTileService"-->
<!-- android:exported="true"-->
<!-- android:icon="@drawable/ic_frontlight"-->
<!-- android:label="@string/front_light"-->
<!-- android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.service.quicksettings.action.QS_TILE" />-->
<!-- </intent-filter>-->
<!-- </service>-->
</application>

</manifest>
105 changes: 105 additions & 0 deletions app/src/main/java/info/plateaukao/quickrotate/FrontLightActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package info.plateaukao.quickrotate

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.SeekBar
import androidx.annotation.RequiresApi
import com.onyx.android.sdk.api.device.FrontLightController

class FrontLightActivity : Activity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


val dialog = AlertDialog.Builder(this)
.setView(R.layout.front_light_adjust_layout)
.show()
dialog.window?.setLayout(300.dp(this), ViewGroup.LayoutParams.WRAP_CONTENT)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent);
initViews(dialog)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun initViews(dialog: AlertDialog) {
val seekbarTemperature = dialog.findViewById<SeekBar>(R.id.seekbar_light_temperature)
val seekbarBrightness = dialog.findViewById<SeekBar>(R.id.seekbar_light_brightness)
val toggleButton = dialog.findViewById<ImageView>(R.id.iv_frontlight)
val isLightOn = FrontLightController.isColdLightOn(this)
toggleButton.setImageResource(if (isLightOn) R.drawable.ic_light_on else R.drawable.ic_light_off)
toggleButton.setOnClickListener {
if (FrontLightController.isColdLightOn(this)) {
enableFrontLight(false)
toggleButton.setImageResource(R.drawable.ic_light_off)
} else {
enableFrontLight(true)
toggleButton.setImageResource(R.drawable.ic_light_on)
}
}
val warmButton = dialog.findViewById<ImageView>(R.id.iv_temperature)
warmButton.setOnClickListener { seekbarTemperature.progress = 0 }

seekbarBrightness.max = 500
seekbarBrightness.min = 0
seekbarBrightness.progress = FrontLightController.getColdLightConfigValue(this) + FrontLightController.getWarmLightConfigValue(this)

var offSet = currentOffset
seekbarTemperature.max = 100
seekbarTemperature.min = -100
seekbarTemperature.progress = offSet

seekbarBrightness.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
FrontLightController.setColdLightDeviceValue(this@FrontLightActivity, progress / 2)
FrontLightController.setWarmLightDeviceValue(
this@FrontLightActivity,
progress/2 + offSet
)
}

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
seekbarTemperature.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
val coldBrightness =
FrontLightController.getColdLightConfigValue(this@FrontLightActivity)
offSet = progress
FrontLightController.setWarmLightDeviceValue(
this@FrontLightActivity,
coldBrightness + offSet
)
}

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
}

private val currentOffset = FrontLightController.getWarmLightConfigValue(this) -
FrontLightController.getColdLightConfigValue(this)

private fun enableFrontLight(isEnabled: Boolean) {
if (isEnabled) {
FrontLightController.openColdLight()
FrontLightController.openWarmLight()
} else {
FrontLightController.closeColdLight()
FrontLightController.closeWarmLight()
}
}
}

fun Int.dp(context: Context): Int {
val metrics = context.resources.displayMetrics
return (this * (metrics.densityDpi / 160f)).toInt()
}

Original file line number Diff line number Diff line change
@@ -1,20 +1,109 @@
package info.plateaukao.quickrotate.service

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Build
import android.service.quicksettings.TileService
import java.util.*
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.SeekBar
import androidx.annotation.RequiresApi
import com.onyx.android.sdk.api.device.FrontLightController
import info.plateaukao.quickrotate.FrontLightActivity
import info.plateaukao.quickrotate.R
import info.plateaukao.quickrotate.dp

class FrontLightTileService: TileService() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onClick() {
// show an alert dialog with progress bar
// when the progress bar is changed, send a broadcast to the service
AlertDialog.Builder(this)
.setTitle("Front Light")
.setView(R.layout.front_light_dialog)
.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
baseContext.sendBroadcast(it)

startActivity(
Intent(this, FrontLightActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
)
// val dialog = AlertDialog.Builder(this)
// .setView(R.layout.front_light_adjust_layout)
// .create()
// dialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
// dialog.show()
//
// dialog.window?.setLayout(300.dp(this), ViewGroup.LayoutParams.WRAP_CONTENT)
// initViews(dialog)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun initViews(dialog: AlertDialog) {
val seekbarTemperature = dialog.findViewById<SeekBar>(R.id.seekbar_light_temperature)
val seekbarBrightness = dialog.findViewById<SeekBar>(R.id.seekbar_light_brightness)
val toggleButton = dialog.findViewById<ImageView>(R.id.iv_frontlight)
val isLightOn = FrontLightController.isColdLightOn(this)
toggleButton.setImageResource(if (isLightOn) R.drawable.ic_light_on else R.drawable.ic_light_off)
toggleButton.setOnClickListener {
if (FrontLightController.isColdLightOn(this)) {
enableFrontLight(false)
toggleButton.setImageResource(R.drawable.ic_light_off)
} else {
enableFrontLight(true)
toggleButton.setImageResource(R.drawable.ic_light_on)
}
}
val warmButton = dialog.findViewById<ImageView>(R.id.iv_temperature)
warmButton.setOnClickListener { seekbarTemperature.progress = 0 }

seekbarBrightness.max = 500
seekbarBrightness.min = 0
seekbarBrightness.progress = FrontLightController.getColdLightConfigValue(this) + FrontLightController.getWarmLightConfigValue(this)

var offSet = currentOffset
seekbarTemperature.max = 100
seekbarTemperature.min = -100
seekbarTemperature.progress = offSet

seekbarBrightness.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
FrontLightController.setColdLightDeviceValue(this@FrontLightTileService, progress / 2)
FrontLightController.setWarmLightDeviceValue(
this@FrontLightTileService,
progress/2 + offSet
)
}
.show()

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
seekbarTemperature.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
val coldBrightness =
FrontLightController.getColdLightConfigValue(this@FrontLightTileService)
offSet = progress
FrontLightController.setWarmLightDeviceValue(
this@FrontLightTileService,
coldBrightness + offSet
)
}

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
}

private val currentOffset = FrontLightController.getWarmLightConfigValue(this) -
FrontLightController.getColdLightConfigValue(this)

private fun enableFrontLight(isEnabled: Boolean) {
if (isEnabled) {
FrontLightController.openColdLight()
FrontLightController.openWarmLight()
} else {
FrontLightController.closeColdLight()
FrontLightController.closeWarmLight()
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_frontlight.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,21c0,0.5 0.4,1 1,1h4c0.6,0 1,-0.5 1,-1v-1L9,20v1zM12,2C8.1,2 5,5.1 5,9c0,2.4 1.2,4.5 3,5.7L8,17c0,0.5 0.4,1 1,1h6c0.6,0 1,-0.5 1,-1v-2.3c1.8,-1.3 3,-3.4 3,-5.7 0,-3.9 -3.1,-7 -7,-7z"/>
</vector>
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/ic_light_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,5l0,-3l-12,0l0,1.17l1.83,1.83z"/>
<path android:fillColor="@android:color/white" android:pathData="M16,11l2,-3l0,-1l-8.17,0l6.17,6.17z"/>
<path android:fillColor="@android:color/white" android:pathData="M2.81,2.81L1.39,4.22L8,10.83V22h8v-3.17l3.78,3.78l1.41,-1.41L2.81,2.81z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_light_on.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,14l3,3v5h6v-5l3,-3V9H6V14zM11,2h2v3h-2V2zM3.5,5.88l1.41,-1.41l2.12,2.12L5.62,8L3.5,5.88zM16.96,6.59l2.12,-2.12l1.41,1.41L18.38,8L16.96,6.59z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/ic_warm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,12.9l-2.13,2.09C9.31,15.55 9,16.28 9,17.06C9,18.68 10.35,20 12,20s3,-1.32 3,-2.94c0,-0.78 -0.31,-1.52 -0.87,-2.07L12,12.9z"/>
<path android:fillColor="@android:color/white" android:pathData="M16,6l-0.44,0.55C14.38,8.02 12,7.19 12,5.3V2c0,0 -8,4 -8,11c0,2.92 1.56,5.47 3.89,6.86C7.33,19.07 7,18.1 7,17.06c0,-1.32 0.52,-2.56 1.47,-3.5L12,10.1l3.53,3.47c0.95,0.93 1.47,2.17 1.47,3.5c0,1.02 -0.31,1.96 -0.85,2.75c1.89,-1.15 3.29,-3.06 3.71,-5.3C20.52,10.97 18.79,7.62 16,6z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_front_light.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
tools:context=".FrontLightActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
50 changes: 37 additions & 13 deletions app/src/main/res/layout/front_light_adjust_layout.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="400dp"
android:layout_height="150dp"
android:orientation="vertical"
>
android:paddingHorizontal="10dp">

<SeekBar
android:id="@+id/seekbar_light_brightness"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="10"
android:progress="3" />
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_frontlight"
android:src="@drawable/ic_light_on"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
<SeekBar
android:id="@+id/seekbar_light_brightness"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center" />

<SeekBar
android:id="@+id/seekbar_light_temperature"

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:max="10"
android:progress="3" />
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_temperature"
android:src="@drawable/ic_warm"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
<SeekBar
android:id="@+id/seekbar_light_temperature"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"/>
</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<string name="rotate_upside_down">upside down</string>
<string name="back">Back</string>
<string name="home">Home</string>
<string name="front_light">Frontlight</string>
</resources>

0 comments on commit abc3c22

Please sign in to comment.