1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cn.flightfeather.thirdapp.module.inspection
 
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment.DIRECTORY_PODCASTS
import android.os.Parcelable
import android.os.StrictMode
import android.view.View
import cn.flightfeather.thirdapp.R
import cn.flightfeather.thirdapp.module.base.BaseActivity
import cn.flightfeather.thirdapp.util.SystemServiceUtils
import cn.flightfeather.thirdapp.util.file.FileUtil
import cn.flightfeather.thirdapp.util.file.FileUtil.getFileFromMediaFile
import cn.flightfeather.thirdapp.util.photo.ImageMergeUtil
import com.bumptech.glide.Glide
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.activity_share_problem_preview.*
import org.jetbrains.anko.doAsync
import java.io.File
import java.util.*
 
/**
 * @author riku
 * Date: 2020/5/15
 */
class ShareProblemPreViewActivity : BaseActivity(), View.OnClickListener {
 
    companion object {
        const val ARG_MERGE_INFO = "infoList"
        const val PROBLEM_DESCRIBE = "问题描述"
        const val CHANGE_INFO = "整改情况"
    }
 
    private lateinit var util: ImageMergeUtil
 
    private lateinit var resultBitmap:Bitmap
 
    private var infoList: List<Problem>? = null
    private val problemImageList = mutableListOf<Bitmap>()
    private val changeImageList = mutableListOf<Bitmap>()
 
    override fun getLayoutId(): Int = R.layout.activity_share_problem_preview
 
    @Suppress("UNCHECKED_CAST")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        infoList = intent.getSerializableExtra(ARG_MERGE_INFO) as List<Problem>
 
        util = ImageMergeUtil(this)
 
        merge()
 
        txt_back.setOnClickListener(this)
    }
 
    private fun merge() {
        doAsync {
            if (infoList.isNullOrEmpty()) {
                return@doAsync
            }
            resultBitmap = util.createTitle(infoList!![0].sceneName ?: "问题统计", infoList!![0].time)
            var index = 0
 
            infoList?.forEach { vo ->
                changeImageList.clear()
                problemImageList.clear()
                index++
                resultBitmap = util.mergeIndex(resultBitmap, index, resources)
 
                vo.mediaFileList?.forEach {
                    val url = it.second
                    val bitmap = Glide.with(this@ShareProblemPreViewActivity)
                            .asBitmap()
                            .load(url)
                            .submit().get()
                    if (it.first) {
                        changeImageList.add(bitmap)
                    } else {
                        problemImageList.add(bitmap)
                    }
                }
                util.mergeImage(problemImageList).let {
                    val text = (vo.problems ?: "") + if (vo.advise!=null) "," + vo.advise else ""
                    resultBitmap = if (it != null) {
                        val b = util.mergeVerticalText(it, text, PROBLEM_DESCRIBE, R.mipmap.ic_problem_describe, resources)
//                        val b = util.mergeVerticalText(it, text)
                        util.mergeVertical(resultBitmap, b)
                    } else {
                        util.mergeVerticalText(resultBitmap, text, PROBLEM_DESCRIBE, R.mipmap.ic_problem_describe, resources)
                    }
                }
 
                val changeInfo = vo.changeInfo ?: ""
                util.mergeImage(changeImageList).let {
                    resultBitmap = if (it != null) {
                        val b = util.mergeVerticalText(it, changeInfo, CHANGE_INFO, R.mipmap.ic_advice, resources)
                        util.mergeVertical(resultBitmap, b)
                    } else {
                        util.mergeVerticalText(resultBitmap, changeInfo, CHANGE_INFO, R.mipmap.ic_advice, resources)
                    }
                }
 
            }
 
            util.drawBg(resultBitmap, Color.WHITE)
 
            runOnUiThread {
                Glide.with(this@ShareProblemPreViewActivity)
                        .load(resultBitmap)
                        .into(img_preview)
                txt_share.setOnClickListener(this@ShareProblemPreViewActivity)
            }
        }
    }
 
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txt_back -> onBackPressed()
            R.id.txt_share -> {
                Observable.create<File> {emitter ->
                    val path = getExternalFilesDir(DIRECTORY_PODCASTS)?.path
                    path?.let {
                        SystemServiceUtils.saveBitmap(it, resultBitmap)
                        emitter.onNext(File(it))
                    }
                    emitter.onComplete()
                }.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe {
                            val imageUri: Uri = Uri.fromFile(it)
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                val builder = StrictMode.VmPolicy.Builder()
                                StrictMode.setVmPolicy(builder.build())
                            }
                            val intent = Intent()
                            intent.action = Intent.ACTION_SEND
                            intent.putExtra(Intent.EXTRA_STREAM, imageUri)
                            intent.type = "image/*"
                            startActivity(Intent.createChooser(intent, "分享图片"))
                        }
            }
        }
    }
 
    @Parcelize
    data class Problem constructor(
            val sceneName: String?,
            val mediaFileList: MutableList<Pair<Boolean, String>>?,
            var problems: String?,
            var advise: String?,
            var changeInfo: String?,
            val time: Date?
    ):Parcelable
}