Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
bc22b0c4
Commit
bc22b0c4
authored
Dec 17, 2017
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Speed up AppManager
Also solves a cache problem.
parent
1bea09c5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
16 deletions
+14
-16
mobile/src/main/java/com/github/shadowsocks/AppManager.kt
mobile/src/main/java/com/github/shadowsocks/AppManager.kt
+14
-16
No files found.
mobile/src/main/java/com/github/shadowsocks/AppManager.kt
View file @
bc22b0c4
...
...
@@ -28,6 +28,8 @@ import android.content.BroadcastReceiver
import
android.content.ClipData
import
android.content.ClipboardManager
import
android.content.Context
import
android.content.pm.ApplicationInfo
import
android.content.pm.PackageInfo
import
android.content.pm.PackageManager
import
android.graphics.drawable.Drawable
import
android.os.Bundle
...
...
@@ -38,7 +40,6 @@ import android.support.v7.widget.DefaultItemAnimator
import
android.support.v7.widget.LinearLayoutManager
import
android.support.v7.widget.RecyclerView
import
android.support.v7.widget.Toolbar
import
android.util.TypedValue
import
android.view.*
import
android.widget.ImageView
import
android.widget.Switch
...
...
@@ -56,31 +57,33 @@ import java.util.concurrent.atomic.AtomicBoolean
class
AppManager
:
AppCompatActivity
(),
Toolbar
.
OnMenuItemClickListener
{
companion
object
{
data class
ProxiedApp
(
val
name
:
String
,
val
packageName
:
String
,
val
icon
:
Drawable
)
@SuppressLint
(
"StaticFieldLeak"
)
private
var
instance
:
AppManager
?
=
null
private
var
receiver
:
BroadcastReceiver
?
=
null
private
var
cachedApps
:
Array
<
ProxiedApp
>?
=
null
private
fun
getApps
(
pm
:
PackageManager
):
Array
<
ProxiedApp
>
{
private
var
cachedApps
:
List
<
PackageInfo
>?
=
null
private
fun
getApps
(
pm
:
PackageManager
):
List
<
ProxiedApp
>
{
if
(
receiver
==
null
)
receiver
=
app
.
listenForPackageChanges
{
synchronized
(
AppManager
)
{
cachedApps
=
null
}
AppManager
.
instance
?.
reloadApps
()
}
return
synchronized
(
AppManager
)
{
// Labels and icons can change on configuration (locale, etc.) changes, therefore they are not cached.
val
cachedApps
=
cachedApps
?:
pm
.
getInstalledPackages
(
PackageManager
.
GET_PERMISSIONS
)
.
filter
{
it
.
packageName
!=
app
.
packageName
&&
it
.
requestedPermissions
?.
contains
(
Manifest
.
permission
.
INTERNET
)
?:
false
}
.
map
{
ProxiedApp
(
pm
.
getApplicationLabel
(
it
.
applicationInfo
).
toString
(),
it
.
packageName
,
it
.
applicationInfo
.
loadIcon
(
pm
))
}
.
toTypedArray
()
this
.
cachedApps
=
cachedApps
cachedApps
}
}
.
map
{
ProxiedApp
(
pm
,
it
.
applicationInfo
,
it
.
packageName
)
}
}
}
private
class
ProxiedApp
(
private
val
pm
:
PackageManager
,
private
val
appInfo
:
ApplicationInfo
,
val
packageName
:
String
)
{
val
name
:
CharSequence
=
appInfo
.
loadLabel
(
pm
)
// cached for sorting
val
icon
:
Drawable
get
()
=
appInfo
.
loadIcon
(
pm
)
}
private
inner
class
AppViewHolder
(
view
:
View
)
:
RecyclerView
.
ViewHolder
(
view
),
View
.
OnClickListener
{
private
val
icon
=
view
.
findViewById
<
ImageView
>(
R
.
id
.
itemicon
)
private
val
check
=
view
.
findViewById
<
Switch
>(
R
.
id
.
itemcheck
)
...
...
@@ -117,13 +120,8 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
private
var
apps
=
listOf
<
ProxiedApp
>()
fun
reload
()
{
apps
=
getApps
(
packageManager
).
sortedWith
(
Comparator
{
a
,
b
->
when
(
Pair
(
proxiedApps
.
contains
(
a
.
packageName
),
proxiedApps
.
contains
(
b
.
packageName
)))
{
Pair
(
true
,
false
)
->
-
1
Pair
(
false
,
true
)
->
1
else
->
a
.
name
.
compareTo
(
b
.
name
,
true
)
}
})
apps
=
getApps
(
packageManager
)
.
sortedWith
(
compareBy
({
!
proxiedApps
.
contains
(
it
.
packageName
)
},
{
it
.
name
.
toString
()
}))
}
override
fun
onBindViewHolder
(
holder
:
AppViewHolder
,
position
:
Int
)
=
holder
.
bind
(
apps
[
position
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment