Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1ed83019
authored
Mar 09, 2020
by
pye52
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'new_master' into new_for_phone
parents
d07c1b13
f7a10807
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
389 additions
and
1 deletions
+389
-1
app/src/main/AndroidManifest.xml
+13
-0
app/src/main/java/com/bgycc/smartcanteen/broadcast/BootBroadcast.java
+18
-0
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandHandler.java
+2
-1
app/src/main/java/com/bgycc/smartcanteen/utils/DangerousUtils.java
+356
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
1ed83019
...
...
@@ -6,6 +6,7 @@
<uses-permission
android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.WRITE_SETTINGS"
/>
<uses-permission
android:name=
"android.permission.INSTALL_PACKAGES"
/>
<uses-permission
android:name=
"android.permission.CHANGE_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_WIFI_STATE"
/>
...
...
@@ -22,6 +23,7 @@
android:name=
".RootApp"
android:theme=
"@style/AppTheme"
>
<activity
android:name=
".MainActivity"
android:launchMode=
"singleInstance"
android:screenOrientation=
"landscape"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
...
...
@@ -39,6 +41,16 @@
<data
android:scheme=
"canteen"
/>
</intent-filter>
</activity>
<receiver
android:name=
".broadcast.BootBroadcast"
android:enabled=
"true"
android:exported=
"true"
android:permission=
"android.permission.RECEIVE_BOOT_COMPLETED"
>
<intent-filter
android:priority=
"1000"
>
<category
android:name=
"android.intent.category.DEFAULT"
/>
<action
android:name=
"android.intent.action.BOOT_COMPLETED"
/>
</intent-filter>
</receiver>
</application>
</manifest>
\ No newline at end of file
app/src/main/java/com/bgycc/smartcanteen/broadcast/BootBroadcast.java
0 → 100644
View file @
1ed83019
package
com
.
bgycc
.
smartcanteen
.
broadcast
;
import
android.annotation.SuppressLint
;
import
android.content.BroadcastReceiver
;
import
android.content.Context
;
import
android.content.Intent
;
import
com.bgycc.smartcanteen.MainActivity
;
public
class
BootBroadcast
extends
BroadcastReceiver
{
@SuppressLint
(
"UnsafeProtectedBroadcastReceiver"
)
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
Intent
scIntent
=
new
Intent
(
context
,
MainActivity
.
class
);
scIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
context
.
startActivity
(
scIntent
);
}
}
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandHandler.java
View file @
1ed83019
...
...
@@ -5,6 +5,7 @@ import com.bgycc.smartcanteen.api.SCRetrofit;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.CommandResponse
;
import
com.bgycc.smartcanteen.entity.CommandUpdate
;
import
com.bgycc.smartcanteen.utils.DangerousUtils
;
import
com.blankj.utilcode.util.AppUtils
;
import
com.blankj.utilcode.util.FileIOUtils
;
import
com.blankj.utilcode.util.FileUtils
;
...
...
@@ -68,7 +69,7 @@ public class UpdateCommandHandler extends CommandHandler {
(
info
.
getPackageName
().
equals
(
BuildConfig
.
APPLICATION_ID
)
&&
info
.
getVersionCode
()
<
BuildConfig
.
VERSION_CODE
))
{
return
failed
(
"不允许安装低版本"
);
}
else
{
AppUtils
.
installApp
(
updateApk
);
DangerousUtils
.
installAppSilent
(
updateApk
);
return
success
(
"安装更新包"
);
}
}
...
...
app/src/main/java/com/bgycc/smartcanteen/utils/DangerousUtils.java
0 → 100644
View file @
1ed83019
package
com
.
bgycc
.
smartcanteen
.
utils
;
import
android.app.PendingIntent
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.os.Build
;
import
android.os.PowerManager
;
import
android.telephony.SmsManager
;
import
android.telephony.TelephonyManager
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
androidx.annotation.RequiresPermission
;
import
com.blankj.utilcode.util.ShellUtils
;
import
com.blankj.utilcode.util.Utils
;
import
java.io.File
;
import
java.lang.reflect.Method
;
import
java.util.List
;
import
static
android
.
Manifest
.
permission
.
MODIFY_PHONE_STATE
;
import
static
android
.
Manifest
.
permission
.
SEND_SMS
;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2019/06/29
* desc :
* </pre>
*/
public
class
DangerousUtils
{
private
DangerousUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
///////////////////////////////////////////////////////////////////////////
// AppUtils
///////////////////////////////////////////////////////////////////////////
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param filePath The path of file.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
installAppSilent
(
final
String
filePath
)
{
return
installAppSilent
(
getFileByPath
(
filePath
),
null
);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
installAppSilent
(
final
File
file
)
{
return
installAppSilent
(
file
,
null
);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param filePath The path of file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
installAppSilent
(
final
String
filePath
,
final
String
params
)
{
return
installAppSilent
(
getFileByPath
(
filePath
),
params
);
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
installAppSilent
(
final
File
file
,
final
String
params
)
{
return
installAppSilent
(
file
,
params
,
isDeviceRooted
());
}
/**
* Install the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
*
* @param file The file.
* @param params The params of installation(e.g.,<code>-r</code>, <code>-s</code>).
* @param isRooted True to use root, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
installAppSilent
(
final
File
file
,
final
String
params
,
final
boolean
isRooted
)
{
if
(!
isFileExists
(
file
))
return
false
;
String
filePath
=
'"'
+
file
.
getAbsolutePath
()
+
'"'
;
String
command
=
"LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm install "
+
(
params
==
null
?
""
:
params
+
" "
)
+
filePath
;
ShellUtils
.
CommandResult
commandResult
=
ShellUtils
.
execCmd
(
command
,
isRooted
);
if
(
commandResult
.
successMsg
!=
null
&&
commandResult
.
successMsg
.
toLowerCase
().
contains
(
"success"
))
{
return
true
;
}
else
{
Log
.
e
(
"AppUtils"
,
"installAppSilent successMsg: "
+
commandResult
.
successMsg
+
", errorMsg: "
+
commandResult
.
errorMsg
);
return
false
;
}
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
uninstallAppSilent
(
final
String
packageName
)
{
return
uninstallAppSilent
(
packageName
,
false
);
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @param isKeepData Is keep the data.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
uninstallAppSilent
(
final
String
packageName
,
final
boolean
isKeepData
)
{
return
uninstallAppSilent
(
packageName
,
isKeepData
,
isDeviceRooted
());
}
/**
* Uninstall the app silently.
* <p>Without root permission must hold
* {@code android:sharedUserId="android.uid.shell"} and
* {@code <uses-permission android:name="android.permission.DELETE_PACKAGES" />}</p>
*
* @param packageName The name of the package.
* @param isKeepData Is keep the data.
* @param isRooted True to use root, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
uninstallAppSilent
(
final
String
packageName
,
final
boolean
isKeepData
,
final
boolean
isRooted
)
{
if
(
isSpace
(
packageName
))
return
false
;
String
command
=
"LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm uninstall "
+
(
isKeepData
?
"-k "
:
""
)
+
packageName
;
ShellUtils
.
CommandResult
commandResult
=
ShellUtils
.
execCmd
(
command
,
isRooted
);
if
(
commandResult
.
successMsg
!=
null
&&
commandResult
.
successMsg
.
toLowerCase
().
contains
(
"success"
))
{
return
true
;
}
else
{
Log
.
e
(
"AppUtils"
,
"uninstallAppSilent successMsg: "
+
commandResult
.
successMsg
+
", errorMsg: "
+
commandResult
.
errorMsg
);
return
false
;
}
}
private
static
boolean
isFileExists
(
final
File
file
)
{
return
file
!=
null
&&
file
.
exists
();
}
private
static
File
getFileByPath
(
final
String
filePath
)
{
return
isSpace
(
filePath
)
?
null
:
new
File
(
filePath
);
}
private
static
boolean
isSpace
(
final
String
s
)
{
if
(
s
==
null
)
return
true
;
for
(
int
i
=
0
,
len
=
s
.
length
();
i
<
len
;
++
i
)
{
if
(!
Character
.
isWhitespace
(
s
.
charAt
(
i
)))
{
return
false
;
}
}
return
true
;
}
private
static
boolean
isDeviceRooted
()
{
String
su
=
"su"
;
String
[]
locations
=
{
"/system/bin/"
,
"/system/xbin/"
,
"/sbin/"
,
"/system/sd/xbin/"
,
"/system/bin/failsafe/"
,
"/data/local/xbin/"
,
"/data/local/bin/"
,
"/data/local/"
,
"/system/sbin/"
,
"/usr/bin/"
,
"/vendor/bin/"
};
for
(
String
location
:
locations
)
{
if
(
new
File
(
location
+
su
).
exists
())
{
return
true
;
}
}
return
false
;
}
///////////////////////////////////////////////////////////////////////////
// DeviceUtils
///////////////////////////////////////////////////////////////////////////
/**
* Shutdown the device
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.SHUTDOWN" />}
* in manifest.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
shutdown
()
{
try
{
ShellUtils
.
CommandResult
result
=
ShellUtils
.
execCmd
(
"reboot -p"
,
true
);
if
(
result
.
result
==
0
)
return
true
;
Intent
intent
=
new
Intent
(
"android.intent.action.ACTION_REQUEST_SHUTDOWN"
);
intent
.
putExtra
(
"android.intent.extra.KEY_CONFIRM"
,
false
);
Utils
.
getApp
().
startActivity
(
intent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
));
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
/**
* Reboot the device.
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"} in manifest.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
reboot
()
{
try
{
ShellUtils
.
CommandResult
result
=
ShellUtils
.
execCmd
(
"reboot"
,
true
);
if
(
result
.
result
==
0
)
return
true
;
Intent
intent
=
new
Intent
(
Intent
.
ACTION_REBOOT
);
intent
.
putExtra
(
"nowait"
,
1
);
intent
.
putExtra
(
"interval"
,
1
);
intent
.
putExtra
(
"window"
,
0
);
Utils
.
getApp
().
sendBroadcast
(
intent
);
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
/**
* Reboot the device.
* <p>Requires root permission
* or hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.REBOOT" />}</p>
*
* @param reason code to pass to the kernel (e.g., "recovery") to
* request special boot modes, or null.
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
reboot
(
final
String
reason
)
{
try
{
PowerManager
pm
=
(
PowerManager
)
Utils
.
getApp
().
getSystemService
(
Context
.
POWER_SERVICE
);
pm
.
reboot
(
reason
);
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
/**
* Reboot the device to recovery.
* <p>Requires root permission.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
reboot2Recovery
()
{
ShellUtils
.
CommandResult
result
=
ShellUtils
.
execCmd
(
"reboot recovery"
,
true
);
return
result
.
result
==
0
;
}
/**
* Reboot the device to bootloader.
* <p>Requires root permission.</p>
*
* @return {@code true}: success<br>{@code false}: fail
*/
public
static
boolean
reboot2Bootloader
()
{
ShellUtils
.
CommandResult
result
=
ShellUtils
.
execCmd
(
"reboot bootloader"
,
true
);
return
result
.
result
==
0
;
}
/**
* Enable or disable mobile data.
* <p>Must hold {@code android:sharedUserId="android.uid.system"},
* {@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />}</p>
*
* @param enabled True to enabled, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
@RequiresPermission
(
MODIFY_PHONE_STATE
)
public
static
boolean
setMobileDataEnabled
(
final
boolean
enabled
)
{
try
{
TelephonyManager
tm
=
(
TelephonyManager
)
Utils
.
getApp
().
getSystemService
(
Context
.
TELEPHONY_SERVICE
);
if
(
tm
==
null
)
return
false
;
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
tm
.
setDataEnabled
(
enabled
);
return
true
;
}
Method
setDataEnabledMethod
=
tm
.
getClass
().
getDeclaredMethod
(
"setDataEnabled"
,
boolean
.
class
);
setDataEnabledMethod
.
invoke
(
tm
,
enabled
);
return
true
;
}
catch
(
Exception
e
)
{
Log
.
e
(
"NetworkUtils"
,
"setMobileDataEnabled: "
,
e
);
}
return
false
;
}
/**
* Send sms silently.
* <p>Must hold {@code <uses-permission android:name="android.permission.SEND_SMS" />}</p>
*
* @param phoneNumber The phone number.
* @param content The content.
*/
@RequiresPermission
(
SEND_SMS
)
public
static
void
sendSmsSilent
(
final
String
phoneNumber
,
final
String
content
)
{
if
(
TextUtils
.
isEmpty
(
content
))
return
;
PendingIntent
sentIntent
=
PendingIntent
.
getBroadcast
(
Utils
.
getApp
(),
0
,
new
Intent
(
"send"
),
0
);
SmsManager
smsManager
=
SmsManager
.
getDefault
();
if
(
content
.
length
()
>=
70
)
{
List
<
String
>
ms
=
smsManager
.
divideMessage
(
content
);
for
(
String
str
:
ms
)
{
smsManager
.
sendTextMessage
(
phoneNumber
,
null
,
str
,
sentIntent
,
null
);
}
}
else
{
smsManager
.
sendTextMessage
(
phoneNumber
,
null
,
content
,
sentIntent
,
null
);
}
}
}
\ No newline at end of file
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