Revert "Improvements to migration code Will be squashed before commit"

This reverts commit 3dab003b19d5b9fa096de2be2672029f80655acb.
This commit is contained in:
LibretroAdmin 2024-07-01 18:40:40 -07:00
parent 3dab003b19
commit 8b13a4ab0c

View File

@ -13,7 +13,6 @@ import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -24,18 +23,10 @@ import android.util.Pair;
import com.retroarch.R; import com.retroarch.R;
import java.io.File; import java.io.File;
import java.io.IOException; import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.stream.Stream;
@TargetApi(26) @TargetApi(26)
public class MigrateRetroarchFolderActivity extends Activity public class MigrateRetroarchFolderActivity extends Activity
@ -43,9 +34,9 @@ public class MigrateRetroarchFolderActivity extends Activity
final int REQUEST_CODE_GET_OLD_RETROARCH_FOLDER = 125; final int REQUEST_CODE_GET_OLD_RETROARCH_FOLDER = 125;
@Override @Override
public void onCreate(Bundle savedInstanceState) public void onStart()
{ {
super.onCreate(savedInstanceState); super.onStart();
// Needs v26 for some of the file handling functions below. // Needs v26 for some of the file handling functions below.
// Remove the TargetApi annotation to see which. // Remove the TargetApi annotation to see which.
@ -53,7 +44,7 @@ public class MigrateRetroarchFolderActivity extends Activity
if (android.os.Build.VERSION.SDK_INT < 26) { if (android.os.Build.VERSION.SDK_INT < 26) {
finish(); finish();
} }
if(needToMigrate()){ if(true || needToMigrate()){
askToMigrate(); askToMigrate();
}else{ }else{
finish(); finish();
@ -88,7 +79,6 @@ public class MigrateRetroarchFolderActivity extends Activity
void askToMigrate() void askToMigrate()
{ {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.migrate_retroarch_folder_dialog_title); builder.setTitle(R.string.migrate_retroarch_folder_dialog_title);
builder.setMessage(R.string.migrate_retroarch_folder_dialog_message); builder.setMessage(R.string.migrate_retroarch_folder_dialog_message);
builder.setNegativeButton(R.string.migrate_retroarch_folder_dialog_negative, new DialogInterface.OnClickListener() { builder.setNegativeButton(R.string.migrate_retroarch_folder_dialog_negative, new DialogInterface.OnClickListener() {
@ -138,7 +128,7 @@ public class MigrateRetroarchFolderActivity extends Activity
final ProgressDialog pd = new ProgressDialog(this); final ProgressDialog pd = new ProgressDialog(this);
pd.setMax(100); pd.setMax(100);
pd.setTitle(R.string.migrate_retroarch_folder_inprogress); pd.setTitle(R.string.migrate_retroarch_folder_inprogress);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false); pd.setCancelable(false);
CopyThread thread = new CopyThread() CopyThread thread = new CopyThread()
@ -149,10 +139,11 @@ public class MigrateRetroarchFolderActivity extends Activity
pd.show(); pd.show();
} }
@Override @Override
protected void onProgressUpdate(String... params) protected void onProgressUpdate(Pair<Integer, String>... params)
{ {
super.onProgressUpdate(params); super.onProgressUpdate(params);
pd.setMessage(params[0]); pd.setProgress(params[0].first);
pd.setMessage(params[0].second);
} }
@Override @Override
protected void onPostExecute(Boolean ok) protected void onPostExecute(Boolean ok)
@ -170,7 +161,7 @@ public class MigrateRetroarchFolderActivity extends Activity
{ {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean("external_retroarch_folder_needs_migrate", false); editor.putBoolean("external_retroarch_folder_needs_migrate", false);
editor.apply(); editor.commit();
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(ok ? builder.setMessage(ok ?
@ -185,24 +176,27 @@ public class MigrateRetroarchFolderActivity extends Activity
builder.create().show(); builder.create().show();
} }
class CopyThread extends AsyncTask<Uri, String, Boolean> class CopyThread extends AsyncTask<Uri, Pair<Integer, String>, Boolean>
{ {
String PACKAGE_NAME;
ContentResolver resolver; ContentResolver resolver;
Uri sourceRoot;
boolean error; boolean error;
@Override ArrayList<int[]> progress;
protected void onPreExecute() public CopyThread()
{ {
PACKAGE_NAME = MigrateRetroarchFolderActivity.this.getPackageName();
resolver = MigrateRetroarchFolderActivity.this.getContentResolver(); resolver = MigrateRetroarchFolderActivity.this.getContentResolver();
error = false;
} }
@Override @Override
protected Boolean doInBackground(Uri... params) protected Boolean doInBackground(Uri... params)
{ {
Uri source = params[0]; sourceRoot = params[0];
error = false; error = false;
String destination = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + MigrateRetroarchFolderActivity.this.getPackageName() + "/files/RetroArch"; progress = new ArrayList<>();
copyFolder(source, new File(destination));
patchConfig(); String destination = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + PACKAGE_NAME + "/files/RetroArch";
copyFolder(sourceRoot, new File(destination));
return !error; return !error;
} }
void copyFolder(Uri sourceUri, File dest) void copyFolder(Uri sourceUri, File dest)
@ -220,8 +214,8 @@ public class MigrateRetroarchFolderActivity extends Activity
}catch(IllegalArgumentException ex){ //for root selected by document picker }catch(IllegalArgumentException ex){ //for root selected by document picker
sourceChildrenResolver = DocumentsContract.buildChildDocumentsUriUsingTree(sourceUri, DocumentsContract.getTreeDocumentId(sourceUri)); sourceChildrenResolver = DocumentsContract.buildChildDocumentsUriUsingTree(sourceUri, DocumentsContract.getTreeDocumentId(sourceUri));
} }
progress.add(new int[]{0, 1});
try( try(
//list children of directory
Cursor c = resolver.query(sourceChildrenResolver, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE}, null, null, null) Cursor c = resolver.query(sourceChildrenResolver, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE}, null, null, null)
) { ) {
if(c == null) { if(c == null) {
@ -229,6 +223,7 @@ public class MigrateRetroarchFolderActivity extends Activity
error = true; error = true;
return; return;
} }
progress.get(progress.size() - 1)[1] = c.getCount();
while(c.moveToNext()){ //loop through children returned while(c.moveToNext()){ //loop through children returned
String childFilename = c.getString(1); String childFilename = c.getString(1);
Uri childUri = DocumentsContract.buildDocumentUriUsingTree(sourceUri, c.getString(0)); Uri childUri = DocumentsContract.buildDocumentUriUsingTree(sourceUri, c.getString(0));
@ -248,59 +243,24 @@ public class MigrateRetroarchFolderActivity extends Activity
error = true; error = true;
} }
} }
publishProgress(destFile.toString()); progress.get(progress.size() - 1)[0]++;
publishProgress(new Pair<Integer, String>(getProgressPercentage(), destFile.getPath()));
} }
}catch(Exception ex){ }catch(Exception ex){
Log.e("MigrateRetroarchFolder", "Error while copying", ex); Log.e("MigrateRetroarchFolder", "Error while copying", ex);
error = true; error = true;
} }
progress.remove(progress.size() - 1);
} }
void patchConfig(){ int getProgressPercentage()
String appDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + MigrateRetroarchFolderActivity.this.getPackageName() + "/files"; {
String legacyDefaultRetroarchPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/RetroArch"; float sum = 0;
String migratedRetroarchPath = appDir + "/RetroArch"; int lastDenominator = 1;
for(int[] frac : progress){
final ArrayList<Path> files = new ArrayList<>(); sum += ((float) frac[0]) / frac[1] / lastDenominator;
try ( lastDenominator *= frac[1];
Stream<Path> s = Files.find(Paths.get(appDir), 30, new BiPredicate<Path, BasicFileAttributes>() {
@Override
public boolean test(Path path, BasicFileAttributes basicFileAttributes) {
String p = path.toString();
return p.endsWith(".lpl") || p.endsWith(".cfg");
}
})
){
// yes it's a roudabout way to gather it in a list, but Stream.collect is throwing type errors
s.forEach(new Consumer<Path>() {
@Override
public void accept(Path path) {
files.add(path);
}
});
}catch(IOException ex){
Log.e("MigrateRetroarchFolder", "Error searching for config files", ex);
error = true;
return;
}
for(Path file : files){
try{
//back up before patching
Path backupFile = file.resolveSibling(file.getFileName() + ".old");
Files.copy(file, backupFile, StandardCopyOption.REPLACE_EXISTING);
//replace old retroarch prefix with new path
//assumes default old path, doesn't help if user has relocated it
//not doing any syntactical analysis because the search string is pretty long and unique
String contents = new String(Files.readAllBytes(file));
String replaced = contents.replace(legacyDefaultRetroarchPath, migratedRetroarchPath);
Files.write(file, replaced.getBytes(StandardCharsets.UTF_8));
}catch(IOException ex){
Log.e("MigrateRetroarchFolder", "Error patching file " + file.toAbsolutePath(), ex);
error = true;
return;
}
} }
return (int) (sum * 100);
} }
} }
} }