2323import android .os .Bundle ;
2424import android .os .Environment ;
2525import android .os .Handler ;
26+ import android .support .annotation .NonNull ;
2627import android .support .v4 .app .ActivityCompat ;
2728import android .support .v4 .content .ContextCompat ;
2829import android .util .Base64 ;
@@ -104,6 +105,7 @@ public class MainActivity extends NativeActivity {
104105 public static native void onUnicodeChar (int ch );
105106 public static native boolean optionSelected (int index );
106107 public static native void runFile (String fileName );
108+ public static native void setenv (String name , String value );
107109
108110 public void addShortcut (final byte [] pathBytes ) {
109111 final String path = getString (pathBytes );
@@ -168,7 +170,6 @@ public void onClick(DialogInterface dialog, int which) {
168170 mutex .acquire ();
169171 } catch (InterruptedException e ) {
170172 Log .i (TAG , "ask failed: " , e );
171- e .printStackTrace ();
172173 }
173174 Log .i (TAG , "ask result=" + result );
174175 return result .value ;
@@ -229,7 +230,6 @@ public void run() {
229230 mutex .acquire ();
230231 } catch (InterruptedException e ) {
231232 Log .i (TAG , "getClipboardText failed: " , e );
232- e .printStackTrace ();
233233 }
234234 byte [] result ;
235235 try {
@@ -241,26 +241,6 @@ public void run() {
241241 return result ;
242242 }
243243
244- public String getExternalStorage () {
245- String result ;
246- String path = Environment .getExternalStorageDirectory ().getAbsolutePath ();
247- if (isPublicStorage (path )) {
248- File sb = new File (path , FOLDER_NAME );
249- if ((sb .isDirectory () && sb .canWrite ()) || sb .mkdirs ()) {
250- result = path + "/" + FOLDER_NAME ;
251- } else {
252- result = path ;
253- }
254- } else {
255- result = getInternalStorage ();
256- }
257- return result ;
258- }
259-
260- public String getInternalStorage () {
261- return getFilesDir ().getAbsolutePath ();
262- }
263-
264244 public String getIpAddress () {
265245 String result = "" ;
266246 try {
@@ -384,6 +364,16 @@ public boolean onPrepareOptionsMenu(Menu menu) {
384364 return super .onPrepareOptionsMenu (menu );
385365 }
386366
367+ @ Override
368+ public void onRequestPermissionsResult (int requestCode ,
369+ @ NonNull String [] permissions ,
370+ @ NonNull int [] grantResults ) {
371+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
372+ if (requestCode == REQUEST_STORAGE_PERMISSION && grantResults [0 ] != PackageManager .PERMISSION_DENIED ) {
373+ setupStorageEnvironment (true );
374+ }
375+ }
376+
387377 public void optionsBox (final String [] items ) {
388378 this ._options = items ;
389379 runOnUiThread (new Runnable () {
@@ -628,13 +618,16 @@ private String buildTokenForm() {
628618
629619 private void checkFilePermission () {
630620 if (!permitted (Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
621+ setupStorageEnvironment (false );
631622 Runnable handler = new Runnable () {
632623 @ Override
633624 public void run () {
634625 checkPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE , REQUEST_STORAGE_PERMISSION );
635626 }
636627 };
637628 new Handler ().postDelayed (handler , 250 );
629+ } else {
630+ setupStorageEnvironment (true );
638631 }
639632 }
640633
@@ -700,6 +693,27 @@ private void execStream(final String line, DataInputStream inputStream) throws I
700693 runFile (outputFile .getAbsolutePath ());
701694 }
702695
696+ private String getExternalStorage () {
697+ String result ;
698+ String path = Environment .getExternalStorageDirectory ().getAbsolutePath ();
699+ if (isPublicStorage (path )) {
700+ File sb = new File (path , FOLDER_NAME );
701+ if ((sb .isDirectory () && sb .canWrite ()) || sb .mkdirs ()) {
702+ result = path + "/" + FOLDER_NAME ;
703+ } else {
704+ result = path ;
705+ }
706+ } else {
707+ result = getInternalStorage ();
708+ }
709+ return result ;
710+ }
711+
712+ private String getInternalStorage () {
713+ Log .d (TAG , "internal = " + getFilesDir ().getAbsolutePath ());
714+ return getFilesDir ().getAbsolutePath ();
715+ }
716+
703717 private Map <String , String > getPostData (DataInputStream inputStream , final String line ) throws IOException {
704718 int length = 0 ;
705719 final String lengthHeader = "content-length: " ;
@@ -737,7 +751,6 @@ private String getString(final byte[] promptBytes) {
737751 return new String (promptBytes , CP1252 );
738752 } catch (UnsupportedEncodingException e ) {
739753 Log .i (TAG , "getString failed: " , e );
740- e .printStackTrace ();
741754 return "" ;
742755 }
743756 }
@@ -748,7 +761,7 @@ private boolean isPublicStorage(String dir) {
748761 result = false ;
749762 } else {
750763 File file = new File (dir );
751- result = file .isDirectory () && file .canWrite ();
764+ result = file .isDirectory () && file .canRead () && file . canWrite ();
752765 }
753766 return result ;
754767 }
@@ -887,6 +900,13 @@ private void sendResponse(Socket socket, String content) throws IOException {
887900 out .close ();
888901 }
889902
903+ private void setupStorageEnvironment (boolean external ) {
904+ if (external ) {
905+ setenv ("EXTERNAL_DIR" , getExternalStorage ());
906+ }
907+ setenv ("INTERNAL_DIR" , getInternalStorage ());
908+ }
909+
890910 private void startServer (final int socketNum , final String token ) {
891911 Thread socketThread = new Thread (new Runnable () {
892912 public void run () {
0 commit comments