1
1
package eu .vcmi .vcmi ;
2
2
3
+ import android .Manifest ;
3
4
import android .app .Activity ;
4
5
import android .content .Intent ;
5
6
import android .content .pm .PackageManager ;
6
- import android .net .Uri ;
7
7
import android .os .Build ;
8
8
import android .os .Bundle ;
9
- import android .os .Environment ;
10
- import androidx .annotation .NonNull ;
11
9
import android .view .Menu ;
12
10
import android .view .MenuItem ;
13
11
import android .view .View ;
17
15
import org .json .JSONObject ;
18
16
import org .libsdl .app .SDLActivity ;
19
17
20
- import java .io .BufferedReader ;
21
18
import java .io .File ;
22
- import java .io .IOException ;
23
- import java .io .InputStream ;
24
- import java .io .InputStreamReader ;
25
- import java .net .InetAddress ;
26
- import java .net .ServerSocket ;
27
19
import java .util .ArrayList ;
28
20
import java .util .List ;
29
- import java .util .Objects ;
30
21
22
+ import androidx .core .app .ActivityCompat ;
31
23
import eu .vcmi .vcmi .content .AsyncLauncherInitialization ;
32
24
import eu .vcmi .vcmi .settings .CodepageSettingController ;
33
25
import eu .vcmi .vcmi .settings .DoubleConfig ;
39
31
import eu .vcmi .vcmi .settings .ScreenResSettingController ;
40
32
import eu .vcmi .vcmi .settings .SoundSettingController ;
41
33
import eu .vcmi .vcmi .settings .StartGameController ;
42
- import eu .vcmi .vcmi .settings .UpdateVcmiFilesController ;
43
34
import eu .vcmi .vcmi .util .FileUtil ;
44
35
import eu .vcmi .vcmi .util .Log ;
45
36
import eu .vcmi .vcmi .util .SharedPrefs ;
50
41
*/
51
42
public class ActivityLauncher extends ActivityWithToolbar
52
43
{
44
+ public static final int PERMISSIONS_REQ_CODE = 123 ;
45
+
53
46
private final List <LauncherSettingController <?, ?>> mActualSettings = new ArrayList <>();
54
47
private View mProgress ;
55
48
private TextView mErrorMessage ;
@@ -61,7 +54,7 @@ public class ActivityLauncher extends ActivityWithToolbar
61
54
private LauncherSettingController <Float , SharedPrefs > mCtrlPointerMulti ;
62
55
private LauncherSettingController <Integer , Config > mCtrlSoundVol ;
63
56
private LauncherSettingController <Integer , Config > mCtrlMusicVol ;
64
- private LauncherSettingController < Void , Void > mCtrlStorage ;
57
+
65
58
private final AsyncLauncherInitialization .ILauncherCallbacks mInitCallbacks = new AsyncLauncherInitialization .ILauncherCallbacks ()
66
59
{
67
60
@ Override
@@ -110,7 +103,7 @@ public void onCreate(final Bundle savedInstanceState)
110
103
}
111
104
112
105
Log .i (this , "Starting launcher" );
113
- Storage . initStorage ( this );
106
+
114
107
setContentView (R .layout .activity_launcher );
115
108
initToolbar (R .string .launcher_title , true );
116
109
@@ -155,6 +148,33 @@ public boolean onOptionsItemSelected(final MenuItem item)
155
148
return super .onOptionsItemSelected (item );
156
149
}
157
150
151
+ @ Override
152
+ public void onRequestPermissionsResult (
153
+ int requestCode ,
154
+ String [] permissions ,
155
+ int [] grantResults )
156
+ {
157
+ switch (requestCode )
158
+ {
159
+ case PERMISSIONS_REQ_CODE :
160
+ if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED )
161
+ {
162
+ // vm.onUseExternalStorage();
163
+ }
164
+ return ;
165
+ }
166
+ }
167
+
168
+ public void requestStoragePermissions ()
169
+ {
170
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M )
171
+ {
172
+ requestPermissions (
173
+ new String [] {Manifest .permission .WRITE_EXTERNAL_STORAGE },
174
+ PERMISSIONS_REQ_CODE );
175
+ }
176
+ }
177
+
158
178
private void initSettingsGui ()
159
179
{
160
180
mCtrlStart = new StartGameController (this , v -> onLaunchGameBtnPressed ()).init (R .id .launcher_btn_start );
@@ -165,7 +185,6 @@ private void initSettingsGui()
165
185
mCtrlPointerMulti = new PointerMultiplierSettingController (this ).init (R .id .launcher_btn_pointer_multi , mPrefs );
166
186
mCtrlSoundVol = new SoundSettingController (this ).init (R .id .launcher_btn_volume_sound , mConfig );
167
187
mCtrlMusicVol = new MusicSettingController (this ).init (R .id .launcher_btn_volume_music , mConfig );
168
- mCtrlStorage = new UpdateVcmiFilesController (this , v -> onSetupStorage ()).init (R .id .launcher_btn_storage );
169
188
170
189
mActualSettings .clear ();
171
190
mActualSettings .add (mCtrlCodepage );
@@ -178,9 +197,8 @@ private void initSettingsGui()
178
197
mCtrlStart .hide (); // start is initially hidden, until we confirm that everything is okay via AsyncLauncherInitialization
179
198
}
180
199
181
- private void onSetupStorage ()
200
+ private void openVcmiFolder ()
182
201
{
183
- startActivity (new Intent (this , ActivityStorage .class ));
184
202
}
185
203
186
204
private void onLaunchGameBtnPressed ()
@@ -206,13 +224,10 @@ private void saveConfig()
206
224
}
207
225
}
208
226
209
-
210
227
private void loadConfigFile ()
211
228
{
212
229
try
213
230
{
214
- Storage .initStorage (this );
215
-
216
231
final String settingsFileContent = FileUtil .read (
217
232
new File (FileUtil .configFileLocation (Storage .getVcmiDataDir (this ))));
218
233
@@ -265,4 +280,30 @@ private void onInitFailure(final AsyncLauncherInitialization.InitResult initResu
265
280
mErrorMessage .setVisibility (View .VISIBLE );
266
281
mErrorMessage .setText (initResult .mMessage );
267
282
}
283
+
284
+ private boolean requestExternalStoragePermissions ()
285
+ {
286
+ try
287
+ {
288
+ int storageWritePermissions = ActivityCompat .checkSelfPermission (
289
+ this ,
290
+ Manifest .permission .WRITE_EXTERNAL_STORAGE );
291
+
292
+ if (storageWritePermissions == PackageManager .PERMISSION_GRANTED )
293
+ {
294
+ return true ;
295
+ }
296
+ }
297
+ catch (final RuntimeException ignored )
298
+ {
299
+ mErrorMessage .setVisibility (View .VISIBLE );
300
+ mErrorMessage .setText (this .getString (R .string .launcher_error_permission_broken ));
301
+
302
+ return false ;
303
+ }
304
+
305
+ requestStoragePermissions ();
306
+
307
+ return false ;
308
+ }
268
309
}
0 commit comments