optimize code

androidx
Ztiany 5 years ago
parent aea71da6c6
commit 2c66651ed2
  1. 51
      lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java
  2. 2
      lib_base/src/main/java/com/android/base/utils/common/Lang.kt
  3. 4
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/BoxingUcrop.java
  4. 28
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/MediaSelectorStyle.java
  5. 28
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/StyleUtils.java
  6. 11
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/SystemMediaSelector.java
  7. 2
      lib_media_selector/src/main/java/com/android/sdk/mediaselector/Utils.java

@ -1,18 +1,11 @@
package com.android.base.utils.android; package com.android.base.utils.android;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import com.android.base.utils.android.compat.AndroidVersion;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.UUID;
import androidx.annotation.RequiresPermission;
import timber.log.Timber;
public class DevicesUtils { public class DevicesUtils {
@ -30,50 +23,6 @@ public class DevicesUtils {
return model; return model;
} }
/**
* for getDeviceIdDevicesId 形式
* <pre>
* 00000000-1082-436b-ffff-ffffc2e337a1
* ffffffff-a987-897a-0000-000000de11f7
* </pre>
*/
private static final String M_SZ_DEV_ID_SHORT = "35" +
Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
Build.USER.length() % 10; //13 位
/**
* 参考http://blog.csdn.net/nugongahou110/article/details/47003257
* 参考https://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id/2853253#2853253
* 参考https://www.jianshu.com/p/b6f4b0aca6b0
*
* @return device id
*/
@RequiresPermission(value = Manifest.permission.READ_PHONE_STATE)
public static String getDeviceId() {
String serial = null;
try {
if (AndroidVersion.atLeast(26)) {
//add in api 26, need Permission.READ_PHONE_STATE
serial = Build.getSerial();
} else {
serial = Build.class.getField("SERIAL").get(null).toString();
}
} catch (Exception e) {
Timber.w("getDeviceId error: " + e.getMessage());
}
if (serial == null) {
//serial 需要一个初始化
serial = "serial"; // 随便一个初始化
}
//使用硬件信息拼凑出来的15位号码
return new UUID(M_SZ_DEV_ID_SHORT.hashCode(), serial.hashCode()).toString();
}
@SuppressLint("ObsoleteSdkInt") @SuppressLint("ObsoleteSdkInt")
public static void printSystemInfo() { public static void printSystemInfo() {
Date date = new Date(System.currentTimeMillis()); Date date = new Date(System.currentTimeMillis());

@ -77,7 +77,6 @@ fun closeIO(closeable: Closeable?) {
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} }
} }
} }
@ -90,7 +89,6 @@ fun closeIOQuietly(closeable: Closeable?) {
closeable.close() closeable.close()
} catch (ignored: IOException) { } catch (ignored: IOException) {
} }
} }
} }

@ -25,10 +25,8 @@ import android.net.Uri;
import com.bilibili.boxing.loader.IBoxingCrop; import com.bilibili.boxing.loader.IBoxingCrop;
import com.bilibili.boxing.model.config.BoxingCropOption; import com.bilibili.boxing.model.config.BoxingCropOption;
import com.yalantis.ucrop.UCrop; import com.yalantis.ucrop.UCrop;
import com.ztiany.mediaselector.R;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
/** /**
@ -53,7 +51,7 @@ final class BoxingUcrop implements IBoxingCrop {
crop.withAspectRatio(cropConfig.getAspectRatioX(), cropConfig.getAspectRatioY()); crop.withAspectRatio(cropConfig.getAspectRatioX(), cropConfig.getAspectRatioY());
//颜色 //颜色
int color = ContextCompat.getColor(context, R.color.boxing_colorPrimaryDark); int color = MediaSelectorStyle.fetchPrimaryColor();
crop.setToolbarColor(color); crop.setToolbarColor(color);
crop.setStatusBarColor(color); crop.setStatusBarColor(color);

@ -0,0 +1,28 @@
package com.android.sdk.mediaselector;
import android.graphics.Color;
import androidx.annotation.ColorInt;
/**
* @author Ztiany
* Email: ztiany3@gmail.com
* Date : 2019-11-22 11:36
*/
public class MediaSelectorStyle {
private static int sPrimaryColor = -1;
public static void setPrimaryColor(@ColorInt int primaryColor) {
sPrimaryColor = primaryColor;
}
static int fetchPrimaryColor() {
if (sPrimaryColor == -1) {
return Color.WHITE;
} else {
return sPrimaryColor;
}
}
}

@ -1,28 +0,0 @@
package com.android.sdk.mediaselector;
import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
/**
* @author Ztiany
* Email: ztiany3@gmail.com
* Date : 2019-11-22 11:36
*/
class StyleUtils {
//https://stackoverflow.com/questions/27611173/how-to-get-accent-color-programmatically
static int fetchPrimaryColor(Context context) {
int colorAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
colorAttr = android.R.attr.colorPrimary;
} else {
//Get colorAccent defined for AppCompat
colorAttr = context.getResources().getIdentifier("colorPrimary", "attr", context.getPackageName());
}
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(colorAttr, outValue, true);
return outValue.data;
}
}

@ -30,7 +30,7 @@ public class SystemMediaSelector {
private static final int REQUEST_CROP = 197; private static final int REQUEST_CROP = 197;
private static final int REQUEST_ALBUM = 198; private static final int REQUEST_ALBUM = 198;
private static final int REQUEST_FILE = 199; private static final int REQUEST_FILE = 199;
private static final int REQUEST_LINRARY_CROP = 200; private static final int REQUEST_LIBRARY_CROP = 200;
private static final String POSTFIX = ".file.provider"; private static final String POSTFIX = ".file.provider";
private String mAuthority; private String mAuthority;
@ -87,6 +87,7 @@ public class SystemMediaSelector {
mActivity.startActivityForResult(intent, code); mActivity.startActivityForResult(intent, code);
} }
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// setter // setter
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -119,7 +120,7 @@ public class SystemMediaSelector {
} }
/** /**
* 为了保证裁裁剪图片不出问题务必指定CropOptions中的各个参数(不要为0比如魅族手机如果指定OutputX和OutputY为0则只会裁减出一个像素)否则可能出现问题 * 为了保证裁裁剪图片不出问题务必指定 CropOptions 中的各个参数不要为 0否则可能出现问题比如魅族手机如果指定 OutputX OutputY 0则只会裁减出一个像素
*/ */
public boolean takePhotoFromCameraAndCrop(String savePath, @Nullable CropOptions cropOptions, String cropTitle) { public boolean takePhotoFromCameraAndCrop(String savePath, @Nullable CropOptions cropOptions, String cropTitle) {
mSavePhotoPath = savePath; mSavePhotoPath = savePath;
@ -230,7 +231,7 @@ public class SystemMediaSelector {
processAlbumResult(resultCode, data); processAlbumResult(resultCode, data);
} else if (requestCode == REQUEST_FILE) { } else if (requestCode == REQUEST_FILE) {
processFileResult(resultCode, data); processFileResult(resultCode, data);
} else if (requestCode == REQUEST_LINRARY_CROP) { } else if (requestCode == REQUEST_LIBRARY_CROP) {
processUCropResult(data); processUCropResult(data);
} }
} }
@ -264,7 +265,7 @@ public class SystemMediaSelector {
Uri uri = data.getData(); Uri uri = data.getData();
if (mNeedCrop) { if (mNeedCrop) {
if (mUseInnerCrop) { if (mUseInnerCrop) {
Utils.toUCrop(getContext(), mFragment, Utils.getAbsolutePath(getContext(), uri), mSavePhotoPath, getCropOptions(), REQUEST_LINRARY_CROP); Utils.toUCrop(getContext(), mFragment, Utils.getAbsolutePath(getContext(), uri), mSavePhotoPath, getCropOptions(), REQUEST_LIBRARY_CROP);
} else { } else {
boolean success = toCropPhotoFromAlbum(uri); boolean success = toCropPhotoFromAlbum(uri);
if (!success) { if (!success) {
@ -310,7 +311,7 @@ public class SystemMediaSelector {
} }
if (mUseInnerCrop) { if (mUseInnerCrop) {
Utils.toUCrop(getContext(), mFragment, mSavePhotoPathForCropCamera, mSavePhotoPath, getCropOptions(), REQUEST_LINRARY_CROP); Utils.toUCrop(getContext(), mFragment, mSavePhotoPathForCropCamera, mSavePhotoPath, getCropOptions(), REQUEST_LIBRARY_CROP);
} else { } else {
boolean success = toCropPhotoFromCamera(); boolean success = toCropPhotoFromCamera();
if (!success) { if (!success) {

@ -202,7 +202,7 @@ final class Utils {
crop.withAspectRatio(cropConfig.getAspectX(), cropConfig.getAspectY()); crop.withAspectRatio(cropConfig.getAspectX(), cropConfig.getAspectY());
//颜色 //颜色
int color = StyleUtils.fetchPrimaryColor(context); int color = MediaSelectorStyle.fetchPrimaryColor();
crop.setToolbarColor(color); crop.setToolbarColor(color);
crop.setStatusBarColor(color); crop.setStatusBarColor(color);

Loading…
Cancel
Save