Sin descripción

PendingRequests.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.file;
  18. import android.util.SparseArray;
  19. import org.apache.cordova.CallbackContext;
  20. /**
  21. * Holds pending runtime permission requests
  22. */
  23. class PendingRequests {
  24. private int currentReqId = 0;
  25. private SparseArray<Request> requests = new SparseArray<Request>();
  26. /**
  27. * Creates a request and adds it to the array of pending requests. Each created request gets a
  28. * unique result code for use with requestPermission()
  29. * @param rawArgs The raw arguments passed to the plugin
  30. * @param action The action this request corresponds to (get file, etc.)
  31. * @param callbackContext The CallbackContext for this plugin call
  32. * @return The request code that can be used to retrieve the Request object
  33. */
  34. public synchronized int createRequest(String rawArgs, int action, CallbackContext callbackContext) {
  35. Request req = new Request(rawArgs, action, callbackContext);
  36. requests.put(req.requestCode, req);
  37. return req.requestCode;
  38. }
  39. /**
  40. * Gets the request corresponding to this request code and removes it from the pending requests
  41. * @param requestCode The request code for the desired request
  42. * @return The request corresponding to the given request code or null if such a
  43. * request is not found
  44. */
  45. public synchronized Request getAndRemove(int requestCode) {
  46. Request result = requests.get(requestCode);
  47. requests.remove(requestCode);
  48. return result;
  49. }
  50. /**
  51. * Holds the options and CallbackContext for a call made to the plugin.
  52. */
  53. public class Request {
  54. // Unique int used to identify this request in any Android permission callback
  55. private int requestCode;
  56. // Action to be performed after permission request result
  57. private int action;
  58. // Raw arguments passed to plugin
  59. private String rawArgs;
  60. // The callback context for this plugin request
  61. private CallbackContext callbackContext;
  62. private Request(String rawArgs, int action, CallbackContext callbackContext) {
  63. this.rawArgs = rawArgs;
  64. this.action = action;
  65. this.callbackContext = callbackContext;
  66. this.requestCode = currentReqId ++;
  67. }
  68. public int getAction() {
  69. return this.action;
  70. }
  71. public String getRawArgs() {
  72. return rawArgs;
  73. }
  74. public CallbackContext getCallbackContext() {
  75. return callbackContext;
  76. }
  77. }
  78. }