What Sapp Tools
What Sapp Tools
WhatsAppTools;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;
import android.util.Log;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.MimeTypeMap;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.util.NougatUtil;
import com.google.appinventor.components.runtime.util.YailList;
import com.google.appinventor.components.runtime.Form;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
@UsesPermissions(permissionNames = "android.permission.READ_CONTACTS,
android.permission.CALL_PHONE")
@SimpleObject(external = true)
public class WhatsAppTools extends AndroidNonvisibleComponent {
private ComponentContainer container;
private Context context;
private Form form;
private boolean w4bEnable;
private String packageType = "com.whatsapp";
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN,
defaultValue = "False")
@SimpleProperty(userVisible = true)
public void WhatsAppBusiness(boolean enable) {
w4bEnable = enable;
if (enable) {
packageType = "com.whatsapp.w4b";
} else {
packageType = "com.whatsapp";
}
}
@SimpleFunction(description = "")
public void SendMessageDirect(String phoneNumber, String message) {
if (!phoneNumber.isEmpty()) {
try {
String url = "https://api.whatsapp.com/send?phone=" + phoneNumber +
"&text=" + message;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(packageType);
intent.setData(Uri.parse(url));
context.startActivity(intent);
} catch (Exception e) {
Error(e.getMessage());
}
}
}
@SimpleFunction(description = "")
public void SendMessage(String message) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage(packageType);
intent.putExtra(Intent.EXTRA_TEXT, message);
context.startActivity(intent);
} catch (Exception e){
Error(e.getMessage());
}
}
@SimpleFunction(description = "")
public boolean IsAvailable() {
Intent intent =
context.getPackageManager().getLaunchIntentForPackage(packageType);
boolean result = (intent != null) ? true : false ;
return result;
}
@SimpleFunction(description = "")
public void SendFile(String caption, String filePath) {
try {
if (!filePath.startsWith("file://")) {
filePath = "file://" + filePath;
}
Uri uri = Uri.parse(filePath);
File allFile = new File(uri.getPath());
if (allFile.isFile()) {
String fileExtension = filePath.substring(filePath.lastIndexOf(".")
+1).toLowerCase();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);
if (type == null) {
type = "application/octet-stream";
}
Uri shareableUri = NougatUtil.getPackageUri(form, allFile);
@SimpleFunction(description = "")
public void SendFiles(String caption, YailList listFilePath) {
ArrayList<Uri> fileUris = new ArrayList<Uri>();
try {
for(Object item : listFilePath.toArray()) {
String fileItem = item.toString();
if (!fileItem.startsWith("file://")) {
fileItem = "file://" + fileItem;
}
Uri uri = Uri.parse(fileItem);
File allFile = new File(uri.getPath());
if (allFile.isFile()) {
Uri shareableUri = NougatUtil.getPackageUri(form, allFile);
fileUris.add(shareableUri);
}
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_TEXT, caption);
intent.setType("text/plain");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
fileUris);
intent.setType("*/*");
intent.setPackage(packageType);
context.startActivity(intent);
} catch (Exception e) {
Error(e.getMessage());
}
}
@SimpleFunction(description = "")
public void SendFileDirect(String phoneNumber, String caption, String filePath)
{
if (!phoneNumber.isEmpty()) {
phoneNumber = phoneNumber.replace("+", "").replace("-", "").replace("
", "");
try {
if (!filePath.startsWith("file://")) {
filePath = "file://" + filePath;
}
Uri uri = Uri.parse(filePath);
File allFile = new File(uri.getPath());
if (allFile.isFile()) {
String fileExtension =
filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);
if (type == null) {
type = "application/octet-stream";
}
Uri shareableUri = NougatUtil.getPackageUri(form, allFile);
@SimpleFunction(description = "")
public void SendFilesDirect(String phoneNumber, String caption, YailList
listFilePath) {
if (!phoneNumber.isEmpty()) {
ArrayList<Uri> fileUris = new ArrayList<Uri>();
phoneNumber = phoneNumber.replace("+", "").replace("-", "").replace("
", "");
try {
for(Object item : listFilePath.toArray()) {
String fileItem = item.toString();
if (!fileItem.startsWith("file://")) {
fileItem = "file://" + fileItem;
}
Uri uri = Uri.parse(fileItem);
File allFile = new File(uri.getPath());
if (allFile.isFile()) {
Uri shareableUri = NougatUtil.getPackageUri(form, allFile);
fileUris.add(shareableUri);
}
}
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_TEXT, caption);
intent.setType("text/plain");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("*/*");
intent.setPackage(packageType);
intent.putExtra("jid", phoneNumber + "@s.whatsapp.net");
form.startActivity(intent);
} catch (Exception e) {
Error(e.getMessage());
}
}
}
@SimpleFunction(description = "")
public void MakeVoiceCall(String contactName) {
MakeCall(contactName,
"vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
}
@SimpleFunction(description = "")
public void MakeVideoCall(String contactName) {
MakeCall(contactName,
"vnd.android.cursor.item/vnd.com.whatsapp.video.call");
}