Skip to content

Commit 562c2db

Browse files
authored
MainActivity.kt の更新
1 parent 7316121 commit 562c2db

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

app/src/main/java/com/rabimi/javaskinchanger/MainActivity.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class MainActivity : Activity() {
3333
private lateinit var skinView: ImageView
3434

3535
private val REQUEST_SKIN_PICK = 1001
36+
private val REQUEST_LIBRARY = 2001
3637
private var currentSkinBitmap: Bitmap? = null
3738
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
3839

@@ -65,7 +66,6 @@ class MainActivity : Activity() {
6566
btnUpload.visibility = View.GONE
6667
progressBar.visibility = View.GONE
6768

68-
// ★ 文字を白色に統一
6969
val white = getColor(R.color.white)
7070
btnSelect.setTextColor(white)
7171
btnUpload.setTextColor(white)
@@ -79,8 +79,11 @@ class MainActivity : Activity() {
7979
btnSelect.setOnClickListener { selectSkinImage() }
8080
btnUpload.setOnClickListener { handleUpload() }
8181

82+
// ★ ライブラリボタン
8283
btnLibrary.setOnClickListener {
83-
AlertDialog.Builder(this).setMessage("未実装").setPositiveButton("OK", null).show()
84+
val intent = Intent(this, SkinLibraryActivity::class.java)
85+
intent.putExtra("currentSkin", currentSkinBitmap)
86+
startActivityForResult(intent, REQUEST_LIBRARY)
8487
}
8588

8689
btnLogout.setOnClickListener {
@@ -90,7 +93,6 @@ class MainActivity : Activity() {
9093
}
9194
}
9295

93-
/** res/raw/steve.png 読み込み */
9496
private fun loadDefaultSteveSkin() {
9597
val input = resources.openRawResource(R.raw.steve)
9698
val bmp = BitmapFactory.decodeStream(input)
@@ -118,18 +120,25 @@ class MainActivity : Activity() {
118120

119121
override fun onActivityResult(req: Int, res: Int, data: Intent?) {
120122
super.onActivityResult(req, res, data)
123+
124+
// ライブラリから戻ってきた場合
125+
if (req == REQUEST_LIBRARY && res == Activity.RESULT_OK) {
126+
val path = data?.getStringExtra(SkinLibraryActivity.EXTRA_SELECTED_SKIN_PATH) ?: return
127+
val bmp = BitmapFactory.decodeFile(path)
128+
currentSkinBitmap = resizeTo64(bmp)
129+
skinView.setImageBitmap(currentSkinBitmap)
130+
}
131+
132+
// スキン選択から戻ってきた場合
121133
if (req == REQUEST_SKIN_PICK && res == Activity.RESULT_OK) {
122134
val uri = data?.data ?: return
123135
try {
124136
val orig = MediaStore.Images.Media.getBitmap(contentResolver, uri)
125137
val bmp = resizeTo64(orig)
126-
127138
currentSkinBitmap = bmp
128139
skinView.setImageBitmap(bmp)
129-
130140
btnUpload.visibility = View.VISIBLE
131141
btnUpload.backgroundTintList = ColorStateList.valueOf(colorUploadTarget)
132-
133142
} catch (e: Exception) {
134143
e.printStackTrace()
135144
AlertDialog.Builder(this)
@@ -149,7 +158,6 @@ class MainActivity : Activity() {
149158
)
150159
}
151160

152-
/** 実行ボタン */
153161
private fun handleUpload() {
154162
val bmp = currentSkinBitmap ?: return
155163
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
@@ -171,14 +179,12 @@ class MainActivity : Activity() {
171179
}
172180
}
173181

174-
/** 本物の Mojang API 仕様に合わせた multipart */
175182
private suspend fun uploadSkin(
176183
token: String,
177184
bmp: Bitmap,
178185
model: String,
179186
onProgress: (Int) -> Unit
180187
): Boolean = withContext(Dispatchers.IO) {
181-
182188
try {
183189
val boundary = "----RabimiBoundary"
184190
val url = URL("https://api.minecraftservices.com/minecraft/profile/skins")
@@ -195,12 +201,10 @@ class MainActivity : Activity() {
195201

196202
Log.d(TAG, "variant = $model")
197203

198-
// ★ variant が正しいキー名(skinModel ではない)
199204
out.writeBytes("--$boundary\r\n")
200205
out.writeBytes("Content-Disposition: form-data; name=\"variant\"\r\n\r\n")
201206
out.writeBytes("$model\r\n")
202207

203-
// PNG file
204208
out.writeBytes("--$boundary\r\n")
205209
out.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"skin.png\"\r\n")
206210
out.writeBytes("Content-Type: image/png\r\n\r\n")
@@ -242,7 +246,6 @@ class MainActivity : Activity() {
242246
}
243247

244248
code in 200..299
245-
246249
} catch (e: Exception) {
247250
Log.e(TAG, "UPLOAD ERROR", e)
248251
false

0 commit comments

Comments
 (0)