|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.devsupport |
| 9 | + |
| 10 | +import android.annotation.SuppressLint |
| 11 | +import android.app.AlertDialog |
| 12 | +import android.content.Context |
| 13 | +import android.text.InputType |
| 14 | +import android.widget.Button |
| 15 | +import android.widget.EditText |
| 16 | +import android.widget.LinearLayout |
| 17 | +import android.widget.TextView |
| 18 | +import com.facebook.react.R |
| 19 | +import com.facebook.react.modules.debug.interfaces.DeveloperSettings |
| 20 | +import com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getAdbReverseTcpCommand |
| 21 | +import com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getDevServerNetworkIpAndPort |
| 22 | + |
| 23 | +internal object ChangeBundleLocationDialog { |
| 24 | + internal fun interface ChangeBundleLocationDialogListener { |
| 25 | + fun onClick(newHostAndPort: String) |
| 26 | + } |
| 27 | + |
| 28 | + @SuppressLint("SetTextI18n") |
| 29 | + fun show( |
| 30 | + context: Context, |
| 31 | + devSettings: DeveloperSettings, |
| 32 | + onClickListener: ChangeBundleLocationDialogListener |
| 33 | + ) { |
| 34 | + val settings = devSettings.packagerConnectionSettings |
| 35 | + val currentHost = settings.debugServerHost |
| 36 | + settings.debugServerHost = "" |
| 37 | + val defaultHost = settings.debugServerHost |
| 38 | + settings.debugServerHost = currentHost |
| 39 | + |
| 40 | + val layout = LinearLayout(context) |
| 41 | + layout.orientation = LinearLayout.VERTICAL |
| 42 | + val paddingSmall = (4 * context.resources.displayMetrics.density).toInt() |
| 43 | + val paddingLarge = (16 * context.resources.displayMetrics.density).toInt() |
| 44 | + layout.setPadding(paddingLarge, paddingLarge, paddingLarge, paddingLarge) |
| 45 | + |
| 46 | + val label = TextView(context) |
| 47 | + label.text = context.getString(R.string.catalyst_change_bundle_location_input_label) |
| 48 | + label.layoutParams = |
| 49 | + LinearLayout.LayoutParams( |
| 50 | + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) |
| 51 | + |
| 52 | + val input = EditText(context) |
| 53 | + // This makes it impossible to enter a newline in the input field |
| 54 | + input.inputType = InputType.TYPE_CLASS_TEXT |
| 55 | + input.hint = context.getString(R.string.catalyst_change_bundle_location_input_hint) |
| 56 | + input.setBackgroundResource(android.R.drawable.edit_text) |
| 57 | + input.setHintTextColor(-0x333334) |
| 58 | + input.setTextColor(-0x1000000) |
| 59 | + input.setText(currentHost) |
| 60 | + |
| 61 | + val defaultHostSuggestion = Button(context) |
| 62 | + defaultHostSuggestion.text = defaultHost |
| 63 | + defaultHostSuggestion.textSize = 12f |
| 64 | + defaultHostSuggestion.isAllCaps = false |
| 65 | + defaultHostSuggestion.setOnClickListener { input.setText(defaultHost) } |
| 66 | + |
| 67 | + val networkHost = getDevServerNetworkIpAndPort(context) |
| 68 | + val networkHostSuggestion = Button(context) |
| 69 | + networkHostSuggestion.text = networkHost |
| 70 | + networkHostSuggestion.textSize = 12f |
| 71 | + networkHostSuggestion.isAllCaps = false |
| 72 | + networkHostSuggestion.setOnClickListener { input.setText(networkHost) } |
| 73 | + |
| 74 | + val suggestionRow = LinearLayout(context) |
| 75 | + suggestionRow.orientation = LinearLayout.HORIZONTAL |
| 76 | + suggestionRow.layoutParams = |
| 77 | + LinearLayout.LayoutParams( |
| 78 | + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) |
| 79 | + suggestionRow.addView(defaultHostSuggestion) |
| 80 | + suggestionRow.addView(networkHostSuggestion) |
| 81 | + |
| 82 | + val instructions = TextView(context) |
| 83 | + instructions.text = |
| 84 | + context.getString( |
| 85 | + R.string.catalyst_change_bundle_location_instructions, getAdbReverseTcpCommand(context)) |
| 86 | + val instructionsParams = |
| 87 | + LinearLayout.LayoutParams( |
| 88 | + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) |
| 89 | + instructionsParams.setMargins(0, paddingSmall, 0, paddingLarge) |
| 90 | + instructions.layoutParams = instructionsParams |
| 91 | + |
| 92 | + val applyChangesButton = Button(context) |
| 93 | + applyChangesButton.text = context.getString(R.string.catalyst_change_bundle_location_apply) |
| 94 | + |
| 95 | + val cancelButton = Button(context) |
| 96 | + cancelButton.text = context.getString(R.string.catalyst_change_bundle_location_cancel) |
| 97 | + |
| 98 | + layout.addView(label) |
| 99 | + layout.addView(input) |
| 100 | + layout.addView(suggestionRow) |
| 101 | + layout.addView(instructions) |
| 102 | + layout.addView(applyChangesButton) |
| 103 | + layout.addView(cancelButton) |
| 104 | + |
| 105 | + val dialog = |
| 106 | + AlertDialog.Builder(context) |
| 107 | + .setTitle(context.getString(R.string.catalyst_change_bundle_location)) |
| 108 | + .setView(layout) |
| 109 | + .create() |
| 110 | + |
| 111 | + applyChangesButton.setOnClickListener { |
| 112 | + onClickListener.onClick(input.text.toString()) |
| 113 | + dialog.dismiss() |
| 114 | + } |
| 115 | + cancelButton.setOnClickListener { dialog.dismiss() } |
| 116 | + dialog.show() |
| 117 | + } |
| 118 | +} |
0 commit comments