Skip to content

Commit

Permalink
install progress
Browse files Browse the repository at this point in the history
  • Loading branch information
weihuoya committed Jul 30, 2021
1 parent 70c6711 commit 02c4254
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/android/app/src/main/assets/cheats/000400000011C500.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
* Pokémon Alpha Sapphire

[60FPS v1.0]
28C650C1 00000000

[30FPS v1.0]
28C650C1 00000001

[60FPS v1.4]
28C70D20 00000000
28C690A1 00000000

[30FPS v1.4]
28C70D20 00000001
28C690A1 00000001
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public abstract class AbstractFilePickerFragment<T>
protected final HashSet<CheckableViewHolder> mCheckedVisibleViewHolders;
protected int mode = MODE_FILE;
protected T mCurrentPath = null;
protected String mStartPath = null;
protected boolean allowMultiple = false;
protected boolean allowExistingFile = true;
protected boolean singleClick = false;
Expand Down Expand Up @@ -403,9 +404,9 @@ public void onActivityCreated(Bundle savedInstanceState) {
getArguments().getBoolean(KEY_ALLOW_EXISTING_FILE, allowExistingFile);
singleClick = getArguments().getBoolean(KEY_SINGLE_CLICK, singleClick);
if (getArguments().containsKey(KEY_START_PATH)) {
String path = getArguments().getString(KEY_START_PATH);
if (path != null) {
T file = getPath(path.trim());
mStartPath = getArguments().getString(KEY_START_PATH);
if (mStartPath != null) {
T file = getPath(mStartPath.trim());
if (isDir(file)) {
mCurrentPath = file;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package com.nononsenseapps.filepicker;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.FileObserver;
import android.widget.Toast;

Expand All @@ -21,6 +24,9 @@
import androidx.recyclerview.widget.SortedListAdapterCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.citra.emu.R;

/**
Expand Down Expand Up @@ -217,6 +223,38 @@ public Loader<SortedList<File>> getLoader() {
@Override
public SortedList<File> loadInBackground() {
File[] listFiles = mCurrentPath.listFiles();
if (listFiles == null && mStartPath != null) {
String path = mCurrentPath.getPath();
if (mStartPath.startsWith(path)) {
int length = path.length();
if (!path.endsWith(File.separator)) {
length += 1;
}
String subpath = mStartPath.substring(length);
int index = subpath.indexOf(File.separator);
if (index != -1) {
subpath = subpath.substring(0, index);
}
File file = new File(path + File.separator + subpath);

if (path.equals(File.separator)) {
List<String> sdcards = getSdCardPaths(getContext());
List<File> files = new ArrayList<>();
if (file.exists()) {
files.add(file);
}
for (String sdcard : sdcards) {
file = new File(sdcard);
if (file.exists()) {
files.add(file);
}
}
listFiles = files.toArray(new File[0]);
} else if (file.exists()) {
listFiles = new File[]{file};
}
}
}
final int initCap = listFiles == null ? 0 : listFiles.length;

SortedList<File> files = new SortedList<>(
Expand Down Expand Up @@ -332,4 +370,85 @@ protected int compareFiles(@NonNull File lhs, @NonNull File rhs) {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
}

// Unofficial hacks to get a list of SD cards that are not the main "external storage".

This comment has been minimized.

Copy link
@weihuoya

weihuoya Jul 30, 2021

Author Owner

a LOT of unnecessary overhead? are you serious? just keep it for better code reference.

private static List<String> getSdCardPaths(final Context context) {
// Q is the last version that will support normal file access.
List<String> list = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
// Log.i(TAG, "getSdCardPaths: Trying KitKat method");
list = getSdCardPaths19(context);
}

if (list == null) {
// Log.i(TAG, "getSdCardPaths: Attempting fallback");
// Try another method.
list = new ArrayList<String>();
File[] fileList = new File("/storage/").listFiles();
if (fileList != null) {
String removableStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
for (File file : fileList) {
if (!file.getAbsolutePath().equalsIgnoreCase(removableStoragePath) &&
file.isDirectory() &&
file.canRead()) {
list.add(file.getAbsolutePath());
}
}
}
}

// TODO: On older devices, try System.getenv(¡°EXTERNAL_SDCARD_STORAGE¡±)

return list;
}

/**
* returns a list of all available sd cards paths, or null if not found.
*/
private static List<String> getSdCardPaths19(final Context context) {
final File[] externalCacheDirs = context.getExternalCacheDirs();
if (externalCacheDirs == null || externalCacheDirs.length==0)
return null;
if (externalCacheDirs.length == 1) {
if (externalCacheDirs[0] == null)
return null;
final String storageState = Environment.getStorageState(externalCacheDirs[0]);
if (!Environment.MEDIA_MOUNTED.equals(storageState))
return null;
if (Environment.isExternalStorageEmulated())
return null;
}
final List<String> result = new ArrayList<>();
if (externalCacheDirs.length == 1)
result.add(getRootOfInnerSdCardFolder(externalCacheDirs[0]));
for (int i = 1; i < externalCacheDirs.length; ++i) {
final File file=externalCacheDirs[i];
if (file == null)
continue;
final String storageState = Environment.getStorageState(file);
if (Environment.MEDIA_MOUNTED.equals(storageState))
result.add(getRootOfInnerSdCardFolder(externalCacheDirs[i]));
}
if (result.isEmpty())
return null;
return result;
}

/** Given any file/folder inside an sd card, this will return the path of the sd card */
private static String getRootOfInnerSdCardFolder(File file) {
if (file == null)
return null;
final long totalSpace = file.getTotalSpace();
while (true) {
final File parentFile = file.getParentFile();
if (parentFile == null || !parentFile.canRead()) {
break;
}
if (parentFile.getTotalSpace() != totalSpace) {
break;
}
file = parentFile;
}
return file.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static void loadImageFromFile(String path) {
HandleImage(pixels, width, height);
}

public static void updateProgress(String name, int written, int total) {
public static void updateProgress(String name, long written, long total) {
EmulationActivity activity1 = EmulationActivity.get();
if (activity1 != null) {
activity1.runOnUiThread(() -> activity1.updateProgress(name, written, total));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public void rotateScreen() {
}
}

public void updateProgress(String name, int written, int total) {
public void updateProgress(String name, long written, long total) {
mEmulationFragment.updateProgress(name, written, total);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public void addNetPlayMessage(String msg) {
}, 6 * 1000);
}

public void updateProgress(String name, int written, int total) {
public void updateProgress(String name, long written, long total) {
mProgressBar.setVisibility(written < total ? View.VISIBLE : View.INVISIBLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
Expand Down Expand Up @@ -143,6 +144,7 @@ protected void onPostExecute(Void args) {
private String[] mFilesToAdd;
private GameAdapter mAdapter;
private ProgressBar mProgressBar;
private TextView mProgressText;
private TextView mGameEmulationInfo;
private TextView mAppEmulationInfo;
private RecyclerView mGameListView;
Expand All @@ -168,6 +170,7 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);

mProgressBar = findViewById(R.id.progress_bar);
mProgressText = findViewById(R.id.progress_text);
mGameEmulationInfo = findViewById(R.id.game_emulation_info);
mAppEmulationInfo = findViewById(R.id.app_emulation_info);
mBtnAddFiles = findViewById(R.id.btn_add_files);
Expand Down Expand Up @@ -269,6 +272,7 @@ protected void onResume() {
}
mFilesToAdd = null;
final String[] files = filelist.toArray(new String[0]);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
new Thread(() -> NativeLibrary.InstallCIA(files)).start();
}
}
Expand Down Expand Up @@ -484,11 +488,15 @@ public File getGameListCache() {
return list;
}

public void updateProgress(String name, int written, int total) {
public void updateProgress(String name, long written, long total) {
if (written < total) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressText.setVisibility(View.VISIBLE);
mProgressText.setText((written * 100 / total) + "%");
} else {
mProgressBar.setVisibility(View.INVISIBLE);
mProgressText.setVisibility(View.INVISIBLE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (total == 0) {
if (written == 0) {
Toast.makeText(this, getString(R.string.cia_install_success), Toast.LENGTH_LONG).show();
Expand Down
23 changes: 20 additions & 3 deletions src/android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,26 @@

<ProgressBar
android:id="@+id/progress_bar"
android:visibility="invisible"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:visibility="invisible"
android:layout_width="80dp"
android:layout_height="80dp"/>

<TextView
android:id="@+id/progress_text"
android:text="99%"
android:shadowColor="@android:color/black"
android:shadowDx="1.3"
android:shadowDy="1.3"
android:shadowRadius="1.3"
android:textColor="@color/citra_accent"
android:typeface="monospace"
android:textSize="16sp"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.android.tools.build:gradle:7.0.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion src/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
2 changes: 1 addition & 1 deletion src/android/jni/jni_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void NativeLibrary::LoadImageFromFile(std::vector<u8>& pixels, u32& width, u32&
JniHelper::CallStaticMethod<void>(CLASS, "loadImageFromFile", path);
}

void NativeLibrary::UpdateProgress(const std::string& name, u32 progress, u32 total) {
void NativeLibrary::UpdateProgress(const std::string& name, u64 progress, u64 total) {
JniHelper::CallStaticMethod<void>(CLASS, "updateProgress", name, progress, total);
}

Expand Down
2 changes: 1 addition & 1 deletion src/android/jni/jni_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NativeLibrary {
u32 height);
static void LoadImageFromFile(std::vector<u8>& pixels, u32& width, u32& height,
const std::string& path);
static void UpdateProgress(const std::string& name, u32 progress, u32 total);
static void UpdateProgress(const std::string& name, u64 progress, u64 total);
static void ShowInputBoxDialog(int maxLength, const std::string& error, const std::string& hint,
const std::string& button0, const std::string& button1,
const std::string& button2);
Expand Down
4 changes: 4 additions & 0 deletions src/android/jni/jni_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class JniHelper {
return "J";
}

static std::string JniSignature(unsigned long) {
return "J";
}

static std::string JniSignature(float) {
return "F";
}
Expand Down

0 comments on commit 02c4254

Please sign in to comment.