Skip to content

Commit

Permalink
Updated storage handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fornwall committed Dec 28, 2015
1 parent 47634ca commit c3aa9d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
27 changes: 22 additions & 5 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(). */
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -327,8 +339,6 @@ public void onClick(View v) {
mTerminalView.checkForTypeface();
mTerminalView.checkForColors();

TermuxInstaller.setupStorageSymlinks(this);

mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1);
}

Expand Down Expand Up @@ -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);
Expand Down
22 changes: 10 additions & 12 deletions app/src/main/java/com/termux/app/TermuxInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());

Expand All @@ -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();
Expand Down

0 comments on commit c3aa9d9

Please sign in to comment.