From fb8c8c90d5a2de7c0158986a6b3ea7f8bb71fb18 Mon Sep 17 00:00:00 2001 From: Ztiany Date: Thu, 24 Oct 2019 21:03:10 +0800 Subject: [PATCH] update Base --- .../base/app/fragment/BaseDialogFragment.kt | 14 +++++++++ .../android/base/app/fragment/BaseFragment.kt | 14 +++++++++ .../com/android/base/app/fragment/utils.kt | 13 ++++++++ .../com/android/base/imageloader/Source.java | 3 ++ .../base/utils/android/ActFragWrapper.java | 12 +++---- .../android/base/utils/android/BitmapUtils.kt | 21 +++++++++++++ .../com/android/base/utils/common/Files.kt | 31 ++++++------------- .../com/android/sdk/qrcode/CameraUtils.java | 2 +- .../java/com/android/sdk/qrcode/Debug.java | 6 ++-- .../java/com/android/sdk/qrcode/QRCodeView.kt | 2 -- 10 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 lib_base/src/main/java/com/android/base/utils/android/BitmapUtils.kt diff --git a/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.kt index a602eb9..a3d59c5 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.kt @@ -39,6 +39,18 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr private var recentShowingDialogTime: Long = 0 + private var _state: Bundle? = null + + protected val state: Bundle + get() { + var state = _state + if (state == null) { + state = Bundle() + _state = state + } + return state + } + private fun tag() = this.javaClass.simpleName override fun onAttach(context: Context) { @@ -49,6 +61,7 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + _state = getInstanceState(savedInstanceState) Timber.tag(tag()).d("-->onCreate savedInstanceState = $savedInstanceState") fragmentDelegates.onCreate(savedInstanceState) } @@ -156,6 +169,7 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr override fun onSaveInstanceState(outState: Bundle) { fragmentDelegates.onSaveInstanceState(outState) + saveInstanceState(outState, _state) super.onSaveInstanceState(outState) } diff --git a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.kt index 12b6e20..efdddf1 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.kt @@ -50,6 +50,18 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment private var recentShowingDialogTime: Long = 0 + private var _state: Bundle? = null + + protected val state: Bundle + get() { + var state = _state + if (state == null) { + state = Bundle() + _state = state + } + return state + } + private fun tag() = this.javaClass.simpleName override fun onAttach(context: Context) { @@ -60,6 +72,7 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + _state = getInstanceState(savedInstanceState) Timber.tag(tag()).d("-->onCreate savedInstanceState = $savedInstanceState") fragmentDelegates.onCreate(savedInstanceState) } @@ -167,6 +180,7 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment override fun onSaveInstanceState(outState: Bundle) { fragmentDelegates.onSaveInstanceState(outState) + saveInstanceState(outState, _state) super.onSaveInstanceState(outState) } diff --git a/lib_base/src/main/java/com/android/base/app/fragment/utils.kt b/lib_base/src/main/java/com/android/base/app/fragment/utils.kt index a264d78..c802401 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/utils.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/utils.kt @@ -1,5 +1,6 @@ package com.android.base.app.fragment +import android.os.Bundle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import com.android.base.app.ui.LoadingView @@ -36,4 +37,16 @@ internal fun T.dismissDialog(recentShowingDialogTime: Long, minimumMills: Lo } } +} + +private const val FRAGMENT_STATE_KEY = "fragment_state_key" + +internal fun saveInstanceState(outState: Bundle, _state: Bundle?) { + _state?.let { + outState.putBundle(FRAGMENT_STATE_KEY, it) + } +} + +internal fun getInstanceState(state: Bundle?): Bundle? { + return state?.getBundle(FRAGMENT_STATE_KEY) } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/imageloader/Source.java b/lib_base/src/main/java/com/android/base/imageloader/Source.java index 40220d2..8fb89ec 100644 --- a/lib_base/src/main/java/com/android/base/imageloader/Source.java +++ b/lib_base/src/main/java/com/android/base/imageloader/Source.java @@ -13,6 +13,9 @@ public class Source { Uri mUri; byte[] mBytes; + private Source() { + } + public static Source create(String url) { Source source = new Source(); source.mUrl = url; diff --git a/lib_base/src/main/java/com/android/base/utils/android/ActFragWrapper.java b/lib_base/src/main/java/com/android/base/utils/android/ActFragWrapper.java index d5beac6..8706c09 100644 --- a/lib_base/src/main/java/com/android/base/utils/android/ActFragWrapper.java +++ b/lib_base/src/main/java/com/android/base/utils/android/ActFragWrapper.java @@ -24,6 +24,12 @@ public class ActFragWrapper { return context; } + public static ActFragWrapper create(Fragment fragment) { + ActFragWrapper context = new ActFragWrapper(); + context.mFragment = fragment; + return context; + } + public Context getContext() { if (mActivity != null) { return mActivity; @@ -44,12 +50,6 @@ public class ActFragWrapper { } } - public static ActFragWrapper create(Fragment fragment) { - ActFragWrapper context = new ActFragWrapper(); - context.mFragment = fragment; - return context; - } - public void startService(Intent intent) { if (mActivity != null) { mActivity.startService(intent); diff --git a/lib_base/src/main/java/com/android/base/utils/android/BitmapUtils.kt b/lib_base/src/main/java/com/android/base/utils/android/BitmapUtils.kt new file mode 100644 index 0000000..c852f42 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/utils/android/BitmapUtils.kt @@ -0,0 +1,21 @@ +package com.android.base.utils.android + +import android.graphics.Bitmap +import androidx.annotation.IntRange +import com.android.base.utils.common.makeParentPath +import java.io.File +import java.io.FileOutputStream + + +fun Bitmap.saveToFile(format: Bitmap.CompressFormat, @IntRange(from = 1, to = 100) quality: Int, savePath: String): Boolean { + return try { + val file = File(savePath) + file.makeParentPath() + file.createNewFile() + compress(format, quality, FileOutputStream(file)) + true + } catch (e: Exception) { + e.printStackTrace() + false + } +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/utils/common/Files.kt b/lib_base/src/main/java/com/android/base/utils/common/Files.kt index 2beb48a..3ad8db2 100644 --- a/lib_base/src/main/java/com/android/base/utils/common/Files.kt +++ b/lib_base/src/main/java/com/android/base/utils/common/Files.kt @@ -6,20 +6,17 @@ import com.blankj.utilcode.util.FileUtils import timber.log.Timber import java.io.File -fun File?.makeFilePath(): Boolean { - if (this == null) { - return false - } - val parent = this.parentFile - return parent.exists() || makeDir(parent) +fun File.makeParentPath(): Boolean { + val path = this.parentFile + return path.exists() || makeDir(path) } private fun makeDir(file: File?): Boolean { return file != null && (file.exists() || file.mkdirs()) } -fun File?.sizeOf(): Long { - if (this == null || !this.exists()) { +fun File.sizeOf(): Long { + if (!this.exists()) { return 0 } return if (this.isDirectory) { @@ -44,25 +41,15 @@ fun File.deleteSelf() { } } -fun File?.isFileExists(): Boolean { - return this != null && this.isFile && this.exists() -} - -/** - * 获取全路径中的文件拓展名 - * - * @return 文件拓展名 - */ -fun File?.getFileExtension(): String? { - return if (this == null) null else getFileExtension(this.path) +fun File.isFileExists(): Boolean { + return this.isFile && this.exists() } /** * 获取全路径中的文件拓展名 * - * @param filePath 文件路径 * @return 文件拓展名 */ -fun getFileExtension(filePath: String): String { - return FileUtils.getFileExtension(filePath) +fun File.getFileExtension(): String { + return FileUtils.getFileExtension(this.absolutePath) } \ No newline at end of file diff --git a/lib_qrcode/src/main/java/com/android/sdk/qrcode/CameraUtils.java b/lib_qrcode/src/main/java/com/android/sdk/qrcode/CameraUtils.java index 54aa372..87f3f2a 100644 --- a/lib_qrcode/src/main/java/com/android/sdk/qrcode/CameraUtils.java +++ b/lib_qrcode/src/main/java/com/android/sdk/qrcode/CameraUtils.java @@ -127,4 +127,4 @@ public class CameraUtils { return supportedPreviewResolutions.get(supportedPreviewResolutions.size() - 1); } -} +} \ No newline at end of file diff --git a/lib_qrcode/src/main/java/com/android/sdk/qrcode/Debug.java b/lib_qrcode/src/main/java/com/android/sdk/qrcode/Debug.java index ef829c9..c309da3 100644 --- a/lib_qrcode/src/main/java/com/android/sdk/qrcode/Debug.java +++ b/lib_qrcode/src/main/java/com/android/sdk/qrcode/Debug.java @@ -7,16 +7,16 @@ import android.util.Log; * Email: ztiany3@gmail.com * Date : 2019-04-02 12:55 */ -public class Debug { +class Debug { private static final String TAG = "QRCODE"; private static boolean debug = BuildConfig.DEBUG; - public static void setDebug(boolean debug) { + static void setDebug(boolean debug) { Debug.debug = debug; } - public static void log(String msg) { + static void log(String msg) { if (debug && msg != null) { Log.d(TAG, msg); } diff --git a/lib_qrcode/src/main/java/com/android/sdk/qrcode/QRCodeView.kt b/lib_qrcode/src/main/java/com/android/sdk/qrcode/QRCodeView.kt index e82ad9b..49a6372 100644 --- a/lib_qrcode/src/main/java/com/android/sdk/qrcode/QRCodeView.kt +++ b/lib_qrcode/src/main/java/com/android/sdk/qrcode/QRCodeView.kt @@ -197,7 +197,6 @@ abstract class QRCodeView @JvmOverloads constructor( } catch (e: Exception) { e.printStackTrace() } - } /** @@ -209,7 +208,6 @@ abstract class QRCodeView @JvmOverloads constructor( } catch (e: Exception) { e.printStackTrace() } - } /**