Explorar el Código

1.修改Android Apex Drivers文件存储。
2.修改Android Apex Drivers Update参数错误。

Pen Li hace 7 años
padre
commit
e38a02e7de

+ 43 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/FileManager.java

@@ -54,6 +54,49 @@ public class FileManager {
         }
     }
 
+    public static String internalStorageFileDir(Context context) {
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+        {
+            Context ctx = context.createDeviceProtectedStorageContext();
+            File dir = ctx.getFilesDir();
+            return dir.getAbsolutePath();
+        }
+        else
+        {
+            String appName = RAUtil.getApplicationName(context);
+            String dir = SDCardRoot() + appName;
+            File file = new File(dir);
+            if (!file.exists()) {
+                file.mkdirs();
+            }
+
+            return dir;
+        }
+    }
+
+    public static String internalStorageTempDir(Context context) {
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+        {
+            Context ctx = context.createDeviceProtectedStorageContext();
+            File dir = ctx.getCacheDir();
+            return dir.getAbsolutePath();
+        }
+        else
+        {
+            String appName = RAUtil.getApplicationName(context);
+            String dir = SDCardRoot() + appName + File.separator + "temp";
+            File file = new File(dir);
+            if (!file.exists()) {
+                file.mkdirs();
+            }
+
+            return dir;
+        }
+
+    }
+
 
     public static String SDCardRoot() {
         return Environment.getExternalStorageDirectory()+ File.separator;

+ 35 - 17
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/ImageUtil.java

@@ -55,8 +55,14 @@ public class ImageUtil {
             routedPath = path+"/"+file_name;
         }
         File routedFile = new File(routedPath);
+        if (routedFile.exists()) {
+            routedFile.delete();
+        }
+
         try {
 
+//            routedFile.createNewFile();
+
             FileOutputStream outputStream = new FileOutputStream(routedFile);
             newbitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
 
@@ -247,13 +253,18 @@ public class ImageUtil {
     }
 
     public static String appCacheDir(Context context) {
-        String root = FileManager.SDCardRoot();
-        String appName = RAUtil.getApplicationName(context);
-        return root + appName;
+
+//        String root = FileManager.SDCardRoot();
+//        String appName = RAUtil.getApplicationName(context);
+//        return root + appName;
+
+        String dir = FileManager.internalStorageTempDir(context);
+        return dir;
     }
 
     public static String imageCacheDir(Context context) {
         String imgDir = appCacheDir(context) + File.separator + "ImageCache";
+//        String imgDir = appCacheDir(context);
         return imgDir;
     }
 
@@ -266,7 +277,7 @@ public class ImageUtil {
         }
         final BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
-        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
+//        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 
         // Decode bitmap with inSampleSize set
         options.inJustDecodeBounds = false;
@@ -280,20 +291,27 @@ public class ImageUtil {
 //            StrictMode.setVmPolicy(builder.build());
 //        }
 
-        try {
-
-            Uri uri = Uri.fromFile(file);
+        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
 
-            // 7.0 以后文件读取
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-                uri = FileProvider.getUriForFile(context, "com.usai.apex.driver.fileprovider", file);
-            }
-
-            return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
+        return bitmap;
+//        try {
+//
+//            Uri uri = Uri.fromFile(file);
+//
+//            // 7.0 以后文件读取
+//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+//                String root = FileManager.SDCardRoot();
+//                if (file.getAbsolutePath().startsWith(root)) {
+//                    uri = FileProvider.getUriForFile(context, "com.usai.apex.driver.fileprovider", file);
+//                }
+//            }
+//
+//            return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
+//
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//        return null;
 
 //        return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
     }

+ 28 - 21
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/dbgUtil.java

@@ -44,9 +44,21 @@ public class dbgUtil
 	 * this; }
 	 */
 
+	private static String logDir(Context context) {
+
+		String tmpDir = FileManager.internalStorageTempDir(context);
+		String logDir = tmpDir + File.separator + "Log";
+		File file = new File(logDir);
+		if (!file.exists()) {
+			file.mkdirs();
+		}
+		return file.getAbsolutePath();
+	}
+
 	public static void fileLog(Context appContext, String content)
 	{
 
+		String Tag = RAUtil.getApplicationName(appContext);
 
 		String[] permissions = {
 				Manifest.permission.WRITE_EXTERNAL_STORAGE,
@@ -58,24 +70,24 @@ public class dbgUtil
 		boolean result= RAUtil.checkPermissions_23(appContext, permissions);
 
 		if(!result) {
-			Log.d("Apex Drivers", "fileLog: no external storage permission");
+			Log.d(Tag, "fileLog: no external storage permission");
 			return;
 		}
-		File dir = new File(Environment.getExternalStorageDirectory().getPath()
-				+ "/redant/pop");
+
+		String logDir = logDir(appContext);
+
+		File dir = new File(logDir);
 		if (!dir.exists())
 			dir.mkdirs();
 
 		Calendar c = Calendar.getInstance();
-//		c.add(Calendar.DAY_OF_WEEK, -1);
 
-		String newtime = new SimpleDateFormat("yyyy-MM-dd").format(c
-				.getTimeInMillis());
+		String newtime = new SimpleDateFormat("yyyy-MM-dd").format(c.getTimeInMillis());
 
 		File[] f = dir.listFiles();
 		if(f==null) {
-			Log.d("Apex Drivers", "fileLog: list file failed");
-			Log.d("Apex Drivers", Environment.getExternalStorageDirectory().getPath());
+			Log.d(Tag, "fileLog: list file failed");
+			Log.d(Tag, logDir);
 			return;
 		}
 		for (int i = 0; i < f.length; i++)
@@ -92,8 +104,6 @@ public class dbgUtil
 				}
 				if(newtime.compareTo(n)>0)
 					f[i].delete();
-				
-
 
 			}
 		}
@@ -101,16 +111,13 @@ public class dbgUtil
 		// dir.listfiles
 
 		String file = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
-		String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
-				.format(new Date());
+		String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
 
 		FileOutputStream outputStream = null;
-		// try
-		// {
 
-		// dir.createNewFile()
-		File logfile = new File(Environment.getExternalStorageDirectory()
-				.getPath() + "/redant/pop/" + file + ".log");
+		String logPath = logDir + File.separator + file + ".log";
+
+		File logfile = new File(logPath);
 
 		// int mode = Application.MODE_APPEND;
 		if (!logfile.exists())
@@ -138,11 +145,11 @@ public class dbgUtil
 
 		try
 		{
-			Log.d("Apex Drivers", "fileLog: begin write "+ content);
+
+			Log.d(Tag, "fileLog: begin write "+ content);
 			loglock.writeLock().lock();
 
-			fw = new FileWriter(Environment.getExternalStorageDirectory()
-					.getPath() + "/redant/pop/" + file + ".log", true);//
+			fw = new FileWriter(logPath, true);//
 			// 创建FileWriter对象,用来写入字符流
 			bw = new BufferedWriter(fw); // 将缓冲对文件的输出
 			// String myreadline = datetime + "[]" + str;
@@ -152,7 +159,7 @@ public class dbgUtil
 			bw.flush(); // 刷新该流的缓冲
 			bw.close();
 			fw.close();
-			Log.d("Apex Drivers", "fileLog: end write");
+			Log.d(Tag, "fileLog: end write");
 
 		}
 		catch (IOException e)

+ 10 - 1
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -6,6 +6,10 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
 import android.location.Location;
 import android.os.Build;
 import android.os.IBinder;
@@ -16,7 +20,13 @@ import com.usai.redant.apexdrivers.receiver.ApexDriverAlarmReceiver;
 import com.usai.redant.apexdrivers.utils.OperationQueue;
 import com.usai.redant.rautils.receiver.RABroadcast;
 import com.usai.redant.rautils.utils.AESUtil;
+import com.usai.redant.rautils.utils.FileManager;
+import com.usai.redant.rautils.utils.ImageUtil;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.lang.reflect.Field;
 
 public class ApexDriverApplication extends Application {
@@ -187,7 +197,6 @@ public class ApexDriverApplication extends Application {
 
     public String savedUser() {
 
-
         SharedPreferences pref=sharedPreferences();
 
         String user = pref.getString("user", null);

+ 2 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -7,6 +7,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.res.ColorStateList;
+import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.location.Location;
 import android.net.Uri;
@@ -131,6 +132,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
             // 显示Home
             actionBar.setHomeAsUpIndicator(drawable);
             actionBar.setDisplayHomeAsUpEnabled(true);
+
         }
         setTitle(null);
 

+ 11 - 5
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/detail/model/DetailActionModel.java

@@ -46,14 +46,21 @@ public class DetailActionModel extends DetailBaseModel{
 
                     int type = action.optInt("actionType");
                     int subType = action.optInt("actionSubType");
+                    String title = action.optString("actionTitle");
+                    subActionModel.actionTitle = title;
 
                     subActionModel.actionType = type;
                     subActionModel.actionSubType = subType;
 
-                    if (type == DetailSubActionModel.DetailActionType.DetailActionTypeLocal && subType == DetailSubActionModel.DetailActionSubType.DetailActionSubTypeEnum) {
+                    if (type == DetailSubActionModel.DetailActionType.DetailActionTypeLocal) {
 
-                        JSONArray enums = action.optJSONArray("enums");
-                        subActionModel.setEnums(enums);
+                        if (subType == DetailSubActionModel.DetailActionSubType.DetailActionSubTypeEnum) {
+                            JSONArray enums = action.optJSONArray("enums");
+                            subActionModel.setEnums(enums);
+                        } else if (subType == DetailSubActionModel.DetailActionSubType.DetailActionSubTypeUpdate) {
+                            int actionID = action.optInt("actionID");
+                            subActionModel.actionID = actionID;
+                        }
                     }
 
                     if (type == DetailSubActionModel.DetailActionType.DetailActionTypeRemote) {
@@ -69,8 +76,7 @@ public class DetailActionModel extends DetailBaseModel{
                         }
                     }
 
-                    String title = action.optString("actionTitle");
-                    subActionModel.actionTitle = title;
+
 
                     this.actions.add(subActionModel);
                 }

+ 2 - 2
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/detail/model/DetailSubActionModel.java

@@ -58,8 +58,8 @@ public class DetailSubActionModel {
     public Bundle params;
 
     /**
-    * actionType == RADetailActionTypeLocal && actionSubType == DetailActionSubTypeUpdate
-    * */
+     * actionType == RADetailActionTypeLocal && actionSubType == DetailActionSubTypeUpdate
+     * */
     public int actionID;
 
     /**

+ 6 - 4
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/signature/SignatureActivity.java

@@ -96,11 +96,13 @@ public class SignatureActivity extends BasicActivity {
         if (signature != null) {
 
             String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmSS").format(new Date());
-            String signatureName = "JPEG_" + timeStamp + "_";
+            String signatureName = "JPEG_" + timeStamp + ".jpg";
 
-            String root = FileManager.SDCardRoot();
-            String appName = RAUtil.getApplicationName(mCtx);
-            String signatureDir = root + File.separator + appName + File.separator + "signature";
+//            String root = FileManager.SDCardRoot();
+//            String appName = RAUtil.getApplicationName(mCtx);
+//            String signatureDir = root + File.separator + appName + File.separator + "signature";
+
+            String signatureDir = FileManager.internalStorageFileDir(mCtx) + File.separator + "Signature";
             String signaturePath = signatureDir + File.separator + signatureName;
 
             File parentFile = new File(signatureDir);

+ 91 - 8
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/update/UpdateActivity.java

@@ -8,8 +8,12 @@ import android.content.ContentValues;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.database.Cursor;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.location.Location;
+import android.media.Image;
 import android.net.Uri;
 import android.os.Environment;
 import android.os.Handler;
@@ -53,6 +57,9 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.lang.ref.WeakReference;
 import java.text.SimpleDateFormat;
@@ -289,17 +296,20 @@ public class UpdateActivity extends BasicActivity implements UpdateAdapter.Updat
 
                 if (resultCode == Activity.RESULT_OK) {
 
-//                    File scaleFile = compressImageFile(photoFile);
-//                    if (scaleFile != null) {
-//                        photoFile = scaleFile;
-//                    }
+                    File scaleFile = compressImageFile(photoFile);
+//                    File scaleFile = photoFile;
+                    if (scaleFile != null) {
 
-                    ImageUtil.updateGallery(mCtx,photoFile.getAbsolutePath());
-                    if (mPhotoModel != null) {
-                        mPhotoModel.setPhotoPath(photoFile.getAbsolutePath());
+                        ImageUtil.updateGallery(mCtx,scaleFile.getAbsolutePath());
+
+                        if (mPhotoModel != null) {
+                            mPhotoModel.setPhotoPath(scaleFile.getAbsolutePath());
+                        }
+
+                        mPhotoArray.add(scaleFile);
                     }
 
-                    mPhotoArray.add(photoFile);
+
 
                 } else {
                     if (photoFile.exists()) {
@@ -345,6 +355,79 @@ public class UpdateActivity extends BasicActivity implements UpdateAdapter.Updat
 
     }
 
+
+    private File compressImageFile(File imgFile) {
+
+
+        String imageFileName = imgFile.getName();
+
+        File routedFile = ImageUtil.routeBitmap(mCtx,imgFile,null);
+
+        Bitmap source = null;
+        try {
+            FileInputStream stream = new FileInputStream(routedFile);
+            source = BitmapFactory.decodeStream(stream);
+        } catch (FileNotFoundException e) {
+            return routedFile;
+        }
+
+        if (source == null) {
+            return routedFile;
+        }
+
+        int originWidth = source.getWidth();
+        int originHeight = source.getHeight();
+
+        int width = originWidth, height = originHeight;
+
+        if (originHeight > 1024 || originWidth > 1024)
+        {
+            if (originWidth > originHeight)
+            {
+                width = 1024;
+                height = originHeight * 1024 / originWidth;
+
+            }
+            else
+            {
+
+                height = 1024;
+                width = originWidth * 1024 / originHeight;
+
+            }
+        }
+
+
+        Bitmap scaled = Bitmap.createScaledBitmap(source, width, height, true);
+
+        String scalePath = FileManager.internalStorageFileDir(mCtx) + File.separator + "Photo" + File.separator +imageFileName;
+        File scaleFile = new File(scalePath);
+        if (!scaleFile.getParentFile().exists()) {
+            scaleFile.getParentFile().mkdirs();
+        }
+
+        try {
+
+            FileOutputStream outputStream = new FileOutputStream(scaleFile);
+            scaled.compress(Bitmap.CompressFormat.JPEG, 95, outputStream);
+
+            outputStream.flush();
+            outputStream.close();
+
+            String rotedpath = routedFile.getAbsolutePath();
+            routedFile.delete();
+
+            ImageUtil.updateGallery(mCtx,rotedpath);
+
+            return scaleFile;
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            return routedFile;
+        }
+
+    }
+
     /**
      * Camera Permission
      * */