• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.deviceaswebcam.utils;
18 
19 import android.content.Context;
20 import android.util.JsonReader;
21 import android.util.Log;
22 
23 import com.android.DeviceAsWebcam.R;
24 
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Class to parse and store the V4L2 nodes that must be ignored when looking for V4L2 node mounted
33  * by UVC Gadget Driver. These can be configured using RROs. More information on how to use RROs at
34  * {@code impl/res/raw/README.md}
35  */
36 public class IgnoredV4L2Nodes {
37     private static final String TAG = IgnoredV4L2Nodes.class.getSimpleName();
38     private static final String V4L2_NODE_PREFIX = "/dev/video";
39 
40     private static String[] sIgnoredNodes = null;
41 
42     /**
43      * Returns the list of V4L2 nodes that must be ignored when looking for UVC Gadget Driver's V4L2
44      * node.
45      *
46      * @param context context to access service resources with.
47      * @return Array of nodes to ignore. Ex. ["/dev/video33", "/dev/video44", ...]
48      */
getIgnoredNodes(Context context)49     public static synchronized String[] getIgnoredNodes(Context context) {
50         if (sIgnoredNodes != null) {
51             return sIgnoredNodes;
52         }
53 
54         List<String> ignoredNodes = new ArrayList<>();
55         try (InputStream in = context.getResources().openRawResource(R.raw.ignored_v4l2_nodes);
56                 JsonReader jsonReader = new JsonReader(new InputStreamReader(in))) {
57             jsonReader.beginArray();
58             while (jsonReader.hasNext()) {
59                 String node = jsonReader.nextString();
60                 // Don't track nodes that won't be in our search anyway.
61                 if (node.startsWith(V4L2_NODE_PREFIX)) {
62                     ignoredNodes.add(node);
63                 }
64             }
65             jsonReader.endArray();
66         } catch (IOException e) {
67             Log.e(TAG, "Failed to parse JSON. Running with a partial ignored list", e);
68         }
69 
70         sIgnoredNodes = ignoredNodes.toArray(new String[0]);
71         return sIgnoredNodes;
72     }
73 }
74