@@ -146,6 +146,8 @@ public void onCreate(Bundle theSavedInstanceState) {
146146 myContext = new ContextWrapper (this );
147147 myContext .getExternalFilesDir (null );
148148
149+ askUserPermission (android .Manifest .permission .WRITE_EXTERNAL_STORAGE , null );
150+
149151 mySensorMgr = (SensorManager )getSystemService (Context .SENSOR_SERVICE );
150152 mySensorOri = mySensorMgr .getDefaultSensor (Sensor .TYPE_ROTATION_VECTOR );
151153 if (mySensorOri == null ) {
@@ -172,6 +174,53 @@ public void onCreate(Bundle theSavedInstanceState) {
172174 updateHideSystemBars (myToHideStatusBar , myToHideNavBar );
173175 }
174176
177+ /**
178+ * Request user permission.
179+ */
180+ protected void askUserPermission (String thePermission , String theRationale ) {
181+ // Dynamically load methods introduced by API level 23.
182+ // On older system this permission is granted by user during application installation.
183+ java .lang .reflect .Method aMetPtrCheckSelfPermission , aMetPtrRequestPermissions , aMetPtrShouldShowRequestPermissionRationale ;
184+ try {
185+ aMetPtrCheckSelfPermission = myContext .getClass ().getMethod ("checkSelfPermission" , String .class );
186+ aMetPtrRequestPermissions = getClass ().getMethod ("requestPermissions" , String [].class , int .class );
187+ aMetPtrShouldShowRequestPermissionRationale = getClass ().getMethod ("shouldShowRequestPermissionRationale" , String .class );
188+ } catch (SecurityException theError ) {
189+ //postMessage("Unable to find permission methods:\n" + theError.getMessage());
190+ return ;
191+ } catch (NoSuchMethodException theError ) {
192+ //postMessage("Unable to find permission methods:\n" + theError.getMessage());
193+ return ;
194+ }
195+
196+ try {
197+ //int isAlreadyGranted = myContext.checkSelfPermission(thePermission);
198+ int isAlreadyGranted = (Integer )aMetPtrCheckSelfPermission .invoke (myContext , thePermission );
199+ if (isAlreadyGranted == android .content .pm .PackageManager .PERMISSION_GRANTED ) {
200+ return ;
201+ }
202+
203+ //boolean toShowInfo = shouldShowRequestPermissionRationale(thePermission);
204+ boolean toShowInfo = theRationale != null && (Boolean )aMetPtrShouldShowRequestPermissionRationale .invoke (this , thePermission );
205+ if (toShowInfo ) {
206+ postMessage (theRationale );
207+ }
208+
209+ // show dialog to user
210+ //requestPermissions (new String[]{thePermission}, 0);
211+ aMetPtrRequestPermissions .invoke (this , new String []{thePermission }, 0 );
212+ } catch (IllegalArgumentException theError ) {
213+ postMessage ("Internal error: Unable to call permission method:\n " + theError .getMessage ());
214+ return ;
215+ } catch (IllegalAccessException theError ) {
216+ postMessage ("Internal error: Unable to call permission method:\n " + theError .getMessage ());
217+ return ;
218+ } catch (java .lang .reflect .InvocationTargetException theError ) {
219+ postMessage ("Internal error: Unable to call permission method:\n " + theError .getMessage ());
220+ return ;
221+ }
222+ }
223+
175224 /**
176225 * Handle new open file event.
177226 */
0 commit comments