| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- package com.usai.util;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.media.ExifInterface;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.PermissionChecker;
- import android.util.Log;
- import com.usai.redant.raimage.RedAntApplication;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /**
- * Created by ray on 27/06/2017.
- */
- public class RAUtil {
- public static final int MY_PERMISSIONS_REQUEST= 1;
- // public static void checkPermissions1(Activity activity, String[] permissions)
- // {
- // Log.d("_RAIMAGE", "checkPermissions1:==================================== ");
- // for(String permission : permissions) {
- // boolean bshow=ActivityCompat.shouldShowRequestPermissionRationale(activity,permission);
- // boolean granted= ContextCompat.checkSelfPermission(activity,
- // Manifest.permission.ACCESS_FINE_LOCATION)
- // == PackageManager.PERMISSION_GRANTED;
- // boolean pc = PermissionChecker.checkSelfPermission(activity,permission)==PermissionChecker.PERMISSION_GRANTED;
- // Log.d("_RAIMAGE", "checkPermissions1: "+permission+" SHOW "+bshow+" granted "+granted +","+pc);
- // }
- // Log.d("_RAIMAGE", "checkPermissions1:==================================== ");
- // }
- public static boolean isNumeric(String str){
- for (int i = 0; i < str.length(); i++){
- System.out.println(str.charAt(i));
- if (!Character.isDigit(str.charAt(i))){
- return false;
- }
- }
- return true;
- }
- public static Boolean checkPermissions(Activity activity, String[] permissions)
- {
- if (Build.VERSION.SDK_INT >= 23)
- {
- return checkPermissions_23(activity,permissions);
- }
- else
- {
- return false;
- // return checkPermissions_23(activity,permissions);
- }
- }
- public static Boolean checkPermissions_23(String[] permissions)
- {
- Log.d("_RAIMAGE", "checkPermissions23:==================================== ");
- ArrayList<String> request_list = new ArrayList<String>();
- Context context = RedAntApplication.getInstance().getApplicationContext();
- for(String permission : permissions) {
- boolean pc = PermissionChecker.checkSelfPermission(context,permission)==PermissionChecker.PERMISSION_GRANTED;
- if(!pc)
- return false;
- }
- Log.d("_RAIMAGE", "checkPermissions23:==================================== ");
- return true;
- }
- public static Boolean checkPermissions_23(Activity activity, String[] permissions)
- {
- Log.d("_RAIMAGE", "checkPermissions23:==================================== ");
- ArrayList<String> request_list = new ArrayList<String>();
- for(String permission : permissions) {
- boolean bshow=ActivityCompat.shouldShowRequestPermissionRationale(activity,permission);
- // boolean granted= ContextCompat.checkSelfPermission(activity,
- // Manifest.permission.ACCESS_FINE_LOCATION)
- // == PackageManager.PERMISSION_GRANTED;
- boolean pc = PermissionChecker.checkSelfPermission(activity,permission)==PermissionChecker.PERMISSION_GRANTED;
- // Log.d("_RAIMAGE", "checkPermissions23: "+permission+" SHOW "+bshow+" granted "+granted +","+pc);
- if(!pc)
- request_list.add(permission);
- }
- Log.d("_RAIMAGE", "checkPermissions23:==================================== ");
- if(request_list.isEmpty())
- return false;
- ActivityCompat.requestPermissions(activity,
- request_list.toArray(new String[0]),
- MY_PERMISSIONS_REQUEST);
- return true;
- }
- public static Boolean checkPermissions_24(Activity activity, String[] permissions)
- {
- // ArrayList<String> request_list = new ArrayList<String>();
- // for(String permission : permissions) {
- //
- //
- //// boolean bshow=ActivityCompat.shouldShowRequestPermissionRationale(activity,permission);
- ////
- ////
- //// AppOpsManager appOpsManager = (AppOpsManager) activity.getSystemService(Context.APP_OPS_SERVICE);
- //// String opsName = AppOpsManager.permissionToOp(permission);
- //// if (opsName == null) {
- //// return true;
- //// }
- //// int opsMode = appOpsManager.checkOpNoThrow(opsName, Process.myUid(), activity.getPackageName());
- ////// return opsMode == AppOpsManager.MODE_ALLOWED;
- //
- //
- //// if (ContextCompat.checkSelfPermission(activity,
- //// Manifest.permission.ACCESS_FINE_LOCATION)
- //// != PackageManager.PERMISSION_GRANTED||opsMode != AppOpsManager.MODE_ALLOWED)
- //
- // request_list.add(permission);
- //
- // }
- // File logfile = new File(Environment.getExternalStorageDirectory()
- // .getPath() + "/redant/pop/" + "debug" + ".log");
- //
- // // int mode = Application.MODE_APPEND;
- // if (!logfile.exists())
- // {
- // // mode = Application.MODE_PRIVATE;
- // try
- // {
- // logfile.createNewFile();
- // }
- // catch (IOException e)
- // {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- //
- // }
- // }
- if(permissions.length==0)
- return true;
- ActivityCompat.requestPermissions(activity,
- permissions,
- MY_PERMISSIONS_REQUEST);
- return true;
- //
- // if (ContextCompat.checkSelfPermission(activity,
- // Manifest.permission.ACCESS_FINE_LOCATION)
- // != PackageManager.PERMISSION_GRANTED)
- // {
- //
- //
- //// if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
- //// permission))
- //// {
- //// return false;
- //// }
- //// else
- // {
- // ActivityCompat.requestPermissions(activity,
- // new String[]{permission},
- // MY_PERMISSIONS_REQUEST);
- // return true;
- // }
- // } else
- // {
- // return true;
- // }
- // if(true)
- // return true;
- // 调用Context的checkCallingOrSelfPermission来检查权限
- // Activity activity1 = (Activity) activity;
- // if(activity==null)
- // return false;
- // if (PackageManager.PERMISSION_GRANTED!= context.checkCallingOrSelfPermission(permission)) {
- //
- //// String[] tmp = permission.split("\\.");
- //// String permission_name = tmp[tmp.length - 1];
- //// permission_name = permission_name.replace("_"," ");
- ////
- ////
- //// new AlertDialog.Builder(context)
- //// .setTitle("Warning")
- //// .setMessage("RA Image need " + permission_name.toLowerCase() + " permission")
- //// .setPositiveButton("OK", null)
- //// .show();
- ////
- //// return false;
- //
- //
- // if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
- // permission)) {
- //
- // Log.d("_RAIMAGE", "checkPermission: shouldShowRequestPermissionRationale TRUE " + permission);
- // // Show an expanation to the user *asynchronously* -- don't block
- // // this thread waiting for the user's response! After the user
- // // sees the explanation, try again to request the permission.
- //
- // } else {
- //
- // // No explanation needed, we can request the permission.
- //
- //
- // Log.d("_RAIMAGE", "checkPermission: shouldShowRequestPermissionRationale false "+ permission);
- // ActivityCompat.requestPermissions(activity,
- // new String[]{permission},
- // 123);
- //
- // // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
- // // app-defined int constant. The callback method gets the
- // // result of the request.
- // }
- //
- // }
- // return true;
- }
- public static String lastPathComponent(String path){
- int start=path.lastIndexOf("/");
- int end=path.lastIndexOf(".");
- if(start!=-1 && end!=-1){
- return path.substring(start+1,end);
- }else{
- return null;
- }
- }
- public static File routeBitmap(File sourceFile,String file_name)
- {
- /**
- * 获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转
- */
- int degree = RAUtil.readPictureDegree(sourceFile.getAbsolutePath());
- BitmapFactory.Options opts=new BitmapFactory.Options();//获取缩略图显示到屏幕上
- opts.inSampleSize=1;
- Bitmap cbitmap=BitmapFactory.decodeFile(sourceFile.getAbsolutePath(),opts);
- /**
- * 把图片旋转为正的方向
- */
- Bitmap newbitmap = RAUtil.rotaingImageView(degree, cbitmap);
- //sourceFile.get
- String routedPath = "";
- if(file_name==null) {
- String path = sourceFile.getPath();
- routedPath=path.replace(".jpg", "_routed.jpg");
- }
- else
- {
- String path = sourceFile.getParent();
- routedPath = path+"/"+file_name;
- }
- File routedFile = new File(routedPath);
- try {
- FileOutputStream outputStream = new FileOutputStream(routedFile);
- newbitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
- outputStream.flush();
- outputStream.close();
- sourceFile.delete();
- // scaleFile.renameTo(new File(path));
- return routedFile;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 读取图片属性:旋转的角度
- * @param path 图片绝对路径
- * @return degree旋转的角度
- */
- public static int readPictureDegree(String path) {
- int degree = 0;
- try {
- ExifInterface exifInterface = new ExifInterface(path);
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
- switch (orientation) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- degree = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- degree = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- degree = 270;
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return degree;
- }
- /*
- * 旋转图片
- * @param angle
- * @param bitmap
- * @return Bitmap
- */
- public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
- //旋转图片 动作
- Matrix matrix = new Matrix();;
- matrix.postRotate(angle);
- System.out.println("angle2=" + angle);
- // 创建新的图片
- Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
- bitmap.getWidth(), bitmap.getHeight(), matrix, true);
- return resizedBitmap;
- }
- public static JSONObject Bundle2Json(Bundle bundle)
- {
- JSONObject json = new JSONObject();
- Set<String> keys = bundle.keySet();
- for (String key : keys) {
- try {
- Log.d("", "Bundle2Json: key:"+key+" val: "+wrap(bundle.get(key)));
- json.put(key, wrap(bundle.get(key))); //see edit below
- // json.put(key, JSONObject.wrap(bundle.get(key)));
- } catch(JSONException e) {
- //Handle exception here
- }
- }
- return json;
- }
- private static Object wrap(Object o) {
- if (o == null) {
- return JSONObject.NULL;
- }
- if (o instanceof JSONArray || o instanceof JSONObject) {
- return o;
- }
- if (o.equals(JSONObject.NULL)) {
- return o;
- }
- try {
- if (o instanceof Bundle)
- {
- return Bundle2Json((Bundle) o);
- }
- if (o instanceof Collection) {
- return new JSONArray((Collection) o);
- } else if (o.getClass().isArray()) {
- return toJSONArray(o);
- }
- if (o instanceof Map) {
- return new JSONObject((Map) o);
- }
- if (o instanceof Boolean ||
- o instanceof Byte ||
- o instanceof Character ||
- o instanceof Double ||
- o instanceof Float ||
- o instanceof Integer ||
- o instanceof Long ||
- o instanceof Short ||
- o instanceof String) {
- return o;
- }
- if (o.getClass().getPackage().getName().startsWith("java.")) {
- return o.toString();
- }
- } catch (Exception ignored) {
- }
- return null;
- }
- private static JSONArray toJSONArray(Object array) throws JSONException {
- JSONArray result = new JSONArray();
- if (!array.getClass().isArray()) {
- throw new JSONException("Not a primitive array: " + array.getClass());
- }
- final int length = Array.getLength(array);
- for (int i = 0; i < length; ++i) {
- result.put(wrap(Array.get(array, i)));
- }
- return result;
- }
- public static Bundle Json2Bundle(JSONObject s) {
- Bundle bundle = new Bundle();
- for (Iterator<String> it = s.keys(); it.hasNext(); ) {
- String key = it.next();
- try {
- Object o = s.get(key);
- if (o == null) {
- continue;
- }
- else if ( o instanceof JSONObject) {
- bundle.putBundle(key,Json2Bundle((JSONObject) o));
- }
- else if(o instanceof JSONArray )
- {
- throw new JSONException("Json2Bundle does not support Json array: " + o.getClass());// bundle.putarr
- }
- else if (o.equals(JSONObject.NULL)) {
- continue;
- }
- else if (o instanceof Boolean)
- {
- bundle.putBoolean(key,((Boolean) o).booleanValue());
- }
- else if(o instanceof Byte)
- {
- bundle.putByte(key, ((Byte) o).byteValue());
- }
- else if(o instanceof Character)
- {
- bundle.putChar(key, ((Character) o).charValue());
- }
- else if(o instanceof Double)
- {
- bundle.putDouble(key, ((Double) o).doubleValue());
- }
- else if(o instanceof Float)
- {
- bundle.putFloat(key, ((Float) o).floatValue());
- }
- else if(o instanceof Integer)
- {
- bundle.putInt(key, ((Integer) o).intValue());
- }
- else if(o instanceof Long)
- {
- bundle.putLong(key, ((Long) o).longValue());
- }
- else if(o instanceof Short)
- {
- bundle.putShort(key, ((Short) o).shortValue());
- }
- else if(o instanceof String)
- {
- bundle.putString(key, o.toString());
- }
- // ArrayList<Parcelable> a = new ArrayList<>();
- // a.add("");
- // a.add(1);
- // bundle.putParcelableArrayList("arr", a);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- return bundle;
- }
- public static Bundle Json2Bundle1(JSONObject s) {
- Bundle bundle = new Bundle();
- for (Iterator<String> it = s.keys(); it.hasNext(); ) {
- String key = it.next();
- JSONArray arr = s.optJSONArray(key);
- Double num = s.optDouble(key);
- // Integer i = s.optInt(key);
- String str = s.optString(key);
- JSONObject obj = s.optJSONObject(key);
- if (arr != null && arr.length() <= 0)
- {
- bundle.putStringArray(key, new String[]{});
- }
- else if (arr != null && !Double.isNaN(arr.optDouble(0))) {
- double[] newarr = new double[arr.length()];
- for (int i=0; i<arr.length(); i++)
- newarr[i] = arr.optDouble(i);
- bundle.putDoubleArray(key, newarr);
- }
- else if (arr != null && arr.optString(0) != null) {
- String[] newarr = new String[arr.length()];
- for (int i=0; i<arr.length(); i++)
- newarr[i] = arr.optString(i);
- bundle.putStringArray(key, newarr);
- }
- else if (obj != null)
- {
- Bundle b=Json2Bundle(obj);
- bundle.putBundle(key, b);
- }
- else if (!num.isNaN())
- bundle.putDouble(key, num);
- else if (str != null)
- bundle.putString(key, str);
- else
- System.err.println("unable to transform json to bundle " + key);
- }
- return bundle;
- }
- // public static Bundle Json2Bundle(JSONObject jsonObject) throws JSONException {
- // Bundle bundle = new Bundle();
- // Iterator iter = jsonObject.keys();
- // while(iter.hasNext()){
- // String key = (String)iter.next();
- // String value = jsonObject.getString(key);
- // bundle.putString(key,value);
- // }
- // return bundle;
- // }
- }
|