mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-03 12:42:10 +08:00
182 lines
6.4 KiB
Java
182 lines
6.4 KiB
Java
package com.lxmusicmobile.utils;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.net.Uri;
|
|
import android.net.wifi.WifiInfo;
|
|
import android.net.wifi.WifiManager;
|
|
import android.os.Build;
|
|
import android.util.Log;
|
|
import android.view.WindowManager;
|
|
|
|
import androidx.core.content.FileProvider;
|
|
|
|
import com.facebook.react.bridge.Promise;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
import com.facebook.react.bridge.WritableArray;
|
|
import com.facebook.react.bridge.WritableNativeArray;
|
|
|
|
import java.io.File;
|
|
|
|
public class UtilsModule extends ReactContextBaseJavaModule {
|
|
private final ReactApplicationContext reactContext;
|
|
|
|
UtilsModule(ReactApplicationContext reactContext) {
|
|
super(reactContext);
|
|
this.reactContext = reactContext;
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "UtilsModule";
|
|
}
|
|
|
|
@ReactMethod
|
|
public void exitApp() {
|
|
// https://github.com/wumke/react-native-exit-app/blob/master/android/src/main/java/com/github/wumke/RNExitApp/RNExitAppModule.java
|
|
android.os.Process.killProcess(android.os.Process.myPid());
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getSupportedAbis(Promise promise) {
|
|
// https://github.com/react-native-device-info/react-native-device-info/blob/ff8f672cb08fa39a887567d6e23e2f08778e8340/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java#L877
|
|
WritableArray array = new WritableNativeArray();
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
for (String abi : Build.SUPPORTED_ABIS) {
|
|
array.pushString(abi);
|
|
}
|
|
} else {
|
|
array.pushString(Build.CPU_ABI);
|
|
}
|
|
promise.resolve(array);
|
|
}
|
|
|
|
@ReactMethod
|
|
public void installApk(String filePath, String fileProviderAuthority, Promise promise) {
|
|
// https://github.com/mikehardy/react-native-update-apk/blob/master/android/src/main/java/net/mikehardy/rnupdateapk/RNUpdateAPK.java
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
Log.e("Utils", "installApk: file doe snot exist '" + filePath + "'");
|
|
// FIXME this should take a promise and fail it
|
|
promise.reject("Utils", "installApk: file doe snot exist '" + filePath + "'");
|
|
return;
|
|
}
|
|
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
// API24 and up has a package installer that can handle FileProvider content:// URIs
|
|
Uri contentUri;
|
|
try {
|
|
contentUri = FileProvider.getUriForFile(getReactApplicationContext(), fileProviderAuthority, file);
|
|
} catch (Exception e) {
|
|
// FIXME should be a Promise.reject really
|
|
Log.e("Utils", "installApk exception with authority name '" + fileProviderAuthority + "'", e);
|
|
promise.reject("Utils", "installApk exception with authority name '" + fileProviderAuthority + "'");
|
|
return;
|
|
// throw e;
|
|
}
|
|
Intent installApp = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
installApp.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
installApp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
installApp.setData(contentUri);
|
|
installApp.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, reactContext.getApplicationInfo().packageName);
|
|
reactContext.startActivity(installApp);
|
|
promise.resolve(null);
|
|
} else {
|
|
// Old APIs do not handle content:// URIs, so use an old file:// style
|
|
String cmd = "chmod 777 " + file;
|
|
try {
|
|
Runtime.getRuntime().exec(cmd);
|
|
} catch (Exception e) {
|
|
// e.printStackTrace();
|
|
Log.e("Utils", "installApk exception : " + e.getMessage(), e);
|
|
promise.reject("Utils", e.getMessage());
|
|
}
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.setDataAndType(Uri.parse("file://" + file), "application/vnd.android.package-archive");
|
|
reactContext.startActivity(intent);
|
|
promise.resolve(null);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void screenkeepAwake() {
|
|
// https://github.com/corbt/react-native-keep-awake/blob/master/android/src/main/java/com/corbt/keepawake/KCKeepAwake.java
|
|
final Activity activity = getCurrentActivity();
|
|
|
|
if (activity != null) {
|
|
activity.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void screenUnkeepAwake() {
|
|
// https://github.com/corbt/react-native-keep-awake/blob/master/android/src/main/java/com/corbt/keepawake/KCKeepAwake.java
|
|
final Activity activity = getCurrentActivity();
|
|
|
|
if (activity != null) {
|
|
activity.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
Gets the device's WiFi interface IP address
|
|
@return device's WiFi IP if connected to WiFi, else '0.0.0.0'
|
|
*/
|
|
@ReactMethod
|
|
public void getWIFIIPV4Address(final Promise promise) throws Exception {
|
|
// https://github.com/pusherman/react-native-network-info/blob/master/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java
|
|
WifiManager wifi = (WifiManager) reactContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
new Thread(new Runnable() {
|
|
public void run() {
|
|
try {
|
|
WifiInfo info = wifi.getConnectionInfo();
|
|
int ipAddress = info.getIpAddress();
|
|
String stringip = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
|
|
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
|
|
promise.resolve(stringip);
|
|
}catch (Exception e) {
|
|
promise.resolve(null);
|
|
}
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
// https://stackoverflow.com/a/26117646
|
|
@ReactMethod
|
|
public void getDeviceName(final Promise promise) {
|
|
String manufacturer = Build.MANUFACTURER;
|
|
String model = Build.MODEL;
|
|
if (model.startsWith(manufacturer)) {
|
|
promise.resolve(capitalize(model));
|
|
} else {
|
|
promise.resolve(capitalize(manufacturer) + " " + model);
|
|
}
|
|
}
|
|
private String capitalize(String s) {
|
|
if (s == null || s.length() == 0) {
|
|
return "";
|
|
}
|
|
char first = s.charAt(0);
|
|
if (Character.isUpperCase(first)) {
|
|
return s;
|
|
} else {
|
|
return Character.toUpperCase(first) + s.substring(1);
|
|
}
|
|
}
|
|
}
|
|
|