@@ -149,32 +149,43 @@ public String[] getPermissions() {
149149
150150
151151 public void setPermissions (String [] names ) {
152- // just remove all the old ones
152+ boolean hasWakeLock = false ;
153+ boolean hasVibrate = false ;
154+ boolean hasReadExtStorage = false ;
155+
156+ // Remove all the old permissions...
153157 for (XML kid : xml .getChildren ("uses-permission" )) {
154158 String name = kid .getString ("android:name" );
155- // Don't remove required permissions for wallpapers, watchfaces and VR.
156- if (appComp == AndroidBuild .WALLPAPER ) {
157- } else if (appComp == AndroidBuild .WATCHFACE ) {
158- if (name .equals ("WAKE_LOCK" )) continue ;
159- } else if (appComp == AndroidBuild .VR ) {
160- if (name .equals ("VIBRATE" ) ||
161- name .equals ("READ_EXTERNAL_STORAGE" )) continue ;
159+
160+ // ...except the ones for watch faces and VR apps.
161+ if (appComp == AndroidBuild .WATCHFACE && name .equals ("WAKE_LOCK" )) {
162+ hasWakeLock = true ;
163+ continue ;
164+ }
165+ if (appComp == AndroidBuild .VR && name .equals ("VIBRATE" )) {
166+ hasVibrate = true ;
167+ continue ;
168+ }
169+ if (appComp == AndroidBuild .VR && name .equals ("READ_EXTERNAL_STORAGE" )) {
170+ hasReadExtStorage = true ;
171+ continue ;
162172 }
173+
163174 // Don't remove non-standard permissions, such as
164175 // com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA
176+ // because these are set manually by the user.
165177 if (-1 < name .indexOf ("com.google.android" )) continue ;
166178 xml .removeChild (kid );
167179 }
168- // ...and add the new kids back
180+
181+ // ...and add the new permissions back
169182 for (String name : names ) {
170- // Don't add required permissions for wallpapers, watchfaces and VR again.
171- if (appComp == AndroidBuild .WALLPAPER ) {
172- } else if (appComp == AndroidBuild .WATCHFACE ) {
173- if (name .equals ("WAKE_LOCK" )) continue ;
174- } else if (appComp == AndroidBuild .VR ) {
175- if (name .equals ("VIBRATE" ) ||
176- name .equals ("READ_EXTERNAL_STORAGE" )) continue ;
177- }
183+
184+ // Don't add required permissions for watch faces and VR again...
185+ if (appComp == AndroidBuild .WATCHFACE && name .equals ("WAKE_LOCK" )) continue ;
186+ if (appComp == AndroidBuild .VR && name .equals ("VIBRATE" )) continue ;
187+ if (appComp == AndroidBuild .VR && name .equals ("READ_EXTERNAL_STORAGE" )) continue ;
188+
178189 XML newbie = xml .addChild ("uses-permission" );
179190 if (-1 < name .indexOf ("." )) {
180191 // Permission string contains path
@@ -183,9 +194,58 @@ public void setPermissions(String[] names) {
183194 newbie .setString ("android:name" , PERMISSION_PREFIX + name );
184195 }
185196 }
197+
198+ // ...unless they were initially missing.
199+ if (appComp == AndroidBuild .WATCHFACE && !hasWakeLock ) {
200+ xml .addChild ("uses-permission" ).
201+ setString ("android:name" , PERMISSION_PREFIX + "WAKE_LOCK" );
202+ }
203+ if (appComp == AndroidBuild .VR && !hasVibrate ) {
204+ xml .addChild ("uses-permission" ).
205+ setString ("android:name" , PERMISSION_PREFIX + "VIBRATE" );
206+ }
207+ if (appComp == AndroidBuild .VR && !hasReadExtStorage ) {
208+ xml .addChild ("uses-permission" ).
209+ setString ("android:name" , PERMISSION_PREFIX + "READ_EXTERNAL_STORAGE" );
210+ }
211+
186212 save ();
187213 }
188214
215+
216+ private void fixPermissions (XML mf ) {
217+ boolean hasWakeLock = false ;
218+ boolean hasVibrate = false ;
219+ boolean hasReadExtStorage = false ;
220+ for (XML kid : mf .getChildren ("uses-permission" )) {
221+ String name = kid .getString ("android:name" );
222+ if (appComp == AndroidBuild .WATCHFACE && name .equals ("WAKE_LOCK" )) {
223+ hasWakeLock = true ;
224+ continue ;
225+ }
226+ if (appComp == AndroidBuild .VR && name .equals ("VIBRATE" )) {
227+ hasVibrate = true ;
228+ continue ;
229+ }
230+ if (appComp == AndroidBuild .VR && name .equals ("READ_EXTERNAL_STORAGE" )) {
231+ hasReadExtStorage = true ;
232+ continue ;
233+ }
234+ }
235+ if (appComp == AndroidBuild .WATCHFACE && !hasWakeLock ) {
236+ mf .addChild ("uses-permission" ).
237+ setString ("android:name" , PERMISSION_PREFIX + "WAKE_LOCK" );
238+ }
239+ if (appComp == AndroidBuild .VR && !hasVibrate ) {
240+ mf .addChild ("uses-permission" ).
241+ setString ("android:name" , PERMISSION_PREFIX + "VIBRATE" );
242+ }
243+ if (appComp == AndroidBuild .VR && !hasReadExtStorage ) {
244+ mf .addChild ("uses-permission" ).
245+ setString ("android:name" , PERMISSION_PREFIX + "READ_EXTERNAL_STORAGE" );
246+ }
247+ }
248+
189249
190250 private void writeBlankManifest (final File xmlFile , final int appComp ) {
191251 File xmlTemplate = new File (modeFolder , "templates/" + MANIFEST_TEMPLATE [appComp ]);
@@ -231,13 +291,20 @@ protected void writeCopy(File file, String className) throws IOException {
231291 app .setString ("android:label" , className );
232292 }
233293
294+ // Services need the label also in the service section
234295 if (appComp == AndroidBuild .WALLPAPER || appComp == AndroidBuild .WATCHFACE ) {
235296 XML serv = app .getChild ("service" );
236297 label = serv .getString ("android:label" );
237298 if (label .length () == 0 ) {
238299 serv .setString ("android:label" , className );
239300 }
240301 }
302+
303+ // Make sure that the required permissions for watch faces and VR apps are
304+ // included.
305+ if (appComp == AndroidBuild .WATCHFACE || appComp == AndroidBuild .VR ) {
306+ fixPermissions (mf );
307+ }
241308
242309 PrintWriter writer = PApplet .createWriter (file );
243310 writer .print (mf .format (4 ));
0 commit comments