summaryrefslogtreecommitdiff
path: root/src/plugins/db.devices/src/com/motorolamobility/studio/android/db/devices/utils/DeviceDbUtils.java
blob: 124be0aabe6298c6e01bb488810a99f1e2cf4b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.motorolamobility.studio.android.db.devices.utils;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;

import com.motorola.studio.android.adt.DDMSFacade;
import com.motorola.studio.android.adt.DDMSUtils;

public class DeviceDbUtils
{
    /**
     * 
     */
    private static final String DB_EXTENSION = "db"; //$NON-NLS-1$
    private static final String APPNAME_KEY = "#APP_NAME"; //$NON-NLS-1$
    private static final String remoteDbPathMask = "/data/data/" + APPNAME_KEY + "/databases/"; //$NON-NLS-1$ //$NON-NLS-2$
    
    /**
     *  Retrieves the databases path for a given application on the android file system
     */
    public static IPath getRemoteDbFolder(String appName)
    {
        String remoteDbFolder = remoteDbPathMask.replace(APPNAME_KEY, appName);
        return new Path(remoteDbFolder);
    }
    
    /**
     * Retrieves the database file path for a given application's db file on the android file system.
     * @param appName the application name
     * @param dbFileName the db file name
     * @return the full db file path on a android file system
     */
    public static IPath getRemoteDbPath(String appName, String dbFileName)
    {
        IPath remoteDbFolder = getRemoteDbFolder(appName);
        IPath remoteDbPath = remoteDbFolder.append(dbFileName);
        String fileExtension = remoteDbPath.getFileExtension();
        if(fileExtension == null || !fileExtension.equals(DB_EXTENSION))
        {
            remoteDbPath = remoteDbPath.addFileExtension(DB_EXTENSION);   
        }
        return remoteDbPath;
    }

    /**
     * List the installed packages in the device with the serial number Each
     * package entry carries their package location
     * 
     * @param serialNumber
     * @return an Object array that contains:
     *         Item 0: a map<String, String> with the app package as a key and the app path as value.
     *         Item 1: number of applications not included on map due to filterDbApplications being true
     * @throws IOException 
     */
    public static Object[] listInstalledPackages(String serialNumber, boolean filterDBbApplications) throws IOException
    {
        Object[] returnArray = new Object[2];
        
        Map<String, String> allPackages = DDMSUtils.listInstalledPackages(serialNumber);
        
        if(filterDBbApplications)
        {
            Map<String, String> filteredPackages = new LinkedHashMap<String, String>();

            Collection<String> appDataDirs =
                    DDMSFacade.execRemoteApp(serialNumber,
                            "ls /data/data/", new NullProgressMonitor()); //$NON-NLS-1$

            for(String appPackage : appDataDirs)
            {
                IPath remoteDbFolder = getRemoteDbFolder(appPackage);
                Collection<String> databases =
                        DDMSFacade.execRemoteApp(serialNumber,
                                "ls " + remoteDbFolder.toString(), new NullProgressMonitor()); //$NON-NLS-1$

                for (String commandOutline : databases)
                {
                    String[] strings = commandOutline.split(" "); //$NON-NLS-1$
                    for (String string : strings)
                    {
                        if (string.trim().endsWith("." + DB_EXTENSION)) //$NON-NLS-1$
                        {
                            filteredPackages.put(appPackage, allPackages.get(appPackage));
                        }
                    }
                }
            }
            returnArray[0] = filteredPackages;
            returnArray[1] = allPackages.size() - filteredPackages.size();
        }
        else
        {
            returnArray[0] = allPackages;
            returnArray[1] = 0;
        }
        
        
        return returnArray;
    }

}