summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/compat/PinItemRequestCompat.java
blob: f76b77655dcd422bff316abf62a44372371ca472 (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
/*
 * Copyright (C) 2017 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.launcher3.compat;

import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * A wrapper around platform implementation of PinItemRequestCompat until the
 * updated SDK is available.
 */
public class PinItemRequestCompat implements Parcelable {

    public static final String EXTRA_PIN_ITEM_REQUEST = "android.content.pm.extra.PIN_ITEM_REQUEST";

    public static final int REQUEST_TYPE_SHORTCUT = 1;
    public static final int REQUEST_TYPE_APPWIDGET = 2;

    private final Parcelable mObject;

    private PinItemRequestCompat(Parcelable object) {
        mObject = object;
    }

    public int getRequestType() {
        return (Integer) invokeMethod("getRequestType");
    }

    public ShortcutInfo getShortcutInfo() {
        return (ShortcutInfo) invokeMethod("getShortcutInfo");
    }

    public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) {
        try {
            return (AppWidgetProviderInfo) mObject.getClass()
                    .getDeclaredMethod("getAppWidgetProviderInfo", Context.class)
                    .invoke(mObject, context);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isValid() {
        return (Boolean) invokeMethod("isValid");
    }

    public boolean accept() {
        return (Boolean) invokeMethod("accept");
    }

    public boolean accept(Bundle options) {
        try {
            return (Boolean) mObject.getClass().getDeclaredMethod("accept", Bundle.class)
                    .invoke(mObject, options);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Object invokeMethod(String methodName) {
        try {
            return mObject.getClass().getDeclaredMethod(methodName).invoke(mObject);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeParcelable(mObject, i);
    }

    public static final Parcelable.Creator<PinItemRequestCompat> CREATOR =
            new Parcelable.Creator<PinItemRequestCompat>() {
                public PinItemRequestCompat createFromParcel(Parcel source) {
                    Parcelable object = source.readParcelable(null);
                    return new PinItemRequestCompat(object);
                }

                public PinItemRequestCompat[] newArray(int size) {
                    return new PinItemRequestCompat[size];
                }
            };

    public static PinItemRequestCompat getPinItemRequest(Intent intent) {
        Parcelable extra = intent.getParcelableExtra(EXTRA_PIN_ITEM_REQUEST);
        return extra == null ? null : new PinItemRequestCompat(extra);
    }
}