Skip to content

Commit c3aa9d9

Browse files
committed
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.
1 parent 47634ca commit c3aa9d9

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection
9292

9393
private static final int MAX_SESSIONS = 8;
9494

95+
private static final int REQUESTCODE_PERMISSION_STORAGE = 1234;
96+
9597
private static final String RELOAD_STYLE_ACTION = "com.termux.app.reload_style";
9698

9799
/** The main view of the activity showing the terminal. Initialized in onCreate(). */
@@ -130,6 +132,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection
130132
public void onReceive(Context context, Intent intent) {
131133
if (mIsVisible) {
132134
String whatToReload = intent.getStringExtra(RELOAD_STYLE_ACTION);
135+
if ("storage".equals(whatToReload)) {
136+
if (ensureStoragePermissionGranted()) TermuxInstaller.setupStorageSymlinks(TermuxActivity.this);
137+
return;
138+
}
133139
if (whatToReload == null || "colors".equals(whatToReload)) mTerminalView.checkForColors();
134140
if (whatToReload == null || "font".equals(whatToReload)) mTerminalView.checkForTypeface();
135141
if (whatToReload == null || "settings".equals(whatToReload)) mSettings.reloadFromProperties(TermuxActivity.this);
@@ -139,11 +145,17 @@ public void onReceive(Context context, Intent intent) {
139145

140146
/** For processes to access shared internal storage (/sdcard) we need this permission. */
141147
@TargetApi(Build.VERSION_CODES.M)
142-
public void ensureStoragePermissionGranted() {
148+
public boolean ensureStoragePermissionGranted() {
143149
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
144-
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
145-
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1234);
150+
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
151+
return true;
152+
} else {
153+
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTCODE_PERMISSION_STORAGE);
154+
return false;
146155
}
156+
} else {
157+
// Always granted before Android 6.0.
158+
return true;
147159
}
148160
}
149161

@@ -327,8 +339,6 @@ public void onClick(View v) {
327339
mTerminalView.checkForTypeface();
328340
mTerminalView.checkForColors();
329341

330-
TermuxInstaller.setupStorageSymlinks(this);
331-
332342
mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1);
333343
}
334344

@@ -772,6 +782,13 @@ public void onClick(DialogInterface dialog, int which) {
772782
}
773783
}
774784

785+
@Override
786+
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
787+
if (requestCode == REQUESTCODE_PERMISSION_STORAGE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
788+
TermuxInstaller.setupStorageSymlinks(this);
789+
}
790+
}
791+
775792
void toggleImmersive() {
776793
boolean newValue = !mSettings.isFullScreen();
777794
mSettings.setFullScreen(this, newValue);

app/src/main/java/com/termux/app/TermuxInstaller.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,21 @@ static void deleteFolder(File fileOrDirectory) {
202202
}
203203

204204
public static void setupStorageSymlinks(final Context context) {
205+
final String LOG_TAG = "termux-storage";
205206
new Thread() {
206207
public void run() {
207208
try {
208-
File storageDir = new File(TermuxService.FILES_PATH, "storage");
209+
File storageDir = new File(TermuxService.HOME_PATH, "storage");
209210

210-
if (storageDir.exists()) {
211-
if (storageDir.isDirectory()) {
212-
return;
213-
} else {
214-
storageDir.delete();
215-
}
211+
if (storageDir.exists() && !storageDir.delete()) {
212+
Log.e(LOG_TAG, "Could not delete old $HOME/storage");
213+
return;
216214
}
217215

218-
storageDir.mkdirs();
216+
if (!storageDir.mkdirs()) {
217+
Log.e(LOG_TAG, "Unable to mkdirs() for $HOME/storage");
218+
return;
219+
}
219220

220221
File sharedDir = Environment.getExternalStorageDirectory();
221222
Os.symlink(sharedDir.getAbsolutePath(), new File(storageDir, "shared").getAbsolutePath());
@@ -226,9 +227,6 @@ public void run() {
226227
File dcimDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
227228
Os.symlink(dcimDir.getAbsolutePath(), new File(storageDir, "dcim").getAbsolutePath());
228229

229-
File documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
230-
Os.symlink(documentsDir.getAbsolutePath(), new File(storageDir, "documents").getAbsolutePath());
231-
232230
File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
233231
Os.symlink(picturesDir.getAbsolutePath(), new File(storageDir, "pictures").getAbsolutePath());
234232

@@ -244,7 +242,7 @@ public void run() {
244242
Os.symlink(externalDir.getAbsolutePath(), new File(storageDir, "external").getAbsolutePath());
245243
}
246244
} catch (Exception e) {
247-
Log.e("termux", "Error setting up link", e);
245+
Log.e(LOG_TAG, "Error setting up link", e);
248246
}
249247
}
250248
}.start();

0 commit comments

Comments
 (0)