@@ -149,32 +149,43 @@ public String[] getPermissions() {
149
149
150
150
151
151
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...
153
157
for (XML kid : xml .getChildren ("uses-permission" )) {
154
158
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 ;
162
172
}
173
+
163
174
// Don't remove non-standard permissions, such as
164
175
// com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA
176
+ // because these are set manually by the user.
165
177
if (-1 < name .indexOf ("com.google.android" )) continue ;
166
178
xml .removeChild (kid );
167
179
}
168
- // ...and add the new kids back
180
+
181
+ // ...and add the new permissions back
169
182
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
+
178
189
XML newbie = xml .addChild ("uses-permission" );
179
190
if (-1 < name .indexOf ("." )) {
180
191
// Permission string contains path
@@ -183,9 +194,58 @@ public void setPermissions(String[] names) {
183
194
newbie .setString ("android:name" , PERMISSION_PREFIX + name );
184
195
}
185
196
}
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
+
186
212
save ();
187
213
}
188
214
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
+
189
249
190
250
private void writeBlankManifest (final File xmlFile , final int appComp ) {
191
251
File xmlTemplate = new File (modeFolder , "templates/" + MANIFEST_TEMPLATE [appComp ]);
@@ -231,13 +291,20 @@ protected void writeCopy(File file, String className) throws IOException {
231
291
app .setString ("android:label" , className );
232
292
}
233
293
294
+ // Services need the label also in the service section
234
295
if (appComp == AndroidBuild .WALLPAPER || appComp == AndroidBuild .WATCHFACE ) {
235
296
XML serv = app .getChild ("service" );
236
297
label = serv .getString ("android:label" );
237
298
if (label .length () == 0 ) {
238
299
serv .setString ("android:label" , className );
239
300
}
240
301
}
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
+ }
241
308
242
309
PrintWriter writer = PApplet .createWriter (file );
243
310
writer .print (mf .format (4 ));
0 commit comments