1package net.paperclover.snowbound.providerlab;
2
3import android.content.BroadcastReceiver;
4import android.content.Context;
5import android.content.Intent;
6import android.net.Uri;
7import android.os.Bundle;
8import android.provider.DocumentsContract;
9import java.nio.file.Files;
10
11public final class Control extends BroadcastReceiver {
12 @Override public void onReceive(Context context, Intent intent) {
13 Bundle result = new Bundle();
14 try {
15 // Force provider initialization before touching its disposable backing files.
16 try (android.content.ContentProviderClient ignored = context.getContentResolver()
17 .acquireContentProviderClient(LabProvider.AUTHORITY)) {
18 Uri tree = DocumentsContract.buildTreeDocumentUri(LabProvider.AUTHORITY, LabProvider.ROOT);
19 String action = intent.getStringExtra("operation");
20 if (action.equals("reset")) LabProvider.reset();
21 else if (action.equals("revoke")) {
22 context.revokeUriPermission(tree, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
23 } else if (action.equals("revoke-notebook")) {
24 context.revokeUriPermission(DocumentsContract.buildTreeDocumentUri(LabProvider.AUTHORITY, LabProvider.NOTEBOOK),
25 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
26 } else if (action.equals("offline")) LabProvider.offline = true;
27 else if (action.equals("online")) LabProvider.offline = false;
28 else if (action.equals("publish")) Files.write(LabProvider.file("observer").toPath(), Files.readAllBytes(LabProvider.file("writer").toPath()));
29 else if (action.equals("await-pipe")) LabProvider.awaitPipe();
30 else throw new IllegalArgumentException(action);
31 }
32 result.putBoolean("ok", true);
33 result.putInt("uid", android.os.Process.myUid());
34 result.putInt("pid", android.os.Process.myPid());
35 } catch (Exception e) {
36 result.putString("error", e.toString());
37 }
38 setResultExtras(result);
39 }
40}