Skip to content

Commit

Permalink
Polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
ganesh2shiv committed Apr 19, 2023
1 parent 93182f8 commit 4d7bf52
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 86 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/com/custom/app/data/PreferenceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package com.custom.app.data
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.custom.app.util.Constant.*
import com.custom.app.util.Constant.KEY_DEFAULT_DEVICE
import com.custom.app.util.Constant.KEY_DEVICE_NAME
import com.custom.app.util.Constant.KEY_DEVICE_STATUS
import com.custom.app.util.Constant.KEY_IGNORE_VOLUME
import com.custom.app.util.Constant.KEY_REBOOT_ENABLED
import com.custom.app.util.Constant.KEY_SERVICE_ENABLED
import com.custom.app.util.Constant.KEY_UUID
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/custom/app/service/AutoPauseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ class AutoPauseService : Service(), onConnectionListener, onReceiveListener, Aud
if (volume == 1 || (!settings.ignoreVolume() && volume == 0)) {
if (audioManager.isMusicActive) {
audioManager.requestAudioFocus(audioRequest)
AlertUtil.showToast(applicationContext, "Paused...")
AlertUtil.showToast(this, "Paused...")
}
} else {
if (!audioManager.isMusicActive) {
audioManager.abandonAudioFocus(null)
AlertUtil.showToast(applicationContext, "Playing...")
AlertUtil.showToast(this, "Playing...")
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/com/custom/app/ui/setting/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ import com.custom.app.data.DeviceModel
import com.custom.app.service.AutoPauseService
import com.custom.app.service.NotificationHandler
import com.custom.app.util.AlertUtil
import com.custom.app.util.Constant.*
import com.custom.app.util.Constant.KEY_DEFAULT_DEVICE
import com.custom.app.util.Constant.KEY_DEVICE_NAME
import com.custom.app.util.Constant.KEY_IGNORE_VOLUME
import com.custom.app.util.Constant.KEY_NOTIFICATION
import com.custom.app.util.Constant.KEY_PERMISSION
import com.custom.app.util.Constant.KEY_REBOOT_ENABLED
import com.custom.app.util.Constant.KEY_RESET
import com.custom.app.util.Constant.KEY_SERVICE_ENABLED
import com.custom.app.util.Constant.KEY_UUID
import com.custom.app.util.Constant.KEY_VERSION
import com.custom.app.util.Constant.NOTIFICATION_STATUS_CHANNEL
import com.custom.app.util.SpannyText
import com.custom.app.util.SpannyTypeface
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -80,7 +90,7 @@ class SettingFragment : PreferenceFragmentCompat(), SettingManager.ChangeListene
settings.deviceName(newValue.toString())
true
} else {
AlertUtil.showToast(context, "Invalid name!")
AlertUtil.showToast(requireContext(), "Invalid name!")
false
}
}
Expand All @@ -92,18 +102,18 @@ class SettingFragment : PreferenceFragmentCompat(), SettingManager.ChangeListene
settings.uuId(uuId)
true
} catch (e: Exception) {
AlertUtil.showToast(context, "Invalid format!")
AlertUtil.showToast(requireContext(), "Invalid format!")
false
}
}

resetPreference.setOnPreferenceClickListener {
AlertUtil.showActionAlertDialog(
context,
requireContext(),
getString(R.string.msg_reset_default_confirm),
getString(R.string.btn_cancel), getString(R.string.btn_yes)
) { _, _ ->
AlertUtil.showToast(context, "Reset to default!")
AlertUtil.showToast(requireContext(), "Reset to default!")
AutoPauseService.stop(requireContext())
settings.clear()
callback?.onRestartApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import com.custom.app.R
import com.custom.app.data.BooleanPreference
import com.custom.app.data.IntPreference
import com.custom.app.data.StringPreference
import com.custom.app.util.Constant.*
import com.custom.app.util.Constant.KEY_DEFAULT_DEVICE
import com.custom.app.util.Constant.KEY_DEVICE_NAME
import com.custom.app.util.Constant.KEY_DEVICE_STATUS
import com.custom.app.util.Constant.KEY_IGNORE_VOLUME
import com.custom.app.util.Constant.KEY_REBOOT_ENABLED
import com.custom.app.util.Constant.KEY_SERVICE_ENABLED
import com.custom.app.util.Constant.KEY_UUID
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
Expand Down
28 changes: 0 additions & 28 deletions app/src/main/java/com/custom/app/util/AlertUtil.java

This file was deleted.

26 changes: 26 additions & 0 deletions app/src/main/java/com/custom/app/util/AlertUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.custom.app.util

import android.content.Context
import android.content.DialogInterface
import android.widget.Toast
import androidx.appcompat.app.AlertDialog

object AlertUtil {

fun showToast(context: Context, msg: CharSequence?) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}

fun showActionAlertDialog(
context: Context, msg: CharSequence?,
negativeAction: CharSequence?, positiveAction: CharSequence?,
listener: DialogInterface.OnClickListener?
) {
val alert = AlertDialog.Builder(context)
alert.setMessage(msg)
alert.setCancelable(true)
alert.setPositiveButton(positiveAction, listener)
alert.setNegativeButton(negativeAction) { dialog, _: Int -> dialog.dismiss() }
alert.show()
}
}
20 changes: 0 additions & 20 deletions app/src/main/java/com/custom/app/util/Constant.java

This file was deleted.

20 changes: 20 additions & 0 deletions app/src/main/java/com/custom/app/util/Constant.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.custom.app.util

object Constant {

const val NOTIFICATION_STATUS_ID = 1
const val NOTIFICATION_STATUS_CHANNEL = "status"

const val KEY_UUID = "uuid"
const val KEY_DEVICE_NAME = "device_name"
const val KEY_DEVICE_STATUS = "device_status"
const val KEY_DEFAULT_DEVICE = "default_device"
const val KEY_SERVICE_ENABLED = "service_enabled"
const val KEY_REBOOT_ENABLED = "reboot_enabled"
const val KEY_PERMISSION = "permission"
const val KEY_NOTIFICATION = "notification"
const val KEY_IGNORE_VOLUME = "ignore_volume"
const val KEY_RESET = "reset"
const val KEY_VERSION = "version"

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/custom/app/util/SpannyText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ class SpannyTypeface : TypefaceSpan {
paint.typeface = tf
}
}
}
}
28 changes: 0 additions & 28 deletions app/src/main/java/com/custom/app/util/Util.java

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/java/com/custom/app/util/Util.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.custom.app.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
import androidx.core.content.ContextCompat

object Util {

fun isPermissionGranted(context: Context, permission: String): Boolean {
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
}

fun showAppSettings(activity: Activity, packageName: String) {
val intent = Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:$packageName"))
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
activity.startActivity(intent)
}
}

0 comments on commit 4d7bf52

Please sign in to comment.