383 lines
14 KiB
Java
383 lines
14 KiB
Java
package com.avapp;
|
|
|
|
import static android.app.Activity.RESULT_CANCELED;
|
|
|
|
import static com.avapp.MainApplication.isAlfredEnabledFromFirebase;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.location.LocationManager;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.core.content.FileProvider;
|
|
|
|
import com.facebook.react.bridge.ActivityEventListener;
|
|
import com.facebook.react.bridge.BaseActivityEventListener;
|
|
import com.facebook.react.bridge.Promise;
|
|
import com.facebook.react.bridge.Callback;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
import com.facebook.react.bridge.ReadableMap;
|
|
import com.facebook.react.uimanager.UIManagerModule;
|
|
import com.navi.alfred.AlfredManager;
|
|
import com.navi.pulse.PulseManager;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
import android.os.Environment;
|
|
import android.os.Parcelable;
|
|
import android.util.Base64;
|
|
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
import android.net.Uri;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
public class DeviceUtilsModule extends ReactContextBaseJavaModule {
|
|
private ReactApplicationContext RNContext;
|
|
|
|
private File imageFile;
|
|
|
|
private int WHATSAPP_SHARE_REQUEST_CODE = 12345;
|
|
|
|
public DeviceUtilsModule(@Nullable ReactApplicationContext reactContext){
|
|
super(reactContext);
|
|
RNContext = reactContext;
|
|
reactContext.addActivityEventListener(mActivityEventListener);
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "DeviceUtilsModule";
|
|
}
|
|
|
|
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
|
|
@Override
|
|
public void onActivityResult(Activity activity, int i, int i1, @Nullable Intent intent) {
|
|
if(i1 != RESULT_CANCELED) {
|
|
if (i == WHATSAPP_SHARE_REQUEST_CODE && (imageFile!=null)) {
|
|
new File(Uri.fromFile(imageFile).getPath()).delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onNewIntent(Intent intent) {
|
|
|
|
}
|
|
};
|
|
|
|
|
|
@ReactMethod
|
|
public void isLocationEnabled(Promise promise) {
|
|
try {
|
|
LocationManager locationManager = (LocationManager) RNContext.getApplicationContext()
|
|
.getSystemService(Context.LOCATION_SERVICE);
|
|
Boolean isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
|
promise.resolve(isEnabled);
|
|
} catch (Exception err) {
|
|
promise.reject(err);
|
|
}
|
|
}
|
|
|
|
public String getAppIcon(String packageName) {
|
|
try {
|
|
Context context = RNContext.getApplicationContext();
|
|
PackageManager pm = context.getPackageManager();
|
|
ApplicationInfo appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
|
|
|
|
Uri imageUri = Uri.parse("android.resource://" + appInfo.packageName + "/drawable/" + appInfo.icon);
|
|
return imageUri.toString();
|
|
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getAllInstalledApp(Promise promise) {
|
|
try {
|
|
PackageManager packageManager = RNContext.getPackageManager();
|
|
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
|
|
JSONArray jsonArray = new JSONArray();
|
|
for (PackageInfo packageInfo : installedPackages) {
|
|
JSONObject mainObject = new JSONObject();
|
|
JSONObject appDetails = new JSONObject();
|
|
try {
|
|
ApplicationInfo appInfo = packageInfo.applicationInfo;
|
|
String applicationName = (String) packageManager.getApplicationLabel(appInfo);
|
|
appDetails.put("appName", appInfo.processName);
|
|
appDetails.put("firstInstallTime", packageInfo.firstInstallTime);
|
|
appDetails.put("lastUpdateTime", packageInfo.lastUpdateTime);
|
|
appDetails.put("applicationName", applicationName != null ? applicationName : "(unknown)");
|
|
appDetails.put("applicationIcon", getAppIcon(packageInfo.packageName));
|
|
mainObject.put("packageName", packageInfo.packageName);
|
|
mainObject.put("appDetails", appDetails);
|
|
|
|
jsonArray.put(mainObject);
|
|
} catch (Exception e) {
|
|
Log.e("getAllInstalledApp", "Error processing package: " + packageInfo.packageName, e);
|
|
}
|
|
}
|
|
promise.resolve(jsonArray.toString());
|
|
} catch (Exception err) {
|
|
promise.reject(err);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getBuildFlavour(Promise promise) {
|
|
try {
|
|
String buildFlavour = BuildConfig.FLAVOR;
|
|
promise.resolve(buildFlavour);
|
|
} catch (Exception err) {
|
|
promise.reject(err);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getBuildInfo(Promise promise) {
|
|
try {
|
|
JSONObject mainObject = new JSONObject();
|
|
mainObject.put("versionCode", BuildConfig.VERSION_CODE);
|
|
mainObject.put("versionName", BuildConfig.VERSION_NAME);
|
|
promise.resolve(mainObject.toString());
|
|
} catch (Exception err) {
|
|
promise.reject(err);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getBlacklistedApps(ReadableMap blacklistedAppsMap, Promise promise) {
|
|
try {
|
|
PackageManager packageManager = RNContext.getPackageManager();
|
|
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
for (PackageInfo packageInfo : installedPackages) {
|
|
// Skip system apps
|
|
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
|
|
continue;
|
|
}
|
|
|
|
String packageName = packageInfo.packageName;
|
|
if (blacklistedAppsMap.hasKey(packageName)) {
|
|
JSONObject appDetails = new JSONObject();
|
|
try {
|
|
String applicationName = (String) packageManager.getApplicationLabel(packageInfo.applicationInfo);
|
|
appDetails.put("packageName", packageName);
|
|
appDetails.put("applicationName", applicationName != null ? applicationName : "(unknown)");
|
|
appDetails.put("applicationIcon", getAppIcon(packageName));
|
|
|
|
jsonArray.put(appDetails);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
promise.reject("ERROR", e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
promise.resolve(jsonArray.toString());
|
|
} catch (Exception err) {
|
|
err.printStackTrace();
|
|
promise.reject("ERROR", err.getMessage());
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void sendBottomSheetOpenSignal(Boolean isBottomSheetOpen) {
|
|
}
|
|
|
|
@ReactMethod
|
|
public void setUserId(String userId) {
|
|
if (isAlfredEnabledFromFirebase) {
|
|
AlfredManager.INSTANCE.getConfig$navi_alfred_release().setUserId(userId);
|
|
}
|
|
PulseManager.INSTANCE.setUserId(userId);
|
|
}
|
|
|
|
@ReactMethod
|
|
public void handleSWWEvent(String message, String stack, String name) { //<String, Object>
|
|
if (isAlfredEnabledFromFirebase) {
|
|
HashMap<String, String> properties = new HashMap<>();
|
|
properties.put("message", message);
|
|
properties.put("stack", stack);
|
|
properties.put("name", name);
|
|
AlfredManager.INSTANCE.handleSWWEvent(properties);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void handleCrash(String message, String screenName) { //<String, Object>
|
|
if (isAlfredEnabledFromFirebase) {
|
|
HashMap<String, String> properties = new HashMap<>();
|
|
properties.put("message", message);
|
|
properties.put("screenName", screenName);
|
|
AlfredManager.INSTANCE.handleCrashEvent(properties);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void setCodePushVersion(String codePushVersion) {
|
|
if (isAlfredEnabledFromFirebase && codePushVersion != null) {
|
|
AlfredManager.INSTANCE.getConfig$navi_alfred_release().setCodePushVersion(codePushVersion);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void setPhoneNumber(String phoneNumber) {
|
|
if (isAlfredEnabledFromFirebase && phoneNumber != null) {
|
|
AlfredManager.INSTANCE.getConfig$navi_alfred_release().setPhoneNumber(phoneNumber);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void setEmailId(String emailId) {
|
|
if (isAlfredEnabledFromFirebase && emailId != null) {
|
|
AlfredManager.INSTANCE.getConfig$navi_alfred_release().setAgentEmailId(emailId);
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void setBottomSheetView(Integer refID) {
|
|
if (isAlfredEnabledFromFirebase && refID != null) {
|
|
UIManagerModule uiManagerModule = RNContext.getNativeModule(UIManagerModule.class);
|
|
if (uiManagerModule != null) {
|
|
try {
|
|
uiManagerModule.addUIBlock(nativeViewHierarchyManager -> {
|
|
View view = nativeViewHierarchyManager.resolveView(refID);
|
|
AlfredManager.INSTANCE.setBottomSheetView(view);
|
|
});
|
|
} catch (Exception error) {
|
|
Log.d("Alfred", "setBottomSheetView error:" + error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@ReactMethod
|
|
public void clearBottomSheet() {
|
|
if (isAlfredEnabledFromFirebase) {
|
|
AlfredManager.INSTANCE.clearBottomSheetView();
|
|
}
|
|
}
|
|
|
|
private static File convertBase64ToFile(Context context, String base64Data, String format, String fileName) {
|
|
try {
|
|
byte[] decodedBytes = Base64.decode(base64Data, Base64.DEFAULT);
|
|
File outputDir = context.getCacheDir();
|
|
// Using app-specific directory in external storage
|
|
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName + format);
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
fos.write(decodedBytes);
|
|
fos.flush();
|
|
fos.close();
|
|
return file;
|
|
} catch (IOException e) {
|
|
Log.e("ShareUtils", "Failed to convert Base64 to file: " + e.getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ArrayList<String> isWhatsAppInstalled() {
|
|
PackageManager packageManager = RNContext.getPackageManager();
|
|
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
|
|
ArrayList<String> appsInstalled = new ArrayList<String>();
|
|
|
|
for (PackageInfo packageInfo : packages) {
|
|
String packageName = packageInfo.packageName;
|
|
if (packageName.equals("com.whatsapp") || packageName.equals("com.whatsapp.w4b")) {
|
|
appsInstalled.add(packageName);
|
|
}
|
|
}
|
|
return appsInstalled;
|
|
}
|
|
|
|
public Intent getWhatsappShareIntent(String message, String imageUrl, String mimeType, String packageName,
|
|
String format, String fileName) {
|
|
Intent sendIntent = new Intent();
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
|
|
|
|
if (imageUrl.equals("")) {
|
|
sendIntent.setType("text/plain");
|
|
sendIntent.setPackage(packageName);
|
|
} else {
|
|
sendIntent.setType(mimeType);
|
|
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
Context context = getReactApplicationContext();
|
|
imageFile = convertBase64ToFile(context, imageUrl, format, fileName);
|
|
Uri fileUri = FileProvider.getUriForFile(getReactApplicationContext(),
|
|
BuildConfig.APPLICATION_ID + ".provider", new File(
|
|
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
|
|
imageFile.getName()));
|
|
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
|
|
sendIntent.setPackage(packageName);
|
|
}
|
|
|
|
return sendIntent;
|
|
}
|
|
|
|
@ReactMethod
|
|
public void sendContentToWhatsapp(String message, String fileUri, String mimeType, String format, String fileName,
|
|
Promise promise) {
|
|
try {
|
|
ArrayList<String> appsInstalled = isWhatsAppInstalled();
|
|
int numberOfAppsInstalled = appsInstalled.size();
|
|
|
|
if (numberOfAppsInstalled == 0) {
|
|
promise.reject("errorCode", "1");
|
|
return;
|
|
} else if (numberOfAppsInstalled == 1) {
|
|
String packageName = appsInstalled.get(0);
|
|
Intent sendIntent = getWhatsappShareIntent(message, fileUri, mimeType, packageName, format, fileName);
|
|
if (getCurrentActivity() != null) {
|
|
getCurrentActivity().startActivityForResult(sendIntent, WHATSAPP_SHARE_REQUEST_CODE);
|
|
}
|
|
promise.resolve(true);
|
|
return;
|
|
} else {
|
|
// Handle multiple WhatsApp installations
|
|
ArrayList<Intent> appIntents = new ArrayList<>();
|
|
|
|
for (String packageName : appsInstalled) {
|
|
Intent sendIntent = getWhatsappShareIntent(message, fileUri, mimeType, packageName, format,
|
|
fileName);
|
|
appIntents.add(sendIntent);
|
|
}
|
|
|
|
Intent defaultIntent = new Intent(android.content.Intent.ACTION_SEND);
|
|
defaultIntent.setType("text/plain");
|
|
defaultIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Sharing to WhatsApp");
|
|
|
|
Intent chooserIntent = Intent.createChooser(defaultIntent, "Share via");
|
|
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
|
|
appIntents.toArray(new Parcelable[appIntents.size()]));
|
|
|
|
if (getCurrentActivity() != null) {
|
|
getCurrentActivity().startActivityForResult(chooserIntent, WHATSAPP_SHARE_REQUEST_CODE);
|
|
}
|
|
promise.resolve(true);
|
|
return;
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
promise.reject("errorCode", "2");
|
|
}
|
|
}
|
|
|
|
|
|
}
|