diff --git a/app/src/main/java/com/ss/smartstorage/Context.kt b/app/src/main/java/com/ss/smartstorage/Context.kt new file mode 100755 index 0000000..f9ccec9 --- /dev/null +++ b/app/src/main/java/com/ss/smartstorage/Context.kt @@ -0,0 +1,34 @@ +package com.ss.smartstorage + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.text.TextUtils +import android.widget.Toast + + +fun Context.toast(id: Int, length: Int = Toast.LENGTH_SHORT) { + toast(getString(id), length) +} + +fun Context.toast(msg: String, length: Int = Toast.LENGTH_SHORT) { + try { + if (isOnMainThread()) { + if (!TextUtils.isEmpty(msg)) { + Toast.makeText(applicationContext, msg, length).show() + } + } else { + if (!TextUtils.isEmpty(msg)) { + Handler(Looper.getMainLooper()).post { + if (!TextUtils.isEmpty(msg)) { + Toast.makeText(applicationContext, msg, length).show() + } + } + } + } + } catch (_: Exception) {} +} + +fun isOnMainThread() = Looper.myLooper() == Looper.getMainLooper() + + diff --git a/app/src/main/java/com/ss/smartstorage/MainActivity.kt b/app/src/main/java/com/ss/smartstorage/MainActivity.kt index 0cb29d3..a8161eb 100644 --- a/app/src/main/java/com/ss/smartstorage/MainActivity.kt +++ b/app/src/main/java/com/ss/smartstorage/MainActivity.kt @@ -2,24 +2,25 @@ package com.ss.smartstorage import android.annotation.SuppressLint import android.os.Bundle +import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.ui.Modifier +import com.ss.smart_storage.OutputListener import com.ss.smart_storage.SmartStorage import com.ss.smartstorage.ui.theme.SmartStorageTheme -class MainActivity : ComponentActivity() { +class MainActivity : ComponentActivity(), OutputListener { @SuppressLint("NewApi") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val smartStorage = SmartStorage(this) - - + smartStorage.registerListener(this) setContent { SmartStorageTheme { Surface( @@ -41,7 +42,15 @@ class MainActivity : ComponentActivity() { } } + override fun onSuccess(result: String?) { + result?.let { toast(it) } + Log.e("MainAct", "onSuccess result: $result") + } + override fun onFail(error: String?) { + error?.let { toast(it) } + Log.e("MainAct", "onFail error: $error") + } } diff --git a/app/src/main/java/com/ss/smartstorage/ui/theme/Theme.kt b/app/src/main/java/com/ss/smartstorage/ui/theme/Theme.kt index 5d80a49..e403515 100644 --- a/app/src/main/java/com/ss/smartstorage/ui/theme/Theme.kt +++ b/app/src/main/java/com/ss/smartstorage/ui/theme/Theme.kt @@ -39,7 +39,7 @@ private val LightColorScheme = lightColorScheme( @Composable fun SmartStorageTheme( - darkTheme: Boolean = isSystemInDarkTheme(), + darkTheme: Boolean = false, // Dynamic color is available on Android 12+ dynamicColor: Boolean = true, content: @Composable () -> Unit diff --git a/smart-storage/src/main/java/com/ss/smart_storage/OutputListener.kt b/smart-storage/src/main/java/com/ss/smart_storage/OutputListener.kt new file mode 100644 index 0000000..c003672 --- /dev/null +++ b/smart-storage/src/main/java/com/ss/smart_storage/OutputListener.kt @@ -0,0 +1,6 @@ +package com.ss.smart_storage + +interface OutputListener { + fun onSuccess(result: String?) + fun onFail(error: String?) +} \ No newline at end of file diff --git a/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt b/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt index 042c41a..71775db 100644 --- a/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt +++ b/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt @@ -27,7 +27,11 @@ class SmartStorage(private val activity: ComponentActivity) { private var baseDocumentTreeUri: Uri? = null private lateinit var fileDetails: FileDetails + private var listener: OutputListener? = null + fun registerListener(listener: OutputListener) { + this.listener = listener + } @SuppressLint("NewApi") private val permissionManager = @@ -130,8 +134,10 @@ class SmartStorage(private val activity: ComponentActivity) { FileOutputStream(file).use { stream -> stream.write(fileDetails.fileData) } + listener?.onSuccess(file.path) } catch (e: Exception) { e.printStackTrace() + listener?.onFail(e.message) } }