Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
684c5d22
Commit
684c5d22
authored
Apr 06, 2018
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize repo update
parent
b75018b0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
64 deletions
+75
-64
UpdateRepos.java
src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java
+75
-64
No files found.
src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java
View file @
684c5d22
package
com
.
topjohnwu
.
magisk
.
asyncs
;
import
android.content.SharedPreferences
;
import
android.os.AsyncTask
;
import
com.topjohnwu.magisk.MagiskManager
;
import
com.topjohnwu.magisk.ReposFragment
;
import
com.topjohnwu.magisk.container.Repo
;
import
com.topjohnwu.magisk.database.RepoDatabaseHelper
;
import
com.topjohnwu.magisk.utils.Const
;
import
com.topjohnwu.magisk.utils.Logger
;
import
com.topjohnwu.magisk.utils.WebService
;
...
...
@@ -16,6 +14,7 @@ import org.json.JSONObject;
import
java.io.File
;
import
java.net.HttpURLConnection
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -24,55 +23,81 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
UpdateRepos
extends
ParallelTask
<
Void
,
Void
,
Void
>
{
private
static
final
int
CHECK_ETAG
=
0
;
private
static
final
int
LOAD_NEXT
=
1
;
private
static
final
int
LOAD_PREV
=
2
;
private
static
final
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
,
Locale
.
US
);
private
MagiskManager
mm
;
private
List
<
String
>
cached
,
etags
,
newEtags
=
new
ArrayList
<>();
private
RepoDatabaseHelper
repoDB
;
private
SharedPreferences
prefs
;
private
boolean
forceUpdate
;
private
int
tasks
=
0
;
private
AtomicInteger
taskCount
=
new
AtomicInteger
(
0
);
final
private
Object
allDone
=
new
Object
()
;
public
UpdateRepos
(
boolean
force
)
{
MagiskManager
mm
=
MagiskManager
.
get
();
prefs
=
mm
.
prefs
;
repoDB
=
mm
.
repoDB
;
mm
=
MagiskManager
.
get
();
mm
.
repoLoadDone
.
reset
();
// Legacy data cleanup
File
old
=
new
File
(
mm
.
getApplicationInfo
().
dataDir
+
"/shared_prefs"
,
"RepoMap.xml"
);
if
(
old
.
exists
()
||
prefs
.
getString
(
"repomap"
,
null
)
!=
null
)
{
if
(
old
.
exists
()
||
mm
.
prefs
.
getString
(
"repomap"
,
null
)
!=
null
)
{
old
.
delete
();
prefs
.
edit
().
remove
(
"version"
).
remove
(
"repomap"
).
remove
(
Const
.
Key
.
ETAG_KEY
).
apply
();
repoDB
.
clearRepo
();
mm
.
prefs
.
edit
().
remove
(
"version"
).
remove
(
"repomap"
).
remove
(
Const
.
Key
.
ETAG_KEY
).
apply
();
mm
.
repoDB
.
clearRepo
();
}
forceUpdate
=
force
;
}
private
void
queueTask
(
Runnable
task
)
{
// Thread pool's queue has an upper bound, batch it with 64 tasks
while
(
taskCount
.
get
()
>=
64
)
{
waitTasks
();
}
taskCount
.
incrementAndGet
();
AsyncTask
.
THREAD_POOL_EXECUTOR
.
execute
(()
->
{
task
.
run
();
if
(
taskCount
.
decrementAndGet
()
==
0
)
{
synchronized
(
allDone
)
{
allDone
.
notify
();
}
}
});
}
private
void
waitTasks
()
{
if
(
taskCount
.
get
()
==
0
)
return
;
synchronized
(
allDone
)
{
try
{
allDone
.
wait
();
}
catch
(
InterruptedException
e
)
{
// Wait again
waitTasks
();
}
}
}
private
void
loadJSON
(
String
jsonString
)
throws
Exception
{
JSONArray
jsonArray
=
new
JSONArray
(
jsonString
);
// Empty page, throw error
if
(
jsonArray
.
length
()
==
0
)
throw
new
Exception
();
if
(
jsonArray
.
length
()
==
0
)
throw
new
Exception
();
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++)
{
JSONObject
jsonobject
=
jsonArray
.
getJSONObject
(
i
);
String
id
=
jsonobject
.
getString
(
"description"
);
String
name
=
jsonobject
.
getString
(
"name"
);
String
lastUpdate
=
jsonobject
.
getString
(
"pushed_at"
);
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
,
Locale
.
US
);
Date
updatedDate
=
format
.
parse
(
lastUpdate
);
++
tasks
;
AsyncTask
.
THREAD_POOL_EXECUTOR
.
execute
(()
->
{
Repo
repo
=
repoDB
.
getRepo
(
id
);
JSONObject
rawRepo
=
jsonArray
.
getJSONObject
(
i
);
String
id
=
rawRepo
.
getString
(
"description"
);
String
name
=
rawRepo
.
getString
(
"name"
);
Date
date
=
dateFormat
.
parse
(
rawRepo
.
getString
(
"pushed_at"
));
queueTask
(()
->
{
Repo
repo
=
mm
.
repoDB
.
getRepo
(
id
);
Boolean
updated
;
try
{
if
(
repo
==
null
)
{
repo
=
new
Repo
(
name
,
updatedD
ate
);
repo
=
new
Repo
(
name
,
d
ate
);
updated
=
true
;
}
else
{
// Popout from cached
...
...
@@ -81,11 +106,11 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
repo
.
update
();
updated
=
true
;
}
else
{
updated
=
repo
.
update
(
updatedD
ate
);
updated
=
repo
.
update
(
d
ate
);
}
}
if
(
updated
)
{
repoDB
.
addRepo
(
repo
);
mm
.
repoDB
.
addRepo
(
repo
);
publishProgress
();
}
if
(!
id
.
equals
(
repo
.
getId
()))
{
...
...
@@ -93,9 +118,8 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
}
}
catch
(
Repo
.
IllegalRepoException
e
)
{
Logger
.
error
(
e
.
getMessage
());
repoDB
.
removeRepo
(
id
);
mm
.
repoDB
.
removeRepo
(
id
);
}
--
tasks
;
});
}
}
...
...
@@ -111,7 +135,8 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
HttpURLConnection
conn
=
WebService
.
request
(
url
,
header
);
try
{
if
(
conn
==
null
)
throw
new
Exception
();
if
(
conn
==
null
)
throw
new
Exception
();
if
(
conn
.
getResponseCode
()
==
HttpURLConnection
.
HTTP_NOT_MODIFIED
)
{
newEtags
.
add
(
etag
);
return
page
+
1
<
etags
.
size
()
&&
loadPage
(
page
+
1
,
CHECK_ETAG
);
...
...
@@ -143,17 +168,6 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
return
true
;
}
private
Void
waitTasks
()
{
while
(
tasks
>
0
)
{
try
{
Thread
.
sleep
(
5
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
return
null
;
}
@Override
protected
void
onProgressUpdate
(
Void
...
values
)
{
if
(
ReposFragment
.
adapter
!=
null
)
...
...
@@ -162,41 +176,37 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
@Override
protected
void
onPreExecute
()
{
MagiskManager
.
get
()
.
repoLoadDone
.
setPending
();
mm
.
repoLoadDone
.
setPending
();
}
@Override
protected
Void
doInBackground
(
Void
...
voids
)
{
etags
=
new
ArrayList
<>(
Arrays
.
asList
(
prefs
.
getString
(
Const
.
Key
.
ETAG_KEY
,
""
).
split
(
","
)));
cached
=
repoDB
.
getRepoIDList
();
etags
=
new
ArrayList
<>(
Arrays
.
asList
(
mm
.
prefs
.
getString
(
Const
.
Key
.
ETAG_KEY
,
""
).
split
(
","
)));
cached
=
mm
.
repoDB
.
getRepoIDList
();
if
(!
loadPage
(
0
,
CHECK_ETAG
))
{
// Nothing changed online
if
(
forceUpdate
)
{
for
(
String
id
:
cached
)
{
if
(
id
==
null
)
continue
;
++
tasks
;
AsyncTask
.
THREAD_POOL_EXECUTOR
.
execute
(()
->
{
Repo
repo
=
repoDB
.
getRepo
(
id
);
queueTask
(()
->
{
Repo
repo
=
mm
.
repoDB
.
getRepo
(
id
);
try
{
repo
.
update
();
repoDB
.
addRepo
(
repo
);
mm
.
repoDB
.
addRepo
(
repo
);
}
catch
(
Repo
.
IllegalRepoException
e
)
{
Logger
.
error
(
e
.
getMessage
());
repoDB
.
removeRepo
(
repo
);
mm
.
repoDB
.
removeRepo
(
repo
);
}
--
tasks
;
});
}
}
return
waitTasks
();
}
// Wait till all tasks are done
waitTasks
();
}
else
{
waitTasks
();
// The leftover cached means they are removed from online repo
repoDB
.
removeRepo
(
cached
);
mm
.
repoDB
.
removeRepo
(
cached
);
// Update ETag
StringBuilder
etagBuilder
=
new
StringBuilder
();
...
...
@@ -204,13 +214,14 @@ public class UpdateRepos extends ParallelTask<Void, Void, Void> {
if
(
i
!=
0
)
etagBuilder
.
append
(
","
);
etagBuilder
.
append
(
newEtags
.
get
(
i
));
}
prefs
.
edit
().
putString
(
Const
.
Key
.
ETAG_KEY
,
etagBuilder
.
toString
()).
apply
();
mm
.
prefs
.
edit
().
putString
(
Const
.
Key
.
ETAG_KEY
,
etagBuilder
.
toString
()).
apply
();
}
return
null
;
}
@Override
protected
void
onPostExecute
(
Void
v
)
{
MagiskManager
.
get
()
.
repoLoadDone
.
publish
();
mm
.
repoLoadDone
.
publish
();
super
.
onPostExecute
(
v
);
}
}
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