Skip to content

Commit a3c4e6f

Browse files
Feat: Basic starter to add Widget support.
1 parent 364fe2b commit a3c4e6f

File tree

16 files changed

+382
-10
lines changed

16 files changed

+382
-10
lines changed

.vscode/easycode.ignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
node_modules/
2+
dist/
3+
vendor/
4+
cache/
5+
.*/
6+
*.min.*
7+
*.test.*
8+
*.spec.*
9+
*.bundle.*
10+
*.bundle-min.*
11+
*.*.js
12+
*.*.ts
13+
*.log

LICENSE_ASTER

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
652652
If the program does terminal interaction, make it output a short
653653
notice like this when it starts in an interactive mode:
654654

655-
<program> Copyright (C) <year> <name of author>
655+
AsterLauncher Copyright (C) 2024 neophtex
656656
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657657
This is free software, and you are welcome to redistribute it
658658
under certain conditions; type `show c' for details.

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ android {
5151
}
5252
buildFeatures {
5353
viewBinding = true
54+
dataBinding = true
5455
}
5556

5657
applicationVariants.all {

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
tools:ignore="QueryAllPackagesPermission" />
1414
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
1515
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
16+
<uses-permission android:name="android.permission.BIND_APPWIDGET"
17+
tools:ignore="ProtectedPermissions" />
18+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
19+
1620

1721
<queries>
1822
<intent>

app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ object Constants {
44
const val PACKAGE_NAME = "app.easy.launcher"
55
const val PACKAGE_NAME_DEBUG = "$PACKAGE_NAME.debug"
66

7+
const val WIDGETS_PREFS = "EasyLauncherWidgets.pref"
8+
const val APP_WIDGETS_ID = "APP_WIDGETS_ID"
9+
710
const val PREFS_FILENAME = "EasyLauncher.pref"
811
const val FIRST_LAUNCH = "FIRST_LAUNCH"
912
const val SHOW_DATE = "SHOW_DATE"
@@ -39,8 +42,6 @@ object Constants {
3942
const val HOME_TIME_ALIGNMENT = "HOME_TIME_ALIGNMENT"
4043
const val HOME_APP_ALIGNMENT = "HOME_APP_ALIGNMENT"
4144
const val HOME_DAILY_WORD_ALIGNMENT = "HOME_DAILY_WORD_ALIGNMENT"
42-
const val HOME_DRAW_ALIGNMENT = "HOME_DRAW_ALIGNMENT"
43-
const val HOME_BATTERY_ALIGNMENT = "HOME_BATTERY_ALIGNMENT"
4445

4546
const val NOTIFICATION_SERVICE = "statusbar"
4647
const val NOTIFICATION_MANAGER = "android.app.StatusBarManager"
@@ -61,5 +62,6 @@ object Constants {
6162
const val URL_GOOGLE_PLAY_STORE = "https://play.google.com/store/search?c=apps&q"
6263
const val APP_GOOGLE_PLAY_STORE = "market://search?c=apps&q"
6364

65+
const val APP_WIDGET_HOST_ID = 1024
6466
const val REQUEST_CODE_ENABLE_ADMIN = 123
6567
}

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ class AppHelper @Inject constructor() {
101101
fun dayNightMod(context: Context, view: View) {
102102
when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
103103
Configuration.UI_MODE_NIGHT_YES -> {
104-
view.setBackgroundColor(context.resources.getColor(R.color.blackTrans50, context.theme))
104+
view.setBackgroundColor(context.resources.getColor(R.color.blackTrans25, context.theme))
105105
}
106106

107107
Configuration.UI_MODE_NIGHT_NO -> {
108-
view.setBackgroundColor(context.resources.getColor(R.color.whiteTrans50, context.theme))
108+
view.setBackgroundColor(context.resources.getColor(R.color.whiteTrans25, context.theme))
109109
}
110110
}
111111
}

app/src/main/java/com/github/droidworksstudio/launcher/ui/activities/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MainActivity : AppCompatActivity() {
7878
}
7979

8080
private fun observeUI() {
81-
binding.pager.currentItem = 0
81+
binding.pager.currentItem = 1
8282
preferenceViewModel.setShowStatusBar(preferenceHelper.showStatusBar)
8383
preferenceViewModel.showStatusBarLiveData.observe(this) {
8484
if (it) appHelper.showStatusBar(this.window)

app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
4040
OnItemClickedListener.OnAppLongClickedListener,
4141
OnItemClickedListener.BottomSheetDismissListener,
4242
OnItemClickedListener.OnAppStateClickListener,
43-
FingerprintHelper.Callback{
43+
FingerprintHelper.Callback {
4444
private var _binding: FragmentDrawBinding? = null
4545

4646
private val binding get() = _binding!!
@@ -181,14 +181,12 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
181181
override fun onResume() {
182182
super.onResume()
183183
observeDrawerApps()
184-
binding.drawAdapter.scrollToPosition(0)
185184
if (preferenceHelper.automaticKeyboard) binding.searchViewText.showKeyboard()
186185
}
187186

188187
override fun onStart() {
189188
super.onStart()
190189
observeDrawerApps()
191-
binding.drawAdapter.scrollToPosition(0)
192190
}
193191

194192
override fun onStop() {

app/src/main/java/com/github/droidworksstudio/launcher/ui/viewpager/ViewPagerAdapter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import androidx.lifecycle.Lifecycle
66
import androidx.viewpager2.adapter.FragmentStateAdapter
77
import com.github.droidworksstudio.launcher.ui.drawer.DrawFragment
88
import com.github.droidworksstudio.launcher.ui.home.HomeFragment
9+
import com.github.droidworksstudio.launcher.ui.widgetmanager.WidgetManagerFragment
910

1011

1112
class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
1213
FragmentStateAdapter(fragmentManager, lifecycle) {
1314

1415
private val fragments: ArrayList<Fragment> = arrayListOf(
16+
WidgetManagerFragment(),
1517
HomeFragment(),
1618
DrawFragment(),
1719
)
@@ -23,5 +25,4 @@ class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
2325
override fun createFragment(position: Int): Fragment {
2426
return fragments[position]
2527
}
26-
2728
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.droidworksstudio.launcher.ui.widgetmanager
2+
3+
import android.appwidget.AppWidgetHost
4+
import android.appwidget.AppWidgetManager
5+
import android.content.Context
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import android.widget.FrameLayout
10+
import androidx.recyclerview.widget.RecyclerView
11+
import com.github.droidworksstudio.launcher.R
12+
13+
class WidgetAdapter(
14+
private val context: Context,
15+
private val appWidgetHost: AppWidgetHost,
16+
private val onWidgetClick: (Int) -> Unit
17+
) : RecyclerView.Adapter<WidgetAdapter.WidgetViewHolder>() {
18+
19+
private val widgetItems = mutableListOf<WidgetItem>()
20+
21+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WidgetViewHolder {
22+
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_widget, parent, false)
23+
return WidgetViewHolder(itemView)
24+
}
25+
26+
override fun onBindViewHolder(holder: WidgetViewHolder, position: Int) {
27+
val widgetItem = widgetItems[position]
28+
holder.bind(widgetItem)
29+
}
30+
31+
override fun getItemCount(): Int = widgetItems.size
32+
33+
fun addWidget(widgetItem: WidgetItem) {
34+
widgetItems.add(widgetItem)
35+
notifyItemInserted(widgetItems.size - 1)
36+
}
37+
38+
fun removeWidget(appWidgetId: Int) {
39+
val position = widgetItems.indexOfFirst { it.appWidgetId == appWidgetId }
40+
if (position != -1) {
41+
widgetItems.removeAt(position)
42+
notifyItemRemoved(position)
43+
}
44+
}
45+
46+
fun getWidgetIds(): List<Int> = widgetItems.map { it.appWidgetId }
47+
48+
inner class WidgetViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
49+
fun bind(widgetItem: WidgetItem) {
50+
val hostView = appWidgetHost.createView(context, widgetItem.appWidgetId, widgetItem.appWidgetInfo)
51+
itemView.findViewById<FrameLayout>(R.id.widget_frame).addView(hostView)
52+
53+
itemView.setOnClickListener {
54+
onWidgetClick(widgetItem.appWidgetId)
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)