diff --git a/app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt b/app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt index 9953a420..f3d4b3c7 100644 --- a/app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt +++ b/app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt @@ -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 @@ -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 @@ -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() diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt index cb41d195..98e8f8d2 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt @@ -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 @@ -22,7 +20,6 @@ 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 @@ -30,7 +27,6 @@ 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 @@ -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 -> { @@ -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" @@ -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" @@ -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) diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/activities/MainActivity.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/activities/MainActivity.kt index c042d94b..b45227d5 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/activities/MainActivity.kt @@ -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 @@ -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 } diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/bottomsheetdialog/AppInfoBottomSheetFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/bottomsheetdialog/AppInfoBottomSheetFragment.kt index 220aa833..bcc4686b 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/bottomsheetdialog/AppInfoBottomSheetFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/bottomsheetdialog/AppInfoBottomSheetFragment.kt @@ -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 @@ -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() } } diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt index 1ad340ba..647058e5 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt @@ -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 @@ -237,7 +238,7 @@ class DrawFragment : Fragment(), private fun observeBioAuthCheck(appInfo: AppInfo) { if (!appInfo.lock) { - appHelper.launchApp(context, appInfo) + context.launchApp(appInfo) } else { fingerHelper.startFingerprintAuth(appInfo, this) } @@ -245,7 +246,7 @@ class DrawFragment : Fragment(), override fun onAuthenticationSucceeded(appInfo: AppInfo) { context.showLongToast(getString(R.string.authentication_succeeded)) - appHelper.launchApp(context, appInfo) + context.launchApp(appInfo) } override fun onAuthenticationFailed() { diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/favorite/FavoriteFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/favorite/FavoriteFragment.kt index fbc90942..5b872968 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/favorite/FavoriteFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/favorite/FavoriteFragment.kt @@ -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 @@ -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) } @@ -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() { diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/hidden/HiddenFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/hidden/HiddenFragment.kt index 708a3712..b4308963 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/hidden/HiddenFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/hidden/HiddenFragment.kt @@ -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 @@ -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) } @@ -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() { diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt index 888ccc6d..0c5e4976 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt @@ -20,6 +20,11 @@ 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.launchCalendar +import com.github.droidworksstudio.ktx.launchClock +import com.github.droidworksstudio.ktx.openBatteryManager +import com.github.droidworksstudio.ktx.searchView import com.github.droidworksstudio.ktx.showLongToast import com.github.droidworksstudio.launcher.R import com.github.droidworksstudio.launcher.accessibility.ActionService @@ -160,9 +165,9 @@ class HomeFragment : Fragment(), binding.touchArea.setOnTouchListener(getSwipeGestureListener(context)) binding.nestScrollView.setOnTouchListener(getSwipeGestureListener(context)) - binding.clock.setOnClickListener { appHelper.launchClock(context) } - binding.date.setOnClickListener { appHelper.launchCalendar(context) } - binding.battery.setOnClickListener { appHelper.openBatteryManager(context) } + binding.clock.setOnClickListener { context.launchClock() } + binding.date.setOnClickListener { context.launchCalendar() } + binding.battery.setOnClickListener { context.openBatteryManager() } } private fun observeUserInterfaceSettings() { @@ -229,10 +234,7 @@ class HomeFragment : Fragment(), private fun observeBioAuthCheck(appInfo: AppInfo) { if (!appInfo.lock) - appHelper.launchApp( - context, - appInfo - ) + context.launchApp(appInfo) else fingerHelper.startFingerprintAuth(appInfo, this) } @@ -281,7 +283,7 @@ class HomeFragment : Fragment(), override fun onSwipeUp() { super.onSwipeUp() - if (preferenceHelper.swipeSearch) appHelper.searchView(context) + if (preferenceHelper.swipeSearch) context.searchView() } } } @@ -359,7 +361,7 @@ class HomeFragment : Fragment(), } override fun onAuthenticationSucceeded(appInfo: AppInfo) { - appHelper.launchApp(context, appInfo) + context.launchApp(appInfo) requireContext().showLongToast(getString(R.string.authentication_succeeded)) }