summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/people/PeopleProvider.java
blob: 0ba077eb09127db4cbbcd37f9e92a39d148a5265 (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
/*
 * Copyright (C) 2021 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.systemui.people;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;
import android.widget.RemoteViews;

import com.android.systemui.SystemUIAppComponentFactoryBase.ContextAvailableCallback;
import com.android.systemui.SystemUIAppComponentFactoryBase.ContextInitializer;
import com.android.systemui.people.widget.PeopleSpaceWidgetManager;
import com.android.systemui.shared.system.PeopleProviderUtils;

import javax.inject.Inject;

/** API that returns a People Tile preview. */
public class PeopleProvider extends ContentProvider implements
        ContextInitializer {
    private static final String TAG = "PeopleProvider";
    private static final boolean DEBUG = PeopleSpaceUtils.DEBUG;
    private static final String EMPTY_STRING = "";
    private ContextAvailableCallback mCallback;

    @Inject
    PeopleSpaceWidgetManager mPeopleSpaceWidgetManager;

    @Override
    public Bundle call(String method, String arg, Bundle extras) {
        if (!doesCallerHavePermission()) {
            String callingPackage = getCallingPackage();
            Log.w(TAG, "API not accessible to calling package: " + callingPackage);
            throw new SecurityException("API not accessible to calling package: " + callingPackage);
        }
        if (!PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_METHOD.equals(method)) {
            Log.w(TAG, "Invalid method");
            throw new IllegalArgumentException("Invalid method");
        }

        if (extras == null) {
            Log.w(TAG, "Extras can't be null");
            throw new IllegalArgumentException("Extras can't be null");
        }

        String shortcutId = extras.getString(
                PeopleProviderUtils.EXTRAS_KEY_SHORTCUT_ID, EMPTY_STRING);
        String packageName = extras.getString(
                PeopleProviderUtils.EXTRAS_KEY_PACKAGE_NAME, EMPTY_STRING);
        UserHandle userHandle = extras.getParcelable(
                PeopleProviderUtils.EXTRAS_KEY_USER_HANDLE);
        if (shortcutId.isEmpty()) {
            Log.w(TAG, "Invalid shortcut id");
            throw new IllegalArgumentException("Invalid shortcut id");
        }

        if (packageName.isEmpty()) {
            Log.w(TAG, "Invalid package name");
            throw new IllegalArgumentException("Invalid package name");
        }
        if (userHandle == null) {
            Log.w(TAG, "Null user handle");
            throw new IllegalArgumentException("Null user handle");
        }

        if (mPeopleSpaceWidgetManager == null) {
            Log.e(TAG, "Could not initialize people widget manager");
            return null;
        }
        RemoteViews view = mPeopleSpaceWidgetManager.getPreview(shortcutId, userHandle, packageName,
                extras);
        if (view == null) {
            if (DEBUG) Log.d(TAG, "No preview available for shortcutId: " + shortcutId);
            return null;
        }
        final Bundle bundle = new Bundle();
        bundle.putParcelable(PeopleProviderUtils.RESPONSE_KEY_REMOTE_VIEWS, view);
        return bundle;
    }

    private boolean doesCallerHavePermission() {
        return getContext().checkPermission(
                PeopleProviderUtils.GET_PEOPLE_TILE_PREVIEW_PERMISSION,
                Binder.getCallingPid(), Binder.getCallingUid())
                == PackageManager.PERMISSION_GRANTED;
    }

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

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        throw new IllegalArgumentException("Invalid method");
    }

    @Override
    public String getType(Uri uri) {
        throw new IllegalArgumentException("Invalid method");
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        throw new IllegalArgumentException("Invalid method");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        throw new IllegalArgumentException("Invalid method");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        throw new IllegalArgumentException("Invalid method");
    }

    @Override
    public void attachInfo(Context context, ProviderInfo info) {
        mCallback.onContextAvailable(context);
        super.attachInfo(context, info);
    }

    @Override
    public void setContextAvailableCallback(
            ContextAvailableCallback callback) {
        mCallback = callback;
    }
}