Merge pull request #11616 from t895/kotlin-skylanders

Android: Convert Skylanders code to Kotlin
This commit is contained in:
Mai 2023-03-02 15:03:18 -05:00 committed by GitHub
commit b9fd7e7bd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 200 additions and 328 deletions

View File

@ -406,7 +406,7 @@ public final class EmulationActivity extends AppCompatActivity implements ThemeP
outState.putBoolean(EXTRA_MENU_TOAST_SHOWN, mMenuToastShown);
outState.putInt(EXTRA_SKYLANDER_SLOT, mSkylanderSlot);
outState.putInt(EXTRA_SKYLANDER_ID, mSkylanderData.getId());
outState.putInt(EXTRA_SKYLANDER_VAR, mSkylanderData.getVar());
outState.putInt(EXTRA_SKYLANDER_VAR, mSkylanderData.getVariant());
outState.putString(EXTRA_SKYLANDER_NAME, mSkylanderData.getName());
super.onSaveInstanceState(outState);
}
@ -546,10 +546,10 @@ public final class EmulationActivity extends AppCompatActivity implements ThemeP
}
else if (requestCode == REQUEST_CREATE_SKYLANDER)
{
if (!(mSkylanderData.getId() == -1) && !(mSkylanderData.getVar() == -1))
if (!(mSkylanderData.getId() == -1) && !(mSkylanderData.getVariant() == -1))
{
Pair<Integer, String> slot = SkylanderConfig.createSkylander(mSkylanderData.getId(),
mSkylanderData.getVar(),
mSkylanderData.getVariant(),
result.getData().toString(), sSkylanderSlots.get(mSkylanderSlot).getPortalSlot());
clearSkylander(mSkylanderSlot);
sSkylanderSlots.get(mSkylanderSlot).setPortalSlot(slot.first);

View File

@ -1,32 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders;
import android.util.Pair;
import org.dolphinemu.dolphinemu.features.skylanders.model.SkylanderPair;
import java.util.Map;
public class SkylanderConfig
{
public static final Map<SkylanderPair, String> LIST_SKYLANDERS;
public static final Map<String, SkylanderPair> REVERSE_LIST_SKYLANDERS;
static
{
LIST_SKYLANDERS = getSkylanderMap();
REVERSE_LIST_SKYLANDERS = getInverseSkylanderMap();
}
public static native Map<SkylanderPair, String> getSkylanderMap();
public static native Map<String, SkylanderPair> getInverseSkylanderMap();
public static native boolean removeSkylander(int slot);
public static native Pair<Integer, String> loadSkylander(int slot, String fileName);
public static native Pair<Integer, String> createSkylander(int id, int var, String fileName,
int slot);
}

View File

@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders
import android.util.Pair
import org.dolphinemu.dolphinemu.features.skylanders.model.SkylanderPair
object SkylanderConfig {
var LIST_SKYLANDERS: Map<SkylanderPair, String> = getSkylanderMap()
var REVERSE_LIST_SKYLANDERS: Map<String, SkylanderPair> = getInverseSkylanderMap()
private external fun getSkylanderMap(): Map<SkylanderPair, String>
private external fun getInverseSkylanderMap(): Map<String, SkylanderPair>
@JvmStatic
external fun removeSkylander(slot: Int): Boolean
@JvmStatic
external fun loadSkylander(slot: Int, fileName: String?): Pair<Int?, String?>?
@JvmStatic
external fun createSkylander(
id: Int,
variant: Int,
fileName: String,
slot: Int
): Pair<Int, String>
}

View File

@ -1,37 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.model;
public class Skylander
{
private SkylanderPair mPair;
private String mName;
public final static Skylander BLANK_SKYLANDER = new Skylander(-1, -1, "Blank");
public Skylander(int id, int var, String name)
{
mPair = new SkylanderPair(id, var);
mName = name;
}
public String getName()
{
return mName;
}
public void setName(String name)
{
this.mName = name;
}
public int getId()
{
return mPair.getId();
}
public int getVar()
{
return mPair.getVar();
}
}

View File

@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.model
class Skylander(id: Int, variant: Int, var name: String) {
private val pair: SkylanderPair = SkylanderPair(id, variant)
val id: Int get() = pair.id
val variant: Int get() = pair.variant
companion object {
@JvmField
val BLANK_SKYLANDER = Skylander(-1, -1, "Blank")
}
}

View File

@ -1,54 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.model;
import androidx.annotation.Nullable;
public class SkylanderPair
{
private int mId;
private int mVar;
public SkylanderPair(int id, int var)
{
mId = id;
mVar = var;
}
public int getId()
{
return mId;
}
public void setId(int mId)
{
this.mId = mId;
}
public int getVar()
{
return mVar;
}
public void setVar(int mVar)
{
this.mVar = mVar;
}
@Override public int hashCode()
{
return (mId << 16) + mVar;
}
@Override public boolean equals(@Nullable Object obj)
{
if (!(obj instanceof SkylanderPair))
return false;
SkylanderPair pairObj = (SkylanderPair) obj;
if (pairObj.getId() != mId)
return false;
if (pairObj.getVar() != mVar)
return false;
return true;
}
}

View File

@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.model
class SkylanderPair(var id: Int, var variant: Int) {
override fun hashCode(): Int {
return (id shl 16) + variant
}
override fun equals(other: Any?): Boolean {
if (other !is SkylanderPair) return false
if (other.id != id) return false
return other.variant == variant
}
}

View File

@ -1,42 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.ui;
public class SkylanderSlot
{
private String mLabel;
private final int mSlotNum;
private int mPortalSlot;
public SkylanderSlot(String label, int slot)
{
mLabel = label;
mSlotNum = slot;
mPortalSlot = -1;
}
public String getLabel()
{
return mLabel;
}
public void setLabel(String label)
{
mLabel = label;
}
public int getSlotNum()
{
return mSlotNum;
}
public int getPortalSlot()
{
return mPortalSlot;
}
public void setPortalSlot(int mPortalSlot)
{
this.mPortalSlot = mPortalSlot;
}
}

View File

@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.ui
class SkylanderSlot(var label: String, val slotNum: Int) {
var portalSlot: Int = -1
}

View File

@ -1,160 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.ui;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.databinding.DialogCreateSkylanderBinding;
import org.dolphinemu.dolphinemu.databinding.ListItemSkylanderSlotBinding;
import org.dolphinemu.dolphinemu.features.skylanders.SkylanderConfig;
import org.dolphinemu.dolphinemu.features.skylanders.model.SkylanderPair;
import java.util.ArrayList;
import java.util.List;
public class SkylanderSlotAdapter extends RecyclerView.Adapter<SkylanderSlotAdapter.ViewHolder>
implements AdapterView.OnItemClickListener
{
public static class ViewHolder extends RecyclerView.ViewHolder
{
public ListItemSkylanderSlotBinding binding;
public ViewHolder(@NonNull ListItemSkylanderSlotBinding binding)
{
super(binding.getRoot());
this.binding = binding;
}
}
private final List<SkylanderSlot> mSlots;
private final EmulationActivity mActivity;
private DialogCreateSkylanderBinding mBinding;
public SkylanderSlotAdapter(List<SkylanderSlot> slots, EmulationActivity context)
{
mSlots = slots;
mActivity = context;
}
@NonNull
@Override
public SkylanderSlotAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ListItemSkylanderSlotBinding binding =
ListItemSkylanderSlotBinding.inflate(inflater, parent, false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
SkylanderSlot slot = mSlots.get(position);
holder.binding.textSkylanderName.setText(slot.getLabel());
holder.binding.buttonClearSkylander.setOnClickListener(v ->
{
SkylanderConfig.removeSkylander(slot.getPortalSlot());
mActivity.clearSkylander(slot.getSlotNum());
});
holder.binding.buttonLoadSkylander.setOnClickListener(v ->
{
Intent loadSkylander = new Intent(Intent.ACTION_OPEN_DOCUMENT);
loadSkylander.addCategory(Intent.CATEGORY_OPENABLE);
loadSkylander.setType("*/*");
mActivity.setSkylanderData(0, 0, "", position);
mActivity.startActivityForResult(loadSkylander, EmulationActivity.REQUEST_SKYLANDER_FILE);
});
LayoutInflater inflater = LayoutInflater.from(mActivity);
mBinding = DialogCreateSkylanderBinding.inflate(inflater);
List<String> skylanderNames = new ArrayList<>(SkylanderConfig.REVERSE_LIST_SKYLANDERS.keySet());
skylanderNames.sort(String::compareToIgnoreCase);
mBinding.skylanderDropdown.setAdapter(
new ArrayAdapter<>(mActivity, R.layout.support_simple_spinner_dropdown_item,
skylanderNames));
mBinding.skylanderDropdown.setOnItemClickListener(this);
holder.binding.buttonCreateSkylander.setOnClickListener(v ->
{
if (mBinding.getRoot().getParent() != null)
{
((ViewGroup) mBinding.getRoot().getParent()).removeAllViews();
}
AlertDialog createDialog = new MaterialAlertDialogBuilder(mActivity)
.setTitle(R.string.create_skylander_title)
.setView(mBinding.getRoot())
.setPositiveButton(R.string.create_skylander, null)
.setNegativeButton(R.string.cancel, null)
.show();
createDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setOnClickListener(
v1 ->
{
if (!mBinding.skylanderId.getText().toString().isBlank() &&
!mBinding.skylanderVar.getText().toString().isBlank())
{
Intent createSkylander = new Intent(Intent.ACTION_CREATE_DOCUMENT);
createSkylander.addCategory(Intent.CATEGORY_OPENABLE);
createSkylander.setType("*/*");
int id = Integer.parseInt(mBinding.skylanderId.getText().toString());
int var = Integer.parseInt(mBinding.skylanderVar.getText().toString());
String name = SkylanderConfig.LIST_SKYLANDERS.get(new SkylanderPair(id, var));
if (name != null)
{
createSkylander.putExtra(Intent.EXTRA_TITLE,
name + ".sky");
mActivity.setSkylanderData(id, var, name, position);
}
else
{
createSkylander.putExtra(Intent.EXTRA_TITLE,
"Unknown(ID" + id + "Var" + var + ").sky");
mActivity.setSkylanderData(id, var, "Unknown", position);
}
mActivity.startActivityForResult(createSkylander,
EmulationActivity.REQUEST_CREATE_SKYLANDER);
createDialog.dismiss();
}
else
{
Toast.makeText(mActivity, R.string.invalid_skylander,
Toast.LENGTH_SHORT).show();
}
});
});
}
@Override
public int getItemCount()
{
return mSlots.size();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
SkylanderPair skylanderIdVar =
SkylanderConfig.REVERSE_LIST_SKYLANDERS.get(parent.getItemAtPosition(position));
mBinding.skylanderId.setText(String.valueOf(skylanderIdVar.getId()));
mBinding.skylanderVar.setText(String.valueOf(skylanderIdVar.getVar()));
}
}

View File

@ -0,0 +1,132 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.skylanders.ui
import android.app.AlertDialog
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.activities.EmulationActivity
import org.dolphinemu.dolphinemu.databinding.DialogCreateSkylanderBinding
import org.dolphinemu.dolphinemu.databinding.ListItemSkylanderSlotBinding
import org.dolphinemu.dolphinemu.features.skylanders.SkylanderConfig
import org.dolphinemu.dolphinemu.features.skylanders.SkylanderConfig.removeSkylander
import org.dolphinemu.dolphinemu.features.skylanders.model.SkylanderPair
class SkylanderSlotAdapter(
private val slots: List<SkylanderSlot>,
private val activity: EmulationActivity
) : RecyclerView.Adapter<SkylanderSlotAdapter.ViewHolder>(), OnItemClickListener {
class ViewHolder(var binding: ListItemSkylanderSlotBinding) :
RecyclerView.ViewHolder(binding.getRoot())
private lateinit var binding: DialogCreateSkylanderBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ListItemSkylanderSlotBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val slot = slots[position]
holder.binding.textSkylanderName.text = slot.label
holder.binding.buttonClearSkylander.setOnClickListener {
removeSkylander(slot.portalSlot)
activity.clearSkylander(slot.slotNum)
}
holder.binding.buttonLoadSkylander.setOnClickListener {
val loadSkylander = Intent(Intent.ACTION_OPEN_DOCUMENT)
loadSkylander.addCategory(Intent.CATEGORY_OPENABLE)
loadSkylander.type = "*/*"
activity.setSkylanderData(0, 0, "", position)
activity.startActivityForResult(
loadSkylander,
EmulationActivity.REQUEST_SKYLANDER_FILE
)
}
val inflater = LayoutInflater.from(activity)
binding = DialogCreateSkylanderBinding.inflate(inflater)
val nameList = SkylanderConfig.REVERSE_LIST_SKYLANDERS.keys.toMutableList()
nameList.sort()
val skylanderNames: ArrayList<String> = ArrayList(nameList)
binding.skylanderDropdown.setAdapter(
ArrayAdapter(
activity, R.layout.support_simple_spinner_dropdown_item,
skylanderNames
)
)
binding.skylanderDropdown.onItemClickListener = this
holder.binding.buttonCreateSkylander.setOnClickListener {
if (binding.getRoot().parent != null) {
(binding.getRoot().parent as ViewGroup).removeAllViews()
}
val createDialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.create_skylander_title)
.setView(binding.getRoot())
.setPositiveButton(R.string.create_skylander, null)
.setNegativeButton(R.string.cancel, null)
.show()
createDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
if (binding.skylanderId.text.toString().isNotBlank() &&
binding.skylanderVar.text.toString().isNotBlank()
) {
val createSkylander = Intent(Intent.ACTION_CREATE_DOCUMENT)
createSkylander.addCategory(Intent.CATEGORY_OPENABLE)
createSkylander.type = "*/*"
val id = binding.skylanderId.text.toString().toInt()
val variant = binding.skylanderVar.text.toString().toInt()
val name = SkylanderConfig.LIST_SKYLANDERS[SkylanderPair(id, variant)]
if (name != null) {
createSkylander.putExtra(
Intent.EXTRA_TITLE,
"$name.sky"
)
activity.setSkylanderData(id, variant, name, position)
} else {
createSkylander.putExtra(
Intent.EXTRA_TITLE,
"Unknown(ID: " + id + "Variant: " + variant + ").sky"
)
activity.setSkylanderData(id, variant, "Unknown", position)
}
activity.startActivityForResult(
createSkylander,
EmulationActivity.REQUEST_CREATE_SKYLANDER
)
createDialog.dismiss()
} else {
Toast.makeText(
activity, R.string.invalid_skylander,
Toast.LENGTH_SHORT
).show()
}
}
}
}
override fun getItemCount(): Int {
return slots.size
}
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val skylanderIdVar =
SkylanderConfig.REVERSE_LIST_SKYLANDERS[parent.getItemAtPosition(position)]
binding.skylanderId.setText(skylanderIdVar!!.id.toString())
binding.skylanderVar.setText(skylanderIdVar.variant.toString())
}
}