diff --git a/foudations/mogo-utils/src/main/java/com/mogo/utils/FileUtils.java b/foudations/mogo-utils/src/main/java/com/mogo/utils/FileUtils.java index e7067de16b..aaa6e699ee 100644 --- a/foudations/mogo-utils/src/main/java/com/mogo/utils/FileUtils.java +++ b/foudations/mogo-utils/src/main/java/com/mogo/utils/FileUtils.java @@ -5,7 +5,9 @@ import android.content.Context; import android.net.Uri; import android.text.TextUtils; import android.util.Base64; +import android.util.Log; +import androidx.annotation.IntRange; import androidx.core.content.FileProvider; import java.io.BufferedInputStream; @@ -20,98 +22,100 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; - +/** + * 文件工具类 + */ public class FileUtils { - public static boolean createFileDir( String fileDir ) { + public static boolean createFileDir(String fileDir) { - if ( TextUtils.isEmpty( fileDir ) ) { + if (TextUtils.isEmpty(fileDir)) { return false; } try { - File dir = new File( fileDir ); + File dir = new File(fileDir); return dir.exists() || dir.mkdir(); - } catch ( Exception e ) { + } catch (Exception e) { return false; } } - public static boolean createFileDir( File dir ) { - if ( dir == null ) { + public static boolean createFileDir(File dir) { + if (dir == null) { return false; } try { return dir.exists() || dir.mkdir(); - } catch ( Exception e ) { + } catch (Exception e) { return false; } } - public static void writeToFile( String fileDir, String fileName, String content ) { - if ( fileDir == null || fileName == null || content == null ) { + public static void writeToFile(String fileDir, String fileName, String content) { + if (fileDir == null || fileName == null || content == null) { return; } - if ( !createFileDir( fileDir ) ) { + if (!createFileDir(fileDir)) { return; } FileOutputStream fos = null; OutputStreamWriter osw = null; try { - fos = new FileOutputStream( fileDir + fileName, true ); - osw = new OutputStreamWriter( fos ); - osw.write( content ); + fos = new FileOutputStream(fileDir + fileName, true); + osw = new OutputStreamWriter(fos); + osw.write(content); osw.flush(); - } catch ( IOException e ) { + } catch (IOException e) { e.printStackTrace(); } finally { - IOUtils.closeSilently( fos ); - IOUtils.closeSilently( osw ); + IOUtils.closeSilently(fos); + IOUtils.closeSilently(osw); } } /** * Read a text file into a String, optionally limiting the length. */ - public static String readTextFile( File file ) { + public static String readTextFile(File file) { InputStream is = null; BufferedInputStream bis = null; ByteArrayOutputStream bos = null; String text = null; try { - is = new FileInputStream( file ); - bis = new BufferedInputStream( is ); + is = new FileInputStream(file); + bis = new BufferedInputStream(is); bos = new ByteArrayOutputStream(); int len; byte[] data = new byte[1024]; do { - len = bis.read( data ); - if ( len > 0 ) bos.write( data, 0, len ); - } while ( len == data.length ); + len = bis.read(data); + if (len > 0) bos.write(data, 0, len); + } while (len == data.length); text = bos.toString(); - } catch ( Exception e ) { + } catch (Exception e) { e.printStackTrace(); } finally { - IOUtils.closeSilently( is ); - IOUtils.closeSilently( bis ); - IOUtils.closeSilently( bos ); + IOUtils.closeSilently(is); + IOUtils.closeSilently(bis); + IOUtils.closeSilently(bos); } return text; } - public static String fileToBase64( File file ) { + public static String fileToBase64(File file) { String base64 = null; InputStream in = null; try { - in = new FileInputStream( file ); + in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; - int length = in.read( bytes ); - base64 = Base64.encodeToString( bytes, 0, length, Base64.DEFAULT ); - } catch ( FileNotFoundException e ) { + int length = in.read(bytes); + base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT); + } catch (FileNotFoundException e) { e.printStackTrace(); - } catch ( IOException e ) { + } catch (IOException e) { e.printStackTrace(); } finally { - IOUtils.closeSilently( in ); + IOUtils.closeSilently(in); } return base64; } @@ -119,81 +123,81 @@ public class FileUtils { /** * Writes string to file. Basically same as "echo -n $string > $filename" */ - public static void stringToFile( String filename, String string ) { + public static void stringToFile(String filename, String string) { FileWriter out = null; try { - out = new FileWriter( filename ); - out.write( string ); - } catch ( Exception e ) { + out = new FileWriter(filename); + out.write(string); + } catch (Exception e) { e.printStackTrace(); } finally { - IOUtils.closeSilently( out ); + IOUtils.closeSilently(out); } } - public static InputStream stringToStream( String content ) { + public static InputStream stringToStream(String content) { InputStream inputStream = null; try { - inputStream = new ByteArrayInputStream( content.getBytes() ); - } catch ( Exception e ) { + inputStream = new ByteArrayInputStream(content.getBytes()); + } catch (Exception e) { e.printStackTrace(); } return inputStream; } - public static String streamToString( InputStream is ) throws IOException { + public static String streamToString(InputStream is) throws IOException { String content = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int i = -1; - while ( ( i = is.read() ) != -1 ) { - bos.write( i ); + while ((i = is.read()) != -1) { + bos.write(i); } content = bos.toString(); - } catch ( Exception e ) { + } catch (Exception e) { e.printStackTrace(); } return content; } - public static String getStringFromFile( Context context, String fileName ) { + public static String getStringFromFile(Context context, String fileName) { FileInputStream fis = null; ByteArrayOutputStream os = null; String content = null; try { - fis = context.openFileInput( fileName ); + fis = context.openFileInput(fileName); os = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = -1; - while ( ( length = fis.read( buffer ) ) != -1 ) { - os.write( buffer, 0, length ); + while ((length = fis.read(buffer)) != -1) { + os.write(buffer, 0, length); } content = os.toString(); - } catch ( Exception e ) { + } catch (Exception e) { e.printStackTrace(); } finally { - IOUtils.closeSilently( fis ); - IOUtils.closeSilently( os ); + IOUtils.closeSilently(fis); + IOUtils.closeSilently(os); } return content; } - public static InputStream getStreamFromFile( Context context, String fileName ) { + public static InputStream getStreamFromFile(Context context, String fileName) { FileInputStream fis = null; try { - fis = context.openFileInput( fileName ); - } catch ( Exception e ) { + fis = context.openFileInput(fileName); + } catch (Exception e) { e.printStackTrace(); } return fis; } - public static void saveStringToFile( Context context, String content, String fileName ) { + public static void saveStringToFile(Context context, String content, String fileName) { try { - FileOutputStream fos = context.openFileOutput( fileName, Context.MODE_PRIVATE ); - fos.write( content.getBytes() ); - IOUtils.closeSilently( fos ); - } catch ( Exception e ) { + FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); + fos.write(content.getBytes()); + IOUtils.closeSilently(fos); + } catch (Exception e) { e.printStackTrace(); } } @@ -201,10 +205,10 @@ public class FileUtils { /** * 将scheme为file的uri转成FileProvider 提供的content uri */ - public static Uri convertFileUriToFileProviderUri( Context context, Uri uri ) { - if ( uri == null ) return null; - if ( ContentResolver.SCHEME_FILE.equals( uri.getScheme() ) ) { - return getUriForFile( context, new File( uri.getPath() ) ); + public static Uri convertFileUriToFileProviderUri(Context context, Uri uri) { + if (uri == null) return null; + if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { + return getUriForFile(context, new File(uri.getPath())); } return uri; @@ -213,38 +217,195 @@ public class FileUtils { /** * 创建一个用于拍照图片输出路径的Uri (FileProvider) */ - public static Uri getUriForFile( Context context, File file ) { - return FileProvider.getUriForFile( context, getFileProviderName( context ), file ); + public static Uri getUriForFile(Context context, File file) { + return FileProvider.getUriForFile(context, getFileProviderName(context), file); } - public static String getFileProviderName( Context context ) { + public static String getFileProviderName(Context context) { return context.getPackageName() + ".fileprovider"; } /** * 把Uri 解析出文件绝对路径 */ - public static String parseOwnUri( Context context, Uri uri ) { - if ( uri == null ) return null; + public static String parseOwnUri(Context context, Uri uri) { + if (uri == null) return null; String path; - if ( TextUtils.equals( uri.getAuthority(), getFileProviderName( context ) ) ) { - path = new File( uri.getPath() ).getAbsolutePath(); + if (TextUtils.equals(uri.getAuthority(), getFileProviderName(context))) { + path = new File(uri.getPath()).getAbsolutePath(); } else { path = uri.getPath(); } return path; } - public static String getFileStreamPath( Context context, String name ) { + public static String getFileStreamPath(Context context, String name) { String absFileName = null; try { - File file = context.getFileStreamPath( name ); - if ( file != null && file.exists() ) { + File file = context.getFileStreamPath(name); + if (file != null && file.exists()) { absFileName = file.getAbsolutePath(); } - } catch ( Exception e ) { + } catch (Exception e) { e.printStackTrace(); } return absFileName; } + + + /** + * 拷贝文件数据到指定目录 + * + * @param is + * @param to + * @param listener + */ + public static void copy(final InputStream is, final String to, final FileCopyListener listener) { + new Thread(new Runnable() { + @Override + public void run() { + Log.w("FileUtils", "======copy======"); + + if (listener != null) { + listener.onStart(); + } + + try { + + long fileSize = is.available(); + long process = 0; + + byte[] buff = new byte[1024]; + int rc = 0; + + File toFile = new File(to); + if (!toFile.getParentFile().exists()) { + toFile.getParentFile().mkdirs(); + } + + FileOutputStream fos = new FileOutputStream(toFile); + + while ((rc = is.read(buff, 0, 1024)) > 0) { + process += rc; + fos.write(buff, 0, rc); + if (listener != null) { + listener.onProcess(((int) (((float) process) * 100 / fileSize))); + } + } + + fos.flush(); + fos.close(); + is.close(); + + } catch (Exception e) { + if (listener != null) { + listener.onFail(e); + return; + } + } + + if (listener != null) { + listener.onFinish(to); + } + } + }).start(); + } + + /** + * 拷贝文件到制定目录 + * + * @param from + * @param to + * @param listener + */ + public static void copy(final String from, final String to, final FileCopyListener listener) { + new Thread(new Runnable() { + @Override + public void run() { + File file = null; + try { + file = new File(from); + } catch (Exception e) { + if (listener != null) { + listener.onFail(e); + } + return; + } + if (!file.isFile()) { + if (listener != null) { + listener.onFail(new Exception(String.format("%s is not a file", from))); + return; + } + } + if (!file.exists()) { + if (listener != null) { + listener.onFail(new FileNotFoundException(String.format("%s is not exists.", from))); + return; + } + } + + if (listener != null) { + listener.onStart(); + } + + long fileSize = file.length(); + long process = 0; + + try { + FileInputStream fis = new FileInputStream(file); + + byte[] buff = new byte[1024]; + int rc = 0; + + File toFile = new File(to); + if (!toFile.getParentFile().exists()) { + toFile.getParentFile().mkdirs(); + } + + FileOutputStream fos = new FileOutputStream(toFile); + + while ((rc = fis.read(buff, 0, 1024)) > 0) { + process += rc; + fos.write(buff, 0, rc); + if (listener != null) { + listener.onProcess(((int) (((float) process) * 100 / fileSize))); + } + } + + fos.flush(); + fos.close(); + fis.close(); + + } catch (Exception e) { + if (listener != null) { + listener.onFail(e); + return; + } + } + + if (listener != null) { + listener.onFinish(to); + } + } + }).start(); + } + + + /** + * 文件拷贝监听 + */ + public interface FileCopyListener { + // 开始 + void onStart(); + + // 失败 + void onFail(Exception e); + + // 进度 + void onProcess(@IntRange(from = 0, to = 100) int process); + + // 结束成功 + void onFinish(String toPath); + } + } diff --git a/modules/mogo-module-smp/src/main/assets/style.data b/modules/mogo-module-smp/src/main/assets/style.data new file mode 100644 index 0000000000..412a2ab911 Binary files /dev/null and b/modules/mogo-module-smp/src/main/assets/style.data differ diff --git a/modules/mogo-module-smp/src/main/assets/style_extra.data b/modules/mogo-module-smp/src/main/assets/style_extra.data new file mode 100644 index 0000000000..e5890460f3 Binary files /dev/null and b/modules/mogo-module-smp/src/main/assets/style_extra.data differ diff --git a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java index 2fefdb0e72..3c716980f1 100644 --- a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java +++ b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallMapDirectionView.java @@ -3,24 +3,26 @@ package com.mogo.module.small.map; import android.content.Context; import android.graphics.BitmapFactory; import android.util.AttributeSet; +import android.util.Log; import android.view.LayoutInflater; import android.view.animation.LinearInterpolator; import android.widget.ImageView; import androidx.annotation.Nullable; -import com.amap.api.location.AMapLocationClient; -import com.amap.api.location.AMapLocationClientOption; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdate; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.CameraPosition; -import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.navi.AMapNaviView; import com.amap.api.navi.AMapNaviViewOptions; import com.mogo.module.common.view.RoundLayout; import com.mogo.module.small.map.animation.DirectionRotateAnimation; +import com.mogo.utils.FileUtils; + +import java.io.File; +import java.io.IOException; /** * 小地图的方向View @@ -32,12 +34,6 @@ public class SmallMapDirectionView extends RoundLayout { private ImageView mIvMapBorder; private AMapNaviView mAMapNaviView; - private AMap mAMap; - private UiSettings mUiSettings; - private CameraUpdate mCameraUpdate; - private MyLocationStyle myLocationStyle; - private AMapLocationClient mLocationClient; - private AMapLocationClientOption mLocationClientOption; private DirectionRotateAnimation mRotateAnimation; private int lastAngle = 0; @@ -56,6 +52,8 @@ public class SmallMapDirectionView extends RoundLayout { } private void initView(Context context) { + String styleFilePath = "/mnt/sdcard/amap/small_map_style.data"; + mRotateAnimation = new DirectionRotateAnimation(context, null); LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this); @@ -67,15 +65,15 @@ public class SmallMapDirectionView extends RoundLayout { mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE); // 设置电子眼所在路线的可见性 mAMapNaviView.setRouteOverlayVisible(false); - mAMap = mAMapNaviView.getMap(); + AMap aMap = mAMapNaviView.getMap(); // 关闭地图文字标注 - mAMap.showMapText(false); + aMap.showMapText(false); // 设置地图的样式 - mUiSettings = mAMap.getUiSettings(); - mUiSettings.setZoomControlsEnabled(false);// 地图缩放级别的交换按钮 - mUiSettings.setAllGesturesEnabled(false);// 所有手势 - mUiSettings.setMyLocationButtonEnabled(false); // 显示默认的定位按钮 - mUiSettings.setLogoBottomMargin(-150); //设置Logo下边界距离屏幕底部的边距,设置为负值即可 + UiSettings uiSettings = aMap.getUiSettings(); + uiSettings.setZoomControlsEnabled(false);// 地图缩放级别的交换按钮 + uiSettings.setAllGesturesEnabled(false);// 所有手势 + uiSettings.setMyLocationButtonEnabled(false); // 显示默认的定位按钮 + uiSettings.setLogoBottomMargin(-150); //设置Logo下边界距离屏幕底部的边距,设置为负值即可 // 导航地图 AMapNaviViewOptions options = mAMapNaviView.getViewOptions(); @@ -133,22 +131,22 @@ public class SmallMapDirectionView extends RoundLayout { options.setNaviArrowVisible(false); // 通过路线是否自动置灰,仅支持驾车导航 options.setAfterRouteAutoGray(false); - //options.setZoom(((int) 9)); + options.setZoom(((int) 9)); //options.setPointToCenter(0.7D, 0.5D); // 2D模式 options.setTilt(0); // 黑夜模式 - options.setNaviNight(true); +// options.setNaviNight(true); // 自定义地图样式 -// options.setCustomMapStylePath("/mnt/sdcard/amap/style.data"); + options.setCustomMapStylePath(styleFilePath); mAMapNaviView.setViewOptions(options); } //设置希望展示的地图缩放级别 - mCameraUpdate = CameraUpdateFactory.zoomTo(12); - mAMap.moveCamera(mCameraUpdate); + CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12); + aMap.moveCamera(cameraUpdate); - mAMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() { + aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition cameraPosition) { if (cameraPosition != null) { @@ -163,20 +161,66 @@ public class SmallMapDirectionView extends RoundLayout { } }); -// CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions(); -// customMapStyleOptions.setStyleData(MapAssetStyleUtils.getAssetsStyle(context)); -// customMapStyleOptions.setStyleExtraData(MapAssetStyleUtils.getAssetsExtraStyle(context)); -// customMapStyleOptions.setEnable(true); -// mAMap.setCustomMapStyle(customMapStyleOptions); + aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() { - // 设置当前位置的样式 -// myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类 -// myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 -// myLocationStyle.interval(1000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 -// BitmapDescriptor location = BitmapDescriptorFactory.fromResource(R.drawable.module_small_map_view_my_location_logo); -// myLocationStyle.myLocationIcon(location); -// mAMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style -// mAMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 + @Override + public void onMapLoaded() { + //Log.w("onMapLoaded", "===onMapLoaded===="); + // 高德地图有bug,所以需要多次调用设置皮肤才能成功 + if (options != null) { + options.setCustomMapStylePath(styleFilePath); + mAMapNaviView.setViewOptions(options); + } + + //设置希望展示的地图缩放级别 + CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12); + aMap.moveCamera(cameraUpdate); + } + }); + + try { + // 判断是否有样式文件存在 + File styleExit = new File(styleFilePath); + if (!styleExit.exists()) { + FileUtils.copy( + context.getAssets().open("small_map_style.data"), + styleFilePath, + new FileUtils.FileCopyListener() { + + @Override + public void onStart() { + Log.w("FileCopyListener", "onStart="); + + } + + @Override + public void onFail(Exception e) { + Log.w("FileCopyListener", "onFail="); + e.printStackTrace(); + } + + @Override + public void onProcess(int process) { + Log.w("FileCopyListener", "onProcess=" + process); + + } + + @Override + public void onFinish(String toPath) { + Log.w("FileCopyListener", "onFinish toPath=" + toPath); + + // 高德地图有bug,所以需要多次调用设置皮肤才能成功 + if (options != null) { + options.setCustomMapStylePath(styleFilePath); + mAMapNaviView.setViewOptions(options); + } + } + } + ); + } + } catch (IOException e) { + e.printStackTrace(); + } } diff --git a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallVisionProvider.java b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallVisionProvider.java index 6c18a05d75..9f7cb79549 100644 --- a/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallVisionProvider.java +++ b/modules/mogo-module-smp/src/main/java/com/mogo/module/small/map/SmallVisionProvider.java @@ -17,6 +17,9 @@ import com.mogo.service.map.IMogoSmallMapProvider; import com.mogo.service.module.ModuleType; import com.mogo.service.statusmanager.IMogoStatusChangedListener; import com.mogo.service.statusmanager.StatusDescriptor; +import com.mogo.utils.FileUtils; + +import java.io.IOException; /** * @author donghongyu