66import android .app .ProgressDialog ;
77import android .content .DialogInterface ;
88import android .content .DialogInterface .OnCancelListener ;
9+ import android .content .pm .PackageManager ;
910import android .content .pm .PackageManager .NameNotFoundException ;
1011import android .os .AsyncTask ;
1112import android .os .Build ;
1213import android .os .Bundle ;
1314import android .os .Environment ;
15+ import android .support .annotation .NonNull ;
16+ import android .support .v4 .app .ActivityCompat ;
1417import android .text .method .LinkMovementMethod ;
1518import android .text .util .Linkify ;
1619import android .util .Log ;
@@ -32,24 +35,46 @@ public class sdlpluginActivity extends SDLActivity {
3235
3336 private File mSdCardAppDir ;
3437
35- private ProgressDialog pd ;
38+ private ProgressDialog mProgressDialog ;
3639
3740 @ Override
3841 protected void onCreate (Bundle savedInstanceState ) {
3942 Log .v (TAG , "onCreate()" );
4043 super .onCreate (savedInstanceState );
41- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
42- requestPermissions (new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, RC_PERMISSION_WRITE_EXTERNAL_STORAGE );
44+ if (isPermissionGranted ()) {
45+ initUI ();
46+ } else {
47+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
48+ requestPermissions (new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE },
49+ RC_PERMISSION_WRITE_EXTERNAL_STORAGE );
50+ } else {
51+ initUI ();
52+ }
53+ }
54+ }
55+
56+ @ Override
57+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
58+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
59+ if (grantResults .length != 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
60+ initUI ();
4361 }
62+ }
63+
64+ private boolean isPermissionGranted () {
65+ int result = ActivityCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE );
66+ return result == PackageManager .PERMISSION_GRANTED ;
67+ }
68+
4469
70+ private void initUI () {
4571 mSdCardAppDir = new File (Environment .getExternalStorageDirectory (), "CCPlusPlusNIDE" );
46- if (!super . mIsPaused && getIntent ().getExtras () == null ) {
72+ if (!mIsPaused && getIntent ().getExtras () == null ) {
4773 if (mSdCardAppDir .exists () && mSdCardAppDir .isDirectory ()) {
48- if (!new File (mSdCardAppDir + "/SDL/include" ).exists () ||
49- !new File (mSdCardAppDir + "/SDL/lib" ).exists ()) {
50- Log .i (TAG , "Install dev files" );
51-
52- new InstallDevFiles ().execute ();
74+ File include = new File (mSdCardAppDir , "/SDL/include" );
75+ File lib = new File (mSdCardAppDir , "/SDL/lib" );
76+ if (!include .exists () || !lib .exists ()) {
77+ new InstallDevFilesTask ().execute ();
5378 } else {
5479 aboutDialog (1 );
5580 }
@@ -105,45 +130,47 @@ public void onCancel(DialogInterface dialog) {
105130 }
106131
107132 @ SuppressLint ("StaticFieldLeak" )
108- private class InstallDevFiles extends AsyncTask <Void , String , Void > {
133+ private class InstallDevFilesTask extends AsyncTask <Void , String , Void > {
109134 @ Override
110135 protected void onPreExecute () {
111136 super .onPreExecute ();
112- pd = ProgressDialog .show (getContext (), getString (R .string .app_name ),
137+ mProgressDialog = ProgressDialog .show (getContext (),
138+ getString (R .string .app_name ),
113139 getString (R .string .update_checking ), true );
114140 }
115141
116142 @ Override
117143 protected void onProgressUpdate (String ... values ) {
118144 super .onProgressUpdate (values );
119- pd .setMessage (values [0 ]);
145+ mProgressDialog .setMessage (values [0 ]);
120146 }
121147
122148 @ Override
123149 protected Void doInBackground (Void ... params ) {
124150 try {
125- if (!(new File (mSdCardAppDir + "/SDL/lib" ).exists ())) {
126- new File (mSdCardAppDir + "/SDL/lib" ).mkdirs ();
127- Utils .copyDirectory (new File (getCacheDir ().getParentFile ().getAbsolutePath () + "/lib" ),
128- new File (mSdCardAppDir + "/SDL/lib" ));
129- new File (mSdCardAppDir + "/SDL/lib/libmain.so" ).delete ();
130- new File (mSdCardAppDir + "/SDL/lib/libccsdlplugin.so" ).delete ();
151+ File sdlDir = new File (mSdCardAppDir , "SDL" );
152+ File libDir = new File (sdlDir , "/SDL/lib" );
153+ if (!libDir .exists ()) {
154+ libDir .mkdirs ();
155+ Utils .copyDirectory (new File (getCacheDir ().getParentFile ().getAbsolutePath () + "/lib" ), libDir );
156+ new File (libDir , "libmain.so" ).delete ();
157+ new File (libDir , "libccsdlplugin.so" ).delete ();
131158 String arch = Build .CPU_ABI ;
132159 if (arch .startsWith ("mips" )) {
133160 arch = "mips" ;
134161 }
135162 publishProgress (getString (R .string .update_install_libs ));
136163 InputStream is = getAssets ().open ("sdlmain-" + arch + ".zip" );
137- Utils .unpackZip (is , mSdCardAppDir + "/SDL/lib" );
164+ Utils .unpackZip (is , libDir . getAbsolutePath () );
138165 is .close ();
139166 }
140- if (!(new File (mSdCardAppDir + "/SDL/ include" ).exists ())) {
167+ if (!(new File (sdlDir , " include" ).exists ())) {
141168 publishProgress (getString (R .string .update_install_headers ));
142169 InputStream is = getAssets ().open ("headers.zip" );
143- Utils .unpackZip (is , mSdCardAppDir + "/SDL" );
170+ Utils .unpackZip (is , sdlDir . getAbsolutePath () );
144171 is .close ();
145172 }
146- if (!(new File (mSdCardAppDir + "/ Examples/SDL" ).exists ())) {
173+ if (!(new File (mSdCardAppDir , " Examples/SDL" ).exists ())) {
147174 publishProgress (getString (R .string .update_install_examples ));
148175 InputStream is = getAssets ().open ("examples.zip" );
149176 Utils .unpackZip (is , mSdCardAppDir .getAbsolutePath ());
@@ -158,7 +185,7 @@ protected Void doInBackground(Void... params) {
158185 @ Override
159186 protected void onPostExecute (Void result ) {
160187 super .onPostExecute (result );
161- pd .dismiss ();
188+ mProgressDialog .dismiss ();
162189 aboutDialog (1 );
163190 }
164191 }
0 commit comments