1 /*
2  * Copyright (C) 2015 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 
17 package com.android.packageinstaller.wear;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageParser;
23 import android.net.Uri;
24 import android.os.ParcelFileDescriptor;
25 import android.system.ErrnoException;
26 import android.system.Os;
27 import android.text.TextUtils;
28 import android.util.Log;
29 
30 import org.tukaani.xz.LZMAInputStream;
31 import org.tukaani.xz.XZInputStream;
32 
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.ArrayList;
38 
39 public class WearPackageUtil {
40     private static final String TAG = "WearablePkgInstaller";
41 
42     private static final String COMPRESSION_LZMA = "lzma";
43     private static final String COMPRESSION_XZ = "xz";
44 
45     private static final String SHOW_PERMS_SERVICE_PKG_NAME = "com.google.android.wearable.app";
46     private static final String SHOW_PERMS_SERVICE_CLASS_NAME =
47             "com.google.android.clockwork.packagemanager.ShowPermsService";
48     private static final String EXTRA_PACKAGE_NAME
49             = "com.google.android.clockwork.EXTRA_PACKAGE_NAME";
50 
getTemporaryFile(Context context, String packageName)51     public static File getTemporaryFile(Context context, String packageName) {
52         try {
53             File newFileDir = new File(context.getFilesDir(), "tmp");
54             newFileDir.mkdirs();
55             Os.chmod(newFileDir.getAbsolutePath(), 0771);
56             File newFile = new File(newFileDir, packageName + ".apk");
57             return newFile;
58         }   catch (ErrnoException e) {
59             Log.e(TAG, "Failed to open.", e);
60             return null;
61         }
62     }
63 
getIconFile(final Context context, final String packageName)64     public static File getIconFile(final Context context, final String packageName) {
65         try {
66             File newFileDir = new File(context.getFilesDir(), "images/icons");
67             newFileDir.mkdirs();
68             Os.chmod(newFileDir.getAbsolutePath(), 0771);
69             return new File(newFileDir, packageName + ".icon");
70         }   catch (ErrnoException e) {
71             Log.e(TAG, "Failed to open.", e);
72             return null;
73         }
74     }
75 
76     /**
77      * In order to make sure that the Wearable Asset Manager has a reasonable apk that can be used
78      * by the PackageManager, we will parse it before sending it to the PackageManager.
79      * Unfortunately, PackageParser needs a file to parse. So, we have to temporarily convert the fd
80      * to a File.
81      *
82      * @param context
83      * @param fd FileDescriptor to convert to File
84      * @param packageName Name of package, will define the name of the file
85      * @param compressionAlg Can be null. For ALT mode the APK will be compressed. We will
86      *                       decompress it here
87      */
getFileFromFd(Context context, ParcelFileDescriptor fd, String packageName, String compressionAlg)88     public static File getFileFromFd(Context context, ParcelFileDescriptor fd,
89             String packageName, String compressionAlg) {
90         File newFile = getTemporaryFile(context, packageName);
91         if (fd == null || fd.getFileDescriptor() == null)  {
92             return null;
93         }
94         InputStream fr = new ParcelFileDescriptor.AutoCloseInputStream(fd);
95         try {
96             if (TextUtils.equals(compressionAlg, COMPRESSION_XZ)) {
97                 fr = new XZInputStream(fr);
98             } else if (TextUtils.equals(compressionAlg, COMPRESSION_LZMA)) {
99                 fr = new LZMAInputStream(fr);
100             }
101         } catch (IOException e) {
102             Log.e(TAG, "Compression was set to " + compressionAlg + ", but could not decode ", e);
103             return null;
104         }
105 
106         int nRead;
107         byte[] data = new byte[1024];
108         try {
109             final FileOutputStream fo = new FileOutputStream(newFile);
110             while ((nRead = fr.read(data, 0, data.length)) != -1) {
111                 fo.write(data, 0, nRead);
112             }
113             fo.flush();
114             fo.close();
115             Os.chmod(newFile.getAbsolutePath(), 0644);
116             return newFile;
117         } catch (IOException e) {
118             Log.e(TAG, "Reading from Asset FD or writing to temp file failed ", e);
119             return null;
120         }   catch (ErrnoException e) {
121             Log.e(TAG, "Could not set permissions on file ", e);
122             return null;
123         } finally {
124             try {
125                 fr.close();
126             } catch (IOException e) {
127                 Log.e(TAG, "Failed to close the file from FD ", e);
128             }
129         }
130     }
131 
hasLauncherActivity(PackageParser.Package pkg)132     public static boolean hasLauncherActivity(PackageParser.Package pkg) {
133         if (pkg == null || pkg.activities == null) {
134             return false;
135         }
136 
137         final int activityCount = pkg.activities.size();
138         for (int i = 0; i < activityCount; ++i) {
139             if (pkg.activities.get(i).intents != null) {
140                 ArrayList<PackageParser.ActivityIntentInfo> intents =
141                         pkg.activities.get(i).intents;
142                 final int intentsCount = intents.size();
143                 for (int j = 0; j < intentsCount; ++j) {
144                     final PackageParser.ActivityIntentInfo intentInfo = intents.get(j);
145                     if (intentInfo.hasAction(Intent.ACTION_MAIN)) {
146                         if (intentInfo.hasCategory(Intent.CATEGORY_INFO) ||
147                                 intentInfo .hasCategory(Intent.CATEGORY_LAUNCHER)) {
148                             return true;
149                         }
150                     }
151                 }
152             }
153         }
154         return false;
155     }
156 
removeFromPermStore(Context context, String wearablePackageName)157     public static void removeFromPermStore(Context context, String wearablePackageName) {
158         Intent newIntent = new Intent()
159                 .setComponent(new ComponentName(
160                         SHOW_PERMS_SERVICE_PKG_NAME, SHOW_PERMS_SERVICE_CLASS_NAME))
161                 .setAction(Intent.ACTION_UNINSTALL_PACKAGE);
162         newIntent.putExtra(EXTRA_PACKAGE_NAME, wearablePackageName);
163         Log.i(TAG, "Sending removeFromPermStore to ShowPermsService " + newIntent
164                 + " for " + wearablePackageName);
165         context.startService(newIntent);
166     }
167 
168     /**
169      * @return com.google.com from expected formats like
170      * Uri: package:com.google.com, package:/com.google.com, package://com.google.com
171      */
getSanitizedPackageName(Uri packageUri)172     public static String getSanitizedPackageName(Uri packageUri) {
173         String packageName = packageUri.getEncodedSchemeSpecificPart();
174         if (packageName != null) {
175             return packageName.replaceAll("^/+", "");
176         }
177         return packageName;
178     }
179 }
180