Limit to only one instance of the import screen

This commit is contained in:
SerpentSpirale 2021-09-09 16:32:26 +02:00 committed by SerpentSpirale
parent 1002d59cc9
commit 094bd32f2b
2 changed files with 42 additions and 30 deletions

View File

@ -42,6 +42,7 @@
<activity android:name=".ImportControlActivity"
android:exported="true"
android:windowSoftInputMode="stateVisible"
android:launchMode="singleInstance"
android:configChanges="keyboard|keyboardHidden"
>
@ -51,6 +52,7 @@
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/json"/>
<data android:mimeType="text/json"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>

View File

@ -2,14 +2,9 @@ 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;
@ -17,57 +12,72 @@ 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 Uri mUriData;
private boolean mHasIntentChanged = true;
private EditText editText;
private EditText mEditText;
@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));
mEditText = findViewById(R.id.editText_import_control_file_name);
}
/**
* Override the previous loaded intent
* @param intent the intent used to replace the old one.
*/
@Override
protected void onNewIntent(Intent intent) {
if(intent != null) setIntent(intent);
mHasIntentChanged = true;
}
/**
* Update if the intent changed
*/
@Override
protected void onPostResume() {
super.onPostResume();
if(!mHasIntentChanged) return;
getUriData();
//Set the name of the file in the editText.
String editTextString = mUriData.toString().replaceAll("%..", "/");
editTextString = editTextString.substring(editTextString.lastIndexOf('/') + 1);
mEditText.setText(trimFileName(editTextString));
mHasIntentChanged = false;
}
/**
* Start the import.
* @param view the view which called the function
* @return wether the import has started
*/
public void startImport(View view) {
String fileName = trimFileName(editText.getText().toString());
public boolean startImport(View view) {
String fileName = trimFileName(mEditText.getText().toString());
//Step 1 check for suffixes.
if(!isFileNameValid(fileName)){
Toast.makeText(this, "Invalid name or file already exists", Toast.LENGTH_SHORT).show();
return;
return false;
}
Toast.makeText(getApplicationContext(), "Starting importation", Toast.LENGTH_SHORT).show();
//Import thread
new Thread(() -> {
importControlFile(fileName);
runOnUiThread(() -> {
@ -75,18 +85,18 @@ public class ImportControlActivity extends Activity {
finishAndRemoveTask();
});
}).start();
return true;
}
/**
* Copy a the file from the Intent data with a provided name
* into the controlmap folder.
* 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);
is = getContentResolver().openInputStream(mUriData);
OutputStream os = new FileOutputStream(Tools.CTRLMAP_PATH + "/" + fileName + ".json");
byte[] buffer = new byte[1024];
@ -134,10 +144,10 @@ public class ImportControlActivity extends Activity {
* Tries to get an Uri from the various sources
*/
private void getUriData(){
uriData = getIntent().getData();
if(uriData != null) return;
mUriData = getIntent().getData();
if(mUriData != null) return;
try {
uriData = getIntent().getClipData().getItemAt(0).getUri();
mUriData = getIntent().getClipData().getItemAt(0).getUri();
}catch (Exception ignored){}
}