Home | History | Annotate | Download | only in device
      1 /*
      2  * Copyright (C) 2010 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 package com.android.tradefed.device;
     17 
     18 import com.android.ddmlib.FileListingService;
     19 import com.android.ddmlib.FileListingService.FileEntry;
     20 
     21 import java.util.Collection;
     22 import java.util.HashMap;
     23 import java.util.List;
     24 import java.util.Map;
     25 
     26 /**
     27  * Implementation of a {@link IFileEntry}.
     28  */
     29 class FileEntryWrapper implements IFileEntry {
     30 
     31     private final NativeDevice mTestDevice;
     32     private final FileListingService.FileEntry mFileEntry;
     33     private Map<String, IFileEntry> mChildMap = null;
     34 
     35     /**
     36      * Creates a {@link FileEntryWrapper}.
     37      *
     38      * @param testDevice the {@link TestDevice} to use
     39      * @param entry the corresponding {@link FileEntry} to wrap
     40      */
     41     FileEntryWrapper(NativeDevice testDevice, FileEntry entry) {
     42         mTestDevice = testDevice;
     43         mFileEntry = entry;
     44     }
     45 
     46     /**
     47      * {@inheritDoc}
     48      */
     49     @Override
     50     public String getFullEscapedPath() {
     51         return mFileEntry.getFullEscapedPath();
     52     }
     53 
     54     /**
     55      * {@inheritDoc}
     56      */
     57     @Override
     58     public String getFullPath() {
     59         return mFileEntry.getFullPath();
     60     }
     61 
     62     /**
     63      * {@inheritDoc}
     64      */
     65     @Override
     66     public IFileEntry findChild(String name) throws DeviceNotAvailableException {
     67         if (mChildMap == null || !mChildMap.containsKey(name)) {
     68             mChildMap = buildChildrenMap();
     69         }
     70         return mChildMap.get(name);
     71     }
     72 
     73     /**
     74      * {@inheritDoc}
     75      */
     76     @Override
     77     public boolean isDirectory() {
     78         return mFileEntry.isDirectory();
     79     }
     80 
     81     /**
     82      * {@inheritDoc}
     83      */
     84     @Override
     85     public boolean isAppFileName() {
     86         return mFileEntry.isAppFileName();
     87     }
     88 
     89     /**
     90      * {@inheritDoc}
     91      */
     92     @Override
     93     public String getName() {
     94         return mFileEntry.getName();
     95     }
     96 
     97     /**
     98      * {@inheritDoc}
     99      */
    100     @Override
    101     public Collection<IFileEntry> getChildren(boolean useCache) throws DeviceNotAvailableException {
    102         if (!useCache || mChildMap == null) {
    103             mChildMap = buildChildrenMap();
    104         }
    105         return mChildMap.values();
    106     }
    107 
    108     private Map<String, IFileEntry> buildChildrenMap() throws DeviceNotAvailableException {
    109         FileEntry[] childEntrys = mTestDevice.getFileChildren(mFileEntry);
    110         Map<String, IFileEntry> childMap = new HashMap<String, IFileEntry>(childEntrys.length);
    111         for (FileEntry entry : childEntrys) {
    112             childMap.put(entry.getName(), new FileEntryWrapper(mTestDevice, entry));
    113         }
    114         return childMap;
    115     }
    116 
    117     /**
    118      * Helper method that tries to a find the descendant of a {@link IFileEntry}
    119      *
    120      * @param fileEntry the starting point
    121      * @param childSegments the relative path of the {@link IFileEntry} to find
    122      * @return the {@link IFileEntry}, or <code>null</code> if it cannot be found
    123      * @throws DeviceNotAvailableException
    124      */
    125     static IFileEntry getDescendant(IFileEntry fileEntry, List<String> childSegments)
    126             throws DeviceNotAvailableException {
    127         IFileEntry child = fileEntry;
    128         for (String childName: childSegments) {
    129             if (childName.length() > 0) {
    130                 child = child.findChild(childName);
    131                 if (child == null) {
    132                     return null;
    133                 }
    134             }
    135         }
    136         return child;
    137     }
    138 
    139     /**
    140      * {@inheritDoc}
    141      */
    142     @Override
    143     public FileEntry getFileEntry() {
    144         return mFileEntry;
    145     }
    146 
    147     /**
    148      * {@inheritDoc}
    149      */
    150     @Override
    151     public String getTime() {
    152         return mFileEntry.getTime();
    153     }
    154 
    155     /**
    156      * {@inheritDoc}
    157      */
    158     @Override
    159     public String getDate() {
    160         return mFileEntry.getDate();
    161     }
    162 
    163     /**
    164      * {@inheritDoc}
    165      */
    166     @Override
    167     public String getPermissions() {
    168         return mFileEntry.getPermissions();
    169     }
    170 }
    171