adding an initial implementation of the home screen grid

This commit is contained in:
tibbi 2022-09-17 18:58:28 +02:00
parent 0b51fb87d3
commit 889e70468e
6 changed files with 103 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import android.os.Bundle
import android.view.*
import android.view.animation.DecelerateInterpolator
import android.widget.PopupMenu
import android.widget.RelativeLayout
import androidx.core.view.GestureDetectorCompat
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.launcher.BuildConfig
@ -42,6 +43,11 @@ class MainActivity : SimpleActivity(), FlingListener {
fragment.y = mScreenHeight.toFloat()
fragment.beVisible()
}
(home_screen_grid.layoutParams as RelativeLayout.LayoutParams).apply {
topMargin = statusBarHeight
bottomMargin = navigationBarHeight
}
}
override fun onResume() {

View File

@ -1,6 +1,9 @@
package com.simplemobiletools.launcher.extensions
import android.content.Context
import android.content.pm.LauncherApps
import android.graphics.drawable.Drawable
import android.os.Process
import com.simplemobiletools.commons.extensions.portrait
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.helpers.Config
@ -14,3 +17,25 @@ fun Context.getColumnCount(): Int {
resources.getInteger(R.integer.landscape_column_count)
}
}
fun Context.getDrawableForPackageName(packageName: String): Drawable? {
var drawable: Drawable? = null
try {
// try getting the properly colored launcher icons
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityList = launcher.getActivityList(packageName, Process.myUserHandle())[0]
drawable = activityList.getBadgedIcon(0)
} catch (e: Exception) {
} catch (e: Error) {
}
if (drawable == null) {
drawable = try {
packageManager.getApplicationIcon(packageName)
} catch (ignored: Exception) {
null
}
}
return drawable
}

View File

@ -3,11 +3,8 @@ package com.simplemobiletools.launcher.fragments
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Process
import android.provider.Settings
import android.util.AttributeSet
import android.view.*
@ -20,6 +17,7 @@ import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.adapters.LaunchersAdapter
import com.simplemobiletools.launcher.extensions.getColumnCount
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.interfaces.AllAppsListener
import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.all_apps_fragment.view.*
@ -82,24 +80,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val componentInfo = info.activityInfo.applicationInfo
val label = componentInfo.loadLabel(context.packageManager).toString()
val packageName = componentInfo.packageName
var drawable: Drawable? = null
try {
// try getting the properly colored launcher icons
val launcher = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityList = launcher.getActivityList(packageName, Process.myUserHandle())[0]
drawable = activityList.getBadgedIcon(0)
} catch (e: Exception) {
} catch (e: Error) {
}
if (drawable == null) {
drawable = try {
context.packageManager.getApplicationIcon(packageName)
} catch (ignored: Exception) {
continue
}
}
val drawable = context.getDrawableForPackageName(packageName) ?: continue
allPackageNames.add(packageName)
allApps.add(AppLauncher(0, label, packageName, 0, drawable))

View File

@ -0,0 +1,62 @@
package com.simplemobiletools.launcher.views
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.telecom.TelecomManager
import android.util.AttributeSet
import android.view.View
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
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
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
init {
textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
textSize = context.resources.getDimension(R.dimen.normal_text_size)
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (rowXCoords.isEmpty()) {
rowWidth = width / (COLUMN_COUNT - 1)
rowHeight = height / ROW_COUNT
iconSize = rowWidth - 2 * iconMargin
for (i in 0 until COLUMN_COUNT - 1) {
rowXCoords.add(i, i * rowWidth)
}
for (i in 0 until ROW_COUNT) {
rowYCoords.add(i, i * rowHeight)
}
}
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
dialerDrawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
dialerDrawable!!.draw(canvas)
}
}
}
}

View File

@ -4,6 +4,13 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.simplemobiletools.launcher.views.HomeScreenGrid
android:id="@+id/home_screen_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/icon_side_margin"
android:layout_marginEnd="@dimen/icon_side_margin" />
<include
android:id="@+id/all_apps_fragment"
layout="@layout/all_apps_fragment"

View File

@ -2,4 +2,5 @@
<dimen name="launcher_icon_size">55dp</dimen>
<dimen name="long_press_anchor_offset_y">70dp</dimen>
<dimen name="widget_preview_size">140dp</dimen>
<dimen name="icon_side_margin">10dp</dimen>
</resources>