• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos;
17 
18 import java.io.InputStream;
19 
20 /**
21  * bundle uncompress.
22  * step1: parse arguments
23  * step2: verity arguments
24  * step3: uncompress arguments
25  *
26  */
27 public class UncompressEntrance {
28     /**
29      * Parses and returns the hap list that supports the device type.
30      */
31     public static final String PARSE_MODE_HAPLIST = "hap-list";
32 
33     /**
34      * Parses and returns the information about the hap.
35      */
36     public static final String PARSE_MODE_HAPINFO = "hap-info";
37 
38     /**
39      * Parses and returns the informations about the hap list that supports the device type and the haps in the app.
40      */
41     public static final String PARSE_MODE_ALL = "all";
42 
43     /**
44      * Device type of default.
45      */
46     public static final String DEVICE_TYPE_DEFAULT = "default";
47 
48     /**
49      * Device type of phone.
50      */
51     public static final String DEVICE_TYPE_PHONE = "phone";
52 
53     /**
54      * Device type of tablet.
55      */
56     public static final String DEVICE_TYPE_TABLET = "tablet";
57 
58     /**
59      * Device type of tv.
60      */
61     public static final String DEVICE_TYPE_TV = "tv";
62 
63     /**
64      * Device type of car.
65      */
66     public static final String DEVICE_TYPE_CAR = "car";
67 
68     /**
69      * Device type of smartWatch.
70      */
71     public static final String DEVICE_TYPE_SMARTWATCH = "smartWatch";
72 
73     /**
74      * Device type of fitnessWatch.
75      */
76     public static final String DEVICE_TYPE_FITNESSWATCH = "fitnessWatch";
77 
78     /**
79      * Device type of fitnessBand.
80      */
81     public static final String DEVICE_TYPE_FITNESSBAND = "fitnessBand";
82 
83     private static final int EXIT_STATUS_NORMAL = 0;
84     private static final int EXIT_STATUS_EXCEPTION = 1;
85     private static final Log LOG = new Log(UncompressEntrance.class.toString());
86 
87     /**
88      * Unpack the app.
89      *
90      * @param appPath Indicates the app path.
91      * @param outPath Indicates the out path.
92      * @param deviceType Indicates the device type supported by the haps.If this parameter is null, all the haps will
93      *                   be unpacked.
94      * @param unpackApk Indicates whether to decompress the apk file in the hap.The default value is {@code false},
95      *                  not unpack the apk file.
96      * @return Return the unpack result.
97      */
unpack(String appPath, String outPath, String deviceType, boolean unpackApk)98     public static boolean unpack(String appPath, String outPath, String deviceType, boolean unpackApk) {
99         if (appPath == null || appPath.isEmpty()) {
100             LOG.error("UncompressEntrance::unpack appPath is invalid!");
101             return false;
102         }
103 
104         if (outPath == null || outPath.isEmpty()) {
105             LOG.error("UncompressEntrance::unpack outPath is invalid!");
106             return false;
107         }
108 
109         Utility utility = new Utility();
110         utility.setMode(Utility.MODE_APP);
111         utility.setAppPath(appPath);
112         utility.setDeviceType(deviceType == null ? "" : deviceType);
113         utility.setOutPath(outPath);
114         utility.setUnpackApk(String.valueOf(unpackApk));
115         utility.setForceRewrite("true");
116 
117         if (!UncompressVerify.commandVerify(utility)) {
118             LOG.error("CompressEntrance::unpack verity failed");
119             return false;
120         }
121 
122         if (!Uncompress.unpackageProcess(utility)) {
123             LOG.error("UncompressEntrance::unpackageProcess failed");
124             return false;
125         }
126 
127         return true;
128     }
129 
130     /**
131      * Unpack the hap.
132      *
133      * @param hapPath Indicates the hap path.
134      * @param outPath Indicates the out path.
135      * @param unpackApk Indicates whether to decompress the apk file in the hap.The default value is {@code false},
136      *                  not unpack the apk file.
137      * @return Return the unpack result.
138      */
unpackHap(String hapPath, String outPath, boolean unpackApk)139     public static boolean unpackHap(String hapPath, String outPath, boolean unpackApk) {
140         if (hapPath == null || hapPath.isEmpty()) {
141             LOG.error("UncompressEntrance::unpackHap hapPath is invalid!");
142             return false;
143         }
144 
145         if (outPath == null || outPath.isEmpty()) {
146             LOG.error("UncompressEntrance::unpackHap outPath is invalid!");
147             return false;
148         }
149 
150         Utility utility = new Utility();
151         utility.setMode(Utility.MODE_HAP);
152         utility.setHapPath(hapPath);
153         utility.setDeviceType("");
154         utility.setOutPath(outPath);
155         utility.setUnpackApk(String.valueOf(unpackApk));
156         utility.setForceRewrite("true");
157 
158         if (!UncompressVerify.commandVerify(utility)) {
159             LOG.error("CompressEntrance::unpackHap verity failed");
160             return false;
161         }
162 
163         if (!Uncompress.unpackageProcess(utility)) {
164             LOG.error("UncompressEntrance::unpackageProcess failed");
165             return false;
166         }
167 
168         return true;
169     }
170 
171     /**
172      * Parse the app.
173      *
174      * @param appPath Indicates the app path.
175      * @param parseMode Indicates the parse mode, which can be {@link #PARSE_MODE_HAPLIST}, {@link #PARSE_MODE_HAPINFO},
176      *                  {@link #PARSE_MODE_ALL}.
177      * @param deviceType Indicates the device type supported by the haps, This parameter is required
178      *                   when {@code #parseMode} is {@link #PARSE_MODE_HAPLIST}.
179      * @param hapName Indicates the hap name, This parameter is required when {@code #parseMode}
180      *                is {@link #PARSE_MODE_HAPINFO}.
181      * @param outPath Indicates the out path to unpack the files.
182      * @return Return the uncomperss result of parseApp
183      */
parseApp(String appPath, String parseMode, String deviceType, String hapName, String outPath)184     public static UncomperssResult parseApp(String appPath, String parseMode, String deviceType, String hapName,
185             String outPath) {
186         UncomperssResult compressResult = new UncomperssResult();
187 
188         if (appPath == null || appPath.isEmpty()) {
189             LOG.error("UncompressEntrance::parseApp appPath is invalid!");
190             compressResult.setResult(false);
191             compressResult.setMessage("ParseApp appPath is invalid");
192             return compressResult;
193         }
194 
195         if (parseMode == null || parseMode.isEmpty()) {
196             LOG.error("UncompressEntrance::parseApp parseMode is invalid!");
197             compressResult.setResult(false);
198             compressResult.setMessage("ParseApp parseMode is invalid");
199             return compressResult;
200         }
201 
202         Utility utility = new Utility();
203         utility.setMode(Utility.MODE_APP);
204         utility.setAppPath(appPath);
205         utility.setParseMode(parseMode);
206         utility.setDeviceType(deviceType == null ? "" : deviceType);
207         utility.setHapName(hapName == null ? "" : hapName);
208         utility.setForceRewrite("true");
209         if (outPath != null && !outPath.isEmpty()) {
210             utility.setOutPath(outPath);
211         }
212 
213         if (!UncompressVerify.commandVerify(utility)) {
214             LOG.error("CompressEntrance::parseApp verity failed");
215             compressResult.setResult(false);
216             compressResult.setMessage("ParseApp verity failed");
217             return compressResult;
218         }
219 
220         compressResult = Uncompress.uncompressApp(utility);
221 
222         return compressResult;
223     }
224 
225     /**
226      * Parse the app.
227      *
228      * @param input Indicates the InputStream about the app package.
229      * @param parseMode Indicates the parse mode, which can be {@link #PARSE_MODE_HAPLIST}, {@link #PARSE_MODE_HAPINFO},
230      *                  {@link #PARSE_MODE_ALL}.
231      * @param deviceType Indicates the device type supported by the haps, This parameter is required
232      *                   when {@code #parseMode} is {@link #PARSE_MODE_HAPLIST}.
233      * @param hapName Indicates the hap name, This parameter is required when {@code #parseMode}
234      *                is {@link #PARSE_MODE_HAPINFO}.
235      * @param outPath Indicates the out path to unzip temp files.
236      * @return Return the uncomperss result of parseApp
237      */
parseApp(InputStream input, String parseMode, String deviceType, String hapName, String outPath)238     public static UncomperssResult parseApp(InputStream input, String parseMode, String deviceType, String hapName,
239             String outPath) {
240         UncomperssResult compressResult = new UncomperssResult();
241 
242         if (input == null) {
243             LOG.error("UncompressEntrance::parseApp input is null!");
244             compressResult.setResult(false);
245             compressResult.setMessage("ParseApp input is null");
246             return compressResult;
247         }
248 
249         if (parseMode == null || parseMode.isEmpty()) {
250             LOG.error("UncompressEntrance::parseApp parseMode is invalid!");
251             compressResult.setResult(false);
252             compressResult.setMessage("ParseApp parseMode is invalid");
253             return compressResult;
254         }
255 
256         Utility utility = new Utility();
257         utility.setMode(Utility.MODE_APP);
258         utility.setParseMode(parseMode);
259         utility.setDeviceType(deviceType == null ? "" : deviceType);
260         utility.setHapName(hapName == null ? "" : hapName);
261         utility.setForceRewrite("true");
262         utility.setOutPath(outPath == null ? "" : outPath);
263 
264         if (!UncompressVerify.inputParseVerify(utility, input)) {
265             LOG.error("CompressEntrance::parseApp verity failed");
266             compressResult.setResult(false);
267             compressResult.setMessage("ParseApp verity failed");
268             return compressResult;
269         }
270 
271         compressResult = Uncompress.uncompressAppByInput(utility, input);
272 
273         return compressResult;
274     }
275 
276     /**
277      * Parse the hap.
278      *
279      * @param hapPath Indicates the hap path.
280      * @return Return the uncomperss result of parseHap
281      */
parseHap(String hapPath)282     public static UncomperssResult parseHap(String hapPath) {
283         UncomperssResult compressResult = new UncomperssResult();
284 
285         if (hapPath == null || hapPath.isEmpty()) {
286             LOG.error("UncompressEntrance::parseHap hapPath is invalid!");
287             compressResult.setResult(false);
288             compressResult.setMessage("ParseHap hapPath is invalid");
289             return compressResult;
290         }
291 
292         Utility utility = new Utility();
293         utility.setMode(Utility.MODE_HAP);
294         utility.setParseMode(PARSE_MODE_HAPINFO);
295         utility.setHapPath(hapPath);
296         utility.setForceRewrite("true");
297 
298         if (!UncompressVerify.commandVerify(utility)) {
299             LOG.error("UncompressEntrance::parseHap verity failed");
300             compressResult.setResult(false);
301             compressResult.setMessage("ParseHap verity failed");
302             return compressResult;
303         }
304 
305         compressResult = Uncompress.uncompressHap(utility);
306 
307         return compressResult;
308     }
309 
310     /**
311      * Parse the hap.
312      *
313      * @param input Indicates the InputStream about the app package.
314      * @return Return the uncomperss result of parseHap
315      */
parseHap(InputStream input)316     public static UncomperssResult parseHap(InputStream input) {
317         UncomperssResult compressResult = new UncomperssResult();
318 
319         if (input == null) {
320             LOG.error("UncompressEntrance::parseHap input is null!");
321             compressResult.setResult(false);
322             compressResult.setMessage("ParseHap input is null");
323             return compressResult;
324         }
325 
326         Utility utility = new Utility();
327         utility.setMode(Utility.MODE_HAP);
328         utility.setParseMode(PARSE_MODE_HAPINFO);
329         utility.setForceRewrite("true");
330 
331         if (!UncompressVerify.inputParseVerify(utility, input)) {
332             LOG.error("UncompressEntrance::parseHap verity failed");
333             compressResult.setResult(false);
334             compressResult.setMessage("ParseHap verity failed");
335             return compressResult;
336         }
337 
338         compressResult = Uncompress.uncompressHapByInput(utility, input);
339 
340         return compressResult;
341     }
342 
343     /**
344      * uncompress tool main function.
345      *
346      * @param args command line
347      */
main(String[] args)348     public static void main(String[] args) {
349         Utility utility = new Utility();
350 
351         if (!CommandParser.commandParser(utility, args)) {
352             LOG.error("UncompressEntrance::main exit, parser failed");
353             ShowHelp.uncompressHelp();
354             System.exit(EXIT_STATUS_EXCEPTION);
355         }
356 
357         if (!UncompressVerify.commandVerify(utility)) {
358             LOG.error("UncompressEntrance::main exit, verity failed");
359             ShowHelp.uncompressHelp();
360             System.exit(EXIT_STATUS_EXCEPTION);
361         }
362 
363         if (!Uncompress.unpackageProcess(utility)) {
364             LOG.error("UncompressEntrance::main exit, uncompress failed");
365             ShowHelp.uncompressHelp();
366             System.exit(EXIT_STATUS_EXCEPTION);
367         }
368 
369         System.exit(EXIT_STATUS_NORMAL);
370     }
371 }