Skip to content

Commit

Permalink
Add prompt to disable battery optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud0808 committed Aug 19, 2024
1 parent 456ef42 commit ec0c339
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<queries>
<intent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class OnboardingFragment : Fragment() {
adapter.addFragment(OnboardingItem1Fragment())
adapter.addFragment(OnboardingItem2Fragment())
adapter.addFragment(OnboardingItem3Fragment())
adapter.addFragment(OnboardingItem4Fragment())

binding.viewPager.adapter = adapter
binding.viewPager.offscreenPageLimit = adapter.itemCount
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.drdisagree.colorblendr.ui.fragments

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.ColorInt
import androidx.fragment.app.Fragment
import com.drdisagree.colorblendr.common.Const
import com.drdisagree.colorblendr.databinding.FragmentOnboardingItem3Binding

class OnboardingItem3Fragment : Fragment() {
Expand All @@ -19,16 +31,96 @@ class OnboardingItem3Fragment : Fragment() {
): View {
binding = FragmentOnboardingItem3Binding.inflate(inflater, container, false)

binding.root.setOnClickListener {
Const.WORKING_METHOD = Const.WorkMethod.ROOT
binding.shizuku.isSelected = false
}
binding.batteryOptimization.setOnClickListener {
binding.batteryOptimization.isSelected = false

binding.shizuku.setOnClickListener {
Const.WORKING_METHOD = Const.WorkMethod.SHIZUKU
binding.root.isSelected = false
if (!isBatteryOptimizationDisabled()) {
requestDisableBatteryOptimization()
}
}

updateUI()

return binding.getRoot()
}

override fun onResume() {
super.onResume()

updateUI()
}

private fun isBatteryOptimizationDisabled(): Boolean {
return (requireContext()
.getSystemService(Context.POWER_SERVICE) as PowerManager)
.isIgnoringBatteryOptimizations(requireContext().packageName)
}

@SuppressLint("BatteryLife")
private fun requestDisableBatteryOptimization() {
batteryOptimizationLauncher.launch(
Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:" + requireContext().packageName)
}
)
}

private val batteryOptimizationLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { _ ->
updateUI()
}

private fun updateUI() {
val batteryOptimizationDisabled = isBatteryOptimizationDisabled()
binding.batteryOptimization.isSelected = batteryOptimizationDisabled
binding.batteryView.setBatteryImageViewColor(batteryOptimizationDisabled)
}

private fun ImageView.setBatteryImageViewColor(selected: Boolean) {
val context = requireContext()
val typedValue = TypedValue()
val theme = context.theme
@ColorInt val backgroundColor: Int
@ColorInt val foregroundColor: Int
val isDarkMode = (context.resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_YES) == Configuration.UI_MODE_NIGHT_YES

if (selected) {
theme.resolveAttribute(
if (isDarkMode) {
com.google.android.material.R.attr.colorSurfaceContainerHigh
} else {
com.google.android.material.R.attr.colorSurfaceContainerHighest
},
typedValue,
true
)
backgroundColor = typedValue.data

theme.resolveAttribute(
com.google.android.material.R.attr.colorPrimary,
typedValue,
true
)
foregroundColor = typedValue.data
} else {
theme.resolveAttribute(
com.google.android.material.R.attr.colorErrorContainer,
typedValue,
true
)
backgroundColor = typedValue.data

theme.resolveAttribute(
com.google.android.material.R.attr.colorError,
typedValue,
true
)
foregroundColor = typedValue.data
}

setColorFilter(backgroundColor, PorterDuff.Mode.SRC_IN)
foreground.setTintList(ColorStateList.valueOf(foregroundColor))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.drdisagree.colorblendr.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.drdisagree.colorblendr.common.Const
import com.drdisagree.colorblendr.databinding.FragmentOnboardingItem4Binding

class OnboardingItem4Fragment : Fragment() {

private lateinit var binding: FragmentOnboardingItem4Binding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentOnboardingItem4Binding.inflate(inflater, container, false)

binding.root.setOnClickListener {
Const.WORKING_METHOD = Const.WorkMethod.ROOT
binding.shizuku.isSelected = false
}

binding.shizuku.setOnClickListener {
Const.WORKING_METHOD = Const.WorkMethod.SHIZUKU
binding.root.isSelected = false
}

return binding.getRoot()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.google.android.material.color.MaterialColors

class SelectableViewWidget : RelativeLayout {

private var context: Context? = null
private var container: MaterialCardView? = null
private var titleTextView: TextView? = null
private var descriptionTextView: TextView? = null
Expand All @@ -47,7 +46,6 @@ class SelectableViewWidget : RelativeLayout {
}

private fun init(context: Context, attrs: AttributeSet?) {
this.context = context
inflate(context, R.layout.view_widget_selectable, this)

initializeId()
Expand Down Expand Up @@ -108,7 +106,7 @@ class SelectableViewWidget : RelativeLayout {

if (enabled) {
val typedValue: TypedValue = TypedValue()
val a: TypedArray = getContext().obtainStyledAttributes(
val a: TypedArray = context.obtainStyledAttributes(
typedValue.data,
intArrayOf(com.google.android.material.R.attr.colorPrimary)
)
Expand Down Expand Up @@ -188,8 +186,8 @@ class SelectableViewWidget : RelativeLayout {
val isLandscape: Boolean = config.orientation == Configuration.ORIENTATION_LANDSCAPE

if (isLandscape) {
val screenWidth: Int = context!!.resources.displayMetrics.widthPixels
val screenHeight: Int = context!!.resources.displayMetrics.heightPixels
val screenWidth: Int = resources.displayMetrics.widthPixels
val screenHeight: Int = resources.displayMetrics.heightPixels

val isSmallHeightDevice: Boolean = screenWidth >= screenHeight * 1.8

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_battery_landscape_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24.0dp"
android:height="12.0dp"
android:viewportWidth="24.0"
android:viewportHeight="12.0">
<path
android:fillColor="?attr/colorErrorContainer"
android:fillType="nonZero"
android:pathData="M1.83,0.17L20.50,0.17A1.83 1.83 0 0 1 22.33,2.00L22.33,10.00A1.83 1.83 0 0 1 20.50,11.83L1.83,11.83A1.83 1.83 0 0 1 0.00,10.00L-0.00,2.00A1.83 1.83 0 0 1 1.83,0.17zM22.08,3.59L22.92,3.59A1.08 1.08 0 0 1 24.00,4.68L24.00,7.32A1.08 1.08 0 0 1 22.92,8.41L22.08,8.41A1.08 1.08 0 0 1 21.00,7.32L21.00,4.68A1.08 1.08 0 0 1 22.08,3.59z"
android:strokeLineCap="square"
android:strokeLineJoin="miter" />
<path
android:fillColor="@color/transparent"
android:fillType="nonZero"
android:pathData="M2.65,1.60L4.48,1.60A1.02 1.02 0 0 1 5.50,2.62L5.50,9.32A1.02 1.02 0 0 1 4.48,10.33L2.65,10.33A1.02 1.02 0 0 1 1.63,9.32L1.63,2.62A1.02 1.02 0 0 1 2.65,1.60zM6.57,2.65L6.57,9.35A1.02 1.02 0 0 0 7.58,10.37L9.42,10.37A1.02 1.02 0 0 0 10.43,9.35L10.43,2.65A1.02 1.02 0 0 0 9.42,1.63L7.58,1.63A1.02 1.02 0 0 0 6.57,2.65zM11.57,2.65L11.57,9.35A1.02 1.02 0 0 0 12.58,10.37L14.42,10.37A1.02 1.02 0 0 0 15.43,9.35L15.43,2.65A1.02 1.02 0 0 0 14.42,1.63L12.58,1.63A1.02 1.02 0 0 0 11.57,2.65z"
android:strokeLineCap="square"
android:strokeLineJoin="miter" />
</vector>
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_battery_landscape_fg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24.0dp"
android:height="12.0dp"
android:viewportWidth="24.0"
android:viewportHeight="12.0">
<path
android:fillColor="@color/transparent"
android:fillType="nonZero"
android:pathData="M1.83,0.17L20.50,0.17A1.83 1.83 0 0 1 22.33,2.00L22.33,10.00A1.83 1.83 0 0 1 20.50,11.83L1.83,11.83A1.83 1.83 0 0 1 0.00,10.00L-0.00,2.00A1.83 1.83 0 0 1 1.83,0.17zM22.08,3.59L22.92,3.59A1.08 1.08 0 0 1 24.00,4.68L24.00,7.32A1.08 1.08 0 0 1 22.92,8.41L22.08,8.41A1.08 1.08 0 0 1 21.00,7.32L21.00,4.68A1.08 1.08 0 0 1 22.08,3.59z"
android:strokeLineCap="square"
android:strokeLineJoin="miter" />
<path
android:fillColor="?attr/colorError"
android:fillType="nonZero"
android:pathData="M2.65,1.60L4.48,1.60A1.02 1.02 0 0 1 5.50,2.62L5.50,9.32A1.02 1.02 0 0 1 4.48,10.33L2.65,10.33A1.02 1.02 0 0 1 1.63,9.32L1.63,2.62A1.02 1.02 0 0 1 2.65,1.60zM6.57,2.65L6.57,9.35A1.02 1.02 0 0 0 7.58,10.37L9.42,10.37A1.02 1.02 0 0 0 10.43,9.35L10.43,2.65A1.02 1.02 0 0 0 9.42,1.63L7.58,1.63A1.02 1.02 0 0 0 6.57,2.65zM11.57,2.65L11.57,9.35A1.02 1.02 0 0 0 12.58,10.37L14.42,10.37A1.02 1.02 0 0 0 15.43,9.35L15.43,2.65A1.02 1.02 0 0 0 14.42,1.63L12.58,1.63A1.02 1.02 0 0 0 11.57,2.65z"
android:strokeLineCap="square"
android:strokeLineJoin="miter" />
</vector>
23 changes: 13 additions & 10 deletions app/src/main/res/layout-land/fragment_onboarding_item3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
style="@style/TextAppearance.Material3.HeadlineLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose_method"
android:text="@string/optimization"
app:layout_constraintBottom_toBottomOf="@id/guidelineBottom"
app:layout_constraintEnd_toEndOf="@id/guidelineMiddle"
app:layout_constraintStart_toStartOf="@id/guidelineLeft"
Expand Down Expand Up @@ -39,20 +39,23 @@
android:gravity="center"
android:orientation="vertical">

<com.drdisagree.colorblendr.ui.widgets.SelectableViewWidget
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<ImageView
android:id="@+id/battery_view"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
app:descriptionText="@string/mode_one_desc"
app:titleText="@string/mode_one_title" />
android:adjustViewBounds="true"
android:foreground="@drawable/ic_battery_landscape_fg"
android:importantForAccessibility="no"
android:src="@drawable/ic_battery_landscape_bg" />

<com.drdisagree.colorblendr.ui.widgets.SelectableViewWidget
android:id="@+id/shizuku"
android:id="@+id/battery_optimization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:descriptionText="@string/mode_two_desc"
app:titleText="@string/mode_two_title" />
android:layout_marginBottom="16dp"
app:descriptionText="@string/perm_four_desc"
app:titleText="@string/perm_four_title" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>
Expand Down
95 changes: 95 additions & 0 deletions app/src/main/res/layout-land/fragment_onboarding_item4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.OnboardingItem4Fragment">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView"
style="@style/TextAppearance.Material3.HeadlineLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose_method"
app:layout_constraintBottom_toBottomOf="@id/guidelineBottom"
app:layout_constraintEnd_toEndOf="@id/guidelineMiddle"
app:layout_constraintStart_toStartOf="@id/guidelineLeft"
app:layout_constraintTop_toTopOf="@id/guidelineTop" />

<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fadingEdgeLength="16dp"
android:fillViewport="true"
android:gravity="center"
android:requiresFadingEdge="vertical"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="@id/guidelineBottom"
app:layout_constraintEnd_toEndOf="@id/guidelineRight"
app:layout_constraintStart_toStartOf="@id/guidelineMiddle"
app:layout_constraintTop_toTopOf="@id/guidelineTop">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="14dp"
android:gravity="center"
android:orientation="vertical">

<com.drdisagree.colorblendr.ui.widgets.SelectableViewWidget
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:descriptionText="@string/mode_one_desc"
app:titleText="@string/mode_one_title" />

<com.drdisagree.colorblendr.ui.widgets.SelectableViewWidget
android:id="@+id/shizuku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:descriptionText="@string/mode_two_desc"
app:titleText="@string/mode_two_title" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.03" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.16" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.88" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit ec0c339

Please sign in to comment.