-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFileActivity.kt
More file actions
266 lines (225 loc) · 8.92 KB
/
FileActivity.kt
File metadata and controls
266 lines (225 loc) · 8.92 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.commit451.gitlab.activity
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import com.google.android.material.snackbar.Snackbar
import androidx.core.content.ContextCompat
import androidx.appcompat.widget.Toolbar
import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.webkit.MimeTypeMap
import android.webkit.WebView
import butterknife.BindView
import butterknife.ButterKnife
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.extension.base64Decode
import com.commit451.gitlab.extension.with
import com.commit451.gitlab.model.api.RepositoryFile
import com.commit451.gitlab.rx.CustomSingleObserver
import com.commit451.gitlab.util.FileUtil
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.charset.Charset
class FileActivity : BaseActivity() {
companion object {
private val REQUEST_PERMISSION_WRITE_STORAGE = 1337
private val MAX_FILE_SIZE = (1024 * 1024).toLong()
private val EXTRA_PROJECT_ID = "extra_project_id"
private val EXTRA_PATH = "extra_path"
private val EXTRA_REF = "extra_ref"
fun newIntent(context: Context, projectId: Long, path: String, ref: String): Intent {
val intent = Intent(context, FileActivity::class.java)
intent.putExtra(EXTRA_PROJECT_ID, projectId)
intent.putExtra(EXTRA_PATH, path)
intent.putExtra(EXTRA_REF, ref)
return intent
}
fun fileExtension(filename: String): String {
val extStart = filename.lastIndexOf(".") + 1
if (extStart < 1) {
return ""
}
return filename.substring(extStart)
}
}
@BindView(R.id.root)
lateinit var root: ViewGroup
@BindView(R.id.toolbar)
lateinit var toolbar: Toolbar
@BindView(R.id.file_blob)
lateinit var webViewFileBlob: WebView
@BindView(R.id.progress)
lateinit var progress: View
var projectId: Long = 0
var path: String? = null
var ref: String? = null
var repositoryFile: RepositoryFile? = null
var fileName: String? = null
var blob: ByteArray? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_file)
ButterKnife.bind(this)
projectId = intent.getLongExtra(EXTRA_PROJECT_ID, -1)
path = intent.getStringExtra(EXTRA_PATH)
ref = intent.getStringExtra(EXTRA_REF)
toolbar.setNavigationIcon(R.drawable.ic_back_24dp)
toolbar.setNavigationOnClickListener { onBackPressed() }
toolbar.setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener { item ->
when (item.itemId) {
R.id.action_open -> {
openFile()
return@OnMenuItemClickListener true
}
R.id.action_save -> {
checkAccountPermission()
return@OnMenuItemClickListener true
}
}
false
})
loadData()
}
private fun loadData() {
progress.visibility = View.VISIBLE
App.get().gitLab.getFile(projectId, path!!, ref!!)
.with(this)
.subscribe(object : CustomSingleObserver<RepositoryFile>() {
override fun error(t: Throwable) {
Timber.e(t)
progress.visibility = View.GONE
Snackbar.make(root, R.string.file_load_error, Snackbar.LENGTH_SHORT)
.show()
}
override fun success(repositoryFile: RepositoryFile) {
progress.visibility = View.GONE
bindFile(repositoryFile)
}
})
}
private fun bindFile(repositoryFile: RepositoryFile) {
this.repositoryFile = repositoryFile
fileName = repositoryFile.fileName
toolbar.title = fileName
if (repositoryFile.size > MAX_FILE_SIZE) {
Snackbar.make(root, R.string.file_too_big, Snackbar.LENGTH_SHORT)
.show()
} else {
loadBlob(repositoryFile)
}
}
private fun loadBlob(repositoryFile: RepositoryFile) {
repositoryFile.content.base64Decode()
.with(this)
.subscribe(object : CustomSingleObserver<ByteArray>() {
override fun error(t: Throwable) {
Snackbar.make(root, R.string.failed_to_load, Snackbar.LENGTH_SHORT)
.show()
}
override fun success(bytes: ByteArray) {
bindBlob(bytes)
}
})
}
private fun bindBlob(blob: ByteArray) {
this.blob = blob
val content: String
var mimeType: String?
val extension = fileExtension(fileName!!)
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
if (mimeType != null) {
mimeType = mimeType.toLowerCase()
}
if (mimeType != null && mimeType.startsWith("image/")) {
val imageURL = "data:" + mimeType + ";base64," + repositoryFile!!.content
content = "<!DOCTYPE html>" +
"<html>" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
"<body>" +
"<img style=\"width: 100%;\" src=\"" + imageURL + "\">" +
"</body>" +
"</html>"
} else {
val text = String(this.blob!!, Charset.forName("UTF-8"))
content = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<link href=\"github.css\" rel=\"stylesheet\" />" +
"</head>" +
"<body>" +
"<pre><code>" +
Html.escapeHtml(text) +
"</code></pre>" +
"<script src=\"highlight.pack.js\"></script>" +
"<script>hljs.initHighlightingOnLoad();</script>" +
"</body>" +
"</html>"
}
webViewFileBlob.loadDataWithBaseURL("file:///android_asset/", content, "text/html", "utf8", null)
toolbar.inflateMenu(R.menu.menu_file)
}
@SuppressLint("NewApi")
private fun checkAccountPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveBlob()
} else {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION_WRITE_STORAGE)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_PERMISSION_WRITE_STORAGE -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveBlob()
}
}
}
}
private fun saveBlob(): File? {
val state = Environment.getExternalStorageState()
if (Environment.MEDIA_MOUNTED == state && blob != null) {
val targetFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName!!)
var outputStream: FileOutputStream? = null
try {
outputStream = FileOutputStream(targetFile)
outputStream.write(blob!!)
Snackbar.make(root, getString(R.string.file_saved), Snackbar.LENGTH_SHORT)
.show()
return targetFile
} catch (e: IOException) {
Timber.e(e)
Snackbar.make(root, getString(R.string.save_error), Snackbar.LENGTH_SHORT)
.show()
} finally {
if (outputStream != null) {
try {
outputStream.close()
} catch (e: IOException) {
Timber.e(e)
}
}
}
} else {
Snackbar.make(root, getString(R.string.save_error), Snackbar.LENGTH_SHORT)
.show()
}
return null
}
fun openFile() {
if (blob != null && fileName != null) {
if(!FileUtil.openFile(this, fileName!!, blob!!)){
Snackbar.make(root, getString(R.string.open_error), Snackbar.LENGTH_SHORT).show()
}
} else {
Snackbar.make(root, getString(R.string.open_error), Snackbar.LENGTH_SHORT).show()
}
}
}