Skip to content

Commit a9c6c7d

Browse files
authored
Merge pull request #19 from rosuH/dev
Release 1.01.1
2 parents 61749b4 + ba37f13 commit a9c6c7d

30 files changed

Lines changed: 496 additions & 176 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Third party libraries used.
8383
- [daniel-stonuk/material-about-library](https://github.com/daniel-stoneuk/material-about-library)
8484
- [skydoves/ColorPickerView](https://github.com/skydoves/ColorPickerView)
8585
- [material-components/material-components-android](https://github.com/material-components/material-components-android)
86+
- [Compressor](https://github.com/zetbaitsu/Compressor/)
8687

8788
## Feedback and contribution
8889
We welcome your comments and suggestions in the issues section, or you can contribute your code by submitting a PR directly to us.

README_zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
- [daniel-stoneuk/material-about-library](https://github.com/daniel-stoneuk/material-about-library)
8282
- [skydoves/ColorPickerView](https://github.com/skydoves/ColorPickerView)
8383
- [material-components/material-components-android](https://github.com/material-components/material-components-android)
84+
- [Compressor](https://github.com/zetbaitsu/Compressor/)
8485

8586
## 反馈与贡献
8687
我们非常欢迎您在 issues 区发表您任何意见与建议,或者直接提交 PR 以贡献您的代码。

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313
android {
1414
compileSdkVersion(Apps.compileSdk)
1515
buildToolsVersion(Apps.buildTools)
16-
16+
ndkVersion = "21.3.6528147"
1717
defaultConfig {
1818
applicationId = "me.rosuh.easywatermark"
1919
minSdkVersion(Apps.minSdk)
@@ -88,6 +88,6 @@ dependencies {
8888
implementation(Libs.recycleView)
8989
implementation(Libs.constraintLayout)
9090
implementation(Libs.coreKtx)
91-
implementation(Libs.gifDrawable)
91+
implementation(Libs.exif)
9292
testImplementation(TestLibs.junit)
9393
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Easy WaterMark·Debug</string>
4+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">简单水印·测试</string>
3+
</resources>

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
android:theme="@style/AppTheme">
2121
<activity
2222
android:name=".ui.MainActivity"
23+
android:configChanges="orientation|keyboardHidden"
24+
android:screenOrientation="portrait"
2325
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
2426
<intent-filter>
2527
<action android:name="android.intent.action.MAIN" />
@@ -29,6 +31,8 @@
2931
</activity>
3032
<activity
3133
android:name=".ui.about.AboutActivity"
34+
android:configChanges="orientation|keyboardHidden"
35+
android:screenOrientation="portrait"
3236
android:parentActivityName=".ui.MainActivity"
3337
android:theme="@style/AppTheme.MaterialAboutActivity">
3438
<meta-data
@@ -37,6 +41,8 @@
3741
</activity>
3842
<activity
3943
android:name=".ui.about.OpenSourceActivity"
44+
android:configChanges="orientation|keyboardHidden"
45+
android:screenOrientation="portrait"
4046
android:parentActivityName=".ui.about.AboutActivity">
4147
<meta-data
4248
android:name="android.support.PARENT_ACTIVITY"

app/src/main/java/me/rosuh/easywatermark/MyApp.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ class MyApp : Application() {
1717

1818
private fun catchException() {
1919
Thread.setDefaultUncaughtExceptionHandler { t, e ->
20+
// Because intent limit data to 1mb, so that we should limit the stack track by magic number below
21+
val maxStringLength = 1024 * 1024 / 2 / 10 // the 10 is a magic number ;)
22+
var fullStackTrace = Log.getStackTraceString(e)
23+
if (fullStackTrace.length > maxStringLength) {
24+
fullStackTrace = fullStackTrace.substring(IntRange(0, maxStringLength))
25+
}
2026
getSharedPreferences(SP_NAME, MODE_PRIVATE).edit(true) {
2127
putBoolean(KEY_IS_CRASH, true)
2228
putString(
2329
KEY_STACK_TRACE, """
2430
Crash in ${t.name}:
25-
${Log.getStackTraceString(e)}
31+
$fullStackTrace
2632
""".trimIndent()
2733
)
2834
}

app/src/main/java/me/rosuh/easywatermark/ktx/PainKtx.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@ import android.graphics.Color
44
import android.graphics.Paint
55
import me.rosuh.easywatermark.model.WaterMarkConfig
66

7-
fun Paint.applyConfig(config: WaterMarkConfig?): Paint {
8-
textSize = config?.textSize ?: 14f
7+
/**
8+
* @author rosuh@qq.com
9+
* @date 2020/9/8
10+
* 因为预览和实际图像之间存在缩放,所以在预览时要除去缩放比。而在保存时,就不需要了
11+
* Because there is a zoom between the preview and the actual image, the zoom ratio should be removed when previewing
12+
* When saving, it’s not needed
13+
*/
14+
fun Paint.applyConfig(
15+
config: WaterMarkConfig?,
16+
scale: Boolean = true
17+
): Paint {
18+
textSize = (config?.textSize ?: 14f) / if (scale) {
19+
(config?.imageScale ?: 1f)
20+
} else {
21+
1f
22+
}
923
color = config?.textColor ?: Color.RED
1024
alpha = config?.alpha ?: 128
1125
style = config?.textStyle ?: Paint.Style.FILL

app/src/main/java/me/rosuh/easywatermark/model/WaterMarkConfig.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class WaterMarkConfig {
3232

3333
var markMode: MarkMode
3434

35+
var imageScale: Float
36+
3537
init {
3638
with(
3739
MyApp.instance.getSharedPreferences(SP_NAME, MODE_PRIVATE)
@@ -68,6 +70,7 @@ class WaterMarkConfig {
6870
} else {
6971
Uri.parse("")
7072
}
73+
imageScale = getFloat(SP_KEY_TEXT_SIZE, 1f)
7174
}
7275
}
7376

@@ -99,6 +102,7 @@ class WaterMarkConfig {
99102
MarkMode.Image -> 1
100103
}
101104
)
105+
putFloat(SP_KEY_IMAGE_SCALE, imageScale)
102106
}
103107
}
104108

@@ -131,6 +135,7 @@ class WaterMarkConfig {
131135
const val SP_KEY_ICON_URI = SP_NAME + "_key_icon_uri"
132136
const val SP_KEY_MODE = SP_NAME + "_key_type"
133137
const val SP_KEY_CHANGE_LOG = SP_NAME + "_key_change_log"
138+
const val SP_KEY_IMAGE_SCALE = SP_NAME + "_key_image_scale"
134139

135140
}
136141
}

app/src/main/java/me/rosuh/easywatermark/ui/MainActivity.kt

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import me.rosuh.easywatermark.ui.dialog.SaveImageBSDialogFragment
3636
import me.rosuh.easywatermark.ui.panel.ContentFragment
3737
import me.rosuh.easywatermark.ui.panel.LayoutFragment
3838
import me.rosuh.easywatermark.ui.panel.StyleFragment
39-
import pl.droidsonroids.gif.GifDrawable
40-
import pl.droidsonroids.gif.GifImageView
39+
import me.rosuh.easywatermark.utils.FileUtils
4140
import kotlin.math.abs
4241

4342

@@ -55,7 +54,6 @@ class MainActivity : AppCompatActivity() {
5554
scope.launch {
5655
initView()
5756
initObserver()
58-
(iv_logo.drawable as? GifDrawable)?.start()
5957
cl_root.setTransition(R.id.transition_launch)
6058
cl_root.transitionToEnd()
6159
checkHadCrash()
@@ -65,6 +63,16 @@ class MainActivity : AppCompatActivity() {
6563
}
6664
}
6765

66+
override fun onStart() {
67+
super.onStart()
68+
iv_logo.start()
69+
}
70+
71+
override fun onPause() {
72+
super.onPause()
73+
iv_logo.stop()
74+
}
75+
6876
private fun checkHadCrash() {
6977
with(getSharedPreferences(MyApp.SP_NAME, MODE_PRIVATE)) {
7078
val isCrash = getBoolean(MyApp.KEY_IS_CRASH, false)
@@ -106,16 +114,16 @@ class MainActivity : AppCompatActivity() {
106114
try {
107115
cl_root.setTransition(R.id.transition_open_image)
108116
cl_root.transitionToEnd()
109-
(iv_logo.drawable as? GifDrawable)?.stop()
110117
iv_photo?.config = it
118+
iv_logo.stop()
111119
} catch (se: SecurityException) {
112120
se.printStackTrace()
113121
// reset the uri because we don't have permission -_-
114122
viewModel.updateUri(Uri.parse(""))
115123
}
116124
})
117125

118-
viewModel.tipsStatus.observe(this, Observer { tips ->
126+
viewModel.tipsStatus.observe(this, { tips ->
119127
when (tips) {
120128
is MainViewModel.TipsStatus.None -> {
121129
tv_data_tips.apply {
@@ -137,7 +145,7 @@ class MainActivity : AppCompatActivity() {
137145
}
138146
})
139147

140-
viewModel.saveState.observe(this, Observer { state ->
148+
viewModel.saveState.observe(this, { state ->
141149
when (state) {
142150
MainViewModel.State.SaveOk -> {
143151
Toast.makeText(this, getString(R.string.tips_save_ok), Toast.LENGTH_SHORT)
@@ -215,7 +223,9 @@ class MainActivity : AppCompatActivity() {
215223
performClick()
216224
scope.launch {
217225
delay(300)
218-
viewModel.updateTips(MainViewModel.TipsStatus.None)
226+
val isAlphaZero = (config?.alpha ?: 0) == 0
227+
val isTextSize = (config?.textSize ?: 0) == 0
228+
viewModel.updateTips(MainViewModel.TipsStatus.None(isAlphaZero || isTextSize))
219229
}
220230
}
221231
}
@@ -271,11 +281,7 @@ class MainActivity : AppCompatActivity() {
271281
}
272282

273283
R.id.action_pick -> {
274-
if (viewModel.isPermissionGrated(this)) {
275-
performFileSearch(READ_REQUEST_CODE)
276-
} else {
277-
viewModel.requestPermission(this)
278-
}
284+
performFileSearch(READ_REQUEST_CODE)
279285
true
280286
}
281287

@@ -292,17 +298,30 @@ class MainActivity : AppCompatActivity() {
292298
* Fires an intent to spin up the "file chooser" UI and select an image.
293299
*/
294300
fun performFileSearch(requestCode: Int) {
301+
if (!viewModel.isPermissionGrated(this)) {
302+
viewModel.requestPermission(this)
303+
return
304+
}
295305
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
296306
flags = (Intent.FLAG_GRANT_READ_URI_PERMISSION
297307
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
298308
or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
299309
addCategory(Intent.CATEGORY_OPENABLE)
300-
type = "image/*"
310+
type = "*/*"
311+
}
312+
val result = kotlin.runCatching {
313+
startActivityForResult(
314+
intent,
315+
requestCode
316+
)
317+
}
318+
if (result.isFailure) {
319+
Toast.makeText(
320+
this,
321+
getString(R.string.tips_not_app_can_open_imaegs),
322+
Toast.LENGTH_LONG
323+
).show()
301324
}
302-
startActivityForResult(
303-
intent,
304-
requestCode
305-
)
306325
}
307326

308327
override fun onRequestPermissionsResult(
@@ -325,35 +344,39 @@ class MainActivity : AppCompatActivity() {
325344

326345
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
327346
super.onActivityResult(requestCode, resultCode, data)
347+
if (resultCode != Activity.RESULT_OK) {
348+
Toast.makeText(
349+
this,
350+
getString(R.string.tips_do_not_choose_image),
351+
Toast.LENGTH_SHORT
352+
).show()
353+
return
354+
}
328355
when (requestCode) {
329356
READ_REQUEST_CODE -> {
330-
if (resultCode == Activity.RESULT_OK) {
331-
data?.data?.also { uri ->
332-
viewModel.updateUri(uri)
333-
takePersistableUriPermission(uri)
334-
}
357+
val uri = data?.data
358+
if (FileUtils.isImage(this.contentResolver, uri)) {
359+
viewModel.updateUri(uri!!)
360+
takePersistableUriPermission(uri)
335361
} else {
336362
Toast.makeText(
337363
this,
338-
getString(R.string.tips_do_not_choose_image),
364+
getString(R.string.tips_choose_other_file_type),
339365
Toast.LENGTH_SHORT
340-
)
341-
.show()
366+
).show()
342367
}
343368
}
344369
ICON_REQUEST_CODE -> {
345-
if (resultCode == Activity.RESULT_OK) {
346-
data?.data?.also { uri ->
347-
viewModel.updateIcon(uri)
348-
takePersistableUriPermission(uri)
349-
}
370+
val uri = data?.data
371+
if (FileUtils.isImage(this.contentResolver, uri)) {
372+
viewModel.updateIcon(uri!!)
373+
takePersistableUriPermission(uri)
350374
} else {
351375
Toast.makeText(
352376
this,
353-
getString(R.string.tips_do_not_choose_image),
377+
getString(R.string.tips_choose_other_file_type),
354378
Toast.LENGTH_SHORT
355-
)
356-
.show()
379+
).show()
357380
}
358381
}
359382
}

0 commit comments

Comments
 (0)