- Import screen W.I.P

- Rounded background menu corners
This commit is contained in:
SerpentSpirale
2021-09-08 22:03:12 +02:00
committed by SerpentSpirale
parent bb52f8cd1c
commit 1002d59cc9
7 changed files with 254 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="net.kdt.pojavlaunch">
<uses-feature android:glEsVersion="0x00020000"/>
@@ -38,6 +39,22 @@
</intent-filter>
</activity>
<activity android:name=".ImportControlActivity"
android:exported="true"
android:windowSoftInputMode="stateVisible"
android:configChanges="keyboard|keyboardHidden"
>
<intent-filter android:scheme="content"
tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/json"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:theme="@style/MenuDialog"
android:screenOrientation="sensorLandscape"

View File

@@ -0,0 +1,144 @@
package net.kdt.pojavlaunch;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.utils.FileUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.StandardCopyOption;
/**
* An activity dedicated to importing control files.
*/
public class ImportControlActivity extends Activity {
private Uri uriData;
private EditText editText;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Check if the intent is valid.
if (getIntent() == null) finishAndRemoveTask();
getUriData();
if(uriData == null) finishAndRemoveTask();
Tools.initContextConstants(getApplicationContext());
setContentView(R.layout.import_control_layout);
editText = findViewById(R.id.editText_import_control_file_name);
//Set the name of the file in the editText.
String editTextString = uriData.toString().replaceAll("%..", "/");
editTextString = editTextString.substring(editTextString.lastIndexOf('/') + 1);
editText.setText(trimFileName(editTextString));
}
/**
* Start the import.
* @param view the view which called the function
*/
public void startImport(View view) {
String fileName = trimFileName(editText.getText().toString());
//Step 1 check for suffixes.
if(!isFileNameValid(fileName)){
Toast.makeText(this, "Invalid name or file already exists", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getApplicationContext(), "Starting importation", Toast.LENGTH_SHORT).show();
new Thread(() -> {
importControlFile(fileName);
runOnUiThread(() -> {
Toast.makeText(getApplicationContext(), "Importation finished", Toast.LENGTH_SHORT).show();
finishAndRemoveTask();
});
}).start();
}
/**
* Copy a the file from the Intent data with a provided name
* into the controlmap folder.
* @param fileName The file name to use.
* @return whether the file was successfully imported
*/
private boolean importControlFile(String fileName){
InputStream is;
try {
is = getContentResolver().openInputStream(uriData);
OutputStream os = new FileOutputStream(Tools.CTRLMAP_PATH + "/" + fileName + ".json");
byte[] buffer = new byte[1024];
while(is.read(buffer) != -1)
os.write(buffer);
os.close();
is.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* Tell if the clean version of the filename is valid.
* @param fileName the string to test
* @return whether the filename is valid
*/
private static boolean isFileNameValid(String fileName){
fileName = trimFileName(fileName);
if(fileName.isEmpty()) return false;
if (FileUtils.exists(Tools.CTRLMAP_PATH + "/" + fileName + ".json")) return false;
return true;
}
/**
* Remove or undesirable chars from the string
* @param fileName The string to trim
* @return The trimmed string
*/
private static String trimFileName(String fileName){
return fileName
.replace(".json", "")
.replaceAll("%..", "/")
.replace("/", "")
.replace("\\", "")
.trim();
}
/**
* Tries to get an Uri from the various sources
*/
private void getUriData(){
uriData = getIntent().getData();
if(uriData != null) return;
try {
uriData = getIntent().getClipData().getItemAt(0).getUri();
}catch (Exception ignored){}
}
}

View File

@@ -259,12 +259,12 @@ public class ControlData {
private static void setExpression(String stringExpression){
if(builder.get() == null) bypassExpressionBuilder();
try {
expression.get().set(builder.get(), (String) stringExpression);
expression.get().set(builder.get(), stringExpression);
}catch (IllegalAccessException e){}
}
/**
* Build a shared conversion map without the view dependent values
* Build a shared conversion map without the ControlData dependent values
* You need to set the view dependent values before using it.
*/
private static void buildConversionMap() {
@@ -285,7 +285,9 @@ public class ControlData {
}
/**
* Fill the conversionMap with
* Fill the conversionMap with controlData dependent values.
* The returned valueMap should NOT be kept in memory.
* @return the valueMap to use.
*/
private Map<String, String> fillConversionMap(){
ArrayMap<String, String> valueMap = conversionMap.get();

View File

@@ -1,5 +1,9 @@
package net.kdt.pojavlaunch.utils;
public class FileUtils
{
import java.io.File;
public class FileUtils {
public static boolean exists(String filePath){
return new File(filePath).exists();
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="5dp"/>
<solid
android:color="#272727" />
</shape>

View File

@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="-10dp"
android:scaleType="centerCrop"
android:src="@drawable/sign_in_background" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:background="@drawable/menu_background"
android:paddingVertical="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"
tools:layout_editor_absoluteX="16dp">
<TextView
android:id="@+id/textView_import_control_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="5dp"
android:text="File name:"
app:layout_constraintBottom_toTopOf="@+id/editText_import_control_file_name"
app:layout_constraintStart_toStartOf="@+id/editText_import_control_file_name" />
<com.kdt.mcgui.MineEditText
android:id="@+id/editText_import_control_file_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="35dp"
android:layout_marginBottom="20dp"
android:hint="File name"
android:imeOptions="flagForceAscii"
app:layout_constraintBottom_toTopOf="@+id/mineButton_import_control" />
<com.kdt.mcgui.MineButton
android:id="@+id/mineButton_import_control"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:background="@drawable/mine_button_background"
android:onClick="startImport"
android:text="IMPORT"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editText_import_control_file_name"
app:layout_constraintStart_toStartOf="@+id/editText_import_control_file_name"
app:layout_constraintVertical_bias="0.70" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -47,7 +47,7 @@
app:layout_constraintWidth_max="480dp"
android:layout_height="300dp"
android:background="#272727"
android:background="@drawable/menu_background"
android:translationZ="-1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"