Skip to content

Commit

Permalink
Build 178
Browse files Browse the repository at this point in the history
  - Added notification permission
  • Loading branch information
Hamza417 committed Nov 22, 2023
1 parent fde07c2 commit 1617728
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-feature
android:glEsVersion="0x00020000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,93 @@ package app.simple.positional.activities.service
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import app.simple.positional.R
import app.simple.positional.decorations.ripple.DynamicRippleButton
import app.simple.positional.extensions.activity.BaseActivity
import app.simple.positional.util.PermissionUtils.hasNotificationPermission
import app.simple.positional.util.PermissionUtils.isIgnoringBatteryOptimizations


class BatteryOptimizationActivity : BaseActivity() {

private lateinit var enable: DynamicRippleButton
private lateinit var grantBattery: DynamicRippleButton
private lateinit var grantNotification: DynamicRippleButton

private var appWidgetCode = 0

private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
setNotificationButton()
} else {
Toast.makeText(baseContext, R.string.notification_permission_desc, Toast.LENGTH_LONG).show()
}
}

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

enable = findViewById(R.id.grant)
grantBattery = findViewById(R.id.grant_battery_optimization)
grantNotification = findViewById(R.id.grant_notification_access)

appWidgetCode = intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
?: AppWidgetManager.INVALID_APPWIDGET_ID

if (isIgnoringBatteryOptimizations()) {
if (isIgnoringBatteryOptimizations() && hasNotificationPermission()) {
close()
}

enable.setOnClickListener {
grantBattery.setOnClickListener {
if (isIgnoringBatteryOptimizations()) {
close()
if (hasNotificationPermission()) {
close()
} else {
Toast.makeText(baseContext, R.string.notification_permission_desc, Toast.LENGTH_LONG).show()
}
} else {
startActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS))
}
}

grantNotification.setOnClickListener {
if (hasNotificationPermission()) {
if (isIgnoringBatteryOptimizations()) {
close()
} else {
Toast.makeText(baseContext, R.string.battery_optimization_desc, Toast.LENGTH_LONG).show()
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
}
}

override fun onResume() {
super.onResume()
setBatteryOptimizationButton()
setNotificationButton()
}

private fun setBatteryOptimizationButton() {
if (isIgnoringBatteryOptimizations()) {
enable.setText(R.string.button_close)
grantBattery.setText(R.string.button_close)
} else {
grantBattery.setText(R.string.button_grant)
}
}

private fun setNotificationButton() {
if (hasNotificationPermission()) {
grantNotification.setText(R.string.button_close)
} else {
enable.setText(R.string.button_grant)
grantNotification.setText(R.string.button_grant)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ abstract class ClockWidgetService : Service(), OnSharedPreferenceChangeListener
// Leave the service running in the foreground
// because Android 14 is killing the service when
// the screen is off
// stopForeground(STOP_FOREGROUND_REMOVE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Log.d("ClockWidgetService", "Screen is off, we're on Android 14+")
} else {
stopForeground(STOP_FOREGROUND_REMOVE)
}
removeCallbacks()
Log.d("ClockWidgetService", "Screen is off")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.simple.positional.util

import android.Manifest
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.PermissionInfo
Expand Down Expand Up @@ -41,6 +42,11 @@ object PermissionUtils {
return powerManager.isIgnoringBatteryOptimizations(packageName)
}

fun Context.hasNotificationPermission(): Boolean {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
return notificationManager.areNotificationsEnabled()
}

@Nullable
fun String.getPermissionInfo(context: Context): PermissionInfo? {
try {
Expand Down
113 changes: 104 additions & 9 deletions app/src/main/res/layout/activity_battery_optimization.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,131 @@
<?xml version="1.0" encoding="utf-8"?>
<app.simple.positional.decorations.padding.PaddingAwareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<app.simple.positional.decorations.padding.PaddingAwareLinearLayout 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:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/toolbar_padding">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:layout_weight="1">

<ImageView
android:layout_width="@dimen/clock_image_shadow_size"
android:layout_height="@dimen/clock_image_shadow_size"
android:layout_gravity="center"
android:src="@drawable/ic_clock_shadow"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/sweep_seconds"
android:layout_width="@dimen/clock_image_dimensions"
android:layout_height="@dimen/clock_image_dimensions"
android:layout_gravity="center"
android:padding="5dp"
android:rotation="-90"
android:src="@drawable/clock_trail"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/clock_face"
android:layout_width="@dimen/clock_image_dimensions"
android:layout_height="@dimen/clock_image_dimensions"
android:layout_gravity="center"
android:src="@drawable/clock_face"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/day_night_indicator"
android:layout_width="@dimen/clock_day_night_indicator"
android:layout_height="@dimen/clock_day_night_indicator"
android:layout_gravity="center"
android:layout_marginBottom="60dp"
android:src="@drawable/ic_night"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/hour"
android:layout_width="@dimen/clock_needle_dimensions"
android:layout_height="@dimen/clock_needle_dimensions"
android:layout_gravity="center"
android:rotation="70"
android:src="@drawable/clock_needle_minimal_hour"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/minutes"
android:layout_width="@dimen/clock_needle_dimensions"
android:layout_height="@dimen/clock_needle_dimensions"
android:layout_gravity="center"
android:rotation="235"
android:src="@drawable/clock_needle_minimal_minute"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/seconds"
android:layout_width="wrap_content"
android:layout_height="@dimen/clock_needle_dimensions"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:rotation="120"
android:src="@drawable/clock_needle_minimal_second"
tools:ignore="ContentDescription" />

</FrameLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
android:orientation="vertical"
android:padding="@dimen/toolbar_padding">

<TextView
android:id="@+id/adapter_address_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="75dp"
android:fontFamily="@font/regular"
android:gravity="center"
android:gravity="start"
android:text="@string/battery_optimization_desc"
android:textColor="@color/textTertiary"
android:textSize="@dimen/settings_primary_text_size" />

<app.simple.positional.decorations.ripple.DynamicRippleButton
android:id="@+id/grant"
android:id="@+id/grant_battery_optimization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="16dp"
android:fontFamily="@font/medium"
android:text="@string/button_grant"
android:textAllCaps="false" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:fontFamily="@font/regular"
android:gravity="start"
android:text="@string/notification_permission_desc"
android:textColor="@color/textTertiary"
android:textSize="@dimen/settings_primary_text_size" />

<app.simple.positional.decorations.ripple.DynamicRippleButton
android:id="@+id/grant_notification_access"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_gravity="end"
android:layout_marginTop="16dp"
android:fontFamily="@font/medium"
android:text="@string/button_grant"
android:textAllCaps="false" />

</LinearLayout>

</app.simple.positional.decorations.padding.PaddingAwareFrameLayout>
</app.simple.positional.decorations.padding.PaddingAwareLinearLayout>
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 @@ -333,4 +333,5 @@
<string name="share">Share</string>
<string name="telegram_group">Telegram Group</string>
<string name="buy_full_desc">Buy full version and unlock the full potential of the app.</string>
<string name="notification_permission_desc">Notification access is required to persistently run the clock widget service.</string>
</resources>

0 comments on commit 1617728

Please sign in to comment.