Skip to content

Commit

Permalink
feat: some system apps are added to open
Browse files Browse the repository at this point in the history
  • Loading branch information
diyews committed Mar 31, 2022
1 parent d2ad58d commit 7fe44e4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
110 changes: 66 additions & 44 deletions app/src/main/java/ws/diye/statusbarplus/AllInstalledAppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import android.content.pm.PackageInfo
import android.content.pm.ApplicationInfo
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import android.widget.*
import androidx.appcompat.widget.SearchView
import androidx.core.content.edit
import androidx.preference.PreferenceManager
Expand All @@ -33,11 +31,11 @@ import kotlin.collections.ArrayList


class AllInstalledAppActivity : AppCompatActivity() {
private val allAppsList: ArrayList<AppModel> = ArrayList()
private val appsToSearchList: ArrayList<AppModel> = ArrayList()
private lateinit var recyclerView: RecyclerView
private lateinit var installedAppsList: ArrayList<AppModel>
private lateinit var installedAppAdapter: AppAdapter

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_all_installed_app)
Expand All @@ -49,34 +47,39 @@ class AllInstalledAppActivity : AppCompatActivity() {
LinearLayout.LayoutParams.WRAP_CONTENT
)
loadingDialog.setCancelable(false)
installedAppsList = ArrayList()
loadingDialog.show()
Handler(Looper.getMainLooper()).postDelayed({
getInstalledApps()
getAllApps()
setAppsToSearch()
loadingDialog.dismiss()
findViewById<TextView>(R.id.totalInstalledApp).text =
"${getString(R.string.total_installed_apps)} ${installedAppsList.size}"
installedAppAdapter = AppAdapter(this, installedAppsList)
recyclerView.adapter = installedAppAdapter
setRecyclerViewDataSet(appsToSearchList)
}, 500)

}

@SuppressLint("QueryPermissionsNeeded")
private fun getInstalledApps(): ArrayList<AppModel> {
installedAppsList.clear()
private fun getAllApps(): ArrayList<AppModel> {
val packs = packageManager.getInstalledPackages(0)
for (i in packs.indices) {
val p = packs[i]
if (!isSystemPackage(p)) {
val isSystem = isSystemPackage(p)
val checkFlagsPass: Boolean = (p.applicationInfo.flags and ApplicationInfo.FLAG_SUPPORTS_RTL != 0 && p.applicationInfo.flags and ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA != 0)
.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
it && p.applicationInfo.flags and ApplicationInfo.FLAG_EXTRACT_NATIVE_LIBS != 0
} else {
it
}
}
if (!isSystem || checkFlagsPass) {
val appName = p.applicationInfo.loadLabel(packageManager).toString()
val icon = p.applicationInfo.loadIcon(packageManager)
val packages = p.applicationInfo.packageName
installedAppsList.add(AppModel(appName, icon, packages))
allAppsList.add(AppModel(appName, icon, packages, isSystem))
}
}
installedAppsList.sortBy { it.getName().capitalized() }
return installedAppsList
allAppsList.sortBy { it.name.capitalized() }
return allAppsList
}
private fun String.capitalized(): String {
return this.replaceFirstChar {
Expand All @@ -85,15 +88,24 @@ class AllInstalledAppActivity : AppCompatActivity() {
else it.toString()
}
}
private fun setAppsToSearch(show: Boolean = false) {
appsToSearchList.clear()
if (show) {
appsToSearchList.addAll(allAppsList)
} else {
appsToSearchList.addAll(
allAppsList.filter { !it.isSystemPackage })
}
}
private fun isSystemPackage(pkgInfo: PackageInfo): Boolean {
return pkgInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val search = menu.findItem(R.id.app_bar_search)
val checkBoxAllApps = menu.findItem(R.id.app_bar_checkbox_all_apps).actionView as CheckBox
val searchView = menu.findItem(R.id.app_bar_search).actionView as SearchView

val searchView = search.actionView as SearchView
searchView.maxWidth = android.R.attr.width
searchView.queryHint = "Search app name or package"
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
Expand All @@ -105,14 +117,14 @@ class AllInstalledAppActivity : AppCompatActivity() {
override fun onQueryTextChange(newText: String?): Boolean {
val appModelArrayList: ArrayList<AppModel> = ArrayList()

for (i in installedAppsList) {
if (i.getName().lowercase(Locale.getDefault()).contains(
for (i in appsToSearchList) {
if (i.name.lowercase(Locale.getDefault()).contains(
newText!!.lowercase(
Locale.getDefault()
)
)
||
i.getPackages().lowercase(Locale.getDefault()).contains(
i.packages.lowercase(Locale.getDefault()).contains(
newText.lowercase(
Locale.getDefault()
)
Expand All @@ -121,33 +133,43 @@ class AllInstalledAppActivity : AppCompatActivity() {
appModelArrayList.add(i)
}
}
installedAppAdapter =
AppAdapter(this@AllInstalledAppActivity, appModelArrayList)

recyclerView.adapter = installedAppAdapter
installedAppAdapter.notifyDataSetChanged()
setRecyclerViewDataSet(appModelArrayList, needNotify = true)
return true
}
})
searchView.setOnSearchClickListener {
checkBoxAllApps.visibility = View.GONE
}
searchView.setOnCloseListener {
checkBoxAllApps.visibility = View.VISIBLE
false
}

checkBoxAllApps.setOnCheckedChangeListener { _, checked ->
setAppsToSearch(checked)
setRecyclerViewDataSet(appsToSearchList, needNotify = true)
}

return super.onCreateOptionsMenu(menu)
}
}

private class AppModel(private var name:String, private var icon: Drawable, private var packages:String) {
fun getName(): String {
return name
}
@SuppressLint("NotifyDataSetChanged")
private fun setRecyclerViewDataSet(dataList: ArrayList<AppModel>, needNotify: Boolean = false) {
findViewById<TextView>(R.id.totalInstalledApp).text =
getString(R.string.total_installed_apps, dataList.size)

fun getIcon(): Drawable {
return icon
}
installedAppAdapter =
AppAdapter(this, dataList)

fun getPackages(): String {
return packages
recyclerView.adapter = installedAppAdapter
if (needNotify) {
installedAppAdapter.notifyDataSetChanged()
}
}
}

private data class AppModel(val name: String, val icon: Drawable, val packages: String, val isSystemPackage: Boolean)

private class AppAdapter(private val context: AllInstalledAppActivity, private var appModelList: ArrayList<AppModel>) :
RecyclerView.Adapter<AppAdapter.ViewHolder>() {

Expand All @@ -166,9 +188,9 @@ private class AppAdapter(private val context: AllInstalledAppActivity, private v
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.appNameTxt.text = appModelList[position].getName()
holder.appIcon.setImageDrawable(appModelList[position].getIcon())
holder.appPackageNameTxt.text = appModelList[position].getPackages()
holder.appNameTxt.text = appModelList[position].name
holder.appIcon.setImageDrawable(appModelList[position].icon)
holder.appPackageNameTxt.text = appModelList[position].packages

holder.itemView.setOnClickListener {
val dialogListTitle = arrayOf("Set to open", "App Info")
Expand All @@ -180,7 +202,7 @@ private class AppAdapter(private val context: AllInstalledAppActivity, private v
when (which) {
0 -> {
val intent =
context.packageManager.getLaunchIntentForPackage(appModelList[position].getPackages())
context.packageManager.getLaunchIntentForPackage(appModelList[position].packages)
if (intent != null) {
context.intent.extras?.getString("action_type_preference_key")
.also { preferenceKey ->
Expand All @@ -198,7 +220,7 @@ private class AppAdapter(private val context: AllInstalledAppActivity, private v

actionData.apply {
type = ActionExecuteType.APP
packageId = appModelList[position].getPackages()
packageId = appModelList[position].packages
lastModifiedTimeMs = System.currentTimeMillis()
}

Expand All @@ -216,7 +238,7 @@ private class AppAdapter(private val context: AllInstalledAppActivity, private v
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
intent.data =
Uri.parse("package:${appModelList[position].getPackages()}")
Uri.parse("package:${appModelList[position].packages}")
context.startActivity(intent)
}
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_all_installed_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
android:id="@+id/totalInstalledApp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/total_installed_apps"
android:textStyle="bold"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/menu/search_menu.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/app_bar_checkbox_all_apps"
android:title="@string/all_apps"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />

<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<string name="preview_status_bar">Preview</string>
<string name="accessibility_service_label">Status Bar+ Service</string>
<string name="accessibility_service_description">Status Bar+ Service\nUse service to create an overlay over status bar to detect touch gesture, then perform actions like lock screen</string>
<string name="total_installed_apps">Installed apps</string>
<string name="total_installed_apps">Installed apps %1$d</string>
<string name="image_app_icon">App icon</string>
<string name="left">Left</string>
<string name="center">Center</string>
Expand All @@ -15,6 +15,7 @@
<string name="swipe_left_bottom">Left-Bottom</string>
<string name="swipe_right_bottom">Right-Bottom</string>
<string name="search">Search</string>
<string name="all_apps">All apps</string>
<string name="disabled_when_fullscreen">Disabled when fullscreen</string>
<string name="volume_up">Volume Up</string>
<string name="volume_down">Volume Down</string>
Expand Down

0 comments on commit 7fe44e4

Please sign in to comment.