Move button install to background thread and fix

Fix a crash related to file creation access unavailable

Move button install to background thread and fix
This commit is contained in:
TwistedUmbrella 2018-03-27 02:11:55 -04:00
parent f0e2b7831c
commit 068eedc325
2 changed files with 55 additions and 43 deletions

View File

@ -32,6 +32,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
@ -151,42 +152,6 @@ public class FileBrowser extends Fragment {
public void onViewCreated(View view, Bundle savedInstanceState) {
// setContentView(R.layout.activity_main);
parentActivity = getActivity();
try {
File buttons = null;
String theme = mPrefs.getString(Config.pref_theme, null);
if (theme != null) {
buttons = new File(theme);
}
File file = new File(home_directory, "data/buttons.png");
if (buttons != null && buttons.exists()) {
InputStream in = new FileInputStream(buttons);
OutputStream out = new FileOutputStream(file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} else if (!file.exists()) {
file.createNewFile();
OutputStream fo = new FileOutputStream(file);
InputStream png = parentActivity.getAssets()
.open("buttons.png");
byte[] buffer = new byte[4096];
int len = 0;
while ((len = png.read(buffer)) != -1) {
fo.write(buffer, 0, len);
}
fo.close();
png.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
vib = (Vibrator) parentActivity
.getSystemService(Context.VIBRATOR_SERVICE);
@ -207,6 +172,8 @@ public class FileBrowser extends Fragment {
MainActivity.showToastMessage(getActivity(),
getActivity().getString(R.string.config_home),
R.drawable.ic_notification, Toast.LENGTH_LONG);
} else {
(new installGraphics()).execute();
}
if (!ImgBrowse && !games) {
@ -216,6 +183,51 @@ public class FileBrowser extends Fragment {
}
}
private class installGraphics extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
try {
File buttons = null;
String theme = mPrefs.getString(Config.pref_theme, null);
if (theme != null) {
buttons = new File(theme);
}
File file = new File(home_directory, "data/buttons.png");
if (buttons != null && buttons.exists()) {
InputStream in = new FileInputStream(buttons);
OutputStream out = new FileOutputStream(file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} else if (!file.exists()) {
file.getParentFile().mkdirs();
OutputStream fo = new FileOutputStream(file);
InputStream png = parentActivity.getAssets()
.open("buttons.png");
byte[] buffer = new byte[4096];
int len = 0;
while ((len = png.read(buffer)) != -1) {
fo.write(buffer, 0, len);
}
fo.close();
png.close();
}
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
}
private final class LocateGames extends AsyncTask<String, Integer, List<File>> {
private int array;

View File

@ -16,6 +16,7 @@ import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
@ -111,13 +112,12 @@ public class MainActivity extends AppCompatActivity implements
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
}, PERMISSION_REQUEST);
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
PERMISSION_REQUEST);
}
home_directory = mPrefs.getString("home_directory", home_directory);