|
|
@@ -0,0 +1,216 @@ |
|
|
|
package com.cyhd.henhoandroid.plugin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.android.gms.common.ConnectionResult;
|
|
|
|
import com.google.android.gms.common.GoogleApiAvailability;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
|
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
|
|
|
import io.flutter.plugin.common.MethodChannel.Result;
|
|
|
|
import io.flutter.plugin.common.PluginRegistry.Registrar;
|
|
|
|
import io.flutter.view.FlutterView;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ScreenshotShareImagePlugin
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public class ScreenShot implements MethodCallHandler {
|
|
|
|
|
|
|
|
private Registrar registrar;
|
|
|
|
private FlutterView flutterView;
|
|
|
|
private MethodChannel channel;
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
public static double pi = 3.1415926535897932384626;
|
|
|
|
public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
|
|
|
|
|
|
|
|
|
|
|
|
public ScreenShot(Context context, Registrar registrar, FlutterView flutterView, MethodChannel channel) {
|
|
|
|
this.context = context;
|
|
|
|
this.registrar = registrar;
|
|
|
|
this.flutterView = flutterView;
|
|
|
|
this.channel = channel;
|
|
|
|
this.channel.setMethodCallHandler(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin registration.
|
|
|
|
*/
|
|
|
|
public static void registerWith(Context context, Registrar registrar) {
|
|
|
|
|
|
|
|
final MethodChannel channel = new MethodChannel(registrar.messenger(), "make.photo.screen.hibok");
|
|
|
|
channel.setMethodCallHandler(new ScreenShot(context, registrar, registrar.view(), channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMethodCall(MethodCall call, Result result) {
|
|
|
|
if (call.method.equals("makePhotoScreen")) {
|
|
|
|
takeScreenshot(result);
|
|
|
|
} else if (call.method.equals("getListForMap")) {
|
|
|
|
getOtherMapApp(result);
|
|
|
|
} else if (call.method.equals("openMapForOth")) {
|
|
|
|
|
|
|
|
String mapType = call.argument("mapType");
|
|
|
|
ArrayList<Double> lanLng = call.argument("latLng");
|
|
|
|
String address = call.argument("address");
|
|
|
|
|
|
|
|
Log.d("1", "地址参数 " + address);
|
|
|
|
openMapForOth(mapType, lanLng, address);
|
|
|
|
} else if(call.method.equals("isSupportGoogle")){
|
|
|
|
result.success(onCheckGooglePlayServices());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.notImplemented();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getOtherMapApp(Result result) {
|
|
|
|
|
|
|
|
|
|
|
|
//获取packagemanager
|
|
|
|
final PackageManager packageManager = context.getPackageManager();
|
|
|
|
//获取所有已安装程序的包信息
|
|
|
|
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
|
|
|
|
//用于存储所有已安装程序的包名
|
|
|
|
List<String> packageNames = new ArrayList<String>();
|
|
|
|
//从pinfo中将包名字逐一取出,压入pName list中
|
|
|
|
if (packageInfos != null) {
|
|
|
|
for (int i = 0; i < packageInfos.size(); i++) {
|
|
|
|
String packName = packageInfos.get(i).packageName;
|
|
|
|
packageNames.add(packName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList mapList = new ArrayList();
|
|
|
|
if (packageNames.contains("com.baidu.BaiduMap")) {
|
|
|
|
mapList.add("baidu");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packageNames.contains("com.autonavi.minimap")) {
|
|
|
|
mapList.add("minimap");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packageNames.contains("com.google.android.apps.maps")) {
|
|
|
|
mapList.add("google");
|
|
|
|
}
|
|
|
|
|
|
|
|
result.success(mapList);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void openMapForOth(String mapType, ArrayList latLng, String address) {
|
|
|
|
|
|
|
|
Log.d("1", mapType);
|
|
|
|
if (mapType.equals("minimap")) {
|
|
|
|
|
|
|
|
Log.d("1", "打开高德地图 " + address);
|
|
|
|
String uri = String.format("androidamap://viewReGeo?sourceApplication=amap&lat=%f&lon=%f&dev=1", latLng.get(0), latLng.get(1));
|
|
|
|
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
|
|
intent.setPackage("com.autonavi.minimap");
|
|
|
|
intent.setData(Uri.parse(uri));
|
|
|
|
|
|
|
|
context.startActivity(intent);
|
|
|
|
} else if (mapType.equals("baidu")) {
|
|
|
|
Log.d("1", "打开百度地图");
|
|
|
|
double[] location = gcj02_To_Bd09((double) latLng.get(0), (double) latLng.get(1));
|
|
|
|
String uri = String.format("baidumap://map/geocoder?location=%f,%f&src=com.cyhd.henhoandroid",
|
|
|
|
location[0], location[1]);
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setData(Uri.parse(uri));
|
|
|
|
context.startActivity(intent);
|
|
|
|
} else if (mapType.equals("google")) {
|
|
|
|
Log.d("1", "打开谷歌地图" + address);
|
|
|
|
String gmmIntentUri = String.format("https://ditu.google.com/maps/dir/?q=%f,%f&destination=%s",
|
|
|
|
latLng.get(0), latLng.get(1), address);
|
|
|
|
|
|
|
|
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(gmmIntentUri));
|
|
|
|
mapIntent.setPackage("com.google.android.apps.maps");
|
|
|
|
context.startActivity(mapIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
|
|
|
|
*
|
|
|
|
* @param lat
|
|
|
|
* @param lon
|
|
|
|
*/
|
|
|
|
private double[] gcj02_To_Bd09(double lat, double lon) {
|
|
|
|
double x = lon, y = lat;
|
|
|
|
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
|
|
|
|
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
|
|
|
|
double tempLon = z * Math.cos(theta) + 0.0065;
|
|
|
|
double tempLat = z * Math.sin(theta) + 0.006;
|
|
|
|
double[] gps = {tempLat, tempLon};
|
|
|
|
return gps;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void takeScreenshot(Result result) {
|
|
|
|
try {
|
|
|
|
Bitmap bitmap = flutterView.getBitmap();
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
|
|
|
|
result.success(isSuccess ? baos.toByteArray() : "");
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查 Google Play 服务
|
|
|
|
*/
|
|
|
|
private boolean onCheckGooglePlayServices() {
|
|
|
|
// 验证是否已在此设备上安装并启用Google Play服务,以及此设备上安装的旧版本是否为此客户端所需的版本
|
|
|
|
int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
|
|
|
|
if (code == ConnectionResult.SUCCESS) {
|
|
|
|
// 支持Google服务
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
// /**
|
|
|
|
// * 依靠 Play 服务 SDK 运行的应用在访问 Google Play 服务功能之前,应始终检查设备是否拥有兼容的 Google Play 服务 APK。
|
|
|
|
// * 我们建议您在以下两个位置进行检查:主 Activity 的 onCreate() 方法中,及其 onResume() 方法中。
|
|
|
|
// * onCreate() 中的检查可确保该应用在检查成功之前无法使用。
|
|
|
|
// * onResume() 中的检查可确保当用户通过一些其他方式返回正在运行的应用(比如通过返回按钮)时,检查仍将继续进行。
|
|
|
|
// * 如果设备没有兼容的 Google Play 服务版本,您的应用可以调用以下方法,以便让用户从 Play 商店下载 Google Play 服务。
|
|
|
|
// * 它将尝试在此设备上提供Google Play服务。如果Play服务已经可用,则Task可以立即完成返回。
|
|
|
|
// */
|
|
|
|
// GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);
|
|
|
|
//
|
|
|
|
// // 或者使用以下代码
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * 通过isUserResolvableError来确定是否可以通过用户操作解决错误
|
|
|
|
// */
|
|
|
|
// if (GoogleApiAvailability.getInstance().isUserResolvableError(code)) {
|
|
|
|
// /**
|
|
|
|
// * 返回一个对话框,用于解决提供的errorCode。
|
|
|
|
// * @param activity 用于创建对话框的父活动
|
|
|
|
// * @param code 通过调用返回的错误代码
|
|
|
|
// * @param activity 调用startActivityForResult时给出的requestCode
|
|
|
|
// */
|
|
|
|
// GoogleApiAvailability.getInstance().getErrorDialog(this, code, 200).show();
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} |