prefill the bottom bar with some default apps

This commit is contained in:
tibbi 2022-09-18 19:14:06 +02:00
parent 69ee50167b
commit e9c248b520
9 changed files with 150 additions and 25 deletions

View file

@ -1,24 +1,38 @@
package com.simplemobiletools.launcher.activities
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.provider.Telephony
import android.telecom.TelecomManager
import android.view.*
import android.view.animation.DecelerateInterpolator
import android.widget.PopupMenu
import android.widget.RelativeLayout
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.marginLeft
import androidx.core.view.marginTop
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.launcher.BuildConfig
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.config
import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB
import com.simplemobiletools.launcher.extensions.launchApp
import com.simplemobiletools.launcher.fragments.AllAppsFragment
import com.simplemobiletools.launcher.fragments.MyFragment
import com.simplemobiletools.launcher.fragments.WidgetsFragment
import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.interfaces.FlingListener
import com.simplemobiletools.launcher.models.HomeScreenGridItem
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : SimpleActivity(), FlingListener {
private val ANIMATION_DURATION = 150L
@ -51,6 +65,13 @@ class MainActivity : SimpleActivity(), FlingListener {
topMargin = statusBarHeight
bottomMargin = navigationBarHeight
}
if (!config.wasHomeScreenInit) {
ensureBackgroundThread {
getDefaultAppPackages()
config.wasHomeScreenInit = true
}
}
}
override fun onResume() {
@ -157,7 +178,7 @@ class MainActivity : SimpleActivity(), FlingListener {
fun homeScreenClicked(x: Float, y: Float) {
if (x >= home_screen_grid.left && x <= home_screen_grid.right && y >= home_screen_grid.top && y <= home_screen_grid.bottom) {
home_screen_grid.gridClicked(x, y) { packageName ->
home_screen_grid.gridClicked(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) { packageName ->
launchApp(packageName)
}
}
@ -196,4 +217,51 @@ class MainActivity : SimpleActivity(), FlingListener {
mIgnoreUpEvent = true
hideFragment(all_apps_fragment)
}
private fun getDefaultAppPackages() {
val homeScreenGridItems = ArrayList<HomeScreenGridItem>()
try {
val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 1, ROW_COUNT, defaultDialerPackage)
homeScreenGridItems.add(dialerIcon)
} catch (e: Exception) {
}
try {
val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this)
val SMSMessengerIcon = HomeScreenGridItem(null, 1, ROW_COUNT - 1, 2, ROW_COUNT, defaultSMSMessengerPackage)
homeScreenGridItems.add(SMSMessengerIcon)
} catch (e: Exception) {
}
try {
val browserIntent = Intent("android.intent.action.VIEW", Uri.parse("http://"))
val resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName
val browserIcon = HomeScreenGridItem(null, 2, ROW_COUNT - 1, 3, ROW_COUNT, defaultBrowserPackage)
homeScreenGridItems.add(browserIcon)
} catch (e: Exception) {
}
try {
val potentialStores = arrayListOf("com.android.vending", "org.fdroid.fdroid", "com.aurora.store")
val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) }
if (storePackage != null) {
val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 4, ROW_COUNT, storePackage)
homeScreenGridItems.add(storeIcon)
}
} catch (e: Exception) {
}
try {
val cameraIntent = Intent("android.media.action.IMAGE_CAPTURE")
val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY)
val defaultCameraPackage = resolveInfo!!.activityInfo.packageName
val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 5, ROW_COUNT, defaultCameraPackage)
homeScreenGridItems.add(cameraIcon)
} catch (e: Exception) {
}
homeScreenGridItemsDB.insertAll(homeScreenGridItems)
}
}

View file

@ -5,13 +5,17 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
import com.simplemobiletools.launcher.interfaces.HomeScreenGridItemsDao
import com.simplemobiletools.launcher.models.AppLauncher
import com.simplemobiletools.launcher.models.HomeScreenGridItem
@Database(entities = [AppLauncher::class], version = 1)
@Database(entities = [AppLauncher::class, HomeScreenGridItem::class], version = 1)
abstract class AppsDatabase : RoomDatabase() {
abstract fun AppLaunchersDao(): AppLaunchersDao
abstract fun HomeScreenGridItemsDao(): HomeScreenGridItemsDao
companion object {
private var db: AppsDatabase? = null

View file

@ -9,11 +9,14 @@ import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.databases.AppsDatabase
import com.simplemobiletools.launcher.helpers.Config
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
import com.simplemobiletools.launcher.interfaces.HomeScreenGridItemsDao
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.launchersDB: AppLaunchersDao get() = AppsDatabase.getInstance(applicationContext).AppLaunchersDao()
val Context.homeScreenGridItemsDB: HomeScreenGridItemsDao get() = AppsDatabase.getInstance(applicationContext).HomeScreenGridItemsDao()
fun Context.getColumnCount(): Int {
return if (portrait) {
resources.getInteger(R.integer.portrait_column_count)

View file

@ -108,7 +108,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
}
private fun gotLaunchers(appLaunchers: ArrayList<AppLauncher>) {
val sorted = appLaunchers.sortedBy { it.title.normalizeString().lowercase() }.toList() as ArrayList<AppLauncher>
val sorted = appLaunchers.sortedBy { it.title.normalizeString().lowercase() }.toMutableList() as ArrayList<AppLauncher>
setupAdapter(sorted)
}

View file

@ -7,4 +7,8 @@ class Config(context: Context) : BaseConfig(context) {
companion object {
fun newInstance(context: Context) = Config(context)
}
var wasHomeScreenInit: Boolean
get() = prefs.getBoolean(WAS_HOME_SCREEN_INIT, false)
set(wasHomeScreenInit) = prefs.edit().putBoolean(WAS_HOME_SCREEN_INIT, wasHomeScreenInit).apply()
}

View file

@ -2,3 +2,10 @@ package com.simplemobiletools.launcher.helpers
const val WIDGET_LIST_SECTION = 0
const val WIDGET_LIST_ITEMS_HOLDER = 1
// shared prefs
const val WAS_HOME_SCREEN_INIT = "was_home_screen_init"
// default home screen grid size
const val ROW_COUNT = 6
const val COLUMN_COUNT = 5

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.launcher.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.launcher.models.HomeScreenGridItem
@Dao
interface HomeScreenGridItemsDao {
@Query("SELECT * FROM home_screen_grid_items")
fun getAllItems(): List<HomeScreenGridItem>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(items: List<HomeScreenGridItem>)
}

View file

@ -1,3 +1,17 @@
package com.simplemobiletools.launcher.models
data class HomeScreenGridItem(var left: Int, val top: Int, val right: Int, val bottom: Int, val packageName: String)
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
// grid coords are from 0-5 by default. Icons occupy 1 slot only, widgets can be bigger
@Entity(tableName = "home_screen_grid_items", indices = [(Index(value = ["id"], unique = true))])
data class HomeScreenGridItem(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "left") var left: Int,
@ColumnInfo(name = "top") val top: Int,
@ColumnInfo(name = "right") val right: Int,
@ColumnInfo(name = "bottom") val bottom: Int,
@ColumnInfo(name = "package_name") val packageName: String
)

View file

@ -5,47 +5,58 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.telecom.TelecomManager
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.models.HomeScreenGridItem
class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
private val ROW_COUNT = 6
private val COLUMN_COUNT = 6
private var iconMargin = context.resources.getDimension(R.dimen.icon_side_margin).toInt()
private var textPaint: Paint
private var defaultDialerPackage = (context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage
private var dialerDrawable = context.getDrawableForPackageName(defaultDialerPackage)
// let's use a 5x5 grid for now with 1 special row at the bottom, prefilled with default apps
// let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps
private var rowXCoords = ArrayList<Int>(COLUMN_COUNT)
private var rowYCoords = ArrayList<Int>(ROW_COUNT)
private var rowWidth = 0
private var rowHeight = 0
private var iconSize = 0
private var gridItems = arrayListOf<HomeScreenGridItem>()
private var appIcons = ArrayList<HomeScreenGridItem>()
private var appIconDrawables = HashMap<String, Drawable>()
init {
textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
textSize = context.resources.getDimension(R.dimen.normal_text_size)
}
ensureBackgroundThread {
appIcons = context.homeScreenGridItemsDB.getAllItems() as ArrayList<HomeScreenGridItem>
appIcons.forEach { item ->
val drawable = context.getDrawableForPackageName(item.packageName)
if (drawable != null) {
appIconDrawables[item.packageName] = drawable
}
}
}
}
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (rowXCoords.isEmpty()) {
rowWidth = width / (COLUMN_COUNT - 1)
rowWidth = width / (COLUMN_COUNT)
rowHeight = height / ROW_COUNT
iconSize = rowWidth - 2 * iconMargin
for (i in 0 until COLUMN_COUNT - 1) {
for (i in 0 until COLUMN_COUNT) {
rowXCoords.add(i, i * rowWidth)
}
@ -54,23 +65,21 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
}
}
if (dialerDrawable != null) {
for (i in 0 until COLUMN_COUNT - 1) {
val drawableX = rowXCoords[i] + iconMargin
val drawableY = rowYCoords[COLUMN_COUNT - 1] + rowHeight - iconSize - iconMargin * 2
appIcons.forEach { icon ->
val drawable = appIconDrawables[icon.packageName]
if (drawable != null) {
val drawableX = rowXCoords[icon.left] + iconMargin
val drawableY = rowYCoords[icon.top] + rowHeight - iconSize - iconMargin * 2
dialerDrawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
dialerDrawable!!.draw(canvas)
val gridItem = HomeScreenGridItem(drawableX, drawableY, drawableX + rowWidth, drawableY + rowHeight, defaultDialerPackage)
gridItems.add(gridItem)
drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
drawable.draw(canvas)
}
}
}
fun gridClicked(x: Float, y: Float, callback: (packageName: String) -> Unit) {
for (gridItem in gridItems) {
if (x >= gridItem.left && x <= gridItem.right && y >= gridItem.top && y <= gridItem.bottom) {
for (gridItem in appIcons) {
if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) {
callback(gridItem.packageName)
break
}