openEventsPlatformApp/plugins/cordova-plugin-file/src/android/PendingRequests.java
plugins/cordova-plugin-file/src/android/PendingRequests.javaRaw
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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19package org.apache.cordova.file;
20
21import android.util.SparseArray;
22
23import org.apache.cordova.CallbackContext;
24
25/**
26 * Holds pending runtime permission requests
27 */
28class PendingRequests {
29 private int currentReqId = 0;
30 private SparseArray<Request> requests = new SparseArray<Request>();
31
32 /**
33 * Creates a request and adds it to the array of pending requests. Each created request gets a
34 * unique result code for use with requestPermission()
35 * @param rawArgs The raw arguments passed to the plugin
36 * @param action The action this request corresponds to (get file, etc.)
37 * @param callbackContext The CallbackContext for this plugin call
38 * @return The request code that can be used to retrieve the Request object
39 */
40 public synchronized int createRequest(String rawArgs, int action, CallbackContext callbackContext) {
41 Request req = new Request(rawArgs, action, callbackContext);
42 requests.put(req.requestCode, req);
43 return req.requestCode;
44 }
45
46 /**
47 * Gets the request corresponding to this request code and removes it from the pending requests
48 * @param requestCode The request code for the desired request
49 * @return The request corresponding to the given request code or null if such a
50 * request is not found
51 */
52 public synchronized Request getAndRemove(int requestCode) {
53 Request result = requests.get(requestCode);
54 requests.remove(requestCode);
55 return result;
56 }
57
58 /**
59 * Holds the options and CallbackContext for a call made to the plugin.
60 */
61 public class Request {
62
63 // Unique int used to identify this request in any Android permission callback
64 private int requestCode;
65
66 // Action to be performed after permission request result
67 private int action;
68
69 // Raw arguments passed to plugin
70 private String rawArgs;
71
72 // The callback context for this plugin request
73 private CallbackContext callbackContext;
74
75 private Request(String rawArgs, int action, CallbackContext callbackContext) {
76 this.rawArgs = rawArgs;
77 this.action = action;
78 this.callbackContext = callbackContext;
79 this.requestCode = currentReqId ++;
80 }
81
82 public int getAction() {
83 return this.action;
84 }
85
86 public String getRawArgs() {
87 return rawArgs;
88 }
89
90 public CallbackContext getCallbackContext() {
91 return callbackContext;
92 }
93 }
94}