OSDN Git Service

578ea32319a0335392adc57b13a24e7e06e764c6
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / model / FileSystemObject.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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.cyanogenmod.filemanager.model;
18
19 import android.content.ContentResolver;
20 import android.net.Uri;
21 import com.cyanogenmod.filemanager.R;
22 import com.cyanogenmod.filemanager.util.FileHelper;
23
24 import java.io.File;
25 import java.io.Serializable;
26 import java.util.Date;
27
28 /**
29  * A class that represents an abstract file system object.
30  *
31  * @see RegularFile
32  * @see Directory
33  * @see Symlink
34  * @see SystemFile
35  */
36 public abstract class FileSystemObject implements Serializable, Comparable<FileSystemObject> {
37
38     private static final long serialVersionUID = -571144166609728391L;
39
40     //Resource identifier for default icon
41     private static final int RESOURCE_ICON_DEFAULT = R.drawable.ic_fso_default;
42
43     private int mResourceIconId;
44     private String mName;
45     private String mParent;
46     private User mUser;
47     private Group mGroup;
48     private Permissions mPermissions;
49     private long mSize;
50     private Date mLastAccessedTime;
51     private Date mLastModifiedTime;
52     private Date mLastChangedTime;
53     private boolean mIsSecure;
54     private boolean mIsRemote;
55
56     /**
57      * Constructor of <code>FileSystemObject</code>.
58      *
59      * @param name The name of the object
60      * @param parent The parent folder of the object
61      * @param user The user proprietary of the object
62      * @param group The group proprietary of the object
63      * @param permissions The permissions of the object
64      * @param size The size in bytes of the object
65      * @param lastAccessedTime The last time that the object was accessed
66      * @param lastModifiedTime The last time that the object was modified
67      * @param lastChangedTime The last time that the object was changed
68      */
69     public FileSystemObject(String name, String parent, User user, Group group,
70             Permissions permissions, long size,
71             Date lastAccessedTime, Date lastModifiedTime, Date lastChangedTime) {
72         super();
73         this.mName = name;
74         this.mParent = parent;
75         this.mUser = user;
76         this.mGroup = group;
77         this.mPermissions = permissions;
78         this.mSize = size;
79         this.mLastAccessedTime = lastAccessedTime;
80         this.mLastModifiedTime = lastModifiedTime;
81         this.mLastChangedTime = lastChangedTime;
82         this.mResourceIconId = RESOURCE_ICON_DEFAULT;
83         this.mIsSecure = false;
84         this.mIsRemote = false;
85     }
86
87     /**
88      * Method that returns the character that identifies the object in unix.
89      *
90      * @return char The character that identifies the object in unix
91      */
92     public abstract char getUnixIdentifier();
93
94     /**
95      * Method that returns the name of the object.
96      *
97      * @return String The name of the object
98      */
99     public String getName() {
100         return this.mName;
101     }
102
103     /**
104      * Method that sets the name of the object.
105      *
106      * @param name The name to set
107      */
108     public void setName(String name) {
109         this.mName = name;
110     }
111
112     /**
113      * Method that returns the parent folder of the object.
114      *
115      * @return String The parent folder of the object
116      */
117     public String getParent() {
118         return this.mParent;
119     }
120
121     /**
122      * Method that sets the parent folder of the object.
123      *
124      * @param parent The parent folder of the object
125      */
126     public void setParent(String parent) {
127         this.mParent = parent;
128     }
129
130     /**
131      * Method that returns the user proprietary of the object.
132      *
133      * @return User The user proprietary of the object
134      */
135     public User getUser() {
136         return this.mUser;
137     }
138
139     /**
140      * Method that sets the user proprietary of the object.
141      *
142      * @param user The user proprietary of the object
143      */
144     public void setUser(User user) {
145         this.mUser = user;
146     }
147
148     /**
149      * Method that returns the group proprietary of the object.
150      *
151      * @return Group The group proprietary of the object
152      */
153     public Group getGroup() {
154         return this.mGroup;
155     }
156
157     /**
158      * Method that sets the group proprietary of the object.
159      *
160      * @param group The group proprietary of the object
161      */
162     public void setGroup(Group group) {
163         this.mGroup = group;
164     }
165
166     /**
167      * Method that returns the permissions of the object.
168      *
169      * @return Permissions The permissions of the object
170      */
171     public Permissions getPermissions() {
172         return this.mPermissions;
173     }
174
175     /**
176      * Method that sets the permissions of the object.
177      *
178      * @param permissions The permissions of the object
179      */
180     public void setPermissions(Permissions permissions) {
181         this.mPermissions = permissions;
182     }
183
184     /**
185      * Method that returns the size in bytes of the object.
186      *
187      * @return long The size in bytes of the object
188      */
189     public long getSize() {
190         return this.mSize;
191     }
192
193     /**
194      * Method that sets the size in bytes of the object.
195      *
196      * @param size The size in bytes of the object
197      */
198     public void setSize(long size) {
199         this.mSize = size;
200     }
201
202     /**
203      * Method that returns the last time that the object was accessed.
204      *
205      * @return Date The last time that the object was accessed
206      */
207     public Date getLastAccessedTime() {
208         return this.mLastAccessedTime;
209     }
210
211     /**
212      * Method that sets the last time that the object was accessed.
213      *
214      * @param lastAccessedTime The last time that the object was accessed
215      */
216     public void setLastAccessedTime(Date lastAccessedTime) {
217         this.mLastAccessedTime = lastAccessedTime;
218     }
219
220     /**
221      * Method that returns the last time that the object was modified.
222      *
223      * @return Date The last time that the object was modified
224      */
225     public Date getLastModifiedTime() {
226         return this.mLastModifiedTime;
227     }
228
229     /**
230      * Method that sets the last time that the object was modified.
231      *
232      * @param lastModifiedTime The last time that the object was modified
233      */
234     public void setLastModifiedTime(Date lastModifiedTime) {
235         this.mLastModifiedTime = lastModifiedTime;
236     }
237
238     /**
239      * Method that returns the last time that the object was changed.
240      *
241      * @return Date The last time that the object was changed
242      */
243     public Date getLastChangedTime() {
244         return this.mLastChangedTime;
245     }
246
247     /**
248      * Method that sets the last time that the object was changed.
249      *
250      * @param lastChangedTime The last time that the object was changed
251      */
252     public void setLastChangedTime(Date lastChangedTime) {
253         this.mLastChangedTime = lastChangedTime;
254     }
255
256     /**
257      * Method that returns if the object is hidden object.
258      *
259      * @return boolean If the object is hidden object
260      */
261     public boolean isHidden() {
262         return this.mName.startsWith("."); //$NON-NLS-1$
263     }
264
265     /**
266      * Method that returns if the object is secure
267      *
268      * @return boolean If the object is secure
269      */
270     public boolean isSecure() {
271         return mIsSecure;
272     }
273
274     /**
275      * Mehtod that sets if the object is secure
276      *
277      * @param secure if the object is secure
278      */
279     public void setSecure(boolean secure) {
280         mIsSecure = secure;
281     }
282
283     /**
284      * Method that returns if the object is remote
285      *
286      * @return boolean If the object is remote
287      */
288     public boolean isRemote() {
289         return mIsRemote;
290     }
291
292     /**
293      * Mehtod that sets if the object is remote
294      *
295      * @param remote if the object is remote
296      */
297     public void setRemote(boolean remote) {
298         mIsRemote = remote;
299     }
300
301     /**
302      * Method that returns the identifier of the drawable icon associated
303      * to the object.
304      *
305      * @return int The identifier of the drawable icon
306      * @hide
307      */
308     public int getResourceIconId() {
309         return this.mResourceIconId;
310     }
311
312     /**
313      * Method that sets the identifier of the drawable icon associated
314      * to the object.
315      *
316      * @param resourceIconId The identifier of the drawable icon
317      * @hide
318      */
319     protected void setResourceIconId(int resourceIconId) {
320         this.mResourceIconId = resourceIconId;
321     }
322
323     /**
324      * Method that returns the full path of the file system object.
325      *
326      * @return String The full path of the file system object
327      */
328     public String getFullPath() {
329         if (FileHelper.isRootDirectory(this)) {
330             return FileHelper.ROOT_DIRECTORY;
331         } else if (FileHelper.isParentRootDirectory(this)) {
332             if (this.mParent == null) {
333                 return FileHelper.ROOT_DIRECTORY + this.mName;
334             }
335             return this.mParent + this.mName;
336         }
337         return this.mParent + File.separator + this.mName;
338     }
339
340     /**
341      * creates a file uri that references this FileSystemObject
342      * @return a file uri
343      */
344     public Uri getFileUri() {
345         Uri uri = new Uri.Builder()
346                 .scheme(ContentResolver.SCHEME_FILE)
347                 .path(getFullPath())
348                 .build();
349
350         return uri;
351     }
352
353     /**
354      * {@inheritDoc}
355      */
356     @Override
357     public int compareTo(FileSystemObject another) {
358         String o1 = this.getFullPath();
359         String o2 = another.getFullPath();
360         return o1.compareTo(o2);
361     }
362
363     /**
364      * {@inheritDoc}
365      */
366     @Override
367     public int hashCode() {
368         final int prime = 31;
369         int result = 1;
370         result = prime * result + ((this.mName == null) ? 0 : this.mName.hashCode());
371         result = prime * result + ((this.mParent == null) ? 0 : this.mParent.hashCode());
372         return result;
373     }
374
375     /**
376      * {@inheritDoc}
377      */
378     @Override
379     public boolean equals(Object obj) {
380         if (this == obj)
381             return true;
382         if (obj == null)
383             return false;
384         FileSystemObject other = (FileSystemObject) obj;
385         if (this.mName == null) {
386             if (other.mName != null)
387                 return false;
388         } else if (!this.mName.equals(other.mName))
389             return false;
390         if (this.mParent == null) {
391             if (other.mParent != null)
392                 return false;
393         } else if (!this.mParent.equals(other.mParent))
394             return false;
395         return true;
396     }
397
398     /**
399      * Method that returns the unix string representation of the type and permissions of the
400      * file system object.
401      *
402      * @return String The string representation
403      */
404     public String toRawPermissionString() {
405         return Character.toString(getUnixIdentifier())
406                 + getPermissions().toRawString();
407     }
408
409     /**
410      * {@inheritDoc}
411      */
412     @Override
413     public String toString() {
414         return "FileSystemObject [mResourceIconId=" + this.mResourceIconId //$NON-NLS-1$
415                 + ", mName=" + this.mName + ", mParent=" + this.mParent //$NON-NLS-1$ //$NON-NLS-2$
416                 + ", mUser=" + this.mUser + ", mGroup=" + this.mGroup //$NON-NLS-1$ //$NON-NLS-2$
417                 + ", mPermissions=" + this.mPermissions //$NON-NLS-1$
418                 + ", mSize=" + this.mSize //$NON-NLS-1$
419                 + ", mLastAccessedTime=" + this.mLastAccessedTime //$NON-NLS-1$
420                 + ", mLastModifiedTime=" + this.mLastModifiedTime //$NON-NLS-1$
421                 + ", mLastChangedTime=" + this.mLastChangedTime //$NON-NLS-1$
422                 + ", mIsSecure=" + mIsSecure //$NON-NLS-1$
423                 + ", mIsRemote=" + mIsRemote //$NON-NLS-1$
424                 + "]"; //$NON-NLS-1$
425     }
426
427 }