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
8458553b
Commit
8458553b
authored
Jul 20, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update database helper
parent
55ecc41d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
83 deletions
+46
-83
MagiskManager.java
app/src/main/java/com/topjohnwu/magisk/MagiskManager.java
+4
-1
LoadRepos.java
app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java
+1
-1
RepoDatabaseHelper.java
...ava/com/topjohnwu/magisk/database/RepoDatabaseHelper.java
+8
-18
SuDatabaseHelper.java
.../java/com/topjohnwu/magisk/database/SuDatabaseHelper.java
+29
-59
Utils.java
app/src/main/java/com/topjohnwu/magisk/utils/Utils.java
+4
-4
No files found.
app/src/main/java/com/topjohnwu/magisk/MagiskManager.java
View file @
8458553b
...
...
@@ -12,6 +12,7 @@ import android.preference.PreferenceManager;
import
android.text.TextUtils
;
import
android.widget.Toast
;
import
com.topjohnwu.magisk.database.RepoDatabaseHelper
;
import
com.topjohnwu.magisk.database.SuDatabaseHelper
;
import
com.topjohnwu.magisk.module.Module
;
import
com.topjohnwu.magisk.module.Repo
;
...
...
@@ -89,6 +90,7 @@ public class MagiskManager extends Application {
// Global resources
public
SharedPreferences
prefs
;
public
SuDatabaseHelper
suDB
;
public
RepoDatabaseHelper
repoDB
;
public
Shell
shell
;
private
static
Handler
mHandler
=
new
Handler
();
...
...
@@ -99,6 +101,8 @@ public class MagiskManager extends Application {
new
File
(
getApplicationInfo
().
dataDir
).
mkdirs
();
/* Create the app data directory */
prefs
=
PreferenceManager
.
getDefaultSharedPreferences
(
this
);
shell
=
Shell
.
getShell
();
suDB
=
new
SuDatabaseHelper
(
this
);
repoDB
=
new
RepoDatabaseHelper
(
this
);
}
public
void
toast
(
String
msg
,
int
duration
)
{
...
...
@@ -161,7 +165,6 @@ public class MagiskManager extends Application {
}
public
void
initSUConfig
()
{
suDB
=
new
SuDatabaseHelper
(
this
);
suRequestTimeout
=
Utils
.
getPrefsInt
(
prefs
,
"su_request_timeout"
,
10
);
suResponseType
=
Utils
.
getPrefsInt
(
prefs
,
"su_auto_response"
,
0
);
suNotificationType
=
Utils
.
getPrefsInt
(
prefs
,
"su_notification"
,
1
);
...
...
app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java
View file @
8458553b
...
...
@@ -45,8 +45,8 @@ public class LoadRepos extends ParallelTask<Void, Void, Void> {
public
LoadRepos
(
Context
context
)
{
super
(
context
);
prefs
=
getMagiskManager
().
prefs
;
repoDB
=
getMagiskManager
().
repoDB
;
String
prefsPath
=
context
.
getApplicationInfo
().
dataDir
+
"/shared_prefs"
;
repoDB
=
new
RepoDatabaseHelper
(
context
);
// Legacy data cleanup
File
old
=
new
File
(
prefsPath
,
"RepoMap.xml"
);
if
(
old
.
exists
()
||
!
prefs
.
getString
(
"repomap"
,
"empty"
).
equals
(
"empty"
))
{
...
...
app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java
View file @
8458553b
...
...
@@ -17,8 +17,11 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
private
static
final
String
TABLE_NAME
=
"repos"
;
private
static
final
int
MIN_TEMPLATE_VER
=
3
;
private
SQLiteDatabase
mDb
;
public
RepoDatabaseHelper
(
Context
context
)
{
super
(
context
,
"repo.db"
,
null
,
DATABASE_VER
);
mDb
=
getWritableDatabase
();
}
@Override
...
...
@@ -43,29 +46,23 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
}
public
void
addRepoMap
(
ValueSortedMap
<
String
,
Repo
>
map
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
Collection
<
Repo
>
list
=
map
.
values
();
for
(
Repo
repo
:
list
)
{
Logger
.
dev
(
"Add to DB: "
+
repo
.
getId
());
d
b
.
replace
(
TABLE_NAME
,
null
,
repo
.
getContentValues
());
mD
b
.
replace
(
TABLE_NAME
,
null
,
repo
.
getContentValues
());
}
db
.
close
();
}
public
void
clearRepo
()
{
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
delete
(
TABLE_NAME
,
null
,
null
);
db
.
close
();
mDb
.
delete
(
TABLE_NAME
,
null
,
null
);
}
public
void
removeRepo
(
ValueSortedMap
<
String
,
Repo
>
map
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
Collection
<
Repo
>
list
=
map
.
values
();
for
(
Repo
repo
:
list
)
{
Logger
.
dev
(
"Remove from DB: "
+
repo
.
getId
());
d
b
.
delete
(
TABLE_NAME
,
"id=?"
,
new
String
[]
{
repo
.
getId
()
});
mD
b
.
delete
(
TABLE_NAME
,
"id=?"
,
new
String
[]
{
repo
.
getId
()
});
}
db
.
close
();
}
public
ValueSortedMap
<
String
,
Repo
>
getRepoMap
()
{
...
...
@@ -74,20 +71,13 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
public
ValueSortedMap
<
String
,
Repo
>
getRepoMap
(
boolean
filtered
)
{
ValueSortedMap
<
String
,
Repo
>
ret
=
new
ValueSortedMap
<>();
SQLiteDatabase
db
=
getReadableDatabase
();
Repo
repo
;
try
(
Cursor
c
=
db
.
query
(
TABLE_NAME
,
null
,
null
,
null
,
null
,
null
,
null
))
{
try
(
Cursor
c
=
mDb
.
query
(
TABLE_NAME
,
null
,
"template>=?"
,
new
String
[]
{
filtered
?
String
.
valueOf
(
MIN_TEMPLATE_VER
)
:
"0"
}
,
null
,
null
,
null
))
{
while
(
c
.
moveToNext
())
{
repo
=
new
Repo
(
c
);
if
(
repo
.
getTemplateVersion
()
<
MIN_TEMPLATE_VER
&&
filtered
)
{
Logger
.
dev
(
"Outdated repo: "
+
repo
.
getId
());
}
else
{
// Logger.dev("Load from DB: " + repo.getId());
ret
.
put
(
repo
.
getId
(),
repo
);
}
ret
.
put
(
repo
.
getId
(),
repo
);
}
}
db
.
close
();
return
ret
;
}
}
app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java
View file @
8458553b
...
...
@@ -30,11 +30,13 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
private
MagiskManager
magiskManager
;
private
PackageManager
pm
;
private
SQLiteDatabase
mDb
;
public
SuDatabaseHelper
(
Context
context
)
{
super
(
context
,
"su.db"
,
null
,
DATABASE_VER
);
magiskManager
=
Utils
.
getMagiskManager
(
context
);
pm
=
context
.
getPackageManager
();
mDb
=
getWritableDatabase
();
cleanup
();
}
...
...
@@ -93,12 +95,11 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
}
private
void
cleanup
()
{
SQLiteDatabase
db
=
getWritableDatabase
();
// Clear outdated policies
d
b
.
delete
(
POLICY_TABLE
,
"until > 0 AND until < ?"
,
mD
b
.
delete
(
POLICY_TABLE
,
"until > 0 AND until < ?"
,
new
String
[]
{
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
)
});
// Clear outdated logs
d
b
.
delete
(
LOG_TABLE
,
"time < ?"
,
new
String
[]
{
String
.
valueOf
(
mD
b
.
delete
(
LOG_TABLE
,
"time < ?"
,
new
String
[]
{
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
-
magiskManager
.
suLogTimeout
*
86400
)
});
}
...
...
@@ -107,25 +108,16 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
}
public
void
deletePolicy
(
String
pkg
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
delete
(
POLICY_TABLE
,
"package_name=?"
,
new
String
[]
{
pkg
});
db
.
close
();
mDb
.
delete
(
POLICY_TABLE
,
"package_name=?"
,
new
String
[]
{
pkg
});
}
public
void
deletePolicy
(
int
uid
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
deletePolicy
(
db
,
uid
);
db
.
close
();
}
private
void
deletePolicy
(
SQLiteDatabase
db
,
int
uid
)
{
db
.
delete
(
POLICY_TABLE
,
"uid=?"
,
new
String
[]{
String
.
valueOf
(
uid
)});
mDb
.
delete
(
POLICY_TABLE
,
"uid=?"
,
new
String
[]{
String
.
valueOf
(
uid
)});
}
public
Policy
getPolicy
(
int
uid
)
{
Policy
policy
=
null
;
SQLiteDatabase
db
=
getReadableDatabase
();
try
(
Cursor
c
=
db
.
query
(
POLICY_TABLE
,
null
,
"uid=?"
,
new
String
[]
{
String
.
valueOf
(
uid
)
},
null
,
null
,
null
))
{
try
(
Cursor
c
=
mDb
.
query
(
POLICY_TABLE
,
null
,
"uid=?"
,
new
String
[]
{
String
.
valueOf
(
uid
)
},
null
,
null
,
null
))
{
if
(
c
.
moveToNext
())
{
policy
=
new
Policy
(
c
,
pm
);
}
...
...
@@ -133,14 +125,12 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
deletePolicy
(
uid
);
return
null
;
}
db
.
close
();
return
policy
;
}
public
Policy
getPolicy
(
String
pkg
)
{
Policy
policy
=
null
;
SQLiteDatabase
db
=
getReadableDatabase
();
try
(
Cursor
c
=
db
.
query
(
POLICY_TABLE
,
null
,
"package_name=?"
,
new
String
[]
{
pkg
},
null
,
null
,
null
))
{
try
(
Cursor
c
=
mDb
.
query
(
POLICY_TABLE
,
null
,
"package_name=?"
,
new
String
[]
{
pkg
},
null
,
null
,
null
))
{
if
(
c
.
moveToNext
())
{
policy
=
new
Policy
(
c
,
pm
);
}
...
...
@@ -148,76 +138,64 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
deletePolicy
(
pkg
);
return
null
;
}
db
.
close
();
return
policy
;
}
public
void
addPolicy
(
Policy
policy
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
replace
(
POLICY_TABLE
,
null
,
policy
.
getContentValues
());
db
.
close
();
mDb
.
replace
(
POLICY_TABLE
,
null
,
policy
.
getContentValues
());
}
public
void
updatePolicy
(
Policy
policy
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
updatePolicy
(
db
,
policy
);
db
.
close
();
}
private
void
updatePolicy
(
SQLiteDatabase
db
,
Policy
policy
)
{
db
.
update
(
POLICY_TABLE
,
policy
.
getContentValues
(),
"package_name=?"
,
mDb
.
update
(
POLICY_TABLE
,
policy
.
getContentValues
(),
"package_name=?"
,
new
String
[]
{
policy
.
packageName
});
}
public
List
<
Policy
>
getPolicyList
(
PackageManager
pm
)
{
List
<
Policy
>
ret
=
new
ArrayList
<>();
SQLiteDatabase
db
=
getWritableDatabase
();
Policy
policy
;
try
(
Cursor
c
=
db
.
query
(
POLICY_TABLE
,
null
,
null
,
null
,
null
,
null
,
null
))
{
try
(
Cursor
c
=
mDb
.
query
(
POLICY_TABLE
,
null
,
null
,
null
,
null
,
null
,
null
))
{
List
<
Policy
>
ret
=
new
ArrayList
<>(
c
.
getCount
());
while
(
c
.
moveToNext
())
{
try
{
policy
=
new
Policy
(
c
,
pm
);
Policy
policy
=
new
Policy
(
c
,
pm
);
// The application changed UID for some reason, check user config
if
(
policy
.
info
.
uid
!=
policy
.
uid
)
{
if
(
magiskManager
.
suReauth
)
{
// Reauth required, remove from DB
deletePolicy
(
db
,
policy
.
uid
);
deletePolicy
(
policy
);
continue
;
}
else
{
// No reauth, update to use the new UID
policy
.
uid
=
policy
.
info
.
uid
;
updatePolicy
(
db
,
policy
);
updatePolicy
(
policy
);
}
}
ret
.
add
(
policy
);
}
catch
(
PackageManager
.
NameNotFoundException
e
)
{
// The app no longer exist, remove from DB
deletePolicy
(
db
,
c
.
getInt
(
c
.
getColumnIndex
(
"uid"
)));
deletePolicy
(
c
.
getInt
(
c
.
getColumnIndex
(
"uid"
)));
}
}
Collections
.
sort
(
ret
);
return
ret
;
}
db
.
close
();
Collections
.
sort
(
ret
);
return
ret
;
}
private
List
<
SuLogEntry
>
getLogList
(
SQLiteDatabase
db
,
String
selection
)
{
List
<
SuLogEntry
>
ret
=
new
ArrayList
<>();
try
(
Cursor
c
=
db
.
query
(
LOG_TABLE
,
null
,
selection
,
null
,
null
,
null
,
"time DESC"
))
{
List
<
SuLogEntry
>
ret
=
new
ArrayList
<>(
c
.
getCount
());
while
(
c
.
moveToNext
())
{
ret
.
add
(
new
SuLogEntry
(
c
));
}
return
ret
;
}
db
.
close
();
return
ret
;
}
private
void
migrateLegacyLogList
(
File
oldDB
,
SQLiteDatabase
newDB
)
{
SQLiteDatabase
d
b
=
SQLiteDatabase
.
openDatabase
(
oldDB
.
getPath
(),
null
,
SQLiteDatabase
.
OPEN_READWRITE
);
List
<
SuLogEntry
>
logs
=
getLogList
(
d
b
,
null
);
SQLiteDatabase
oldD
b
=
SQLiteDatabase
.
openDatabase
(
oldDB
.
getPath
(),
null
,
SQLiteDatabase
.
OPEN_READWRITE
);
List
<
SuLogEntry
>
logs
=
getLogList
(
oldD
b
,
null
);
for
(
SuLogEntry
log
:
logs
)
{
newDB
.
insert
(
LOG_TABLE
,
null
,
log
.
getContentValues
());
}
oldDb
.
close
();
}
public
List
<
SuLogEntry
>
getLogList
()
{
...
...
@@ -225,39 +203,31 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
}
public
List
<
SuLogEntry
>
getLogList
(
String
selection
)
{
return
getLogList
(
getReadableDatabase
()
,
selection
);
return
getLogList
(
mDb
,
selection
);
}
public
void
addLog
(
SuLogEntry
log
)
{
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
insert
(
LOG_TABLE
,
null
,
log
.
getContentValues
());
db
.
close
();
mDb
.
insert
(
LOG_TABLE
,
null
,
log
.
getContentValues
());
}
public
void
clearLogs
()
{
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
delete
(
LOG_TABLE
,
null
,
null
);
db
.
close
();
mDb
.
delete
(
LOG_TABLE
,
null
,
null
);
}
public
void
setSettings
(
String
key
,
int
value
)
{
ContentValues
data
=
new
ContentValues
();
data
.
put
(
"key"
,
key
);
data
.
put
(
"value"
,
value
);
SQLiteDatabase
db
=
getWritableDatabase
();
db
.
replace
(
SETTINGS_TABLE
,
null
,
data
);
db
.
close
();
mDb
.
replace
(
SETTINGS_TABLE
,
null
,
data
);
}
public
int
getSettings
(
String
key
,
int
defaultValue
)
{
SQLiteDatabase
db
=
getReadableDatabase
();
int
value
=
defaultValue
;
try
(
Cursor
c
=
db
.
query
(
SETTINGS_TABLE
,
null
,
"key=?"
,
new
String
[]
{
key
},
null
,
null
,
null
))
{
while
(
c
.
moveToNext
())
{
try
(
Cursor
c
=
mDb
.
query
(
SETTINGS_TABLE
,
null
,
"key=?"
,
new
String
[]
{
key
},
null
,
null
,
null
))
{
if
(
c
.
moveToNext
())
{
value
=
c
.
getInt
(
c
.
getColumnIndex
(
"value"
));
}
}
db
.
close
();
return
value
;
}
}
app/src/main/java/com/topjohnwu/magisk/utils/Utils.java
View file @
8458553b
...
...
@@ -140,11 +140,11 @@ public class Utils {
}.
requestTest
();
}
public
static
void
clearRepoCache
(
Activity
activity
)
{
MagiskManager
magiskManager
=
getMagiskManager
(
activity
);
public
static
void
clearRepoCache
(
Context
context
)
{
MagiskManager
magiskManager
=
getMagiskManager
(
context
);
magiskManager
.
prefs
.
edit
().
remove
(
LoadRepos
.
ETAG_KEY
).
apply
();
new
RepoDatabaseHelper
(
activity
)
.
clearRepo
();
Toast
.
makeText
(
activity
,
R
.
string
.
repo_cache_cleared
,
Toast
.
LENGTH_SHORT
).
show
(
);
magiskManager
.
repoDB
.
clearRepo
();
magiskManager
.
toast
(
R
.
string
.
repo_cache_cleared
,
Toast
.
LENGTH_SHORT
);
}
public
static
String
getNameFromUri
(
Context
context
,
Uri
uri
)
{
...
...
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