Skip to content

Commit

Permalink
Refactor: Moved functions into there correct folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed May 26, 2024
1 parent 11cddd3 commit e45a918
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import android.graphics.drawable.AdaptiveIconDrawable
import android.net.Uri
import android.os.Build
import android.os.UserHandle
import android.provider.AlarmClock
import android.provider.CalendarContract
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
Expand All @@ -27,11 +32,29 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
import java.util.Calendar
import java.util.Date
import kotlin.math.pow
import kotlin.math.sqrt

fun Context.isTabletConfig(): Boolean =
resources.configuration.smallestScreenWidthDp >= SMALLEST_WIDTH_600

fun Context.isTablet(): Boolean {
val windowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(metrics)
val widthInches = metrics.widthPixels / metrics.xdpi
val heightInches = metrics.heightPixels / metrics.ydpi
val diagonalInches =
sqrt(widthInches.toDouble().pow(2.0) + heightInches.toDouble().pow(2.0))
if (diagonalInches >= 7.0) return true
return false
}

fun Context.isPortraitSw600Config(): Boolean =
resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&
resources.configuration.smallestScreenWidthDp >= SMALLEST_WIDTH_600
Expand Down Expand Up @@ -173,6 +196,74 @@ private fun Context.notRunningStockAndroid() {
}
}

fun Context.searchView() {
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, "")
this.startActivity(intent)
}

fun Context.unInstallApp(appInfo: AppInfo) {
val intent = Intent(Intent.ACTION_DELETE)
intent.data = Uri.parse("package:${appInfo.packageName}")
this.startActivity(intent)
}

fun Context.appInfo(appInfo: AppInfo) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.fromParts("package", appInfo.packageName, null)
this.startActivity(intent)
}

fun Context.launchApp(appInfo: AppInfo) {
val intent = this.packageManager.getLaunchIntentForPackage(appInfo.packageName)
if (intent != null) {
this.startActivity(intent)
} else {
showLongToast("Failed to open the application")
}
}

fun Context.launchClock() {
try {
val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
this.startActivity(intent)
} catch (e: Exception) {
Log.e("launchClock", "Error launching clock app: ${e.message}")
}
}

fun Context.launchCalendar() {
try {
val cal: Calendar = Calendar.getInstance()
cal.time = Date()
val time = cal.time.time
val builder: Uri.Builder = CalendarContract.CONTENT_URI.buildUpon()
builder.appendPath("time")
builder.appendPath(time.toString())
this.startActivity(Intent(Intent.ACTION_VIEW, builder.build()))
} catch (e: Exception) {
try {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
this.startActivity(intent)
} catch (e: Exception) {
Log.d("openCalendar", e.toString())
}
}
}

fun Context.openBatteryManager() {
try {
val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
this.startActivity(intent)
} catch (e: ActivityNotFoundException) {
// Battery manager settings cannot be opened
// Handle this case as needed
showLongToast("Battery manager settings are not available on this device.")
}
}

fun Context.isPackageInstalled(
packageName: String,
userHandle: UserHandle = android.os.Process.myUserHandle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.github.droidworksstudio.launcher.helper

import android.annotation.SuppressLint
import android.app.SearchManager
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.net.Uri
Expand All @@ -22,15 +20,13 @@ import android.view.Window
import android.view.WindowInsets
import android.view.WindowManager
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.LinearLayoutCompat
import com.github.droidworksstudio.ktx.showLongToast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.github.droidworksstudio.launcher.Constants
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.accessibility.ActionService
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
import java.util.Calendar
import java.util.Date
import javax.inject.Inject
Expand Down Expand Up @@ -68,12 +64,6 @@ class AppHelper @Inject constructor() {
}
}

fun searchView(context: Context) {
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, "")
context.startActivity(intent)
}

fun dayNightMod(context: Context, view: View) {
when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> {
Expand Down Expand Up @@ -109,44 +99,6 @@ class AppHelper @Inject constructor() {
}
}

fun launchApp(context: Context, appInfo: AppInfo) {
val intent = context.packageManager.getLaunchIntentForPackage(appInfo.packageName)
if (intent != null) {
context.startActivity(intent)
} else {
context.showLongToast("Failed to open the application")
}
}

fun launchClock(context: Context) {
try {
val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
context.startActivity(intent)
} catch (e: Exception) {
Log.e("launchClock", "Error launching clock app: ${e.message}")
}
}

fun launchCalendar(context: Context) {
try {
val cal: Calendar = Calendar.getInstance()
cal.time = Date()
val time = cal.time.time
val builder: Uri.Builder = CalendarContract.CONTENT_URI.buildUpon()
builder.appendPath("time")
builder.appendPath(time.toString())
context.startActivity(Intent(Intent.ACTION_VIEW, builder.build()))
} catch (e: Exception) {
try {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
context.startActivity(intent)
} catch (e: Exception) {
Log.d("openCalendar", e.toString())
}
}
}

fun openDigitalWellbeing(context: Context) {
try {
val packageName = "com.google.android.apps.wellbeing"
Expand All @@ -163,30 +115,6 @@ class AppHelper @Inject constructor() {
}
}

fun openBatteryManager(context: Context) {
try {
val intent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
// Battery manager settings cannot be opened
// Handle this case as needed
context.showLongToast("Battery manager settings are not available on this device.")
}
}

fun unInstallApp(context: Context, appInfo: AppInfo) {
val intent = Intent(Intent.ACTION_DELETE)
intent.data = Uri.parse("package:${appInfo.packageName}")
context.startActivity(intent)
}

fun appInfo(context: Context, appInfo: AppInfo) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.fromParts("package", appInfo.packageName, null)
context.startActivity(intent)
}

fun gravityToString(gravity: Int): String? {
return when (gravity) {
Gravity.CENTER -> "CENTER"
Expand Down Expand Up @@ -230,19 +158,6 @@ class AppHelper @Inject constructor() {
}
}

fun isTablet(context: Context): Boolean {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(metrics)
val widthInches = metrics.widthPixels / metrics.xdpi
val heightInches = metrics.heightPixels / metrics.ydpi
val diagonalInches =
sqrt(widthInches.toDouble().pow(2.0) + heightInches.toDouble().pow(2.0))
if (diagonalInches >= 7.0) return true
return false
}

fun wordOfTheDay(resources: Resources): String {
val dailyWordsArray =
resources.getStringArray(R.array.settings_appearance_daily_word_default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import com.github.droidworksstudio.ktx.isTablet
import com.github.droidworksstudio.launcher.Constants
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.databinding.ActivityMainBinding
Expand Down Expand Up @@ -94,7 +95,7 @@ class MainActivity : AppCompatActivity() {

@SuppressLint("SourceLockedOrientationActivity")
private fun setupOrientation() {
if (appHelper.isTablet(this)) return
if (this.isTablet()) return
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.github.droidworksstudio.ktx.appInfo
import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.ktx.unInstallApp
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.data.entities.AppInfo
Expand Down Expand Up @@ -167,12 +169,12 @@ class AppInfoBottomSheetFragment(private val appInfo: AppInfo) : BottomSheetDial

binding.bottomSheetUninstall.setOnClickListener {
appStateClickListener?.onAppStateClicked(appInfo)
appHelper.unInstallApp(requireContext(), appInfo)
requireContext().unInstallApp(appInfo)
dismiss()
}

binding.bottomSheetInfo.setOnClickListener {
appHelper.appInfo(requireContext(), appInfo)
requireContext().appInfo(appInfo)
dismiss()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.droidworksstudio.ktx.hideKeyboard
import com.github.droidworksstudio.ktx.launchApp
import com.github.droidworksstudio.ktx.openSearch
import com.github.droidworksstudio.ktx.showKeyboard
import com.github.droidworksstudio.ktx.showLongToast
Expand Down Expand Up @@ -237,15 +238,15 @@ class DrawFragment : Fragment(),

private fun observeBioAuthCheck(appInfo: AppInfo) {
if (!appInfo.lock) {
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
} else {
fingerHelper.startFingerprintAuth(appInfo, this)
}
}

override fun onAuthenticationSucceeded(appInfo: AppInfo) {
context.showLongToast(getString(R.string.authentication_succeeded))
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
}

override fun onAuthenticationFailed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.droidworksstudio.ktx.launchApp
import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.data.entities.AppInfo
Expand Down Expand Up @@ -199,7 +200,7 @@ class FavoriteFragment : Fragment(),

private fun observeBioAuthCheck(appInfo: AppInfo) {
if (!appInfo.lock) {
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
} else {
fingerHelper.startFingerprintAuth(appInfo, this)
}
Expand All @@ -215,7 +216,7 @@ class FavoriteFragment : Fragment(),

override fun onAuthenticationSucceeded(appInfo: AppInfo) {
context.showLongToast(getString(R.string.authentication_succeeded))
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
}

override fun onAuthenticationFailed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.droidworksstudio.ktx.launchApp
import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.data.entities.AppInfo
Expand Down Expand Up @@ -112,7 +113,7 @@ class HiddenFragment : Fragment(),

private fun observeBioAuthCheck(appInfo: AppInfo) {
if (!appInfo.lock) {
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
} else {
fingerHelper.startFingerprintAuth(appInfo, this)
}
Expand Down Expand Up @@ -156,7 +157,7 @@ class HiddenFragment : Fragment(),

override fun onAuthenticationSucceeded(appInfo: AppInfo) {
context.showLongToast(getString(R.string.authentication_succeeded))
appHelper.launchApp(context, appInfo)
context.launchApp(appInfo)
}

override fun onAuthenticationFailed() {
Expand Down
Loading

0 comments on commit e45a918

Please sign in to comment.