From c3aa9d9662af63b9b42a872c0790eaca1dfb516a Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 28 Dec 2015 01:37:00 +0100 Subject: [PATCH] Updated storage handling Let the user run termux-setup-storage, which will ensure that storage permission has been granted and setup $HOME/storage/ folder with symlinks to storage folders. --- .../java/com/termux/app/TermuxActivity.java | 27 +++++++++++++++---- .../java/com/termux/app/TermuxInstaller.java | 22 +++++++-------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index e6c6084fc5..53764cdf4f 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -92,6 +92,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection private static final int MAX_SESSIONS = 8; + private static final int REQUESTCODE_PERMISSION_STORAGE = 1234; + private static final String RELOAD_STYLE_ACTION = "com.termux.app.reload_style"; /** The main view of the activity showing the terminal. Initialized in onCreate(). */ @@ -130,6 +132,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection public void onReceive(Context context, Intent intent) { if (mIsVisible) { String whatToReload = intent.getStringExtra(RELOAD_STYLE_ACTION); + if ("storage".equals(whatToReload)) { + if (ensureStoragePermissionGranted()) TermuxInstaller.setupStorageSymlinks(TermuxActivity.this); + return; + } if (whatToReload == null || "colors".equals(whatToReload)) mTerminalView.checkForColors(); if (whatToReload == null || "font".equals(whatToReload)) mTerminalView.checkForTypeface(); if (whatToReload == null || "settings".equals(whatToReload)) mSettings.reloadFromProperties(TermuxActivity.this); @@ -139,11 +145,17 @@ public void onReceive(Context context, Intent intent) { /** For processes to access shared internal storage (/sdcard) we need this permission. */ @TargetApi(Build.VERSION_CODES.M) - public void ensureStoragePermissionGranted() { + public boolean ensureStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1234); + if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + return true; + } else { + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTCODE_PERMISSION_STORAGE); + return false; } + } else { + // Always granted before Android 6.0. + return true; } } @@ -327,8 +339,6 @@ public void onClick(View v) { mTerminalView.checkForTypeface(); mTerminalView.checkForColors(); - TermuxInstaller.setupStorageSymlinks(this); - mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1); } @@ -772,6 +782,13 @@ public void onClick(DialogInterface dialog, int which) { } } + @Override + public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { + if (requestCode == REQUESTCODE_PERMISSION_STORAGE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + TermuxInstaller.setupStorageSymlinks(this); + } + } + void toggleImmersive() { boolean newValue = !mSettings.isFullScreen(); mSettings.setFullScreen(this, newValue); diff --git a/app/src/main/java/com/termux/app/TermuxInstaller.java b/app/src/main/java/com/termux/app/TermuxInstaller.java index bc3a2477ce..996fcd6d02 100644 --- a/app/src/main/java/com/termux/app/TermuxInstaller.java +++ b/app/src/main/java/com/termux/app/TermuxInstaller.java @@ -202,20 +202,21 @@ static void deleteFolder(File fileOrDirectory) { } public static void setupStorageSymlinks(final Context context) { + final String LOG_TAG = "termux-storage"; new Thread() { public void run() { try { - File storageDir = new File(TermuxService.FILES_PATH, "storage"); + File storageDir = new File(TermuxService.HOME_PATH, "storage"); - if (storageDir.exists()) { - if (storageDir.isDirectory()) { - return; - } else { - storageDir.delete(); - } + if (storageDir.exists() && !storageDir.delete()) { + Log.e(LOG_TAG, "Could not delete old $HOME/storage"); + return; } - storageDir.mkdirs(); + if (!storageDir.mkdirs()) { + Log.e(LOG_TAG, "Unable to mkdirs() for $HOME/storage"); + return; + } File sharedDir = Environment.getExternalStorageDirectory(); Os.symlink(sharedDir.getAbsolutePath(), new File(storageDir, "shared").getAbsolutePath()); @@ -226,9 +227,6 @@ public void run() { File dcimDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); Os.symlink(dcimDir.getAbsolutePath(), new File(storageDir, "dcim").getAbsolutePath()); - File documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); - Os.symlink(documentsDir.getAbsolutePath(), new File(storageDir, "documents").getAbsolutePath()); - File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); Os.symlink(picturesDir.getAbsolutePath(), new File(storageDir, "pictures").getAbsolutePath()); @@ -244,7 +242,7 @@ public void run() { Os.symlink(externalDir.getAbsolutePath(), new File(storageDir, "external").getAbsolutePath()); } } catch (Exception e) { - Log.e("termux", "Error setting up link", e); + Log.e(LOG_TAG, "Error setting up link", e); } } }.start();