| 1 | package net.paperclover.snowbound.providerlab; |
| 2 | |
| 3 | import android.content.BroadcastReceiver; |
| 4 | import android.content.Context; |
| 5 | import android.content.Intent; |
| 6 | import android.net.Uri; |
| 7 | import android.os.Bundle; |
| 8 | import android.provider.DocumentsContract; |
| 9 | import java.nio.file.Files; |
| 10 | |
| 11 | public 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 | } |