-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb93e0f
commit 1a3088a
Showing
15 changed files
with
424 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
app_pojavlauncher/src/main/java/com/firefly/ui/prefs/ChooseTurnipListPref.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.firefly.ui.prefs; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.LinearLayout; | ||
import android.widget.ListView; | ||
import android.widget.Toast; | ||
|
||
import androidx.appcompat.app.AlertDialog; | ||
import androidx.preference.ListPreference; | ||
|
||
import net.kdt.pojavlaunch.R; | ||
import net.kdt.pojavlaunch.Tools; | ||
import com.firefly.utils.TurnipUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ChooseTurnipListPref extends ListPreference { | ||
private List<String> defaultLibs; | ||
private OnPreferenceChangeListener preferenceChangeListener; | ||
private View.OnClickListener confirmButtonListener; | ||
|
||
public ChooseTurnipListPref(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
loadDefaultLibs(context); | ||
} | ||
|
||
private void loadDefaultLibs(Context context) { | ||
defaultLibs = Arrays.asList(context.getResources().getStringArray(R.array.turnip_values)); | ||
} | ||
|
||
@Override | ||
protected void onClick() { | ||
showDialog(); | ||
} | ||
|
||
private void showDialog() { | ||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); | ||
builder.setTitle(getDialogTitle()); | ||
|
||
CharSequence[] entriesCharSequence = getEntries(); | ||
String[] entries = new String[entriesCharSequence.length]; | ||
for (int i = 0; i < entriesCharSequence.length; i++) { | ||
entries[i] = entriesCharSequence[i].toString(); | ||
} | ||
|
||
builder.setItems(entries, (dialog, which) -> { | ||
String newValue = getEntryValues()[which].toString(); | ||
if (preferenceChangeListener != null) { | ||
if (preferenceChangeListener.onPreferenceChange(this, newValue)) { | ||
setValue(newValue); | ||
} | ||
} else { | ||
setValue(newValue); | ||
} | ||
dialog.dismiss(); | ||
}); | ||
|
||
LinearLayout layout = new LinearLayout(getContext()); | ||
layout.setOrientation(LinearLayout.VERTICAL); | ||
|
||
Button createButton = new Button(getContext()); | ||
createButton.setText(R.string.pgw_settings_custom_turnip_creat); | ||
createButton.setOnClickListener(view -> { | ||
if (confirmButtonListener != null) { | ||
confirmButtonListener.onClick(view); | ||
} | ||
}); | ||
layout.addView(createButton); | ||
builder.setView(layout); | ||
|
||
AlertDialog dialog = builder.create(); | ||
dialog.show(); | ||
|
||
ListView listView = dialog.getListView(); | ||
listView.setOnItemLongClickListener((adapterView, view, position, id) -> { | ||
String selectedVersion = getEntryValues()[position].toString(); | ||
if (defaultLibs.contains(selectedVersion)) { | ||
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_delete_defaultlib, Toast.LENGTH_SHORT).show(); | ||
} else { | ||
showDeleteConfirmationDialog(selectedVersion); | ||
} | ||
dialog.dismiss(); | ||
return true; | ||
}); | ||
} | ||
|
||
@Override | ||
public void setOnPreferenceChangeListener(OnPreferenceChangeListener listener) { | ||
this.preferenceChangeListener = listener; | ||
super.setOnPreferenceChangeListener(listener); | ||
} | ||
|
||
public void setConfirmButton(String buttonText, View.OnClickListener listener) { | ||
this.confirmButtonListener = listener; | ||
} | ||
|
||
private void showDeleteConfirmationDialog(String version) { | ||
new AlertDialog.Builder(getContext()) | ||
.setTitle(R.string.pgw_settings_ctu_delete_title) | ||
.setMessage(getContext().getString(R.string.pgw_settings_ctu_delete_message, version)) | ||
.setPositiveButton(R.string.alertdialog_done, (dialog, which) -> { | ||
boolean success = TurnipUtils.INSTANCE.deleteTurnipDriver(version); | ||
if (success) { | ||
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_deleted, Toast.LENGTH_SHORT).show(); | ||
setEntriesAndValues(); | ||
} else { | ||
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_delete_fail, Toast.LENGTH_SHORT).show(); | ||
} | ||
}) | ||
.setNegativeButton(R.string.alertdialog_cancel, null) | ||
.show(); | ||
} | ||
|
||
private void setEntriesAndValues() { | ||
Tools.IListAndArry array = Tools.getCompatibleCTurnipDriver(getContext()); | ||
setEntries(array.getArray()); | ||
setEntryValues(array.getList().toArray(new String[0])); | ||
String currentValue = getValue(); | ||
if (!array.getList().contains(currentValue)) { | ||
setValueIndex(0); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
app_pojavlauncher/src/main/java/com/firefly/utils/TurnipUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.firefly.utils; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.Environment; | ||
import android.widget.Toast; | ||
|
||
import net.kdt.pojavlaunch.Tools; | ||
import net.kdt.pojavlaunch.R; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class TurnipUtils { | ||
|
||
public static final TurnipUtils INSTANCE = new TurnipUtils(); | ||
private final File turnipDir; | ||
|
||
private TurnipUtils() { | ||
this.turnipDir = new File(Tools.TURNIP_DIR); | ||
if (!turnipDir.exists() && !turnipDir.mkdirs()) { | ||
throw new RuntimeException("Failed to create Turnip directory"); | ||
} | ||
} | ||
|
||
public List<String> getTurnipDriverList() { | ||
List<String> list = new ArrayList<>(); | ||
File[] files = turnipDir.listFiles(); | ||
for (File file : files) { | ||
if (file.isDirectory() && new File(file.getAbsolutePath() + "/libvulkan_freedreno.so").exists()) { | ||
list.add(file.getName()); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
public String getTurnipDriver(String version) { | ||
return Tools.TURNIP_DIR + "/" + version; | ||
} | ||
|
||
public boolean saveTurnipDriver(Context context, Uri fileUri, String folderName) { | ||
try { | ||
File targetDir = new File(turnipDir, folderName); | ||
if (!targetDir.exists() && !targetDir.mkdirs()) { | ||
return false; | ||
} | ||
|
||
File targetFile = new File(targetDir, "libvulkan_freedreno.so"); | ||
|
||
try (InputStream inputStream = context.getContentResolver().openInputStream(fileUri); | ||
OutputStream outputStream = new FileOutputStream(targetFile)) { | ||
if (inputStream == null) return false; | ||
|
||
byte[] buffer = new byte[4096]; | ||
int bytesRead; | ||
while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
} | ||
} | ||
return true; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
public boolean deleteTurnipDriver(String version) { | ||
File libDir = new File(turnipDir, version); | ||
if (libDir.exists()) { | ||
return deleteDirectory(libDir); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean deleteDirectory(File directoryToBeDeleted) { | ||
File[] allContents = directoryToBeDeleted.listFiles(); | ||
if (allContents != null) { | ||
for (File file : allContents) { | ||
deleteDirectory(file); | ||
} | ||
} | ||
return directoryToBeDeleted.delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.