ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
android-saf-fs.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ANDROID_SAF_FILESYSTEM_H
23 #define ANDROID_SAF_FILESYSTEM_H
24 
25 #include <jni.h>
26 
27 #include "backends/fs/abstract-fs.h"
28 #include "common/ptr.h"
29 
30 #include "backends/fs/android/android-fs.h"
31 
38 protected:
44  class GlobalRef final : public Common::SharedPtr<_jobject> {
45  struct Deleter {
46  void operator()(_jobject *obj);
47  };
48  public:
50  GlobalRef(const GlobalRef &ref) : Common::SharedPtr<_jobject>(ref) {}
51  GlobalRef(JNIEnv *env, jobject jobj) : Common::SharedPtr<_jobject>(jobj ? env->NewGlobalRef(jobj) : nullptr, Deleter()) {
52  // Make sure NewGlobalRef succeeded
53  assert((jobj == nullptr) == (get() == nullptr));
54  }
55  GlobalRef &operator=(const GlobalRef &r) {
57  return *this;
58  }
59 
60  operator jobject() {
62  }
63  operator jobject() const {
65  }
66  };
67 
74  class NodeRef final {
75  private:
76  jlong _ref;
77 
78  public:
79  NodeRef() : _ref(0) {}
80  ~NodeRef() { reset(); }
81  NodeRef(const NodeRef &r) { reset(r); }
82  NodeRef(JNIEnv *env, jobject node) { reset(env, node); }
83 
84  void reset();
85  void reset(const NodeRef &r);
86  void reset(JNIEnv *env, jobject node);
87 
88  NodeRef &operator=(const NodeRef &r) {
89  reset(r);
90  return *this;
91  }
92 
93  bool operator==(const NodeRef &r) const {
94  return _ref == r._ref;
95  }
96 
97  bool operator!=(const NodeRef &r) const {
98  return _ref != r._ref;
99  }
100 
101  explicit operator bool() const {
102  return _ref != 0;
103  }
104 
105  jlong get() const { return _ref; }
106  jobject localRef(JNIEnv *env) const;
107  };
108 
109  // SAFFSTree
110  static jclass _CLS_SAFFSTree;
111 
112  static jmethodID _MID_addNodeRef;
113  static jmethodID _MID_decNodeRef;
114  static jmethodID _MID_refToNode;
115  static jmethodID _MID_getTreeId;
116  static jmethodID _MID_pathToNode;
117  static jmethodID _MID_getChildren;
118  static jmethodID _MID_getChild;
119  static jmethodID _MID_createDirectory;
120  static jmethodID _MID_createFile;
121  static jmethodID _MID_createReadStream;
122  static jmethodID _MID_createWriteStream;
123  static jmethodID _MID_removeNode;
124  static jmethodID _MID_removeTree;
125 
126  static jfieldID _FID__treeName;
127  static jfieldID _FID__root;
128 
129  // SAFFSNode
130  static jmethodID _MID_addRef;
131 
132  static jfieldID _FID__parent;
133  static jfieldID _FID__path;
134  static jfieldID _FID__documentId;
135  static jfieldID _FID__flags;
136 
137  static bool _JNIinit;
138 
139 protected:
140  static const int DIRECTORY = 1;
141  static const int WRITABLE = 2;
142  static const int READABLE = 4;
143 
144  GlobalRef _safTree;
145  // When 0, node doesn't exist yet
146  // In this case _path is the parent path, _newName the node name and _safParent the parent SAF object
147  NodeRef _safNode;
148 
149  Common::String _path;
150  int _flags;
151  NodeRef _safParent;
152 
153  // Used when creating a new node
154  // Also used for root node to store its pretty name
155  Common::String _newName;
156 
157 public:
158  static const char SAF_MOUNT_POINT[];
159 
164  static void initJNI();
165 
172 
178  static AndroidSAFFilesystemNode *makeFromTree(jobject safTree);
179 
180  bool exists() const override { return (bool)_safNode; }
182  Common::String getName() const override;
183  Common::String getPath() const override;
184  bool isDirectory() const override { return _flags & DIRECTORY; }
185  bool isReadable() const override { return _flags & READABLE; }
186  bool isWritable() const override { return _flags & WRITABLE; }
187 
188  AbstractFSNode *getChild(const Common::String &n) const override;
189  bool getChildren(AbstractFSList &list, ListMode mode, bool hidden) const override;
190  AbstractFSNode *getParent() const override;
191 
193  Common::SeekableWriteStream *createWriteStream(bool atomic) override;
194  bool createDirectory() override;
195 
196  bool remove() override;
197 
202  void removeTree();
203 protected:
210  AndroidSAFFilesystemNode(const GlobalRef &safTree, jobject safNode);
211 
220  AndroidSAFFilesystemNode(const GlobalRef &safTree, jobject safParent,
221  const Common::String &path, const Common::String &name);
222 
231  AndroidSAFFilesystemNode(const GlobalRef &safTree, const NodeRef &safParent,
232  const Common::String &path, const Common::String &name);
233 
234  void cacheData(JNIEnv *env, jobject node);
235 };
236 
237 class AddSAFFakeNode final : public AbstractFSNode, public AndroidFSNode {
238 protected:
239  AbstractFSNode *getChild(const Common::String &name) const override;
240  AbstractFSNode *getParent() const override;
241 
242 public:
243  static const char SAF_ADD_FAKE_PATH[];
244 
245  AddSAFFakeNode(bool fromPath) : _proxied(nullptr), _fromPath(fromPath) { }
246  ~AddSAFFakeNode() override;
247 
248  bool exists() const override;
249 
250  bool getChildren(AbstractFSList &list, ListMode mode, bool hidden) const override;
251 
252  // I18N: This is displayed in the file browser to let the user choose a new folder for Android Storage Attached Framework
253  Common::U32String getDisplayName() const override;
254  Common::String getName() const override;
255  Common::String getPath() const override;
256 
257  bool isDirectory() const override { return true; }
258  bool isReadable() const override;
259  bool isWritable() const override;
260 
261  Common::SeekableReadStream *createReadStream() override { return nullptr; }
262  Common::SeekableWriteStream *createWriteStream(bool atomic) override { return nullptr; }
263 
264  bool createDirectory() override { return false; }
265  bool remove() override { return false; }
266 
267 private:
268  void makeProxySAF() const;
269 
270  bool _fromPath;
271  mutable AbstractFSNode *_proxied;
272 };
273 #endif
Definition: str.h:59
Common::SeekableReadStream * createReadStream() override
bool isWritable() const override
Definition: android-saf-fs.h:186
Definition: array.h:52
bool isReadable() const override
Definition: android-saf-fs.h:185
static AndroidSAFFilesystemNode * makeFromPath(const Common::String &path)
Definition: android-saf-fs.h:44
void reset()
Definition: ptr.h:278
Definition: stream.h:745
Definition: android-saf-fs.h:37
bool isDirectory() const override
Definition: android-saf-fs.h:257
PointerType get() const
Definition: ptr.h:229
Common::SeekableWriteStream * createWriteStream(bool atomic) override
Definition: android-fs.h:29
AndroidSAFFilesystemNode(const GlobalRef &safTree, jobject safNode)
bool createDirectory() override
Definition: ustr.h:57
Definition: android-saf-fs.h:74
AbstractFSNode * getChild(const Common::String &n) const override
Definition: android-saf-fs.h:237
ListMode
Definition: fs.h:86
Common::String getName() const override
Common::String getPath() const override
Common::SeekableWriteStream * createWriteStream(bool atomic) override
Definition: android-saf-fs.h:262
AbstractFSNode * getParent() const override
bool getChildren(AbstractFSList &list, ListMode mode, bool hidden) const override
Definition: stream.h:351
Common::SeekableReadStream * createReadStream() override
Definition: android-saf-fs.h:261
bool createDirectory() override
Definition: android-saf-fs.h:264
Definition: ptr.h:159
Definition: abstract-fs.h:41
static AndroidSAFFilesystemNode * makeFromTree(jobject safTree)
Common::U32String getDisplayName() const override
Definition: android-saf-fs.h:181
bool isDirectory() const override
Definition: android-saf-fs.h:184