Wednesday, May 9, 2018

android kotlin - Screen brightness programmatically

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.content.Context
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.provider.Settings
import android.widget.SeekBar
import android.widget.Toast
import android.content.Intent
import android.net.Uri


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Set the SeekBar initial progress from screen current brightness
        val brightness = brightness
        seek_bar.progress = brightness

        text_view.text = "Screen Brightness : $brightness"

        // If app has no permission to write system settings
        if(!canWrite){
            seek_bar.isEnabled = false
            allowWritePermission()
        }

        // Set a SeekBar change listener
        seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                // Display the current progress of SeekBar
                text_view.text = "Screen Brightness : $i"

                // Change the screen brightness
                if (canWrite){
                    setBrightness(i)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {

            }
        })
    }
}



// Extension property to get write system settings permission status
val Context.canWrite:Boolean
get() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return Settings.System.canWrite(this)
    }else{
        return true
    }
}


// Extension function to allow write system settings
fun Context.allowWritePermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val intent = Intent(
                    Settings.ACTION_MANAGE_WRITE_SETTINGS,
                    Uri.parse("package:$packageName")
            )
            startActivity(intent)
    }
}


// Extension property to get screen brightness programmatically
val Context.brightness:Int
get() {
    return Settings.System.getInt(
            this.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS,
            0
    )
}


// Extension method to set screen brightness programmatically
fun Context.setBrightness(value:Int):Unit{
    Settings.System.putInt(
            this.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS,
            value
    )
}


// Extension function to show toast message quickly
fun Context.toast(message:String){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show()
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_layout"
    android:background="#e7faef"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginBottom="25sp"
        />
    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:padding="15dp"
        />
</LinearLayout>
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>