summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/wear/WearPackageIconProvider.java
blob: 02b9d298db0e56a6062942472932d4e49893c9f1 (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
/*
 * Copyright (C) 2015 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.android.packageinstaller.wear;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class WearPackageIconProvider extends ContentProvider {
    private static final String TAG = "WearPackageIconProvider";
    public static final String AUTHORITY = "com.google.android.packageinstaller.wear.provider";

    private static final String REQUIRED_PERMISSION =
            "com.google.android.permission.INSTALL_WEARABLE_PACKAGES";

    /** MIME types. */
    public static final String ICON_TYPE = "vnd.android.cursor.item/cw_package_icon";

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        throw new UnsupportedOperationException("Query is not supported.");
    }

    @Override
    public String getType(Uri uri) {
        if (uri == null) {
            throw new IllegalArgumentException("URI passed in is null.");
        }

        if (AUTHORITY.equals(uri.getEncodedAuthority())) {
            return ICON_TYPE;
        }
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException("Insert is not supported.");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        if (uri == null) {
            throw new IllegalArgumentException("URI passed in is null.");
        }

        enforcePermissions(uri);

        if (ICON_TYPE.equals(getType(uri))) {
            final File file = WearPackageUtil.getIconFile(
                    this.getContext().getApplicationContext(), getPackageNameFromUri(uri));
            if (file != null) {
                file.delete();
            }
        }

        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException("Update is not supported.");
    }

    @Override
    public ParcelFileDescriptor openFile(
            Uri uri, @SuppressWarnings("unused") String mode) throws FileNotFoundException {
        if (uri == null) {
            throw new IllegalArgumentException("URI passed in is null.");
        }

        enforcePermissions(uri);

        if (ICON_TYPE.equals(getType(uri))) {
            final File file = WearPackageUtil.getIconFile(
                    this.getContext().getApplicationContext(), getPackageNameFromUri(uri));
            if (file != null) {
                return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            }
        }
        return null;
    }

    public static Uri getUriForPackage(final String packageName) {
        return Uri.parse("content://" + AUTHORITY + "/icons/" + packageName + ".icon");
    }

    private String getPackageNameFromUri(Uri uri) {
        if (uri == null) {
            return null;
        }
        List<String> pathSegments = uri.getPathSegments();
        String packageName = pathSegments.get(pathSegments.size() - 1);

        if (packageName.endsWith(".icon")) {
            packageName = packageName.substring(0, packageName.lastIndexOf("."));
        }
        return packageName;
    }

    /**
     * Make sure the calling app is either a system app or the same app or has the right permission.
     * @throws SecurityException if the caller has insufficient permissions.
     */
    @TargetApi(Build.VERSION_CODES.BASE_1_1)
    private void enforcePermissions(Uri uri) {
        // Redo some of the permission check in {@link ContentProvider}. Just add an extra check to
        // allow System process to access this provider.
        Context context = getContext();
        final int pid = Binder.getCallingPid();
        final int uid = Binder.getCallingUid();
        final int myUid = android.os.Process.myUid();

        if (uid == myUid || isSystemApp(context, pid)) {
            return;
        }

        if (context.checkPermission(REQUIRED_PERMISSION, pid, uid) == PERMISSION_GRANTED) {
            return;
        }

        // last chance, check against any uri grants
        if (context.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION)
                == PERMISSION_GRANTED) {
            return;
        }

        throw new SecurityException("Permission Denial: reading "
                + getClass().getName() + " uri " + uri + " from pid=" + pid
                + ", uid=" + uid);
    }

    /**
     * From the pid of the calling process, figure out whether this is a system app or not. We do
     * this by checking the application information corresponding to the pid and then checking if
     * FLAG_SYSTEM is set.
     */
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    private boolean isSystemApp(Context context, int pid) {
        // Get the Activity Manager Object
        ActivityManager aManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        // Get the list of running Applications
        List<ActivityManager.RunningAppProcessInfo> rapInfoList =
                aManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
            if (rapInfo.pid == pid) {
                try {
                    PackageInfo pkgInfo = context.getPackageManager().getPackageInfo(
                            rapInfo.pkgList[0], 0);
                    if (pkgInfo != null && pkgInfo.applicationInfo != null &&
                            (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                        Log.d(TAG, pid + " is a system app.");
                        return true;
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(TAG, "Could not find package information.", e);
                    return false;
                }
            }
        }
        return false;
    }
}