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

View File

@ -2,14 +2,9 @@ package net.kdt.pojavlaunch;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; 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.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
@ -17,57 +12,72 @@ import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.utils.FileUtils; import net.kdt.pojavlaunch.utils.FileUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI;
import java.nio.file.StandardCopyOption;
/** /**
* An activity dedicated to importing control files. * An activity dedicated to importing control files.
*/ */
public class ImportControlActivity extends Activity { public class ImportControlActivity extends Activity {
private Uri uriData; private Uri mUriData;
private boolean mHasIntentChanged = true;
private EditText mEditText;
private EditText editText;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//Check if the intent is valid.
if (getIntent() == null) finishAndRemoveTask();
getUriData();
if(uriData == null) finishAndRemoveTask();
Tools.initContextConstants(getApplicationContext()); Tools.initContextConstants(getApplicationContext());
setContentView(R.layout.import_control_layout); setContentView(R.layout.import_control_layout);
editText = findViewById(R.id.editText_import_control_file_name); mEditText = 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));
/**
* 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. * Start the import.
* @param view the view which called the function * @param view the view which called the function
* @return wether the import has started
*/ */
public void startImport(View view) { public boolean startImport(View view) {
String fileName = trimFileName(editText.getText().toString()); String fileName = trimFileName(mEditText.getText().toString());
//Step 1 check for suffixes. //Step 1 check for suffixes.
if(!isFileNameValid(fileName)){ if(!isFileNameValid(fileName)){
Toast.makeText(this, "Invalid name or file already exists", Toast.LENGTH_SHORT).show(); 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(); Toast.makeText(getApplicationContext(), "Starting importation", Toast.LENGTH_SHORT).show();
//Import thread
new Thread(() -> { new Thread(() -> {
importControlFile(fileName); importControlFile(fileName);
runOnUiThread(() -> { runOnUiThread(() -> {
@ -75,18 +85,18 @@ public class ImportControlActivity extends Activity {
finishAndRemoveTask(); finishAndRemoveTask();
}); });
}).start(); }).start();
return true;
} }
/** /**
* Copy a the file from the Intent data with a provided name * Copy a the file from the Intent data with a provided name into the controlmap folder.
* into the controlmap folder.
* @param fileName The file name to use. * @param fileName The file name to use.
* @return whether the file was successfully imported * @return whether the file was successfully imported
*/ */
private boolean importControlFile(String fileName){ private boolean importControlFile(String fileName){
InputStream is; InputStream is;
try { try {
is = getContentResolver().openInputStream(uriData); is = getContentResolver().openInputStream(mUriData);
OutputStream os = new FileOutputStream(Tools.CTRLMAP_PATH + "/" + fileName + ".json"); OutputStream os = new FileOutputStream(Tools.CTRLMAP_PATH + "/" + fileName + ".json");
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
@ -134,10 +144,10 @@ public class ImportControlActivity extends Activity {
* Tries to get an Uri from the various sources * Tries to get an Uri from the various sources
*/ */
private void getUriData(){ private void getUriData(){
uriData = getIntent().getData(); mUriData = getIntent().getData();
if(uriData != null) return; if(mUriData != null) return;
try { try {
uriData = getIntent().getClipData().getItemAt(0).getUri(); mUriData = getIntent().getClipData().getItemAt(0).getUri();
}catch (Exception ignored){} }catch (Exception ignored){}
} }