summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.checkers/src/com/motorolamobility/preflighting/checkers/deviceCompatibility/SmallScreensSupportCondition.java
blob: 2d1619ffffcdca4dd20300f8753cb67052b1808d (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* 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.preflighting.checkers.deviceCompatibility;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.motorolamobility.preflighting.checkers.CheckerPlugin;
import com.motorolamobility.preflighting.checkers.i18n.CheckerNLS;
import com.motorolamobility.preflighting.core.applicationdata.ApplicationData;
import com.motorolamobility.preflighting.core.applicationdata.XMLElement;
import com.motorolamobility.preflighting.core.checker.condition.CanExecuteConditionStatus;
import com.motorolamobility.preflighting.core.checker.condition.Condition;
import com.motorolamobility.preflighting.core.checker.condition.ICondition;
import com.motorolamobility.preflighting.core.devicelayoutspecification.ParametersType;
import com.motorolamobility.preflighting.core.devicespecification.DeviceSpecification;
import com.motorolamobility.preflighting.core.exception.PreflightingCheckerException;
import com.motorolamobility.preflighting.core.internal.cond.utils.ConditionUtils;
import com.motorolamobility.preflighting.core.utils.ManifestConstants;
import com.motorolamobility.preflighting.core.utils.XmlUtils;
import com.motorolamobility.preflighting.core.validation.ValidationManagerConfiguration;
import com.motorolamobility.preflighting.core.validation.ValidationResult;
import com.motorolamobility.preflighting.core.validation.ValidationResultData;

/**
 * Check if device has small screen but AndroidManifest set smallScreens=false.
 */
public class SmallScreensSupportCondition extends Condition implements ICondition
{

    /*
     * AndroidManifest.xml constants
     */

    /**
     * Elements to validate
     */
    private XMLElement manifestElement;

    private final String SMALL_SCREEN = "small"; //$NON-NLS-1$

    //small screen are supported since API level 4
    private final int SMALL_SCREEN_THRESHOLD = 4;

    @Override
    public CanExecuteConditionStatus canExecute(ApplicationData data,
            List<DeviceSpecification> deviceSpecs) throws PreflightingCheckerException
    {
        CanExecuteConditionStatus status =
                new CanExecuteConditionStatus(IStatus.OK, CheckerPlugin.PLUGIN_ID, "");

        manifestElement = data.getManifestElement();
        if (manifestElement == null)
        {
            status =
                    new CanExecuteConditionStatus(IStatus.ERROR, CheckerPlugin.PLUGIN_ID,
                            CheckerNLS.Invalid_ManifestFile);
        }
        else
        {
            Document manifestDoc = manifestElement.getDocument();

            if (manifestDoc == null)
            {
                status =
                        new CanExecuteConditionStatus(IStatus.ERROR, CheckerPlugin.PLUGIN_ID,
                                CheckerNLS.Invalid_ManifestFile);
            }
        }

        status.setConditionId(getId());
        return status;
    }

    @Override
    public void execute(ApplicationData data, List<DeviceSpecification> deviceSpecs,
            ValidationManagerConfiguration valManagerConfig, ValidationResult results)
            throws PreflightingCheckerException
    {
        checkSmallScreenCondition(deviceSpecs, valManagerConfig, results);
    }

    private void checkSmallScreenCondition(List<DeviceSpecification> deviceSpecs,
            ValidationManagerConfiguration valManagerConfig, ValidationResult results)
            throws PreflightingCheckerException
    {
        boolean userDeclaredUnsupported = false;
        boolean supportSmallScreen = true;
        Document manifestDoc = manifestElement.getDocument();
        Node usesSdkNode = null;

        // get supports-screen node
        NodeList supportsScreenLst =
                manifestDoc.getElementsByTagName(ManifestConstants.SUPPORTS_SCREEN_TAG);
        if (supportsScreenLst.getLength() > 0)
        {
            Node supportsScreenNode = supportsScreenLst.item(0); //Get the first occurrence.
            NamedNodeMap map = supportsScreenNode.getAttributes();
            //small-screen attribute
            Node smallScreenNode = map.getNamedItem(ManifestConstants.SMALL_SCREENS_ATTRIBUTE);
            if (smallScreenNode != null)
            {
                String smallScreenNodeValue = smallScreenNode.getNodeValue();
                try
                {
                    userDeclaredUnsupported = !Boolean.parseBoolean(smallScreenNodeValue);
                }
                catch (Exception e)
                {
                    //Do Nothing, the value will be assumed to be false.
                }
            }
        }

        if (!userDeclaredUnsupported)
        {
            NodeList usesSdkList = manifestDoc.getElementsByTagName(ManifestConstants.USES_SDK_TAG);
            if (usesSdkList.getLength() > 0)
            {
                usesSdkNode = usesSdkList.item(0);
                supportSmallScreen = checkIfMinSdkTooLowFilteringSmallDevices(usesSdkNode);
            }
        }

        //small screens are not supported by user application
        if (!supportSmallScreen)
        {
            ArrayList<String> smallDevicesList = new ArrayList<String>();
            //check if is there any device with small screen on the list
            for (DeviceSpecification currentSpec : deviceSpecs)
            {
                ParametersType params = currentSpec.getDeviceInfo().getDefault();
                String deviceScreenSize = params.getScreenSize();
                if (deviceScreenSize.equals(SMALL_SCREEN))
                {
                    smallDevicesList.add(currentSpec.getDeviceInfo().getName());
                }
            }

            int currentIssuedLine = manifestElement.getNodeLineNumber(usesSdkNode);
            String preview = XmlUtils.getXMLNodeAsString(usesSdkNode, false);
            //list of small screen devices
            for (String currentDevice : smallDevicesList)
            {
                ValidationResultData resultData = new ValidationResultData();
                resultData.setSeverity(getSeverityLevel());
                resultData.setConditionID(getId());
                resultData
                        .setIssueDescription(CheckerNLS
                                .bind(CheckerNLS.DeviceCompatibilityChecker_SMALL_SCREEN_SUPPORT_ISSUE_DESCRIPTION,
                                        currentDevice));

                String fixSuggestionMessage =
                        CheckerNLS.DeviceCompatibilityChecker_SMALL_SCREEN_SUPPORT_FIX_SUGGESTION;

                resultData.setQuickFixSuggestion(fixSuggestionMessage);

                ArrayList<Integer> lines = new ArrayList<Integer>();
                lines.add(currentIssuedLine);
                resultData.addFileToIssueLines(manifestElement.getFile(), lines);
                resultData.setInfoURL(ConditionUtils.getDescriptionLink(getChecker().getId(),
                        getId(), valManagerConfig));
                resultData.setPreview(preview);
                results.addValidationResult(resultData);
            }
        }
    }

    //Verify if minSdk and targetSdk values are less than 4 (the SMALL_SCREEN_THRESHOLD)
    private boolean checkIfMinSdkTooLowFilteringSmallDevices(Node usesSdkNode)
            throws PreflightingCheckerException
    {
        boolean supportsSmallScreens = true;
        Node minSdkAtr =
                usesSdkNode.getAttributes().getNamedItem(
                        ManifestConstants.MIN_SDK_VERSION_ATTRIBUTE);
        try
        {
            if (minSdkAtr != null)
            {
                String minSdkStr = minSdkAtr.getNodeValue().trim();
                try
                {
                    Integer minSdk = Integer.valueOf(minSdkStr);
                    if (minSdk < SMALL_SCREEN_THRESHOLD) //$NON-NLS-1$
                    {
                        // min sdk too low => check target version
                        Node targetSdkVersionAtr =
                                usesSdkNode.getAttributes().getNamedItem(
                                        ManifestConstants.ANDROID_TARGET_SDK_VERSION_ATTRIBUTE);
                        if ((targetSdkVersionAtr == null)
                                || (targetSdkVersionAtr.getNodeValue().trim().length() == 0))
                        {
                            supportsSmallScreens = false;
                        }
                        else
                        {
                            // target sdk declared => check if value is ok
                            String targetSdkVersionStr = targetSdkVersionAtr.getNodeValue().trim();
                            try
                            {
                                Integer targetSdkVersion = Integer.valueOf(targetSdkVersionStr);
                                if (targetSdkVersion < SMALL_SCREEN_THRESHOLD)
                                {
                                    supportsSmallScreens = false;
                                }
                            }
                            catch (NumberFormatException nfe)
                            {
                                // Do nothing, it will be handled on the target sdk is a preview condition.
                            }
                        }

                    }
                }
                catch (NumberFormatException nfe)
                {
                    // Do nothing, it will be handled on the min sdk is a preview condition.
                }
            }
        }
        catch (DOMException e)
        {
            // Error retrieving attribute
            throw new PreflightingCheckerException(CheckerNLS.bind(
                    CheckerNLS.AndroidMarketFiltersChecker_Exception_Getting_Manifest_Attribute,
                    ManifestConstants.ANDROID_TARGET_SDK_VERSION_ATTRIBUTE), e);
        }
        return supportsSmallScreens;
    }

}