Skip to content

Commit afc5a08

Browse files
committed
see 03/10 log
1 parent 402e734 commit afc5a08

File tree

3 files changed

+102
-25
lines changed

3 files changed

+102
-25
lines changed

utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static void installApp(final Activity activity,
110110
* @return {@code true}: success<br>{@code false}: fail
111111
*/
112112
public static boolean installAppSilent(final String filePath) {
113-
return installAppSilent(getFileByPath(filePath));
113+
return installAppSilent(getFileByPath(filePath), null);
114114
}
115115

116116
/**
@@ -122,19 +122,47 @@ public static boolean installAppSilent(final String filePath) {
122122
* @return {@code true}: success<br>{@code false}: fail
123123
*/
124124
public static boolean installAppSilent(final File file) {
125+
return installAppSilent(file, null);
126+
}
127+
128+
129+
/**
130+
* Install the app silently.
131+
* <p>Without root permission must hold
132+
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
133+
*
134+
* @param filePath The path of file.
135+
* @param params The params of installation.
136+
* @return {@code true}: success<br>{@code false}: fail
137+
*/
138+
public static boolean installAppSilent(final String filePath, final String params) {
139+
return installAppSilent(getFileByPath(filePath), null);
140+
}
141+
142+
/**
143+
* Install the app silently.
144+
* <p>Without root permission must hold
145+
* {@code <uses-permission android:name="android.permission.INSTALL_PACKAGES" />}</p>
146+
*
147+
* @param file The file.
148+
* @param params The params of installation.
149+
* @return {@code true}: success<br>{@code false}: fail
150+
*/
151+
public static boolean installAppSilent(final File file, final String params) {
125152
if (!isFileExists(file)) return false;
126153
boolean isRoot = isDeviceRooted();
127154
String filePath = file.getAbsolutePath();
128-
String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install " + filePath;
155+
String command = "LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm install " +
156+
(params == null ? "" : params + " ")
157+
+ filePath;
129158
ShellUtils.CommandResult commandResult = ShellUtils.execCmd(command, isRoot);
130159
if (commandResult.successMsg != null
131160
&& commandResult.successMsg.toLowerCase().contains("success")) {
132161
return true;
133162
} else {
134-
command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib64 pm install " + filePath;
135-
commandResult = ShellUtils.execCmd(command, isRoot, true);
136-
return commandResult.successMsg != null
137-
&& commandResult.successMsg.toLowerCase().contains("success");
163+
Log.e("AppUtils", "installAppSilent successMsg: " + commandResult.successMsg +
164+
", errorMsg: " + commandResult.errorMsg);
165+
return false;
138166
}
139167
}
140168

@@ -160,7 +188,10 @@ public static void uninstallApp(final Activity activity,
160188
final String packageName,
161189
final int requestCode) {
162190
if (isSpace(packageName)) return;
163-
activity.startActivityForResult(IntentUtils.getUninstallAppIntent(packageName), requestCode);
191+
activity.startActivityForResult(
192+
IntentUtils.getUninstallAppIntent(packageName),
193+
requestCode
194+
);
164195
}
165196

166197
/**
@@ -187,20 +218,17 @@ public static boolean uninstallAppSilent(final String packageName) {
187218
public static boolean uninstallAppSilent(final String packageName, final boolean isKeepData) {
188219
if (isSpace(packageName)) return false;
189220
boolean isRoot = isDeviceRooted();
190-
String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm uninstall "
221+
String command = "LD_LIBRARY_PATH=/vendor/lib*:/system/lib* pm uninstall "
191222
+ (isKeepData ? "-k " : "")
192223
+ packageName;
193224
ShellUtils.CommandResult commandResult = ShellUtils.execCmd(command, isRoot, true);
194225
if (commandResult.successMsg != null
195226
&& commandResult.successMsg.toLowerCase().contains("success")) {
196227
return true;
197228
} else {
198-
command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib64 pm uninstall "
199-
+ (isKeepData ? "-k " : "")
200-
+ packageName;
201-
commandResult = ShellUtils.execCmd(command, isRoot, true);
202-
return commandResult.successMsg != null
203-
&& commandResult.successMsg.toLowerCase().contains("success");
229+
Log.e("AppUtils", "uninstallAppSilent successMsg: " + commandResult.successMsg +
230+
", errorMsg: " + commandResult.errorMsg);
231+
return false;
204232
}
205233
}
206234

utilcode/src/main/java/com/blankj/utilcode/util/DeviceUtils.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import android.support.annotation.RequiresPermission;
1212

1313
import java.io.File;
14+
import java.net.InetAddress;
1415
import java.net.NetworkInterface;
15-
import java.util.Collections;
16-
import java.util.List;
16+
import java.net.SocketException;
17+
import java.util.Enumeration;
1718

1819
import static android.Manifest.permission.ACCESS_WIFI_STATE;
1920
import static android.Manifest.permission.INTERNET;
@@ -98,6 +99,10 @@ public static String getMacAddress() {
9899
if (!"02:00:00:00:00:00".equals(macAddress)) {
99100
return macAddress;
100101
}
102+
macAddress = getMacAddressByInetAddress();
103+
if (!"02:00:00:00:00:00".equals(macAddress)) {
104+
return macAddress;
105+
}
101106
macAddress = getMacAddressByFile();
102107
if (!"02:00:00:00:00:00".equals(macAddress)) {
103108
return macAddress;
@@ -122,16 +127,39 @@ private static String getMacAddressByWifiInfo() {
122127

123128
private static String getMacAddressByNetworkInterface() {
124129
try {
125-
List<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
126-
for (NetworkInterface ni : nis) {
127-
if (!ni.getName().equalsIgnoreCase("wlan0")) continue;
130+
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
131+
while (nis.hasMoreElements()) {
132+
NetworkInterface ni = nis.nextElement();
133+
if (ni == null || !ni.getName().equalsIgnoreCase("wlan0")) continue;
128134
byte[] macBytes = ni.getHardwareAddress();
129135
if (macBytes != null && macBytes.length > 0) {
130-
StringBuilder res1 = new StringBuilder();
136+
StringBuilder sb = new StringBuilder();
131137
for (byte b : macBytes) {
132-
res1.append(String.format("%02x:", b));
138+
sb.append(String.format("%02x:", b));
139+
}
140+
return sb.substring(0, sb.length() - 1);
141+
}
142+
}
143+
} catch (Exception e) {
144+
e.printStackTrace();
145+
}
146+
return "02:00:00:00:00:00";
147+
}
148+
149+
private static String getMacAddressByInetAddress() {
150+
try {
151+
InetAddress inetAddress = getInetAddress();
152+
if (inetAddress != null) {
153+
NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);
154+
if (ni != null) {
155+
byte[] macBytes = ni.getHardwareAddress();
156+
if (macBytes != null && macBytes.length > 0) {
157+
StringBuilder sb = new StringBuilder();
158+
for (byte b : macBytes) {
159+
sb.append(String.format("%02x:", b));
160+
}
161+
return sb.substring(0, sb.length() - 1);
133162
}
134-
return res1.deleteCharAt(res1.length() - 1).toString();
135163
}
136164
}
137165
} catch (Exception e) {
@@ -140,6 +168,28 @@ private static String getMacAddressByNetworkInterface() {
140168
return "02:00:00:00:00:00";
141169
}
142170

171+
private static InetAddress getInetAddress() {
172+
try {
173+
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
174+
while (nis.hasMoreElements()) {
175+
NetworkInterface ni = nis.nextElement();
176+
// To prevent phone of xiaomi return "10.0.2.15"
177+
if (!ni.isUp()) continue;
178+
Enumeration<InetAddress> addresses = ni.getInetAddresses();
179+
while (addresses.hasMoreElements()) {
180+
InetAddress inetAddress = addresses.nextElement();
181+
if (!inetAddress.isLoopbackAddress()) {
182+
String hostAddress = inetAddress.getHostAddress();
183+
if (hostAddress.indexOf(':') < 0) return inetAddress;
184+
}
185+
}
186+
}
187+
} catch (SocketException e) {
188+
e.printStackTrace();
189+
}
190+
return null;
191+
}
192+
143193
private static String getMacAddressByFile() {
144194
ShellUtils.CommandResult result = ShellUtils.execCmd("getprop wifi.interface", false);
145195
if (result.result == 0) {
@@ -162,7 +212,6 @@ private static String getMacAddressByFile() {
162212
*
163213
* @return the manufacturer of the product/hardware
164214
*/
165-
166215
public static String getManufacturer() {
167216
return Build.MANUFACTURER;
168217
}

utilcode/src/main/java/com/blankj/utilcode/util/NetworkUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ private static NetworkInfo getActiveNetworkInfo() {
349349
@RequiresPermission(INTERNET)
350350
public static String getIPAddress(final boolean useIPv4) {
351351
try {
352-
for (Enumeration<NetworkInterface> nis =
353-
NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
352+
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
353+
while (nis.hasMoreElements()) {
354354
NetworkInterface ni = nis.nextElement();
355355
// To prevent phone of xiaomi return "10.0.2.15"
356356
if (!ni.isUp()) continue;

0 commit comments

Comments
 (0)